Springboot集成ChatGPT,无需额外依赖,实现畅聊

        无需引用ChatGPT额外依赖,实现和ChatGPT对话。

        1.创建一个springboot工程

        只引入 web 和 lombok依赖,无需额外依赖        

<?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.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>chatgpt</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>chatgpt</name>
    <description>chatgpt</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

        2. Application 启动类

@RestController
@SpringBootApplication
public class ChatgptApplication {

    public static final String MODEL_3 = "gpt-3.5-turbo";
    public static final String API_URL = "https://api.openai.com/v1/chat/completions";
    private static final String OPENAI_KEY = {your_key};

    public static void main(String[] args) {
        SpringApplication.run(ChatgptApplication.class, args);
    }

    @GetMapping("/chat")
    public String chat(@RequestParam("prompt") String prompt) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add((request, body, execution) -> {
            final HttpHeaders headers = request.getHeaders();
            headers.add("Authorization", "Bearer " + OPENAI_KEY);
            headers.add("Content-Type", "application/json");
            return execution.execute(request, body);
        });
        List<Map<String, Object>> messages = new ArrayList<>();

        Map<String, Object> message = new HashMap<>();
        message.put("role", "user");
        message.put("content", prompt);
        messages.add(message);

        Map<String, Object> param = new HashMap<>();
        param.put("model", MODEL_3);
        param.put("messages", messages);
        param.put("temperature", 0.7);

        ChatResponse response = restTemplate.postForObject(API_URL, param, ChatResponse.class);
        return response.getChoices().get(0).getMessage().getContent();
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class ChatResponse {

    private List<Choice> choices;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Choice {

        private int index;
        private Message message;
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Message {
    private String role;
    private String content; //prompt
}

3.启动代理配置

访问ChatGPT服务器需要借助科学工具,再IDEA中需要增加启动参数

-Djava.net.useSystemProxies=true

4.效果

下期讲集成带有 assistant 助手的 ChatGPT 调用方式。

  • 20
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值