赞
踩
前提:
需要科学上网,key没有官方的,就找中转的key
1 pom依赖,注意添加的依赖和仓库配置
- <?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.5</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>spring_openAi</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>spring_openAi</name>
- <description>spring_openAi</description>
- <properties>
- <java.version>17</java.version>
- <spring-ai.version>0.8.1</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-openai-spring-boot-starter</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </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>false</enabled>
- </snapshots>
- </repository>
- </repositories>
-
- </project>
2 yml配置,如果是官方key,base-url就不用填了
- spring:
- ai:
- openai:
- base-url: https://xxxxx
- api-key: xxxxxxx
- chat:
- options:
- model: gpt-3.5-turbo
- temperature: 0.6
3 测试调用
- import org.springframework.ai.chat.ChatResponse;
- import org.springframework.ai.chat.prompt.Prompt;
- import org.springframework.ai.openai.OpenAiChatClient;
- import org.springframework.ai.openai.OpenAiChatOptions;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/openai")
- public class AiController_1 {
-
- @Autowired
- private OpenAiChatClient openAiChatClient;
-
- @RequestMapping("/chart_1")
- public Object chart_1() {
- String call = openAiChatClient.call("随便说点什么,10个字以内");
- return call;
- }
-
- @RequestMapping("/chart_2")
- public Object chart_2(String message) {
- //代码中配置,会覆盖application.yml中的配置
- Prompt prompt = new Prompt(
- message,
- OpenAiChatOptions.builder()
- .withModel("gpt-3.5-turbo") //大模型用哪个
- .withTemperature(0.9f) //温度高,更发散,准确性降低,温度低,更保守,准确性高
- .build());
-
- ChatResponse call = openAiChatClient.call(prompt);
- return call.getResult().getOutput();
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。