Ollama

官网: https://ollama.com/

优点

  1. Ollama 是一个用于部署和运行大模型的工具
  2. 它能够帮助用户快速在本地运行各种大模型, 简化了大模型在本地运行的过程
  3. 用户通过几条命令就能在本地运行大模型

安装

Ollama 本地运行大模型_人工智能


Ollama 本地运行大模型_xml_02


Ollama 本地运行大模型_spring_03

使用

Ollama 本地运行大模型_java_04


Ollama 本地运行大模型_spring_05

Open WebUI

官网: https://openwebui.com/

安装

安装文档

  1. 使用 docker 运行下列命令:
docker run -d -p 18080:8080 --add-host=host.docker.internal:host-gateway -v /opt/open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main
  1. 访问 localhost:18080
  2. Ollama 本地运行大模型_xml_06

  3. 注册账号
  4. 登录

Ollama 本地运行大模型_java_07

Spring AI 介绍

官方连接:https://spring.io/projects/spring-ai

  1. Spring AI 是一个 AI 工程领域的应用框架
  2. 它的目标是将 Spring 生态系统的设计原则应用于人工智能领域
  3. Spring Ai 并不是要从零设计一个新的大模型

特点

Spring AI 提供 API 支持跨提供商的聊天, 文本图像, 嵌入模型等

演示

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 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.3.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wtw.ai</groupId>
    <artifactId>spring-ai-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-ai-demo</name>
    <description>spring-ai-demo</description>
    
    <properties>
        <java.version>17</java.version>
        <spring-ai.version>1.0.0.M1</spring-ai.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.18</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>${spring-ai.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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

</project>

application.yml

server:
  port: 8888
spring:
  application:
    name: spring-ai
  ai:
    ollama:
      base-url: http://localhost:11434

Controller

package com.wtw.ai.controller;

import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ollama")
public class OllamaController {

    @Autowired
    private OllamaChatModel chatModel;

    @PostMapping("/chat")
    public String chat(@RequestParam("msg") String msg) {
        ChatResponse ans = chatModel.call(new Prompt(msg, OllamaOptions.create().withModel("qwen:0.5b-chat")));
        return ans.getResult().getOutput().getContent();
    }

}