当前位置:首页 » 《随便一记》 » 正文

Open AI:springboot 调用open ai 接口

9 人参与  2024年04月24日 13:06  分类 : 《随便一记》  评论

点击全文阅读


Spring Boot可以通过HTTP客户端调用Open AI的API接口,具体步骤如下:

目录

1.在Open AI官方网站上注册账号并获取API密钥

2.创建一个Spring Boot项目,并添加相关的依赖,例如

3.创建一个Java类作为Open AI的HTTP客户端,例如

4.在Spring Boot的配置文件中添加Open AI的API密钥

5.在Spring Boot的控制器中调用Open AI的API接口,例如


1.在Open AI官方网站上注册账号并获取API密钥

2.创建一个Spring Boot项目,并添加相关的依赖,例如

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-validation</artifactId></dependency><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-test</artifactId>   <scope>test</scope></dependency><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-security</artifactId></dependency><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency>   <groupId>org.springframework.data</groupId>   <artifactId>spring-data-rest-webmvc</artifactId></dependency>

3.创建一个Java类作为Open AI的HTTP客户端,例如

package com.example.demo.service;import java.net.URI;import org.springframework.beans.factory.annotation.Value;import org.springframework.http.HttpHeaders;import org.springframework.http.MediaType;import org.springframework.http.RequestEntity;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class OpenAiApiService {        @Value("${openai.api.key}")    private String apiKey;        private RestTemplate restTemplate;        public OpenAiApiService() {        restTemplate = new RestTemplate();    }        public String generateText(String prompt) {        String apiUrl = "https://api.openai.com/v1/engines/davinci-codex/completions";                HttpHeaders headers = new HttpHeaders();        headers.setContentType(MediaType.APPLICATION_JSON);        headers.setBearerAuth(apiKey);                String requestBody = "{\"prompt\": \"" + prompt + "\", \"max_tokens\": 60}";                RequestEntity<String> requestEntity = RequestEntity            .post(URI.create(apiUrl))            .headers(headers)            .body(requestBody);                ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);        return responseEntity.getBody();    }}

4.在Spring Boot的配置文件中添加Open AI的API密钥

openai:  api:    key: YOUR_API_KEY_HERE

5.在Spring Boot的控制器中调用Open AI的API接口,例如

package com.example.demo.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;import com.example


点击全文阅读


本文链接:http://zhangshiyu.com/post/99520.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1