SpringBoot集成qwen:0.5b实现聊天功能

365 阅读6分钟

SpringBoot集成qwen:0.5b实现聊天功能

1. qwen:0.5b是什么?

型号介绍:

Qwen 1.5是阿里云推出的一系列大语言模型。Qwen是阿里云推出的一系列基于Transformer的大语言模型,这些模型是在大量数据上进行预训练的,包括网页文本、书籍、代码等。

硬件要求:

CPU配置:CPU最低第6代Intel Core 4核心,AMD ZEN 4核心或以上;建议使用第12代Intel Core 8核心,AMD ZEN 8核心或以上内存要求:运行内存:4G及以上

Qwen 1.5的新功能如下:

6种模型大小,包括0.5B、1.8B、4B(默认)、7B、14B、32B(新)和72B显著改善人类对话模型的偏好基础模型和对话模型都支持多种语言所有模型大小都是稳定的,支持32K上下文长度原始Qwen模型提供四种不同的参数大小:1.8B、7B、14B和72B。

型号特点:

低成本部署:推理过程的最低内存需求小于2GB。大规模、高质量的训练语料库:该模型在超过22万亿个令牌的语料库上进行预训练,包括中文、英文、多语言文本、代码和数学,涵盖通用和专业领域。通过大量的消融实验,优化了预训练语料的分布。良好的性能:Qwen支持长上下文长度(1.8b、7b、14b参数模型为8K,72b参数模型为32K),在多个中英文下游评测任务(包括常识、推理、代码、数学等)上,显著优于现有开源模型,甚至在一些基准测试中的一些更大的模型。更全面的词汇覆盖:与其他基于中文和英文词汇的开源模型相比,Qwen使用了超过15万个token的词汇。该词汇表对多种语言更加友好,允许用户直接增强他们说特定语言的能力,而无需扩展词汇表。系统提示:通过系统提示,Qwen可以实现角色扮演、语言风格切换、任务设置、行为设置等功能。

2.什么是Olama?

Ollama是一个工具,可以轻松地在本地部署和运行大型语言模型(LLM)。通俗地说,如果你想在自己的电脑上运行像GPT-3这样的大型AI模型,而不是通过互联网连接到它们,那么Ollama就是一个这样做的工具。让我们仔细看看Ollama的功能和用例。

主要特点:

  1. 本地运行大型语言模型:Ollama允许用户直接在自己的设备上运行各种大型语言模型,包括Llama 2、Mistral、海豚Phi等。这允许用户在没有互联网连接的情况下使用这些先进的AI模型。
  2. 跨平台支持:Ollama支持macOS、Windows(预览版)、Linux和Docker,几乎所有主流操作系统的用户都可以使用它。
  3. 语言库和第三方库支持:它提供了一个模型库,用户可以从中下载和运行各种模型。此外,通过诸如ollama-python和ollama-js等库支持与其他软件的集成。
  4. 快速启动和易于自定义:用户可以使用简单的命令运行模型。对于那些想要自定义模型的人,Ollama还提供了一些功能,例如从GGUF导入模型,调整参数和系统消息,以及创建自定义提示。

3.什么是Open-Webui?

Open WebUI(以前称为Ollama WebUI)是一个用户友好的大型语言模型(LLM)的Web界面,支持Ollama和OpenAI兼容的API。通过直观的界面,它为用户提供了一种方便的方式来交互,训练和管理语言模型。

主要特点:

  • 直观的界面:受ChatGPT的启发,它保证了用户友好的体验。
  • 响应式设计:在桌面或移动的设备上享受无缝体验。
  • 快速响应:快速的性能和响应能力。
  • 简单的安装过程:通过Docker或Kubernetes安装,旨在提供轻松的体验。
  • 代码语法突出显示:提高代码的可读性。
  • 完整的Markdown和LaTeX支持:为了丰富交互体验,提供了广泛的Markdown和LaTeX功能。
  • 原生RAG集成:未来聊天交互的特色功能,通过#command加载文档或添加文件。
  • Web浏览功能:使用#commands来丰富聊天体验。
  • 快速预置支持:使用/Command快速访问预置提示。
  • RLHF注释:帮助构建带有评分消息的RLHF数据集。
  • 会话标签:轻松分类和查找特定聊天。
  • 多模式支持:在不同的聊天模式之间轻松切换。
  • 多模态支持:允许与支持多模态的模型(如图像)进行交互。

4.环境准备

version: '3'
services:
  ollama:
    image: ollama/ollama
    container_name: ollama
    restart: unless-stopped
    ports:
      - 11434:11434
    volumes:
      - ./data:/root/.ollama

运行

docker-compose up -d

访问http://localhost:11434/在docker shell中返回“Ollama is running“并运行命令

ollama pull llama2-chinese:7b

运行模型llama 2-chinese:7 b

ollama run llama2-chinese:7b

open-webui

version: '3'
services:
  ollama-webui:
    image: ghcr.io/open-webui/open-webui:main
    container_name: ollama-webui
    restart: unless-stopped
    ports:
      - 11433:8080
    volumes:
      - ./data:/app/backend/data
    environment:
      - OLLAMA_API_BASE_URL=http://10.11.68.77:11434/api
      - WEBUI_SECRET_KEY=TkjGEiQ@5K^j

运行

docker-compose up -d

http://localhost:11433/

您需要首次注册一个帐户

5.代码

目的

使用Open AI软件包与qwen:0.5b互联

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
   <artifactId>Qwen</artifactId>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>0.8.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <fork>true</fork>
                        <failOnError>false</failOnError>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.2</version>
                    <configuration>
                        <forkCount>0</forkCount>
                        <failIfNoTests>false</failIfNoTests>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

controller

package com.et.qwen.controller;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public Map<String, Object> showHelloWorld(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld");
        return map;
    }
    @Autowired
    ChatClient chatClient;
    @GetMapping("/ai/chat")
    public String chat(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(message);
        return chatClient.call(prompt).getResult().getOutput().getContent();
    }
}

application.yaml

server:
  port: 8088
spring:
  ai:
    openai:
      base-url: https://api.openai.com/
      api-key: sk-xxx
      embedding:
        options:
          model: text-davinci-003
      chat:
   
        api-key: sk-xxx
        base-url: http://localhost:11434
        options:
          model: qwen:0.5b 

以上只是一些关键代码,所有这些代码都可以在下面的存储库中找到

代码存储库

6.测试

2024-07-15 15:06:28 [GIN] 2024/07/15 - 07:06:28 | 200 | 4.810762037s | 172.20.0.1 | POST "/api/chat"
2024-07-15 15:06:29 [GIN] 2024/07/15 - 07:06:29 | 200 | 1.081850365s | 172.20.0.1 | POST "/v1/chat/completions"
2024-07-15 15:06:46 [GIN] 2024/07/15 - 07:06:46 | 200 | 46.802µs | 172.20.0.1