当前位置:   article > 正文

Open AI:springboot 调用open ai 接口_spring-ai-openai-spring-boot-starter

spring-ai-openai-spring-boot-starter

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项目,并添加相关的依赖,例如

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-validation</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-test</artifactId>
  12. <scope>test</scope>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-data-jpa</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-security</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.data</groupId>
  28. <artifactId>spring-data-rest-webmvc</artifactId>
  29. </dependency>

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

  1. package com.example.demo.service;
  2. import java.net.URI;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.http.HttpHeaders;
  5. import org.springframework.http.MediaType;
  6. import org.springframework.http.RequestEntity;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.web.client.RestTemplate;
  10. @Service
  11. public class OpenAiApiService {
  12. @Value("${openai.api.key}")
  13. private String apiKey;
  14. private RestTemplate restTemplate;
  15. public OpenAiApiService() {
  16. restTemplate = new RestTemplate();
  17. }
  18. public String generateText(String prompt) {
  19. String apiUrl = "https://api.openai.com/v1/engines/davinci-codex/completions";
  20. HttpHeaders headers = new HttpHeaders();
  21. headers.setContentType(MediaType.APPLICATION_JSON);
  22. headers.setBearerAuth(apiKey);
  23. String requestBody = "{\"prompt\": \"" + prompt + "\", \"max_tokens\": 60}";
  24. RequestEntity<String> requestEntity = RequestEntity
  25. .post(URI.create(apiUrl))
  26. .headers(headers)
  27. .body(requestBody);
  28. ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
  29. return responseEntity.getBody();
  30. }
  31. }

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

  1. openai:
  2. api:
  3. key: YOUR_API_KEY_HERE

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

  1. package com.example.demo.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.PostMapping;
  4. import org.springframework.web.bind.annotation.RequestBody;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.example

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/647709
推荐阅读
相关标签
  

闽ICP备14008679号