做新房什么网站好,做电影网站前途,网站服务器续费,廊坊网站建设-纵横网络 网站Spring AI是AI工程师的一个应用框架#xff0c;它提供了一个友好的API和开发AI应用的抽象#xff0c;旨在简化AI应用的开发工序#xff0c;例如开发一款基于ChatGPT的对话应用程序。通过使用Spring Ai使我们更简单直接使用chatgpt
1.创建项目
jdk17 引入依赖 2.依赖配置
…Spring AI是AI工程师的一个应用框架它提供了一个友好的API和开发AI应用的抽象旨在简化AI应用的开发工序例如开发一款基于ChatGPT的对话应用程序。通过使用Spring Ai使我们更简单直接使用chatgpt
1.创建项目
jdk17 引入依赖 2.依赖配置
配置spring仓库 repositoriesrepositoryidspring-milestones/idnameSpring Milestones/nameurlhttps://repo.spring.io/milestone/urlsnapshotsenabledfalse/enabled/snapshots/repositoryrepositoryidspring-snapshots/idnameSpring Snapshots/nameurlhttps://repo.spring.io/snapshot/urlreleasesenabledfalse/enabled/releases/repository/repositories整个pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.2.5/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIdspringai-test/artifactIdversion0.0.1-SNAPSHOT/versionnamespringai-test/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version17/java.versionspring-ai.version0.8.1/spring-ai.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-openai-spring-boot-starter/artifactIdversion0.8.1/version
!-- scopesystem/scope--
!-- systemPath${project.basedir}/src/main/resources/lib/spring-ai-core-0.8.1.jar/systemPath--/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-openai/artifactIdversion0.8.1/version/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion${spring-ai.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes
!-- includeSystemScopetrue/includeSystemScope--/configuration/plugin/plugins/buildrepositoriesrepositoryidspring-milestones/idnameSpring Milestones/nameurlhttps://repo.spring.io/milestone/urlsnapshotsenabledfalse/enabled/snapshots/repositoryrepositoryidspring-snapshots/idnameSpring Snapshots/nameurlhttps://repo.spring.io/snapshot/urlreleasesenabledfalse/enabled/releases/repository/repositories/project
因为阿里仓库暂时还没有对应的spring-ai-openai-spring-boot- starter 依赖,所以Maven的仓库源需要使用Maven默认的仓库 源,在Maven的settings.xml中进行修改即可
mirroridmaven-default-http-blocker/idmirrorOfexternal:http:*/mirrorOfnamePseudo repository to mirror external repositories initially using HTTP ./nameurlhttp://0.0.0.0//urlblockedtrue/blocked
/mirror如果依赖还没引入成功可以下载jar包地址repo.spring.io
将jar包安装至本地仓库后使用pom文件直接引入
mvn install:install-file -Dfile你的jar包路径/你的jar包名字 -DgroupIdorg.springframework.ai -DartifactIdspring-ai-openai-spring-boot-starter -Dversion0.8.1 -Dpackagingjar
3.代码编写
application.properties
spring.application.namedemo
server.port8080spring.ai.openai.api-key你的api-key
#最好找一个中转url,不然得配置代理
spring.ai.openai.base-urlhttp://api.openai.com
spring.ai.openai.chat.options.modelgpt-3.5-turbocontroller
一个直接返回结果一个流式返回
package com.example.demo;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;import java.util.Map;RestController
public class ChatController {//日志打印private final static Logger Log LoggerFactory.getLogger(ChatController.class);//注入OpenAiChatClientAutowiredprivate OpenAiChatClient chatClient;GetMapping(/ai/generate)public Map generate(RequestParam(value message, defaultValue 给我讲个笑话) String message) {Log.info(发送的消息是:{}, message);String result chatClient.call(message);Log.info(返回的消息是:{}, result);return Map.of(generation, result);}GetMapping(value /ai/generateStream, produces text/event-stream)public FluxChatResponse generateStream(RequestParam(value message, defaultValue 给我讲个笑话) String message) {Log.info(发送的消息是:{}, message);Prompt prompt new Prompt(new UserMessage(message));System.out.println(chatClient.stream(prompt));return chatClient.stream(prompt);}
}
4.运行结果 最后附上代码 https://github.com/smx1024/springai-demo