大模型:从依赖引入到文生图实战)
LangChain4j 集成阿里云 DashScopeQwen大模型从依赖引入到文生图实战【免费下载链接】langchain4jLangChain4j is an idiomatic, open-source Java library for building LLM-powered applications on the JVM. It offers a unified API over popular LLM providers and vector stores, and makes implementing tool calling (including MCP support), agents and RAG easy. It integrates seamlessly with enterprise Java frameworks like Quarkus and Spring Boot.项目地址: https://gitcode.com/GitHub_Trending/la/langchain4j导读DashScope百炼是阿里云推出的大模型服务平台Qwen通义千问系列模型在文本生成、摘要、问答与各类 NLP 任务上表现成熟。本指南以 docs/docs/integrations/language-models/dashscope.md 为核心系统讲解如何在 LangChain4j 中接入 DashScope从依赖迁移背景、Maven 坐标与 BOM 管理到QwenChatModel等四个模型类的完整参数说明再到 Plain Java 与 Spring Boot 两种场景的配置与调用代码并延伸到WanxImageModel文生图能力。读完本文你将掌握在 JVM 应用中快速接入通义千问对话、流式对话与文生图模型的完整方案。平台与模型背景DashScope 是阿里云旗下面向 AI/ML 生产环境的模型服务平台提供模型可视化、监控与调试能力Qwen通义千问是阿里云推出的生成式 AI 模型系列覆盖文本生成、摘要、问答等多种 NLP 任务。LangChain4j 对 DashScope 的集成基于 DashScope Java SDK 实现通过QwenChatModel、QwenStreamingChatModel、QwenLanguageModel、QwenStreamingLanguageModel四个模型类覆盖对话与文本生成场景并通过WanxImageModel提供文本生成图片能力。Maven 依赖引入DashScope 集成适用于纯 Java 与 Spring Boot 两种应用形态。版本迁移说明1.0.0-alpha1 前后:::note 自1.0.0-alpha1起原langchain4j-dashscope已迁移至 LangChain4j Community 仓库并更名为langchain4j-community-dashscopeSpring Boot 集成包同步迁移为langchain4j-community-dashscope-spring-boot-starter。 :::1.0.0-alpha1 之前旧坐标仅作兼容参考dependency groupIddev.langchain4j/groupId artifactIdlangchain4j-dashscope/artifactId version${previous version here}/version /dependency1.0.0-alpha1 及以后推荐使用dependency groupIddev.langchain4j/groupId artifactIdlangchain4j-community-dashscope/artifactId version${latest version here}/version /dependencySpring Boot 依赖1.0.0-alpha1 之前旧坐标dependency groupIddev.langchain4j/groupId artifactIdlangchain4j-dashscope-spring-boot-starter/artifactId version${previous version here}/version /dependency1.0.0-alpha1 及以后推荐使用dependency groupIddev.langchain4j/groupId artifactIdlangchain4j-community-dashscope-spring-boot-starter/artifactId version${latest version here}/version /dependency使用 BOM 统一管理版本为避免多个 Community 组件版本不一致推荐通过langchain4j-community-bom统一管理依赖版本dependencyManagement dependency groupIddev.langchain4j/groupId artifactIdlangchain4j-community-bom/artifactId version${latest version here}/version typepom/type scopeimport/scope /dependency /dependencyManagement在 BOM 声明之后各模块只需声明groupId与artifactId无需再写version。可用模型类与参数详解langchain4j-community-dashscope提供 4 个文本模型类另有WanxImageModel负责文生图模型类说明QwenChatModel非流式对话模型面向 ChatModel 接口QwenStreamingChatModel流式对话模型逐 token 返回响应QwenLanguageModel非流式文本生成模型无 listeners 参数QwenStreamingLanguageModel流式文本生成模型无 listeners 参数WanxImageModel文本生成图片T2I模型QwenChatModel 参数表初始化QwenChatModel时可配置以下参数PropertyDescriptionDefault ValuebaseUrl连接地址支持 HTTP 或 websocket 连接 DashScopeText Inference 与 Multi-ModalapiKeyAPI KeymodelName使用的模型名qwen-plustopP核采样的概率阈值控制文本多样性。top_p 越大生成文本越多样反之越小。取值范围(0, 1.0]。一般建议 top_p 与 temperature 只调其一不要同时调整topK生成过程中采样的候选集大小enableSearch生成文本时是否参考互联网搜索结果seed设置种子使生成过程更确定常用于保证结果一致性repetitionPenalty对连续序列中重复内容的惩罚。增大 repetition_penalty 可降低生成重复1.0 表示无惩罚。取值范围(0, inf)temperature采样温度控制文本多样性。温度越高生成越多样反之越收敛。取值范围[0, 2)stops停止参数模型即将生成指定字符串或 token_id 时自动停止maxTokens本次请求返回的最大 token 数listeners监听请求、响应与错误的监听器QwenStreamingChatModel参数与QwenChatModel完全一致QwenLanguageModel与QwenStreamingLanguageModel参数同样一致仅不支持listeners。代码实战Plain Java 快速接入基础初始化ChatModel qwenModel QwenChatModel.builder() .apiKey(You API key here) .modelName(qwen-max) .build();自定义参数初始化ChatModel qwenModel QwenChatModel.builder() .apiKey(You API key here) .modelName(qwen-max) .enableSearch(true) .temperature(0.7) .maxTokens(4096) .stops(List.of(Hello)) .build();说明apiKey从 阿里云百炼控制台 获取modelName可选qwen-plus、qwen-max、qwen-turbo等模型需与 DashScope 平台开通的模型一致enableSearch(true)启用联网搜索增强适用于实时信息类问答temperature(0.7)与maxTokens(4096)分别控制生成随机性与最大输出长度。文生图WanxImageModelWanxImageModel wanxImageModel WanxImageModel.builder() .modelName(wanx2.1-t2i-plus) .apiKey(阿里云百炼apikey) .build(); ResponseImage response wanxImageModel.generate(美女); System.out.println(response.content().url());WanxImageModel实现了 LangChain4j 核心的 ImageModel 接口其generate(String prompt)方法接收一段提示词并返回ResponseImage。从源码看Image 对象携带生成图片的urlURI、base64Data、mimeType与revisedPrompt等字段因此示例中response.content().url()可以直接拿到图片地址用于展示或下载。Spring Boot 集成引入langchain4j-community-dashscope-spring-boot-starter后通过application.properties即可注册QwenChatModelBeanlangchain4j.community.dashscope.chat-model.api-keyYou API Key here langchain4j.community.dashscope.chat-model.model-nameqwen-max # 其余属性与 QwenChatModel 一致 # e.g. # langchain4j.community.dashscope.chat-model.temperature0.7 # langchain4j.community.dashscope.chat-model.max-tokens4096Spring Boot 场景下属性名采用 kebab-case 风格如max-tokens、model-name与 Java Builder 中的驼峰参数一一对应配置完成后可直接通过Autowired注入ChatModel使用。更深入的示例与能力扩展本指南对应的完整集成测试可参考 LangChain4j Community 仓库中models/langchain4j-community-dashscope/src/test/java/dev/langchain4j/community/model/dashscope目录下的测试用例。结合 docs/docs/integrations/language-models/index.md 的能力对照表DashScope 集成支持流式输出Streaming与工具调用Toolssync/streaming输入模态支持 text、image、audio并具备可观测性Observability支持。这意味着你可以通过QwenStreamingChatModel实现逐 token 流式返回的用户体验在AiServices中配置QwenChatModel让通义千问具备工具调用能力结合 LangChain4j 的 ChatMemory 为千问模型赋予多轮对话记忆通过WanxImageModel将文本提示词转换为图片扩展多模态应用。小结本文完整梳理了 LangChain4j 接入阿里云 DashScope 的路径从1.0.0-alpha1的 Community 迁移背景到四种 Qwen 文本模型与WanxImageModel的参数配置再到 Plain Java 与 Spring Boot 双场景的实战代码。核心要点可归纳为新项目一律使用langchain4j-community-dashscope坐标并通过langchain4j-community-bom统一版本QwenChatModel的topP与temperature建议只调其一enableSearch可按需开启联网增强Spring Boot 场景属性名采用 kebab-case与 Java Builder 参数一一对应文生图场景使用WanxImageModelresponse.content().url()即可获取生成的图片地址。在实际生产使用中请务必在阿里云百炼控制台完成模型开通并妥善保管 API Key。【免费下载链接】langchain4jLangChain4j is an idiomatic, open-source Java library for building LLM-powered applications on the JVM. It offers a unified API over popular LLM providers and vector stores, and makes implementing tool calling (including MCP support), agents and RAG easy. It integrates seamlessly with enterprise Java frameworks like Quarkus and Spring Boot.项目地址: https://gitcode.com/GitHub_Trending/la/langchain4j创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考