-
Notifications
You must be signed in to change notification settings - Fork 795
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
Adding NomicEmbeddingModel #592
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
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> | ||
<parent> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-parent</artifactId> | ||
<version>0.27.0-SNAPSHOT</version> | ||
<relativePath>../langchain4j-parent/pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>langchain4j-nomic</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<name>LangChain4j :: Integration :: Nomic</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.retrofit2</groupId> | ||
<artifactId>retrofit</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.retrofit2</groupId> | ||
<artifactId>converter-gson</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.tinylog</groupId> | ||
<artifactId>tinylog-impl</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.tinylog</groupId> | ||
<artifactId>slf4j-tinylog</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/EmbeddingRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dev.langchain4j.model.nomic; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.List; | ||
|
||
@Builder | ||
class EmbeddingRequest { | ||
|
||
private String model; | ||
private List<String> texts; | ||
private String taskType; | ||
} |
12 changes: 12 additions & 0 deletions
12
langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/EmbeddingResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package dev.langchain4j.model.nomic; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
class EmbeddingResponse { | ||
|
||
private List<float[]> embeddings; | ||
private Usage usage; | ||
} |
14 changes: 14 additions & 0 deletions
14
langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/NomicApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.langchain4j.model.nomic; | ||
|
||
import retrofit2.Call; | ||
import retrofit2.http.Body; | ||
import retrofit2.http.Header; | ||
import retrofit2.http.Headers; | ||
import retrofit2.http.POST; | ||
|
||
interface NomicApi { | ||
|
||
@POST("embedding/text") | ||
@Headers({"Content-Type: application/json"}) | ||
Call<EmbeddingResponse> embed(@Body EmbeddingRequest request, @Header("Authorization") String authorizationHeader); | ||
} |
73 changes: 73 additions & 0 deletions
73
langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/NomicClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package dev.langchain4j.model.nomic; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import lombok.Builder; | ||
import okhttp3.OkHttpClient; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
|
||
import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES; | ||
import static dev.langchain4j.internal.ValidationUtils.ensureNotBlank; | ||
|
||
class NomicClient { | ||
|
||
private static final Gson GSON = new GsonBuilder() | ||
.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES) | ||
.setPrettyPrinting() | ||
.create(); | ||
|
||
private final NomicApi nomicApi; | ||
private final String authorizationHeader; | ||
|
||
@Builder | ||
NomicClient(String baseUrl, String apiKey, Duration timeout, Boolean logRequests, Boolean logResponses) { | ||
|
||
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder() | ||
.callTimeout(timeout) | ||
.connectTimeout(timeout) | ||
.readTimeout(timeout) | ||
.writeTimeout(timeout); | ||
|
||
if (logRequests) { | ||
okHttpClientBuilder.addInterceptor(new RequestLoggingInterceptor()); | ||
} | ||
if (logResponses) { | ||
okHttpClientBuilder.addInterceptor(new ResponseLoggingInterceptor()); | ||
} | ||
|
||
Retrofit retrofit = new Retrofit.Builder() | ||
.baseUrl(baseUrl) | ||
.client(okHttpClientBuilder.build()) | ||
.addConverterFactory(GsonConverterFactory.create(GSON)) | ||
.build(); | ||
|
||
this.nomicApi = retrofit.create(NomicApi.class); | ||
this.authorizationHeader = "Bearer " + ensureNotBlank(apiKey, "apiKey"); | ||
} | ||
|
||
public EmbeddingResponse embed(EmbeddingRequest request) { | ||
try { | ||
retrofit2.Response<EmbeddingResponse> retrofitResponse | ||
= nomicApi.embed(request, authorizationHeader).execute(); | ||
|
||
if (retrofitResponse.isSuccessful()) { | ||
return retrofitResponse.body(); | ||
} else { | ||
throw toException(retrofitResponse); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static RuntimeException toException(retrofit2.Response<?> response) throws IOException { | ||
int code = response.code(); | ||
String body = response.errorBody().string(); | ||
String errorMessage = String.format("status code: %s; body: %s", code, body); | ||
return new RuntimeException(errorMessage); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/NomicEmbeddingModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package dev.langchain4j.model.nomic; | ||
|
||
import dev.langchain4j.data.embedding.Embedding; | ||
import dev.langchain4j.data.segment.TextSegment; | ||
import dev.langchain4j.model.embedding.EmbeddingModel; | ||
import dev.langchain4j.model.output.Response; | ||
import dev.langchain4j.model.output.TokenUsage; | ||
import lombok.Builder; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
import static dev.langchain4j.internal.RetryUtils.withRetry; | ||
import static dev.langchain4j.internal.Utils.getOrDefault; | ||
import static dev.langchain4j.internal.ValidationUtils.ensureNotBlank; | ||
import static java.time.Duration.ofSeconds; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
/** | ||
* An integration with Nomic Atlas's Text Embeddings API. | ||
* See more details <a href="https://docs.nomic.ai/reference/endpoints/nomic-embed-text">here</a>. | ||
*/ | ||
public class NomicEmbeddingModel implements EmbeddingModel { | ||
|
||
private static final String DEFAULT_BASE_URL = "https://api-atlas.nomic.ai/v1/"; | ||
|
||
private final NomicClient client; | ||
private final String modelName; | ||
private final String taskType; | ||
private final Integer maxRetries; | ||
|
||
@Builder | ||
public NomicEmbeddingModel( | ||
String baseUrl, | ||
String apiKey, | ||
String modelName, | ||
String taskType, | ||
Duration timeout, | ||
Integer maxRetries, | ||
Boolean logRequests, | ||
Boolean logResponses | ||
) { | ||
this.client = NomicClient.builder() | ||
.baseUrl(getOrDefault(baseUrl, DEFAULT_BASE_URL)) | ||
.apiKey(ensureNotBlank(apiKey, "apiKey")) | ||
.timeout(getOrDefault(timeout, ofSeconds(60))) | ||
.logRequests(getOrDefault(logRequests, false)) | ||
.logResponses(getOrDefault(logResponses, false)) | ||
.build(); | ||
this.modelName = getOrDefault(modelName, "nomic-embed-text-v1"); | ||
this.taskType = taskType; | ||
this.maxRetries = getOrDefault(maxRetries, 3); | ||
} | ||
|
||
public static NomicEmbeddingModel withApiKey(String apiKey) { | ||
return NomicEmbeddingModel.builder().apiKey(apiKey).build(); | ||
} | ||
|
||
@Override | ||
public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) { | ||
|
||
EmbeddingRequest request = EmbeddingRequest.builder() | ||
.model(modelName) | ||
.texts(textSegments.stream().map(TextSegment::text).collect(toList())) | ||
.taskType(taskType) | ||
.build(); | ||
|
||
EmbeddingResponse response = withRetry(() -> client.embed(request), maxRetries); | ||
|
||
List<Embedding> embeddings = response.getEmbeddings().stream() | ||
.map(Embedding::from).collect(toList()); | ||
|
||
TokenUsage tokenUsage = new TokenUsage(response.getUsage().getTotalTokens(), 0); | ||
|
||
return Response.from(embeddings, tokenUsage); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/RequestLoggingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package dev.langchain4j.model.nomic; | ||
|
||
import okhttp3.Headers; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okio.Buffer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.stream.StreamSupport.stream; | ||
|
||
class RequestLoggingInterceptor implements Interceptor { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(RequestLoggingInterceptor.class); | ||
|
||
private static final Pattern BEARER_PATTERN = Pattern.compile("(Bearer\\s*)([\\w-]{5})([\\w-]+)([\\w-]{2})"); | ||
|
||
public Response intercept(Interceptor.Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
log(request); | ||
return chain.proceed(request); | ||
} | ||
|
||
private void log(Request request) { | ||
log.debug( | ||
"Request:\n" + | ||
"- method: {}\n" + | ||
"- url: {}\n" + | ||
"- headers: {}\n" + | ||
"- body: {}", | ||
request.method(), | ||
request.url(), | ||
inOneLine(request.headers()), | ||
getBody(request) | ||
); | ||
} | ||
|
||
static String inOneLine(Headers headers) { | ||
return stream(headers.spliterator(), false) | ||
.map((header) -> { | ||
String headerKey = header.component1(); | ||
String headerValue = header.component2(); | ||
if (headerKey.equals("Authorization")) { | ||
headerValue = maskAuthorizationHeaderValue(headerValue); | ||
} | ||
return String.format("[%s: %s]", headerKey, headerValue); | ||
}).collect(Collectors.joining(", ")); | ||
} | ||
|
||
private static String maskAuthorizationHeaderValue(String authorizationHeaderValue) { | ||
try { | ||
Matcher matcher = BEARER_PATTERN.matcher(authorizationHeaderValue); | ||
StringBuffer sb = new StringBuffer(); | ||
|
||
while (matcher.find()) { | ||
matcher.appendReplacement(sb, matcher.group(1) + matcher.group(2) + "..." + matcher.group(4)); | ||
} | ||
|
||
matcher.appendTail(sb); | ||
return sb.toString(); | ||
} catch (Exception e) { | ||
return "[failed to mask the API key]"; | ||
} | ||
} | ||
|
||
private static String getBody(Request request) { | ||
try { | ||
Buffer buffer = new Buffer(); | ||
request.body().writeTo(buffer); | ||
return buffer.readUtf8(); | ||
} catch (Exception e) { | ||
log.warn("Exception happened while reading request body", e); | ||
return "[Exception happened while reading request body. Check logs for more details.]"; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default task type should be search_document.
If you do not specify the task type in the http request, it will default to this automatically!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
taskType
is not a mandatory field, it does not have a default value here to delegate this to Nomic