SpringAI通过Ollama连接大语言模型通义千问

随着大语言模型发展越来越成熟,Apache开源组织也出了自己的SpringAI开源工程

Spring AI项目旨在简化包含人工智能功能的应用程序的开发,避免不必要的复杂性。
该项目从著名的 Python 项目(例如 LangChain 和 LlamaIndex)中汲取灵感,但 Spring AI 并不是这些项目的直接移植。
支持所有主要模型,例如 OpenAI、Ollama、Azure OpenAI、Amazon Bedrock、Huggingface、Google VertextAI、Mistral AI。
支持的模型类型包括“聊天”和“文本到图像”,还有更多模型类型正在开发中。
支持所有主要矢量数据库提供商,例如 Apache Cassandra、Azure 矢量搜索、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate

本博文分两部分,ollama环境搭建,SpringAI连接通义千问(源代码放在文章最后,可以下载)

说明:本代码使用jdk版本为17

先看效果:

一、ollama环境搭建

可以参考这篇文章的中的第三部分  【三、LLM语言模型搭建】,搭建是一样的,在此就不重复了

FastGTP环境搭建,可直接使用在项目上

二、SpringAI连接通义千问

1、配置文件,如下 上代码

server:
  port: 8080
spring:
  application:
    name: ai
  ai:
    ollama:
      base-url: http://192.168.1.200:11434
      chat:
        enabled: true
        options:
          model: qwen:0.5b

2、java连接关键代码

 String systemPrompt = "{prompt}";
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);
        String userPrompt = message;
        Message userMessage = new UserMessage(userPrompt);
        Message systemMessage = systemPromptTemplate.createMessage(MapUtil.of("prompt", "you are a helpful AI assistant"));

        List<Message> list = new ArrayList<>();
        list.add(userMessage);
        list.add(systemMessage);
        Prompt prompt = new Prompt(list);
        Flux<String> response =chatClient.stream(prompt).flatMap(res->{
            List<Generation> generations = res.getResults();
            if(CollUtil.isNotEmpty(generations)){
                for(Generation generation:generations){
                    AssistantMessage assistantMessage =  generation.getOutput();
                    String content = assistantMessage.getContent();
                    System.out.println(content);
                    return Flux.just(content);
                }
            }
            return Flux.empty();
        });

3、测试,访问 http://localhost:8080/chat?message=用python写个排序算法:

使用Apifox工具测(百度自行下载),截下截图:

三、源代码下载

AI-Auto-config  工程是使用springboot的bootstrap.yml配置连接ollama
AI-Manual-Config  手动配置连接ollama方式

下载地址:百度网盘 请输入提取码

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值