赞
踩
这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
Spring Boot、Spring Cloud、Spring Data,作为一名Java程序员,相信您对这些概览早已耳熟能详,或者天天在用,在Spring体系中,它们都被称为project,而本系列的核心Spring AI,也是个project,和 Spring Boot、Spring Cloud属于同一级别的存在,其定位是面向人工智能的应用框架
Spring AI有以下特点:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
<?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"> <modelVersion>4.0.0</modelVersion> <modules> <module>hello-openai</module> </modules> <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.bolingcavalry</groupId> <artifactId>springai-tutorials</artifactId> <version>1.0-SNAPSHOT</version> <name>springai-tutorials</name> <description>Simple AI Application demos</description> <packaging>pom</packaging> <properties> <java.version>17</java.version> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>0.8.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <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> </project>
<?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> <artifactId>springai-tutorials</artifactId> <groupId>com.bolingcavalry</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>hello-openai</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
[INFO] | \- org.springframework.ai:spring-ai-openai:jar:0.8.1:compile [INFO] | +- org.springframework.ai:spring-ai-core:jar:0.8.1:compile [INFO] | | +- io.swagger.core.v3:swagger-annotations:jar:2.2.20:compile [INFO] | | +- com.github.victools:jsonschema-module-swagger-2:jar:4.33.1:compile [INFO] | | +- org.springframework.cloud:spring-cloud-function-context:jar:4.1.0:compile [INFO] | | | +- net.jodah:typetools:jar:0.6.2:compile [INFO] | | | \- org.springframework.cloud:spring-cloud-function-core:jar:4.1.0:compile [INFO] | | +- org.antlr:stringtemplate:jar:4.0.2:compile [INFO] | | | \- org.antlr:antlr-runtime:jar:3.3:compile [INFO] | | +- org.antlr:antlr4-runtime:jar:4.13.1:compile [INFO] | | +- io.projectreactor:reactor-core:jar:3.6.4:compile [INFO] | | | \- org.reactivestreams:reactive-streams:jar:1.0.4:compile [INFO] | | +- org.springframework:spring-messaging:jar:6.1.5:compile [INFO] | | \- com.knuddels:jtokkit:jar:1.0.0:compile [INFO] | +- org.springframework.ai:spring-ai-retry:jar:0.8.1:compile [INFO] | | +- org.springframework.retry:spring-retry:jar:2.0.5:compile [INFO] | | \- org.springframework:spring-webflux:jar:6.1.5:compile [INFO] | +- io.rest-assured:json-path:jar:5.3.2:compile [INFO] | | +- org.apache.groovy:groovy-json:jar:4.0.20:compile [INFO] | | +- org.apache.groovy:groovy:jar:4.0.20:compile [INFO] | | \- io.rest-assured:rest-assured-common:jar:5.3.2:compile [INFO] | | \- org.apache.commons:commons-lang3:jar:3.13.0:compile [INFO] | +- com.github.victools:jsonschema-generator:jar:4.31.1:compile [INFO] | | \- com.fasterxml:classmate:jar:1.6.0:compile [INFO] | +- com.github.victools:jsonschema-module-jackson:jar:4.31.1:compile [INFO] | \- org.springframework:spring-context-support:jar:6.1.5:compile
# 调用OpenAI接口时表明身份的API Key,前面的章节有提到如何生成一个免费的
spring.ai.openai.api-key=sk-xxxxxx
# 调用OpenAI接口时的基础地址,如果用的是chatanywhere的API Key,这里就要用chatanywhere提供的地址,
# 如果用的是OpenAI的原生API Key,就不用配置这个参数
spring.ai.openai.base-url=https://api.chatanywhere.tech
# 用到的模型
spring.ai.openai.chat.options.model=gpt-3.5-turbo
# temperature越小,回答的内容越严谨,temperature越大,回答的内容越有创造性
spring.ai.openai.chat.options.temperature=0.7
package com.bolingcavalry.helloopenai;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.bolingcavalry.helloopenai.controller; import org.springframework.ai.chat.ChatClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController public class SimpleAiController { // 负责处理OpenAI的bean,所需参数来自properties文件 private final ChatClient chatClient; public SimpleAiController(ChatClient chatClient) { this.chatClient = chatClient; } @PostMapping("/ai/simple") public Map<String, String> completion(@RequestBody Map<String,String> map) { return Map.of("generation", chatClient.call(map.get("message"))); } }
curl --request POST \
--url http://127.0.0.1:8080/ai/simple \
--header 'content-type: application/json' \
--data '{
"message":"你是精通中国历史的专家,请回答:三国时期,魏延的子午谷奇谋是否可行?能在潼关把魏国的援兵堵住吗?如果魏国援兵走武关进入,整个计划是否还有意义?"
}'
{"generation":"作为专家,我可以告诉你,魏延的子午谷奇谋在理论上是可行的。子午谷是一个地势险要的地方,如果能够在那里设下埋伏,堵住魏国的援兵是有可能的。但是,这需要充分的准备和合理的部署,同时也需要对魏国援兵的行动有准确的情报。\n\n如果魏国援兵走武关进入,整个计划的意义可能会有所减弱。因为子午谷的奇谋是建立在堵住潼关的前提下,如果援兵绕道武关进入,那么计划的效果可能会受到影响。在这种情况下,需要及时调整计划,采取其他措施来应对敌人的行动。\n\n总的来说,魏延的子午谷奇谋在三国时期是一个具有一定可行性的战术计划,但是在实际执行过程中需要根据敌军的行动及时调整和变通。"}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。