api
This commit is contained in:
parent
86c312e1ae
commit
76fb9b4092
23
pom.xml
23
pom.xml
|
@ -13,5 +13,28 @@
|
|||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.75</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20210307</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.11.4</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -1,2 +1,155 @@
|
|||
package PACKAGE_NAME;public class Main {
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
String url = "http://119.29.254.99:8000/v1/chat/completions"; // 替换为文心一言的API地址
|
||||
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
// 创建httppost
|
||||
HttpPost httppost = new HttpPost("http://119.29.254.99:8000/v1/chat/completions");
|
||||
httppost.addHeader("Authorization","Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyLWNlbnRlciIsImV4cCI6MTcyNDY3NjM1MiwiaWF0IjoxNzE2OTAwMzUyLCJqdGkiOiJjcGF0NDA1dmJmNnRwNG0wdGUyMCIsInR5cCI6InJlZnJlc2giLCJzdWIiOiJjcGF0NDA1dmJmNnRwNG0wdGR2ZyIsInNwYWNlX2lkIjoiY3BhdDQwNXZiZjZ0cDRtMHRkdjAiLCJhYnN0cmFjdF91c2VyX2lkIjoiY3BhdDQwNXZiZjZ0cDRtMHRkdWcifQ.irkXZJ3vcVi9pk4gVYOcaKab7nhxXCkclpjOFEf1nLTou06z3LoNefUGCBsZ5XAiip5Zh8HbRgZJGzLk08cc6A");
|
||||
httppost.setHeader("Content-Type","application/json");
|
||||
// 创建参数队列
|
||||
|
||||
JSONArray jsonArray=new JSONArray();
|
||||
JSONObject jsonObject=new JSONObject();
|
||||
jsonObject.put("role","user");
|
||||
jsonObject.put("content","给我写一段python爬虫");
|
||||
jsonArray.put(jsonObject);
|
||||
JSONObject object=new JSONObject();
|
||||
object.put("model","kimi");
|
||||
object.put("use_search",true);
|
||||
object.put("stream",false);
|
||||
object.put("messages",jsonArray);
|
||||
|
||||
StringEntity uefEntity;
|
||||
try {
|
||||
uefEntity = new StringEntity(object.toString(), "UTF-8");
|
||||
httppost.setEntity(uefEntity);
|
||||
|
||||
System.out.println("executing request " + httppost.getURI());
|
||||
System.out.println(httppost);
|
||||
CloseableHttpResponse response = httpclient.execute(httppost);
|
||||
try {
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
if (entity != null) {
|
||||
|
||||
String responseString = EntityUtils.toString(entity, "UTF-8");
|
||||
System.out.println(responseString);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Response response1 = mapper.readValue(responseString, Response.class);
|
||||
System.out.println(response1+"\n\n\n\n\n\n\n");
|
||||
JsonNode jsonNode = mapper.readTree(responseString);
|
||||
System.out.println(jsonNode);
|
||||
|
||||
// JSONObject jsonObject1=new JSONObject(responseString);
|
||||
// System.out.println(jsonObject1);
|
||||
System.out.println("--------------------------------------");
|
||||
System.out.println("Response content: " + responseString);
|
||||
|
||||
System.out.println("--------------------------------------");
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
} catch (ClientProtocolException e) {
|
||||
e.printStackTrace();
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
e1.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 关闭连接,释放资源
|
||||
try {
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// URL obj = new URL(url);
|
||||
// HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
||||
//
|
||||
// // 设置请求方法为POST
|
||||
// con.setRequestMethod("POST");
|
||||
// con.setRequestProperty("Authorization","Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyLWNlbnRlciIsImV4cCI6MTcxNjg5NTEzNiwiaWF0IjoxNzE2ODk0MjM2LCJqdGkiOiJjcGFyazc2NzY4ajVhNGxjMjZlMCIsInR5cCI6ImFjY2VzcyIsInN1YiI6ImNwYXJrNzY3NjhqNWE0bGMyNmRnIiwic3BhY2VfaWQiOiJjcGFyazc2NzY4ajVhNGxjMjZkMCIsImFic3RyYWN0X3VzZXJfaWQiOiJjcGFyazc2NzY4ajVhNGxjMjZjZyJ9.HfujoeHNgRZia4oIsH2DhEzQCLsloKFjAu_BItOdDz7AJek-rT8Fn2t2BjWAPRtA97oBCMMNkcZkCeTx8A2UYg");
|
||||
// con.setRequestProperty("Content-Type", "application/json; utf-8");
|
||||
// // 发送POST请求必须设置如下两行
|
||||
// con.setDoOutput(true);
|
||||
// con.setDoInput(true);
|
||||
// // 创建JSON对象
|
||||
// JSONObject jsonObject = new JSONObject();
|
||||
//
|
||||
// // 添加模型名称
|
||||
// jsonObject.put("model", "kimi");
|
||||
// // 创建消息数组
|
||||
// JSONArray messages = new JSONArray();
|
||||
//
|
||||
// // 创建一个消息
|
||||
// JSONObject message = new JSONObject();
|
||||
// message.put("role", "user");
|
||||
// message.put("content", "测试");
|
||||
//
|
||||
// // 将消息添加到消息数组
|
||||
// messages.set(0,message);
|
||||
//
|
||||
// // 将消息数组添加到主对象
|
||||
// jsonObject.put("messages", messages);
|
||||
// System.out.println(messages);
|
||||
//
|
||||
// // 添加其他属性
|
||||
// jsonObject.put("use_search", true);
|
||||
// jsonObject.put("stream", false);
|
||||
// System.out.println(jsonObject);
|
||||
// // 添加请求体
|
||||
//
|
||||
// try(OutputStream os = con.getOutputStream()) {
|
||||
// byte[] input = jsonObject.toJSONString().getBytes("utf-8");
|
||||
// os.write(input, 0, input.length);
|
||||
// }
|
||||
// System.out.println(con.getRequestProperty("body.messages"));
|
||||
// int responseCode = con.getResponseCode();
|
||||
// System.out.println("Response Code : " + responseCode);
|
||||
//
|
||||
// BufferedReader in = new BufferedReader(
|
||||
// new InputStreamReader(con.getInputStream()));
|
||||
// String inputLine;
|
||||
// StringBuffer response = new StringBuffer();
|
||||
//
|
||||
// while ((inputLine = in.readLine()) != null) {
|
||||
// response.append(inputLine);
|
||||
// }
|
||||
// in.close();
|
||||
//
|
||||
// // 打印结果
|
||||
// System.out.println(response.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,2 +1,313 @@
|
|||
package PACKAGE_NAME;public class Response {
|
||||
import java.util.List;
|
||||
|
||||
public class Response {
|
||||
private String id;
|
||||
private String model;
|
||||
private String object;
|
||||
private List<Choice> choices;
|
||||
private Usage usage;
|
||||
private int created;
|
||||
|
||||
public Response() {
|
||||
}
|
||||
|
||||
public Response(String id, String model, String object, List<Choice> choices, Usage usage, int created) {
|
||||
this.id = id;
|
||||
this.model = model;
|
||||
this.object = object;
|
||||
this.choices = choices;
|
||||
this.usage = usage;
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return id
|
||||
*/
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param id
|
||||
*/
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return model
|
||||
*/
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param model
|
||||
*/
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return object
|
||||
*/
|
||||
public String getObject() {
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param object
|
||||
*/
|
||||
public void setObject(String object) {
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return choices
|
||||
*/
|
||||
public List<Choice> getChoices() {
|
||||
return choices;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param choices
|
||||
*/
|
||||
public void setChoices(List<Choice> choices) {
|
||||
this.choices = choices;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return usage
|
||||
*/
|
||||
public Usage getUsage() {
|
||||
return usage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param usage
|
||||
*/
|
||||
public void setUsage(Usage usage) {
|
||||
this.usage = usage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return created
|
||||
*/
|
||||
public int getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param created
|
||||
*/
|
||||
public void setCreated(int created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Response{id = " + id + ", model = " + model + ", object = " + object + ", choices = " + choices + ", usage = " + usage + ", created = " + created + "}";
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
}
|
||||
|
||||
class Choice {
|
||||
private int index;
|
||||
private Message message;
|
||||
private String finish_reason;
|
||||
|
||||
public Choice() {
|
||||
}
|
||||
|
||||
public Choice(int index, Message message, String finish_reason) {
|
||||
this.index = index;
|
||||
this.message = message;
|
||||
this.finish_reason = finish_reason;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return index
|
||||
*/
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param index
|
||||
*/
|
||||
public void setIndex(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return message
|
||||
*/
|
||||
public Message getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param message
|
||||
*/
|
||||
public void setMessage(Message message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return finish_reason
|
||||
*/
|
||||
public String getFinish_reason() {
|
||||
return finish_reason;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param finish_reason
|
||||
*/
|
||||
public void setFinish_reason(String finish_reason) {
|
||||
this.finish_reason = finish_reason;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Choice{index = " + index + ", message = " + message + ", finish_reason = " + finish_reason + "}";
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
}
|
||||
|
||||
class Message {
|
||||
private String role;
|
||||
private String content;
|
||||
|
||||
public Message() {
|
||||
}
|
||||
|
||||
public Message(String role, String content) {
|
||||
this.role = role;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return role
|
||||
*/
|
||||
public String getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param role
|
||||
*/
|
||||
public void setRole(String role) {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return content
|
||||
*/
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param content
|
||||
*/
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Message{role = " + role + ", content = " + content + "}";
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
}
|
||||
|
||||
class Usage {
|
||||
private int prompt_tokens;
|
||||
private int completion_tokens;
|
||||
private int total_tokens;
|
||||
|
||||
public Usage() {
|
||||
}
|
||||
|
||||
public Usage(int prompt_tokens, int completion_tokens, int total_tokens) {
|
||||
this.prompt_tokens = prompt_tokens;
|
||||
this.completion_tokens = completion_tokens;
|
||||
this.total_tokens = total_tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return prompt_tokens
|
||||
*/
|
||||
public int getPrompt_tokens() {
|
||||
return prompt_tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param prompt_tokens
|
||||
*/
|
||||
public void setPrompt_tokens(int prompt_tokens) {
|
||||
this.prompt_tokens = prompt_tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return completion_tokens
|
||||
*/
|
||||
public int getCompletion_tokens() {
|
||||
return completion_tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param completion_tokens
|
||||
*/
|
||||
public void setCompletion_tokens(int completion_tokens) {
|
||||
this.completion_tokens = completion_tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
* @return total_tokens
|
||||
*/
|
||||
public int getTotal_tokens() {
|
||||
return total_tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param total_tokens
|
||||
*/
|
||||
public void setTotal_tokens(int total_tokens) {
|
||||
this.total_tokens = total_tokens;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Usage{prompt_tokens = " + prompt_tokens + ", completion_tokens = " + completion_tokens + ", total_tokens = " + total_tokens + "}";
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue