主题
DashScope(通义千问)
DashScope 是由阿里云 开发的平台。 它提供用于模型可视化、监控与调试的接口,尤其适用于在生产环境中处理 AI/ML 模型的场景。该平台允许用户可视化性能指标、追踪模型行为,并在部署周期的早期 发现潜在问题。
通义(Qwen) 模型是由阿里云 开发的一系列生成式 AI 模型。 Qwen 系列模型专为文本生成、摘要、问答及各类 NLP 任务而设计。
更多细节可参考 DashScope 文档。 LangChain4j 通过 DashScope Java SDK 与 DashScope 集成。
Maven 依赖
你可以在纯 Java 或 Spring Boot 应用中使用千帆与 LangChain4j。
纯 Java
NOTE
自 1.0.0-alpha1 起,langchain4j-dashscope 已迁移至 langchain4j-community,并更名为 langchain4j-community-dashscope。
在 1.0.0-alpha1 之前:
xml
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-dashscope</artifactId>
<version>${previous version here}</version>
</dependency>1.0.0-alpha1 及更高版本:
xml
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope</artifactId>
<version>${latest version here}</version>
</dependency>Spring Boot
NOTE
自 1.0.0-alpha1 起,langchain4j-dashscope-spring-boot-starter 已迁移至 langchain4j-community,并更名为 langchain4j-community-dashscope-spring-boot-starter。
在 1.0.0-alpha1 之前:
xml
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-dashscope-spring-boot-starter</artifactId>
<version>${previous version here}</version>
</dependency>1.0.0-alpha1 及更高版本:
xml
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
<version>${latest version here}</version>
</dependency>或者,你可以使用 BOM 来统一管理依赖:
xml
<dependencyManagement>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-bom</artifactId>
<version>${latest version here}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>可配置参数
langchain4j-community-dashscope 有 4 种模型可供使用:
QwenChatModelQwenStreamingChatModelQwenLanguageModelQwenStreamingLanguageModel
langchain4j-dashscope 提供文本生成图像的模型
WanxImageModel
QwenChatModel
QwenChatModel 在你初始化时有以下可配置参数:
| 属性 | 描述 | 默认值 |
|---|---|---|
| baseUrl | 连接所用的 URL。可用 HTTP 或 websocket 连接 DashScope | 文本推理 与 多模态 |
| apiKey | API 密钥 | |
| modelName | 所使用的模型。 | qwen-plus |
| topP | 核采样的概率阈值,控制模型生成文本的多样性。top_p 越高,生成的文本越多样,反之亦然。取值范围:(0, 1.0]。我们通常建议调整此项或 temperature,但不要同时调整二者。 | |
| topK | 生成过程中采样候选集的大小。 | |
| enableSearch | 模型在生成文本时是否使用互联网搜索结果作为参考。 | |
| seed | 设置 seed 参数会使文本生成过程更确定,通常用于让结果保持一致。 | |
| repetitionPenalty | 模型生成中连续序列的重复度。增大 repetition_penalty 会减少模型生成的重复,1.0 表示无惩罚。取值范围:(0, +inf) | |
| temperature | 采样温度,控制模型生成文本的多样性。温度越高,生成的文本越多样,反之亦然。取值范围:[0, 2) | |
| stops | 通过 stop 参数,当模型即将生成指定的字符串或 token_id 时会自动停止生成文本。 | |
| maxTokens | 本请求返回的最大词元数。 | |
| listeners | 监听请求、响应与错误的监听器。 |
QwenStreamingChatModel
与 QwenChatModel 相同
QwenLanguageModel
与 QwenChatModel 相同,只是没有 listeners。
QwenStreamingLanguageModel
与 QwenChatModel 相同,只是没有 listeners。
示例
纯 Java
你可以用以下代码初始化 QwenChatModel:
java
ChatModel qwenModel = QwenChatModel.builder()
.apiKey("You API key here")
.modelName("qwen-max")
.build();或针对其他参数做更多自定义:
java
ChatModel qwenModel = QwenChatModel.builder()
.apiKey("You API key here")
.modelName("qwen-max")
.enableSearch(true)
.temperature(0.7)
.maxTokens(4096)
.stops(List.of("Hello"))
.build();如何调用文生图:
java
WanxImageModel wanxImageModel = WanxImageModel.builder()
.modelName("wanx2.1-t2i-plus")
.apiKey("阿里云百炼apikey")
.build();
Response<Image> response = wanxImageModel.generate("美女");
System.out.println(response.content().url());Spring Boot
引入 langchain4j-community-dashscope-spring-boot-starter 依赖后,你可以仅用下面的配置注册 QwenChatModel bean:
properties
langchain4j.community.dashscope.chat-model.api-key=<You API Key here>
langchain4j.community.dashscope.chat-model.model-name=qwen-max
# The properties are the same as `QwenChatModel`
# e.g.
# langchain4j.community.dashscope.chat-model.temperature=0.7
# langchain4j.community.dashscope.chat-model.max-tokens=4096更多示例
更多细节可在 LangChain4j Community 中查看