langchain4j整合springboot

发布于:2025-06-17 ⋅ 阅读:(11) ⋅ 点赞:(0)

1.搭建项目架子

你好! 这是你第一次使用 **Markdown编辑器** 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

在这里插入图片描述

配置文件

参考官网配置引入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.yd</groupId>
    <artifactId>langchain4j_springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>langchain4j_springboot</name>
    <description>langchain4j_springboot</description>

    <properties>
        <java.version>17</java.version>
<!--        -->
        <langchain4j.version>1.0.0-beta1</langchain4j.version>
    </properties>
    <dependencies>
<!--        -->
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
            <version>${langchain4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>
<!--    -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>dev.langchain4j</groupId>
                <artifactId>langchain4j-community-bom</artifactId>
                <version>${langchain4j.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

server:
  port: 9090


langchain4j:
  community:
    dashscope:
      chat-model:
        api-key: xxx
        model: qwen-max

Controller测试

package com.yd.langchain4j_springboot;

import dev.langchain4j.community.model.dashscope.QwenChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("ai")
public class ChatController {
    @Autowired
    QwenChatModel qwenChatModel;
    @RequestMapping("/chat")
    public String test(@RequestParam(defaultValue="你是谁") String message){
        String chat=qwenChatModel.chat(message);
        return chat;
    }
    @RequestMapping("/a")
    public String test2(){
        String chat="a";
        return chat;
    }
}

测试结果在这里插入图片描述


补充:

  • 阿里云api注册以及使用:官网在这里插入图片描述
  • 两个报错
    网页漏看路径:Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Mon Jun 16 23:09:14 CST 2025 There was an unexpected error (type=Not Found, status=404).在这里插入图片描述
    环境配置版本问题
  • Consider defining a bean of type 'dev.langchain4j.community.model.dashscope.QwenChatModel
  • dev.langchain4j:langchain4j-community-dashscope-spring-boot-starter:jar:unknown was not found in https://maven.aliyun.com/nexus/content/groups/public/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of alimaven has elapsed or updates are forced

网站公告

今日签到

点亮在社区的每一天
去签到