主题
Google Vertex AI
开始使用
要开始使用,请按照 Vertex AI Gemini 集成教程 中 Get started 部分所列步骤,创建一个 Google Cloud Platform 账户,并建立可访问 Vertex AI API 的新项目。
添加依赖
将以下依赖添加到你的项目的 pom.xml:
xml
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-vertex-ai</artifactId>
<version>1.22.0-beta32</version>
</dependency>1
2
3
4
5
2
3
4
5
或添加到项目的 build.gradle:
groovy
implementation 'dev.langchain4j:langchain4j-vertex-ai:1.22.0-beta32'1
试用示例代码:
PROJECT_ID 字段代表你在创建新的 Google Cloud 项目时所设置的变量。
java
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.vertexai.VertexAiEmbeddingModel;
public class VertexAiEmbeddingModelExample {
private static final String PROJECT_ID = "YOUR-PROJECT-ID";
private static final String MODEL_NAME = "textembedding-gecko@latest";
public static void main(String[] args) {
EmbeddingModel embeddingModel = VertexAiEmbeddingModel.builder()
.project(PROJECT_ID)
.location("us-central1")
.endpoint("us-central1-aiplatform.googleapis.com:443")
.publisher("google")
.modelName(MODEL_NAME)
.build();
Response<Embedding> response = embeddingModel.embed("Hello, how are you?");
Embedding embedding = response.content();
int dimension = embedding.dimension(); // 768
float[] vector = embedding.vector(); // [-0.06050122, -0.046411075, ...
System.out.println(dimension);
System.out.println(embedding.vectorAsList());
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
可用的嵌入模型
| 英文模型 | 多语模型 |
|---|---|
textembedding-gecko@001 | textembedding-gecko-multilingual@001 |
textembedding-gecko@003 | text-multilingual-embedding-002 |
text-embedding-004 |
以 @latest 为后缀的模型名指向该模型的最新版本。
默认情况下,大多数嵌入模型输出 768 维的向量嵌入(接受可配置较低维度的 "Matryoshka" 模型除外)。 该 API 每个待嵌入片段最多接受 2,048 个输入词元。 你一次最多可发送 250 个文本片段。 当你一次要求嵌入超过 250 个片段时,VertexAiEmbeddingModel 类会自动且透明地将请求拆分为多个批次。 嵌入 API 每次调用(跨所有片段)总共限制为 20,000 个词元。达到该限制时,VertexAiEmbeddingModel 会再次分批请求以避免触及该上限。
配置嵌入模型
java
EmbeddingModel embeddingModel = VertexAiEmbeddingModel.builder()
.project(PROJECT_ID)
.location("us-central1")
.endpoint("us-central1-aiplatform.googleapis.com:443") // optional
.publisher("google")
.modelName(MODEL_NAME)
.maxRetries(2) // 2 by default
.maxSegmentsPerBatch(250) // up to 250 segments per batch
.maxTokensPerBatch(2048) // up to 2048 tokens per segment
.taskType() // see below for the different task types
.titleMetadataKey() // for the RETRIEVAL_DOCUMENT task, you can specify a title
// for the text segment to identify its document origin
.autoTruncate(false) // false by default: truncates segments longer than 2,048 input tokens
.outputDimensionality(512) // for models that support different output vector dimensions
.credentials(credentials) // custom Google Cloud credentials
.build();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
嵌入任务类型
嵌入模型可用于不同场景。 为获得更好的嵌入值,你可以指定以下某一 任务:
RETRIEVAL_QUERYRETRIEVAL_DOCUMENTSEMANTIC_SIMILARITYCLASSIFICATIONCLUSTERINGQUESTION_ANSWERINGFACT_VERIFICATIONCODE_RETRIEVAL_QUERY
参见支持的模型列表。