Skip to content

Commit c357959

Browse files
authored
feat: add Azure OpenAi Embedding module (#136)
* feat: add Azure OpenAi Embedding module * fix: properties <langchain4j.version> name
1 parent 001f4e9 commit c357959

14 files changed

Lines changed: 184 additions & 10 deletions

File tree

dat-cli/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<artifactId>dat-embedder-xinference</artifactId>
8787
<version>${project.version}</version>
8888
</dependency>
89+
<dependency>
90+
<groupId>cn.datask</groupId>
91+
<artifactId>dat-embedder-azure-openai</artifactId>
92+
<version>${project.version}</version>
93+
</dependency>
8994

9095
<!-- DAT Reranking Model -->
9196
<dependency>

dat-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<dependency>
2626
<groupId>dev.langchain4j</groupId>
2727
<artifactId>langchain4j</artifactId>
28-
<version>${langchain4j.vserion}</version>
28+
<version>${langchain4j.version}</version>
2929
</dependency>
3030

3131
<!-- Jinjava -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>cn.datask</groupId>
7+
<artifactId>dat-embedders</artifactId>
8+
<version>${revision}</version>
9+
</parent>
10+
11+
<artifactId>dat-embedder-azure-openai</artifactId>
12+
<name>DAT : Embedders : Azure OpenAi</name>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>cn.datask</groupId>
17+
<artifactId>dat-core</artifactId>
18+
<version>${project.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>dev.langchain4j</groupId>
22+
<artifactId>langchain4j-azure-open-ai</artifactId>
23+
<version>${langchain4j.version}</version>
24+
</dependency>
25+
</dependencies>
26+
27+
</project>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package ai.dat.embedder.azure;
2+
3+
import ai.dat.core.configuration.ConfigOption;
4+
import ai.dat.core.configuration.ConfigOptions;
5+
import ai.dat.core.configuration.ReadableConfig;
6+
import ai.dat.core.factories.EmbeddingModelFactory;
7+
import ai.dat.core.utils.FactoryUtil;
8+
import com.google.common.base.Preconditions;
9+
import dev.langchain4j.model.azure.AzureOpenAiEmbeddingModel;
10+
import dev.langchain4j.model.embedding.EmbeddingModel;
11+
12+
import java.time.Duration;
13+
import java.util.LinkedHashSet;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.Set;
17+
18+
/**
19+
*
20+
*/
21+
public class AzureOpenAiEmbeddingModelFactory implements EmbeddingModelFactory {
22+
23+
public static final String IDENTIFIER = "azure-openai";
24+
25+
public static final ConfigOption<String> ENDPOINT =
26+
ConfigOptions.key("endpoint")
27+
.stringType()
28+
.noDefaultValue()
29+
.withDescription("""
30+
Supported Azure OpenAI endpoints
31+
(protocol and hostname, for example: https://aoairesource.openai.azure.com. \
32+
Replace "aoairesource" with your Azure OpenAI resource name).
33+
https://{your-resource-name}.openai.azure.com
34+
""");
35+
36+
public static final ConfigOption<String> DEPLOYMENT_ID =
37+
ConfigOptions.key("deployment-id")
38+
.stringType()
39+
.noDefaultValue()
40+
.withDescription("Deployment ID of the model which was deployed.");
41+
42+
public static final ConfigOption<String> API_VERSION =
43+
ConfigOptions.key("api-version")
44+
.stringType()
45+
.noDefaultValue()
46+
.withDescription("The API version of Azure OpenAI.");
47+
48+
public static final ConfigOption<String> API_KEY =
49+
ConfigOptions.key("api-key")
50+
.stringType()
51+
.noDefaultValue()
52+
.withDescription("Provide Azure OpenAI API key here.");
53+
54+
public static final ConfigOption<Integer> DIMENSIONS =
55+
ConfigOptions.key("dimensions")
56+
.intType()
57+
.noDefaultValue()
58+
.withDescription("Azure OpenAI embedding model dimensions");
59+
60+
public static final ConfigOption<Duration> TIMEOUT =
61+
ConfigOptions.key("timeout")
62+
.durationType()
63+
.noDefaultValue()
64+
.withDescription("The maximum time allowed for the API call to complete.");
65+
66+
public static final ConfigOption<Integer> MAX_RETRIES =
67+
ConfigOptions.key("max-retries")
68+
.intType()
69+
.defaultValue(2)
70+
.withDescription("The maximum number of retries in case of API call failure.");
71+
72+
public static final ConfigOption<Boolean> LOG_REQUESTS_AND_RESPONSES =
73+
ConfigOptions.key("log-requests-and-responses")
74+
.booleanType()
75+
.defaultValue(false)
76+
.withDescription("Whether to print the LLM requests and responses log");
77+
78+
public static final ConfigOption<Map<String, String>> CUSTOM_HEADERS =
79+
ConfigOptions.key("custom-headers")
80+
.mapType()
81+
.noDefaultValue()
82+
.withDescription("Custom HTTP headers. " +
83+
"For example: {\"content-type\": \"application/json\", " +
84+
"\"accept\": \"application/json, text/event-stream\"}");
85+
86+
@Override
87+
public String factoryIdentifier() {
88+
return IDENTIFIER;
89+
}
90+
91+
@Override
92+
public Set<ConfigOption<?>> requiredOptions() {
93+
return new LinkedHashSet<>(List.of(ENDPOINT, DEPLOYMENT_ID, API_VERSION, API_KEY));
94+
}
95+
96+
@Override
97+
public Set<ConfigOption<?>> optionalOptions() {
98+
return new LinkedHashSet<>(List.of(DIMENSIONS, TIMEOUT,
99+
MAX_RETRIES, LOG_REQUESTS_AND_RESPONSES, CUSTOM_HEADERS));
100+
}
101+
102+
@Override
103+
public Set<ConfigOption<?>> fingerprintOptions() {
104+
return Set.of(ENDPOINT, DEPLOYMENT_ID, API_VERSION);
105+
}
106+
107+
@Override
108+
public EmbeddingModel create(ReadableConfig config) {
109+
FactoryUtil.validateFactoryOptions(this, config);
110+
validateConfigOptions(config);
111+
112+
String endpoint = config.get(ENDPOINT);
113+
String deploymentId = config.get(DEPLOYMENT_ID);
114+
String apiVersion = config.get(API_VERSION);
115+
String apiKey = config.get(API_KEY);
116+
117+
AzureOpenAiEmbeddingModel.Builder builder = AzureOpenAiEmbeddingModel.builder()
118+
.endpoint(endpoint)
119+
.deploymentName(deploymentId)
120+
.serviceVersion(apiVersion)
121+
.apiKey(apiKey);
122+
123+
config.getOptional(TIMEOUT).ifPresent(builder::timeout);
124+
config.getOptional(DIMENSIONS).ifPresent(builder::dimensions);
125+
config.getOptional(MAX_RETRIES).ifPresent(builder::maxRetries);
126+
config.getOptional(LOG_REQUESTS_AND_RESPONSES).ifPresent(builder::logRequestsAndResponses);
127+
config.getOptional(CUSTOM_HEADERS).ifPresent(builder::customHeaders);
128+
129+
return builder.build();
130+
}
131+
132+
private void validateConfigOptions(ReadableConfig config) {
133+
Integer maxRetries = config.get(MAX_RETRIES);
134+
Preconditions.checkArgument(maxRetries >= 0,
135+
"'" + MAX_RETRIES.key() + "' value must be greater than or equal to 0");
136+
config.getOptional(DIMENSIONS)
137+
.ifPresent(d -> Preconditions.checkArgument(d > 0,
138+
"'" + DIMENSIONS.key() + "' value must be greater than 0"));
139+
}
140+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ai.dat.embedder.azure.AzureOpenAiEmbeddingModelFactory

dat-embedders/dat-embedder-ollama/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>dev.langchain4j</groupId>
2222
<artifactId>langchain4j-ollama</artifactId>
23-
<version>${langchain4j.vserion}</version>
23+
<version>${langchain4j.version}</version>
2424
</dependency>
2525
</dependencies>
2626

dat-embedders/dat-embedder-openai/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>dev.langchain4j</groupId>
2222
<artifactId>langchain4j-open-ai</artifactId>
23-
<version>${langchain4j.vserion}</version>
23+
<version>${langchain4j.version}</version>
2424
</dependency>
2525
</dependencies>
2626

dat-embedders/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<module>dat-embedder-ollama</module>
2323
<module>dat-embedder-jina</module>
2424
<module>dat-embedder-xinference</module>
25+
<module>dat-embedder-azure-openai</module>
2526
</modules>
2627

2728
</project>

dat-llms/dat-llm-anthropic/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>dev.langchain4j</groupId>
2222
<artifactId>langchain4j-anthropic</artifactId>
23-
<version>${langchain4j.vserion}</version>
23+
<version>${langchain4j.version}</version>
2424
</dependency>
2525
</dependencies>
2626

dat-llms/dat-llm-azure-openai/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>dev.langchain4j</groupId>
2222
<artifactId>langchain4j-azure-open-ai</artifactId>
23-
<version>${langchain4j.vserion}</version>
23+
<version>${langchain4j.version}</version>
2424
</dependency>
2525
</dependencies>
2626

0 commit comments

Comments
 (0)