执门文章
SpringAI通过Ollama连接大语言模型通义千问
于 2024-05-16 23:13:08 发布
访问266
收藏0
文章标签:
大语言模型
人工智能
Java
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

先看效果:SpringAI通过Ollama连接大语言模型通义千问

一、ollama环境搭建

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

https://www.yxtblog.com/article/1/240428230131000136

二、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工具测(百度自行下载),截下截图:

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


三、源代码下载

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

AI-Auto-config  工程是使用springboot的bootstrap.yml配置连接ollama

AI-Manual-Config  手动配置连接ollama方式

下载地址:https://pan.baidu.com/s/10-Xm5_znPtKsh0GqgZpbWw?pwd=im9t

于 2024-05-16 23:13:08 发布
访问266
收藏0

实力雄厚的技术网站

产品

概述

资源

文档

版权问题

请联系客服

联系我们

联系我们


© 财瑞智能科技 2024蜀ICP备2023018175号-2川公网安备51011202000656号