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

前提:在 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();
 }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理