JavaAI:LangChain4j学习(一) 集成SpringBoot和阿里通义千问DashScope

使用资源

Langchain4j官方文档

LangChain4j社区DashScope

Langchain4jMaven仓库

阿里通义千问API

记录

本人使用版本: 1.0.0-beta2

不使用Spring Boot ,仅集成

额外:如果不使用SpringBoot,仅集成LangChain4j - DashScope

如果使用的langChain4j-dashScope版本小于等于1.0.0-alpha1

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-dashscope</artifactId>
    <version>${previous version here}</version>
</dependency>

如果使用的langChain4j-dashScope版本大于等于1.0.0-alpha1

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-community-dashscope</artifactId>
    <version>${langchain4j.version}</version>
</dependency>

集成Spring Boot 启动器

Spring Boot 启动器有助于创建和配置语言模型、嵌入模型、嵌入存储、 和其他核心 LangChain4j 组件。如果要使用Spring Boot 相关启动器, 需要在pom.xml中引入相应依赖项

pom.xml :

如果使用的langChain4j-dashScope版本小于等于1.0.0-alpha1

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
    <version>${langchain4j.version}</version>
</dependency>

如果使用的langChain4j-dashScope版本大于等于1.0.0-alpha1

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
    <version>${latest version here}</version>
</dependency>

或者使用 BOM 管理依赖项:

<dependencyManagement>
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-community-bom</artifactId>
        <version>${latest version here}</version>
        <typ>pom</typ>
        <scope>import</scope>
    </dependency>
</dependencyManagement>

配置

引入依赖后,在propertity中配置注册

langchain4j.community.dashscope.api-key=<API Key>
langchain4j.community.dashscope.model-name==<Model Name>

#是否输出日志
langchain4j.community.dashscope.log-requests=true
langchain4j.community.dashscope.log-responses=true
#日志等级
logging.level.dev.langchain4j=DEBUG

其他相关参数也可添加,例如 QwenChatModel参数

langchain4j.community.dashscope.temperature=0.7
langchain4j.community.dashscope.max-tokens=4096

官方参数如下:
langchain4j-community-dashscope 有四个model可使用:

QwenChatModel
QwenStreamingChatModel
QwenLanguageModel
QwenStreamingLanguageModel

QwenChatModel 参数如下,其他也相同

PropertyDescriptionDefault Value
baseUrlThe URL to connect to. You can use HTTP or websocket to connect to DashScopeText Inference and Multi-Modal
apiKeyThe API Key
modelNameThe model to use.qwen-plus
topPThe probability threshold for kernel sampling controls the diversity of texts generated by the model. the higher the , the more diverse the generated texts, and vice versa. Value range: (0, 1.0]. We generally recommend altering this or temperature but not both.top_p
topKThe size of the sampled candidate set during the generation process.
enableSearchWhether the model uses Internet search results for reference when generating text or not.
seedSetting the seed parameter will make the text generation process more deterministic, and is typically used to make the results consistent.
repetitionPenaltyRepetition in a continuous sequence during model generation. Increasing reduces the repetition in model generation, 1.0 means no penalty. Value range: (0, +inf)repetition_penalty
temperatureSampling temperature that controls the diversity of the text generated by the model. the higher the temperature, the more diverse the generated text, and vice versa. Value range: [0, 2)
stopsWith the stop parameter, the model will automatically stop generating text when it is about to contain the specified string or token_id.
maxTokensThe maximum number of tokens returned by this request.
listenersListeners that listen for request, response and errors.

案例使用

例如 QwenChatModel

ChatLanguageModel qwenModel = QwenChatModel.builder()
                    .apiKey("You API key here")
                    .modelName("qwen-max")
                    .build();
<think>嗯,用户的是如何下载配置langchain4j依赖。结合系统指令站内引用,需要优先参考引用[1][3]中的具体依赖配置信息。用户明确要求"下载依赖",这通常涉及Maven配置。引用[1]直接给出了核心依赖坐标:dev.langchain4langchain4j-open-ai0.29.1,但用户没有说明具体要对接哪个AI服务。考虑到引用[3]显示最新beta版本是1.0.0-beta2,这里存在版本差异需要提示。注意到三个关键点:1基础依赖必须包含langchain4j核心库,如引用[3]所示2按目标AI平台选择子模块:open-aidashscope等3SpringBoot项目建议用spring-boot-starter简化配置(引用[3]示例)用户可能是Java开发者,提很技术化但没说明具体使用场景。准备给出两套方案:通用Maven配置(适合所有Java项目)SpringBoot特化方案(引用[3]方式),这样能覆盖大部分情况。需要特别注意版本冲突风险。引用[1]用0.29.1而引用[3]用1.0.0-beta2,说明目前处在版本过渡期。准备在回答中加入明确的版本提示,避免用户配置错误。扩展题方面,用户可能下步需要:验证依赖是否导入成功,写测试代码,或者配置API密钥。这些应该是自然的延伸题,特别是API密钥配置在引用[3]里有涉及但当前题未提及。</think> 以下是在不同场景下下载配置 LangChain4j 依赖的完整指南: ### 🧩 基础核心依赖(必选) 在 Maven 项目的 `pom.xml` 中添加: ```xml <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-core</artifactId> <!-- 根据需求选择版本 --> <version>0.29.1</version> <!-- 稳定版[^1] --> <!----> <version>1.0.0-beta2</version> <!-- 最新测试版[^3] --> </dependency> ``` ### ⚙️ 按场景扩展依赖 #### 场景 1:连接 OpenAI ```xml <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai</artifactId> <version>0.29.1</version> <!-- 与核心依赖版本保持致[^1] --> </dependency> ``` #### 场景 2:阿里云百炼平台(通义模型) ```xml <!-- 阿里云大模型连接器 --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId> <version>1.0.0-beta2</version> <!-- 专有组件版本[^3] --> </dependency> <!-- 补充 WebFlux 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> ``` #### 场景 3:Spring Boot 集成 ```xml <!-- LangChain4j Spring Starter --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-spring-boot-starter</artifactId> <version>1.0.0-beta2</version> <!-- 需查看官方最新版本[^2] --> </dependency> ``` ### 🔄 配置步骤(以 Spring Boot 为例) 1. **初始化项目**: 使用 [Spring Initializr](https://start.spring.io/) 创建项目,勾选 `Spring Web`/`WebFlux` 2. **添加依赖**: 按上述场景将依赖添加到 `pom.xml` 3. **API 密钥配置**: 在 `application.yml` 中配置模型服务密钥: ```yaml # 以 OpenAI 为例 langchain4j: open-ai: api-key: ${OPENAI_API_KEY} ``` 4. **注入服务**: ```java @Service public class ChatService { private final OpenAiChatModel chatModel; public ChatService(OpenAiChatModel chatModel) { this.chatModel = chatModel; } public String generate(String prompt) { return chatModel.generate(prompt); } } ``` ### 💡 重要提醒 1. **版本致性**: ```mermaid graph LR A[核心依赖] --> B[功能模块] A --> C[Spring Starter] ``` 所有组件版本需保持统(如都用 `0.29.1` 或都用 `1.0.0-beta2` 系列) 2. **阿里云专用配置**: 需配置 `DASHSCOPE_API_KEY` 环境变量或配置文件(引用[^3]) 3. **仓库检查**: 若使用测试版(`beta`),需确保 Maven 配置包含快照仓库: ```xml <repositories> <repository> <id>snapshots</id> <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url> </repository> </repositories> ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值