[Latest] Simple version of Java HttpClient POST request calls OpenAI (ChatGPT3/3.5/4) related interface core methods (with 100 OpenAI/ChatGPT keys)

foreword

At present, there are many amazing technologies in OpenAI, such as ChatGPT3/3.5/4, which can generate high-quality articles, translate languages, automatically generate codes, and have been widely used in many fields. This article will introduce you how to use Java HttpClient to call OpenAI's ChatGPT3/3.5/4 interface (if you need to support Spring, and provide 100 OpenAI/ChatGPT API keys to make your development process more convenient. The article will focus on the following aspects Expand:

  1. Introduction to OpenAI's ChatGPT3/3.5/4: This article will briefly introduce the ChatGPT3/3.5/4 technology, including its application scenarios and characteristics.

  2. Java HttpClient calls OpenAI's ChatGPT3/3.5/4
    interface: This article will provide detailed calling methods and codes to help readers get started quickly.

  3. Integrate OpenAI's ChatGPT3/3.5/4 interface: This article will introduce how to
    integrate OpenAI's ChatGPT3/3.5/4 interface into your project and use it flexibly in the project.

  4. OpenAI/ChatGPT API key: This article will provide 100 OpenAI/ChatGPT API
    keys, so that you can easily start the development process.

Through the study of this article, readers can not only gain an in-depth understanding of OpenAI technology, but also master the method of using Java HttpClient to call OpenAI's ChatGPT3/3.5/4 interface to help you in the development process.

1. OpenAI ChatGPT

GPT-3 (Generative Pre-trained Transformer 3) is a deep learning natural language processing model developed by OpenAI, designed to generate high-quality natural language text, including articles, translations, dialogues, etc. ChatGPT is an application developed based on the GPT-3 model for automatic or semi-automatic generation of language texts, such as chatbots, text generation, etc. Features of ChatGPT include:

  1. Language generation ability: ChatGPT has a very powerful language generation ability, which can generate high-quality natural language text based on questions and text context.

  2. Intelligent language understanding: ChatGPT can parse and understand the input natural language, and can output natural language text closely related to it.

  3. Wide range of application scenarios: ChatGPT can be applied to automated customer service, text generation, automatic reply, intelligent question and answer and other fields, which can help enterprises and organizations improve efficiency and service quality.

Using Java HttpClient to call OpenAI's ChatGPT3/3.5/4 interface can seamlessly integrate ChatGPT3/3.5/4 technology into Java applications to realize intelligent natural language processing functions.

2. Java HttpClient calls OpenAI's ChatGPT

package com.chat;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class TestRequest {
    
    

    public static void main(String[] args) {
    
    
        CloseableHttpClient httpClient = HttpClients.createDefault();

        try {
    
    
            /**
             * 构造POST请求
             * 如请求https://api.openai.com如果出现Connection timed out: connect
             * 表示有网络问题,需要代理或转发
             * */
            HttpPost request = new HttpPost("https://api.openai.com/v1/chat/completions");

            // 添加请求头,Authorization字段包含API key
            request.addHeader("Authorization", "Bearer sk-xxx"); // 替换为自己的OpenAI API key

            // 构造请求体,JSON格式,包含一个字符串参数prompt和一个整数参数max_tokens,如果有其他参数,延续即可。
            String json = "{\"model\": \"text-davinci-003\", \"prompt\": \"Hello, ChatGPT!\", \"max_tokens\": 1000}";
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            request.setEntity(entity);

            // 发送请求并获取响应体
            CloseableHttpResponse response = httpClient.execute(request);
            HttpEntity responseEntity = response.getEntity();

            // 将响应体转换为字符串并打印输出
            if (responseEntity != null) {
    
    
                String result = EntityUtils.toString(responseEntity);
                System.out.println(result);
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            // 关闭连接
            try {
    
    
                httpClient.close();
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        }
    }
}


3. Integrate OpenAI's ChatGPT3/3.5/4 interface

After we encapsulate the above code into SpringBoot, it can be accessed through the interface.

4. One hundred free keys

GPT3/GPT3.5/GPT4 model calling methods, exclusive GPT service deployment, technical services, and private chat with bloggers.

Click the link to get the latest 100 OpenAI free and valid ChatGPT Keys (effective for personal testing).

Or copy the following page to your browser to visit:
https://dr.onlines.asia/resources/key.html

5. Other issues

OpenAI's API address https://api.openai.com/ may not be accessible, such as network connection problems, specific solutions refer to: https://blog.csdn.net/qq_44491709/article/details/129911036

Supongo que te gusta

Origin blog.csdn.net/qq_44491709/article/details/129911738
Recomendado
Clasificación