Java程序接入ChatGPT( 二 )


依赖只需引入即可,我们使用工具发送http post请求,json对象封装等等 。
官方地址:
cn.hutoolhutool-all5.8.10
③Java 代码
**注:**下面贴出的代码中,.("填写自己注册的token") 需要替换成自己创建的Token密钥
import cn.hutool.http.*;import cn.hutool.json.JSONObject;import java.util.HashMap;import java.util.Map;public class ChatGptDemo {public static void main(String[] args) {Map headers = new HashMap();headers.put("Content-Type","application/json;charset=UTF-8");JSONObject json = new JSONObject();//选择模型json.set("model","text-davinci-003");//添加我们需要输入的内容json.set("prompt","Oracle 计算年龄,精确到天,格式为xx岁xx月xx天?");json.set("temperature",0.9);json.set("max_tokens",2048);json.set("top_p",1);json.set("frequency_penalty",0.0);json.set("presence_penalty",0.6);HttpResponse response = HttpRequest.post("https://api.openai.com/v1/completions").headerMap(headers, false).bearerAuth("填写自己注册的token").body(String.valueOf(json)).timeout(5 * 60 * 1000).execute();System.out.println(response.body());}}
//调用结果返回
节点下text节点即为我们想要的结果
{"id": "cmpl-6ONatHFX9tCGfcxgMP6obP6lN1ROf","object": "text_completion","created": 1671268587,"model": "text-davinci-003","choices": [{"text": "\n\nselect trunc(months_between(sysdate, date_of_birth)/12) 岁,\ntrunc(mod(months_between(sysdate, date_of_birth), 12)) 月,\ntrunc(sysdate-add_months(date_of_birth, trunc(months_between(sysdate, date_of_birth)))) 天\nfromtable_name;","index": 0,"logprobs": null,"finish_reason": "stop"}],"usage": {"prompt_tokens": 43,"completion_tokens": 89,"total_tokens": 132}}
我们来玩个好玩的,让它帮我们生成两张漂亮小姐姐图片
import cn.hutool.http.*;import cn.hutool.json.JSONObject;import java.util.HashMap;import java.util.Map;public class ChatGptDemo001 {public static void main(String[] args) {Map headers = new HashMap();headers.put("Content-Type","application/json;charset=UTF-8");JSONObject json = new JSONObject();//搜索关键字json.set("prompt","漂亮小姐姐");//生成图片数json.set("n",2);//生成图片大小json.set("size","1024x1024");//返回格式json.set("response_format","url");//发送请求HttpResponse response = HttpRequest.post("https://api.openai.com/v1/images/generations").headerMap(headers, false).bearerAuth("填写自己注册的token").body(String.valueOf(json)).timeout(5 * 60 * 1000).execute();System.out.println(response.body());}}
//输出结果
{"created": 1671269880,"data": [{"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-pdYv7jlVgMVLo21vFtr0RmLC/user-dWUudL2WFYo8MmkaYdTadc03/img-8fkcdDDEmpvI2ZedT6ddpig2.png?st=2022-12-17T08%3A38%3A00Z&se=2022-12-17T10%3A38%3A00Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-12-17T04%3A00%3A10Z&ske=2022-12-18T04%3A00%3A10Z&sks=b&skv=2021-08-06&sig=R7CiMZmMatTXE2%2B0hyQqypUBFKPlubggA2IIA9zBCQQ%3D"},{"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-pdYv7jlVgMVLo21vFtr0RmLC/user-dWUudL2WFYo8MmkaYdTadc03/img-JQl7Hor0vzScGEMSvhlnAWOd.png?st=2022-12-17T08%3A38%3A00Z&se=2022-12-17T10%3A38%3A00Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-12-17T04%3A00%3A10Z&ske=2022-12-18T04%3A00%3A10Z&sks=b&skv=2021-08-06&sig=y3j8QuYD%2Bzmym6DHrfJpLrutGDZGtbjKsug4O/kQpQ8%3D"}]}