关于后端:使用-Java-代码调用-openAI-的-ChatGPT-API

0次阅读

共计 2930 个字符,预计需要花费 8 分钟才能阅读完成。

前提:在 https://beta.openai.com/account/api-keys 注册一个本人的 API key.

要在 Java Spring Framework 中应用 OpenAI API,您须要应用一个可能解决 HTTP 申请的库。其中一个风行的库是 Spring RestTemplate 库。RestTemplate 是一个弱小而灵便的库,能够轻松地发送 HTTP 申请并解决响应。

首先,您须要将 Spring RestTemplate 库增加到您的我的项目中。您能够通过将以下依赖项增加到您的 build.gradle 文件中来实现:

dependencies {
    // 其余依赖...
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

spring-boot-starter-web 依赖项蕴含了 RestTemplate 库,以及构建应用 Spring 进行 Web 应用程序所需的其余依赖项。

增加依赖项之后,您须要在应用程序中配置 RestTemplate。您能够在 Spring 配置类或间接在应用程序类中创立一个 RestTemplate bean。以下是创立 RestTemplate bean 的示例:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate() {return new RestTemplate();
    }
}

下面的示例中,@Configuration 注解示意该类蕴含 Spring 配置。restTemplate() 办法上的 @Bean 注解创立了一个 RestTemplate bean,能够主动拆卸到其余类中以进行 HTTP 申请。

配置好 RestTemplate 之后,您能够应用它向 OpenAI API 发送 HTTP 申请。您能够应用 getForObject() 或 postForObject() 等办法发送 GET 或 POST 申请。以下是应用 RestTemplate 进行 GET 申请的示例:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OpenAIService {

    private final RestTemplate restTemplate;

    @Autowired
    public OpenAIService(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

    public String getOpenAIResponse() {
        String apiUrl = "https://api.openai.com/v1/your-endpoint";
        // 设置任何必要的申请参数或头部信息

        // 发送 GET 申请并接管响应
        String response = restTemplate.getForObject(apiUrl, String.class);

        return response;
    }
}

在下面的示例中,通过构造函数注入了 RestTemplate bean 到 OpenAIService 类中。在 getOpenAIResponse() 办法中,您能够依据须要自定义 URL、申请参数、头部信息,并解决响应。

请记得将 ”https://api.openai.com/v1/your-endpoint” 替换为您想要与 OpenAI API 通信的理论 URL。

以上就是在 Java Spring Framework 我的项目中应用 Spring RestTemplate 库与 OpenAI API 交互的办法。

更具体的操作,请参考这篇文章。

build.gradle 的内容:

plugins {
 id 'java'
 id 'org.springframework.boot' version '3.0.1'
 id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.openai'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {mavenCentral()
}

dependencies {
 implementation 'org.springframework.boot:spring-boot-starter-web'
 testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {useJUnitPlatform()
}

Java 实现:

@Component
public class OpenAi
{
 private static final String OPENAI_URL = "https://api.openai.com/v1/images/generations";

 private final String apiKey = "<your-api-key";
 private final RestTemplate restTemplate = new RestTemplate();

 public String generateImages(String prompt, float temperature, int maxTokens, String stop, final int logprobs, final boolean echo)
 {HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON);
  headers.set("Authorization", "Bearer" + apiKey);

  // We are including only some of the parameters to the json request
  String requestJson = "{\"prompt\":\"" + prompt + "\",\"n\":"+ n +"}";

  HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
  ResponseEntity<String> response = restTemplate.postForEntity(OPENAI_URL, request, String.class);
  return response.getBody();}
}
正文完
 0