| 12345678910111213141516171819202122232425262728293031323334353637 |
- 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) 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;
- }
- }
|