CR400AF2001 пре 3 година
родитељ
комит
ccf3f4f708

+ 22 - 0
pom.xml

@@ -59,6 +59,28 @@
             <artifactId>mysql-connector-java</artifactId>
             <version>8.0.28</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-web</artifactId>
+            <version>4.3.6.RELEASE</version>
+        </dependency>
+        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
+        <dependency>
+            <groupId>com.squareup.okhttp3</groupId>
+            <artifactId>okhttp</artifactId>
+            <version>4.10.0</version>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.24</version>
+        </dependency>
+        <dependency>
+            <groupId>com.vaadin.external.google</groupId>
+            <artifactId>android-json</artifactId>
+            <version>0.0.20131108.vaadin1</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
     <dependencyManagement>
         <dependencies>

+ 1 - 1
src/main/java/com/seeckg/knowledgegraph_backend/enums/questionAnsweringType.java

@@ -2,7 +2,7 @@ package com.seeckg.knowledgegraph_backend.enums;
 
 public enum questionAnsweringType {
     /**
-     * 离线问答
+     * 本地问答
      */
     OFFLINE,
     /**

+ 29 - 2
src/main/java/com/seeckg/knowledgegraph_backend/remote/GPTRemoteService.java

@@ -1,10 +1,37 @@
 package com.seeckg.knowledgegraph_backend.remote;
 
+import com.alibaba.fastjson.JSONObject;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
+import okhttp3.*;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 @Service
 public class GPTRemoteService {
-    public String getGPTAnswer(String question) {
-        return "";
+    public String getGPTAnswer(String question) throws IOException {
+        String answer = "";
+        OkHttpClient client = new OkHttpClient().newBuilder().build();
+        MediaType mediaType = MediaType.parse("application/json");
+        Map<String, String> input = new HashMap<String, String>();
+        input.put("prompt", question);
+        RequestBody body = RequestBody.create(mediaType, input.toString());
+        Request request = new Request.Builder()
+                .url("https://ai-talk.live/api/chat-process")
+                .method("POST", body)
+                .addHeader("Content-Type", "application/json")
+                .build();
+        Response response = client.newCall(request).execute();
+        if (response.isSuccessful()) {
+            String resp = response.body().string();
+            JSONObject jsonObject = JSONObject.parseObject(resp);
+            answer = jsonObject.getString("text");
+        }
+        else {
+            throw new IOException("GPT error: " + response);
+        }
+        return answer;
     }
 }

+ 9 - 3
src/main/java/com/seeckg/knowledgegraph_backend/service/QuestionAnsweringService.java

@@ -25,7 +25,7 @@ public class QuestionAnsweringService {
 
 
     /**
-     * 选择离线问答或在线问答
+     * 选择本地问答或在线问答
      * 返回的数据格式是String
      * @return 包含上述数据的Response
      */
@@ -41,7 +41,7 @@ public class QuestionAnsweringService {
 
 
     /**
-     * 获取离线问答结果
+     * 获取本地问答结果
      * 返回的数据格式是String
      * @return 包含上述数据的Response
      */
@@ -83,7 +83,13 @@ public class QuestionAnsweringService {
      * @return 包含上述数据的Response
      */
     public Response getOnlineAnswer(String question) {
-        String answer = gptRemoteService.getGPTAnswer(question);
+        String answer = null;
+        try {
+            answer = gptRemoteService.getGPTAnswer(question);
+        } catch (IOException e) {
+            e.printStackTrace();
+            return Response.buildFailed(ReturnCode.SERVER_ERROR.getCode(), "答案生成出错!");
+        }
         return Response.buildSuccess(answer);
     }
 

+ 7 - 1
src/main/java/com/seeckg/knowledgegraph_backend/util/OfflineQuestionAnsweringServive.py

@@ -24,4 +24,10 @@ if __name__ == '__main__':
     contexts = ' '.join(contexts)
     # question = "软件工程的定义?"
     # contexts = "软件工程是一门研究用工程化方法构建和维护有效、实用和高质量的软件的学科。它涉及程序设计语言、数据库、软件开发工具、系统平台、标准、设计件有电子邮件、嵌入式系统、人机界面、办公套件、操作系统、编译器、数据库、游戏等。同时,各个行业几乎都有计算机软件的应用,如工业、农业、银行、航空、政府部门等。这些应用促进了经济和社会的发展,也提高了工作效率和生活效率。"
-    print(getOfflineAnswer(question, contexts))
+    answer = getOfflineAnswer(question, contexts)
+    print("question:")
+    print(question)
+    print("context:")
+    print(contexts)
+    print("answer:")
+    print(answer)