Skip to content
LangChain4j 文档版本 1.22.0 源快照 e8d1f405 更新于 2026-10-14

OVHcloud AI Endpoints

项目搭建

OVHcloud AI Endpoints 模型与 OpenAI API 兼容,因此你可以使用与 OpenAI 模型相同的代码,只需更改 base URL 和 API 密钥。

Maven 依赖

xml
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-open-ai</artifactId>
    <version>1.13.0</version>
</dependency>

API 密钥设置

将你的 OVHcloud AI API 密钥添加到项目中。

java
public static final String OVHAI_AI_API_KEY = System.getenv("OVHAI_AI_API_KEY");

别忘了把你的 API 密钥设置为环境变量。

shell
export OVHAI_AI_API_KEY=your-api-key #For Unix OS based
SET OVHAI_AI_API_KEY=your-api-key #For Windows OS

关于如何获取 OVHcloud AI API 密钥的更多细节可在这里找到

嵌入

OVHcloud AI Embeddings 模型允许你对句子进行嵌入,在应用中使用它很简单。我们提供了一个简单示例,帮助你上手 OVHcloud AI Embeddings 模型集成。

创建一个类并添加以下代码。

java
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.ovhai.OvhAiEmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingMatch;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;

import java.util.List;

public class OvhAiEmbeddingSimpleExample {

    public static void main(String[] args) {
        EmbeddingModel model = OpenAiEmbeddingModel.builder()
                .baseUrl("https://oai.endpoints.kepler.ai.cloud.ovh.net/v1")
                .apiKey(System.getenv("OVHAI_AI_API_KEY"))
                .modelName("Qwen3-Embedding-8B")
                .logRequests(true)
                .logResponses(false) // embeddings are huge in logs
                .build();

        // For simplicity, this example uses an in-memory store, but you can choose any external compatible store for production environments.
        EmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();

        TextSegment segment1 = TextSegment.from("I like football.");
        Embedding embedding1 = embeddingModel.embed(segment1).content();
        embeddingStore.add(embedding1, segment1);

        TextSegment segment2 = TextSegment.from("The weather is good today.");
        Embedding embedding2 = embeddingModel.embed(segment2).content();
        embeddingStore.add(embedding2, segment2);

        String userQuery = "What is your favourite sport?";
        Embedding queryEmbedding = embeddingModel.embed(userQuery).content();
        EmbeddingSearchRequest searchRequest = EmbeddingSearchRequest.builder()
                .queryEmbedding(queryEmbedding)
                .maxResults(1)
                .build();
        EmbeddingSearchResult<TextSegment> searchResult = embeddingStore.search(searchRequest);
        EmbeddingMatch<TextSegment> embeddingMatch = searchResult.matches().get(0);

        System.out.println("Question: " + userQuery); // What is your favourite sport?
        System.out.println("Response: " + embeddingMatch.embedded().text()); // I like football.
    }

}

在本例中,我们会添加 2 个文本片段,但 LangChain4j 内置支持从各种来源加载文档: 文件系统、URL、Amazon S3、Azure Blob Storage、GitHub、腾讯云 COS。 此外,LangChain4j 支持解析多种文档类型: 文本、pdf、doc、xls、ppt。

输出将与下面类似:

plaintext
Question: What is your favourite sport?
Response: I like football.

当然,你可以将 OVHCloud Embeddings 与 RAG(检索增强生成)技术结合使用。

在 RAG 中,你将学习如何使用 LangChain4j 的摄入、检索与高级检索等 RAG 技术。

许多参数是在幕后设置的,例如超时、模型类型与模型参数。 在设置模型参数中,你将学习如何显式地设置这些参数。

更多示例

如果你想查看更多示例,可以在 langchain4j-examples 项目中找到它们。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。