最近灰常火爆,已经成为最有效率的生产工具,认人感觉人工智能的时代来临了。今天聊一下如何快速接入。

一、准备工作

如果想在 Boot应用程序中使用提供的 API,可以按照以下步骤进。

注册账户并创建API密钥

首先,您需要注册账户,并创建一个API密钥。API密钥可以用于调用提供的各种API,包括 API。具体的注册和创建API密钥步骤可以参考的官方文档。注册完即可创建API key,网址:

注意:生成API密钥后,如果关闭弹窗,不会再次显示API密钥,所以需要把它保存下来,以便下次能找回它。

引入-gpt3-java依赖

接下来,需要在项目的pom.xml文件中添加以下-gpt3-java依赖,目前最新版本为0.11.1:

 
 
      com.theokanning.openai-gpt3-java
      service
      0.11.1
 

创建,调用 API

@RestController
public class ChatGPTController {
    private String OPENAPI_TOKEN = "YOUR_API_KEY";
    private Duration TIMEOUT = Duration.ofSeconds(30L);
    private String MODEL = "text-davinci-003";
    @PostMapping("/chat")
    public String chat(@RequestBody ChatMessage input) {
        OpenAiService service = new OpenAiService(OPENAPI_TOKEN, TIMEOUT);
        CompletionRequest.CompletionRequestBuilder builder = CompletionRequest.builder()
                .model(MODEL)
                .prompt(input.getInput())
                .maxTokens(1000);
        CompletionRequest completionRequest = builder.build();
        List questionAnswer = service.createCompletion(completionRequest).getChoices();
        String endQuestionAnswer = "";
        for (CompletionChoice completionChoice : questionAnswer) {
            endQuestionAnswer = endQuestionAnswer.concat(completionChoice.getText());
        }
        return endQuestionAnswer;
    }
}

密钥生成方式_chatgpt生成office密钥_密钥生成中心

@Data
public class ChatMessage {
    private String input;
}

测试

完成接口编写后,下面启动程序,打开页面进行测试:

二、参数分析

是一个用于向 GPT-3 API发送请求的对象,它包含了多个可选参数,以定制化生成的文本。以下是其中一些重要参数的意义:

这些参数并非全部,您可以查看 API文档以了解所有可用参数。在实际使用中,您需要根据生成的文本需要进行相应的参数调整。

三、其它

调用 API需要科学上网环境,这个是前提。

如果调用 接口出现You your quota, check your plan and 异常,就是账户里面没钱了,需要付费才能使用。注册的第一个账号会有5美元的免费token配额,注册的第二个及以后的账号就无免费api token配额了。

下面是请求 API的一些常见错误:

CODE	OVERVIEW
401 - Invalid Authentication	
Cause: Invalid Authentication
Solution: Ensure the correct API key and requesting organization are being used.
解释:认证错误,有可能是API key错误
401 - Incorrect API key provided	
Cause: The requesting API key is not correct.
Solution: Ensure the API key used is correct, clear your browser cache, or generate a new one.
解释:跟401类似,也是API key的问题
401 - You must be a member of an organization to use the API	
Cause: Your account is not part of an organization.
Solution: Contact us to get added to a new organization or ask your organization manager to invite you to an organization.
解释:暂时没遇到此问题
429 - Rate limit reached for requests	
Cause: You are sending requests too quickly.
Solution: Pace your requests. Read the Rate limit guide.
解释:每分钟请求次数太多了,免费的每分钟最多20次
429 - You exceeded your current quota, please check your plan and billing details	
Cause: You have hit your maximum monthly spend (hard limit) which you can view in the account billing section.
Solution: Apply for a quota increase.
解释:账户里面没钱了
429 - The engine is currently overloaded, please try again later	
Cause: Our servers are experiencing high traffic.
Solution: Please retry your requests after a brief wait.
解释:服务器压力过大,稍后重试
500 - The server had an error while processing your request	
Cause: Issue on our servers.
Solution: Retry your request after a brief wait and contact us if the issue persists. Check the status page.
解释:服务器发生错误,稍后重试