From 76fb9b4092967eaf2007a87cd3638bd9ffa9be24 Mon Sep 17 00:00:00 2001
From: Cool <747682928@qq.com>
Date: Fri, 8 Nov 2024 16:51:37 +0800
Subject: [PATCH] api
---
pom.xml | 23 +++
src/main/java/Main.java | 155 +++++++++++++++++-
src/main/java/Response.java | 313 +++++++++++++++++++++++++++++++++++-
3 files changed, 489 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index 1475cbc..5a5ff98 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,5 +13,28 @@
8
UTF-8
+
+
+ com.alibaba
+ fastjson
+ 1.2.75
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.5.13
+
+
+ org.json
+ json
+ 20210307
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.11.4
+
+
+
diff --git a/src/main/java/Main.java b/src/main/java/Main.java
index fb7039d..93de3a7 100644
--- a/src/main/java/Main.java
+++ b/src/main/java/Main.java
@@ -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());
+ }
+
}
diff --git a/src/main/java/Response.java b/src/main/java/Response.java
index 264c299..7977592 100644
--- a/src/main/java/Response.java
+++ b/src/main/java/Response.java
@@ -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 choices;
+ private Usage usage;
+ private int created;
+
+ public Response() {
+ }
+
+ public Response(String id, String model, String object, List 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 getChoices() {
+ return choices;
+ }
+
+ /**
+ * 设置
+ * @param choices
+ */
+ public void setChoices(List 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
}