JAVA实现人工智能,采用框架SpringAI

Spring AI介绍

Spring

AI是AI工程师的一个应用框架,它提供了一个友好的API和开发AI应用的抽象,旨在简化AI应用的开发工序,例如开发一款基于ChatGPT的对话应用程序。

目前该项目已经集成了OpenAI、Azure OpenAI、Hugging

Face、Ollama等API。不过,对于集成了OpenAI接口的项目,只要再搭配One-API项目,就可以调用目前主流的大语言模型了。

使用介绍

在介绍如何使用Spring AI开发一个对话接口之前,我先介绍下ChatGPT应用的开发原理。

首先,ChatGPT是OpenAI推出的一款生成式人工智能大语言模型,OpenAI为了ChatGPT能够得到广泛应用,向开发者提供了ChatGPT的使用接口,开发者只需使用OpenAI为开发者提供的Key,向OpenAI提供的接口地址发起各种形式的请求就可以使用ChatGPT。因此,开发一款ChatGPT应用并不是让你使用人工智能那套技术进行训练和开发,而是作为搬运工,通过向OpenAI提供的ChatGPT接口发起请求来获取ChatGPT响应,基于这一流程来开发的。

第一种方式采用openai

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.2.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ai</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ai</name>
    <description>ai</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>0.8.1-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

yml 复制代码
spring:
  ai:
    openai:
      api-key: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

IndexService

java 复制代码
package com.example.service;

public interface IndexService {

    String send(String msg);
}

IndexServiceImpl

java 复制代码
package com.example.service.impl;

import com.example.service.IndexService;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private OpenAiChatClient openAiChatClient;

    @Override
    public String send(String msg) {


//        Prompt prompt = new Prompt(new UserMessage(msg));
//        return openAiChatClient.stream(prompt);

          return openAiChatClient.call(msg);

    }
}

第二种方式采用Ollama

Spring AI 不仅提供了与 OpenAI 进行API交互,同样支持与 Ollama 进行API交互。Ollama

是一个发布在GitHub上的项目,专为运行、创建和分享大型语言模型而设计,可以轻松地在本地启动和运行大型语言模型。

windows可以下载ollama,你可以用linux都行,我这里图方便

https://ollama.com/download

下载好了打开命令台 你可以自己选model
https://ollama.com/library

shell 复制代码
ollama run llama2-chinese

然后yml配置

yml 复制代码
spring:
  ai:
    ollama:
      base-url: http://127.0.0.1:11434
      chat:
        # 要跟你刚刚ollama run llama2-chinese 后面这个模块一模一样才行
        model: llama2-chinese

IndexServiceImpl

java 复制代码
package com.example.service.impl;

import com.example.service.IndexService;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private OllamaChatClient ollamaChatClient;

    @Override
    public String send(String msg) {


//        Prompt prompt = new Prompt(new UserMessage(msg));
//        return ollamaChatClient.stream(prompt);

        return ollamaChatClient.call(msg);

    }
}
相关推荐
我材不敲代码6 小时前
OpenCV的核心图像处理方法——图像边界处理、图像算术运算、阈值分割、噪声与滤波
图像处理·人工智能·opencv
未秃头的程序猿6 小时前
深入浅出MySQL事务:从ACID到Spring失效场景,2026最新实战指南
java·后端
用户4099322502126 小时前
Vue 3 静态与动态 Props 如何传递?TypeScript 类型约束有何必要?
前端·vue.js·后端
数据中穿行6 小时前
CAM多轴数控控制算法详解
人工智能
ponponon6 小时前
openclaw 打开 gui web 页面遇到 token 失效的问题原因和解决方案
后端
这张生成的图像能检测吗6 小时前
(论文速读)FastGCN:通过重要性采样快速学习图卷积网络
人工智能·深度学习·图神经网络
marteker7 小时前
宜家:对妇女的暴力行为从来都不只是影响妇女。
人工智能
kingmax542120087 小时前
AI大模型应用开发工程师学习路线(后端适合)
人工智能·大模型·后端应用开发
AI成长日志7 小时前
【微调专栏】微调前沿进展:个性化微调、自动化微调与联邦学习微调的技术突破与未来展望
人工智能·自动化