package com.zb.test;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.compress.utils.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ApTest {
public static void main(String[] args) {
Map<String,String> headers = new HashMap<String,String>();
headers.put("Content-Type","application/json;charset=UTF-8");
String message = "你是谁";
String key = "sk-xxxxxxxx";
String link = "最新API,CSDN不让显示链接,所以需要取官网自取。";
JSONObject json = new JSONObject();
Map<String,String> obj = Maps.newHashMap();
obj.put("role","user");
obj.put("content",message);
List<Map<String,String>> objArr = Lists.newArrayList();
objArr.add(obj);
//选择模型
json.set("model","gpt-3.5-turbo");
json.set("messages", objArr);
HttpResponse response = HttpRequest.post(link)
.headerMap(headers, false)
.bearerAuth(key)
.body(String.valueOf(json))
.timeout(5 * 60 * 1000)
.execute();
System.out.println(response.body());
}
}
响应结果
{
"id":"xxx-xxxxx",
"object":"xxx.completion",
"created":1677735996,
"model":"xxxx",
"usage":{
"prompt_tokens":11,
"completion_tokens":47,
"total_tokens":58
},
"choices":[
{
"message":{
"role":"assistant",
"content":"劈啦啪啦"
},
"finish_reason":"stop",
"index":0
}
]
}