This repository was archived by the owner on Apr 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 520
/
Copy pathbardapi.java
124 lines (104 loc) · 5.18 KB
/
bardapi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import okhttp3.*;
import org.json.*;
import java.io.IOException;
import java.util.*;
public class Bard {
private String token;
private int timeout;
private Map<String, String> proxies;
private int reqId;
private String conversationId;
private String responseId;
private String choiceId;
private OkHttpClient client;
private String snim0e;
public Bard(String token, int timeout, Map<String, String> proxies) {
this.token = token;
this.timeout = timeout;
this.proxies = proxies;
this.reqId = new Random().nextInt(10000);
this.conversationId = "";
this.responseId = "";
this.choiceId = "";
this.client = new OkHttpClient.Builder().cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
}
@Override
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
if (token != null) {
return Collections.singletonList(new Cookie.Builder().name("__Secure-1PSID").value(token).build());
}
return Collections.emptyList();
}
}).build();
this.snim0e = getSNlM0e();
}
private String getSNlM0e() {
if (this.token == null || !this.token.endsWith(".")) {
throw new IllegalArgumentException("__Secure-1PSID value must end with a single dot. Enter correct __Secure-1PSID value.");
}
Request request = new Request.Builder()
.url("https://bard.google.com/")
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Response code not 200. Response Status is " + response.code());
}
String responseBody = response.body().string();
Matcher matcher = Pattern.compile("SNlM0e\":\"(.*?)\"").matcher(responseBody);
if (!matcher.find()) {
throw new RuntimeException("SNlM0e value not found in response. Check __Secure-1PSID value.");
}
return matcher.group(1);
} catch (IOException e) {
throw new RuntimeException("Error occurred while fetching SNlM0e value: " + e.getMessage());
}
}
public JSONObject getAnswer(String inputText) {
try {
String inputTextStruct = "[[\"" + inputText + "\"],null,[\"" + conversationId + "\",\"" + responseId + "\",\"" + choiceId + "\"]]";
JSONObject data = new JSONObject()
.put("f.req", new JSONArray().put(null).put(inputTextStruct))
.put("at", snim0e);
RequestBody requestBody = RequestBody.create(data.toString(), MediaType.parse("application/json; charset=utf-8"));
Request request = new Request.Builder()
.url("https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate")
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Response code not 200. Response Status is " + response.code());
}
String responseBody = response.body().string();
JSONArray parsedAnswer = new JSONArray(new JSONArray(responseBody).getJSONArray(0).getString(2));
if (parsedAnswer.isEmpty()) {
return new JSONObject().put("content", "Response Error: " + responseBody);
}
JSONObject bardAnswer = new JSONObject();
bardAnswer.put("content", parsedAnswer.getJSONArray(0).getString(0));
bardAnswer.put("conversation_id", parsedAnswer.getJSONArray(1).getString(0));
bardAnswer.put("response_id", parsedAnswer.getJSONArray(1).getString(1));
bardAnswer.put("factualityQueries", parsedAnswer.getJSONArray(3));
bardAnswer.put("textQuery", parsedAnswer.getJSONArray(2).optString(0, ""));
JSONArray choicesArray = new JSONArray();
JSONArray parsedChoices = parsedAnswer.getJSONArray(4);
for (int i = 0; i < parsedChoices.length(); i++) {
JSONArray choice = parsedChoices.getJSONArray(i);
JSONObject choiceObject = new JSONObject();
choiceObject.put("id", choice.getString(0));
choiceObject.put("content", choice.getString(1));
choicesArray.put(choiceObject);
}
bardAnswer.put("choices", choicesArray);
this.conversationId = bardAnswer.getString("conversation_id");
this.responseId = bardAnswer.getString("response_id");
this.choiceId = bardAnswer.getJSONArray("choices").getJSONObject(0).getString("id");
this.reqId += 100000;
return bardAnswer;
}
} catch (IOException e) {
throw new RuntimeException("Error occurred while fetching Bard answer: " + e.getMessage());
}
}
}