Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor error handling and response handling #43

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 22 additions & 38 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.mule.mulechain</groupId>
<artifactId>mulechain-vectors</artifactId>
<version>0.1.112-SNAPSHOT</version>
<groupId>com.mulesoft.connectors</groupId>
<artifactId>mule4-vectors-connector</artifactId>
<version>0.1.113-SNAPSHOT</version>
<packaging>mule-extension</packaging>
<name>Mule Vectors Connector</name>
<description>MAC Vectors provides access to a broad number of external Vector Stores and Databases.</description>
<name>MuleSoft Vectors Connector - Mule 4</name>
<description>MuleSoft Vectors Connector provides access to a broad number of external Vector Stores.</description>
<url>https://mac-project.ai/docs/mulechain-vectors/connector-overview</url>

<licenses>
<license>
<name>MIT License</name>
<url>https://www.opensource.org/licenses/mit-license.php</url>
</license>
</licenses>

<developers>
<developer>
<name>Amir Khan</name>
Expand All @@ -26,7 +28,7 @@
</developer>
<developer>
<name>Tommaso Bolis</name>
<email>tbolis@salesforce.com</email>
<email>tbolis-at-salesforce-dot-com</email>
<organization>Salesforce</organization>
<organizationUrl>https://www.salesforce.com</organizationUrl>
</developer>
Expand Down Expand Up @@ -61,6 +63,7 @@
<formatterGoal>validate</formatterGoal>
<langchain4jVersion>0.35.0</langchain4jVersion>
<slf4jApi.version>2.0.7</slf4jApi.version>
<mule.sdk.api.version>0.9.0-rc1</mule.sdk.api.version>
</properties>

<build>
Expand All @@ -79,38 +82,11 @@
</execution>
</executions>
</plugin>
<!-- <plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<version>${javaFormatter.plugin.version}</version>
<configuration>
<compilerCompliance>${javaVersion}</compilerCompliance>
<compilerSource>${javaVersion}</compilerSource>
<compilerTargetPlatform>${javaVersion}</compilerTargetPlatform>
<configFile>${basedir}/${formatterConfigPath}</configFile>
<configJsFile>${basedir}/${formatterConfigPath}</configJsFile>
<aggregator>false</aggregator>
<executionRoot>true</executionRoot>
</configuration>
<executions>
<execution>
<id>apply-format</id>
<phase>compile</phase>
<goals>
<goal>${formatterGoal}</goal>
</goals>
<configuration>
<skipFormatting>${skipVerifications}</skipFormatting>
</configuration>
</execution>
</executions>
</plugin> -->
</plugins>
</build>

<dependencyManagement>
<dependencies>

<dependency>
<groupId>org.mule</groupId>
<artifactId>mule-javaee-runtime-bom</artifactId>
Expand All @@ -122,27 +98,30 @@
</dependencyManagement>

<dependencies>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-azure-ai-search</artifactId>
<version>${langchain4jVersion}</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-document-loader-azure-storage-blob</artifactId>
<version>${langchain4jVersion}</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-document-loader-amazon-s3</artifactId>
<version>${langchain4jVersion}</version>
</dependency>

<dependency>
<groupId>org.mule.sdk</groupId>
<artifactId>mule-sdk-api</artifactId>
<version>0.9.0-rc1</version>
</dependency>
<dependency>
<groupId>org.mule.sdk</groupId>
<artifactId>mule-sdk-api</artifactId>
<version>${mule.sdk.api.version}</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
Expand All @@ -155,11 +134,13 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -170,6 +151,7 @@
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-document-parser-apache-tika</artifactId>
Expand Down Expand Up @@ -279,6 +261,7 @@
</dependency>

</dependencies>

<repositories>
<repository>
<id>mule-releases</id>
Expand All @@ -298,4 +281,5 @@
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mule.extension.mulechain.vectors.api.metadata;

import java.io.Serializable;
import java.util.HashMap;

public class DocumentResponseAttributes implements Serializable {

private final HashMap<String, String> documentAttributes;

public DocumentResponseAttributes(HashMap<String, String> documentAttributes) {

this.documentAttributes = documentAttributes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mule.extension.mulechain.vectors.api.metadata;

import java.io.Serializable;
import java.util.HashMap;

public class EmbeddingResponseAttributes implements Serializable {

private final HashMap<String, String> embeddingAttributes;

public EmbeddingResponseAttributes(HashMap<String, String> embeddingAttributes) {

this.embeddingAttributes = embeddingAttributes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mule.extension.mulechain.vectors.internal.error;

import org.mule.runtime.extension.api.error.ErrorTypeDefinition;

/**
* (c) 2003-2024 MuleSoft, Inc. The software in this package is published under the terms of the Commercial Free Software license V.1 a copy of which has been included with this distribution in the LICENSE.md file.
*/
public enum MuleVectorsErrorType implements ErrorTypeDefinition<MuleVectorsErrorType> {

DOCUMENT_OPERATIONS_FAILURE, EMBEDDING_OPERATIONS_FAILURE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* (c) 2003-2024 MuleSoft, Inc. The software in this package is published under the terms of the Commercial Free Software license V.1 a copy of which has been included with this distribution in the LICENSE.md file.
*/
package org.mule.extension.mulechain.vectors.internal.error.provider;

import org.mule.runtime.extension.api.annotation.error.ErrorTypeProvider;
import org.mule.runtime.extension.api.error.ErrorTypeDefinition;

import java.util.HashSet;
import java.util.Set;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;
import static org.mule.extension.mulechain.vectors.internal.error.MuleVectorsErrorType.DOCUMENT_OPERATIONS_FAILURE;

public class DocumentErrorTypeProvider implements ErrorTypeProvider {

@SuppressWarnings("rawtypes")
@Override
public Set<ErrorTypeDefinition> getErrorTypes() {
return unmodifiableSet(new HashSet<>(asList(DOCUMENT_OPERATIONS_FAILURE)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* (c) 2003-2024 MuleSoft, Inc. The software in this package is published under the terms of the Commercial Free Software license V.1 a copy of which has been included with this distribution in the LICENSE.md file.
*/
package org.mule.extension.mulechain.vectors.internal.error.provider;

import org.mule.runtime.extension.api.annotation.error.ErrorTypeProvider;
import org.mule.runtime.extension.api.error.ErrorTypeDefinition;

import java.util.HashSet;
import java.util.Set;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;
import static org.mule.extension.mulechain.vectors.internal.error.MuleVectorsErrorType.EMBEDDING_OPERATIONS_FAILURE;

public class EmbeddingErrorTypeProvider implements ErrorTypeProvider {

@SuppressWarnings("rawtypes")
@Override
public Set<ErrorTypeDefinition> getErrorTypes() {
return unmodifiableSet(new HashSet<>(asList(EMBEDDING_OPERATIONS_FAILURE)));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.mule.extension.mulechain.vectors.internal.extension;

import org.mule.extension.mulechain.vectors.internal.config.Configuration;
import org.mule.extension.mulechain.vectors.internal.error.MuleVectorsErrorType;
import org.mule.runtime.api.meta.Category;
import org.mule.runtime.extension.api.annotation.Extension;
import org.mule.runtime.extension.api.annotation.Configurations;
import org.mule.runtime.extension.api.annotation.dsl.xml.Xml;
import org.mule.runtime.extension.api.annotation.error.ErrorTypes;
import org.mule.runtime.extension.api.annotation.license.RequiresEnterpriseLicense;
import org.mule.sdk.api.annotation.JavaVersionSupport;
import static org.mule.sdk.api.meta.JavaVersion.JAVA_11;
import static org.mule.sdk.api.meta.JavaVersion.JAVA_17;
Expand All @@ -13,9 +17,11 @@
* This is the main class of an extension, is the entry point from which configurations, connection providers, operations
* and sources are going to be declared.
*/
@Xml(prefix = "vectors")
@Extension(name = "MAC Vectors")
@Xml(prefix = "ms-vectors")
@Extension(name = "MuleSoft Vectors Connector", category = Category.SELECT)
@Configurations(Configuration.class)
@RequiresEnterpriseLicense(allowEvaluationLicense = true)
@ErrorTypes(MuleVectorsErrorType.class)
@JavaVersionSupport({JAVA_8, JAVA_11, JAVA_17})
public class Connector {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.mule.extension.mulechain.vectors.internal.helper;

import org.mule.extension.mulechain.vectors.api.metadata.DocumentResponseAttributes;
import org.mule.extension.mulechain.vectors.api.metadata.EmbeddingResponseAttributes;
import org.mule.runtime.extension.api.runtime.operation.Result;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static org.apache.commons.io.IOUtils.toInputStream;

public final class ResponseHelper {

private ResponseHelper() {
}

public static Result<InputStream, EmbeddingResponseAttributes> createEmbeddingResponse(
String response,
Map<String, String> embeddingAttributes) {

return Result.<InputStream, EmbeddingResponseAttributes>builder()
.attributes(new EmbeddingResponseAttributes((HashMap<String, String>) embeddingAttributes))
.attributesMediaType(org.mule.runtime.api.metadata.MediaType.APPLICATION_JAVA)
.output(toInputStream(response, StandardCharsets.UTF_8))
.mediaType(org.mule.runtime.api.metadata.MediaType.APPLICATION_JSON)
.build();
}

public static Result<InputStream, DocumentResponseAttributes> createDocumentResponse(
String response,
Map<String, String> documentAttributes) {

return Result.<InputStream, DocumentResponseAttributes>builder()
.attributes(new DocumentResponseAttributes((HashMap<String, String>) documentAttributes))
.attributesMediaType(org.mule.runtime.api.metadata.MediaType.APPLICATION_JAVA)
.output(toInputStream(response, StandardCharsets.UTF_8))
.mediaType(org.mule.runtime.api.metadata.MediaType.APPLICATION_JSON)
.build();
}
}
Loading