-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBardClientTest.java
More file actions
192 lines (161 loc) · 7.83 KB
/
BardClientTest.java
File metadata and controls
192 lines (161 loc) · 7.83 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.api.bard;
import com.api.bard.model.Answer;
import com.api.bard.model.Question;
import com.api.bard.translator.GoogleTranslatorAdaptor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.InetSocketAddress;
import java.net.Proxy;
public class BardClientTest {
private String token;
private String authUser;
private String authPassword;
@BeforeEach
public void setup() {
token = System.getenv("_BARD_API_KEY");
authUser = System.getenv("authUser");
authPassword = System.getenv("authPassword");
Assertions.assertNotNull(token);
Assertions.assertNotNull(authUser);
Assertions.assertNotNull(authPassword);
}
/**
* Simple usage
*/
@Test
public void testGetAnswer_happyCase() {
IBardClient bardClient = BardClient.builder(token).build();
// Simplest way to get answer
Answer answer = bardClient.getAnswer("Who is current president of USA?");
Assertions.assertNotNull(answer.getAnswer());
// Get answer with Question object
Answer answer2 = bardClient.getAnswer(
Question.builder()
.question("Who is his wife?")
.build());
Assertions.assertNotNull(answer2.getAnswer());
// Reset session
bardClient.reset();
Answer answer3 = bardClient.getAnswer("Who is his wife?");
Assertions.assertNotNull(answer3.getAnswer());
}
/**
* Advanced usage: customize connection properties,
* such as set custom http headers and timeout properties
*/
@Test
public void testGetAnswer_customConnection() {
IBardClient bardClient = BardClient.builder(token)
// set configurator to customize connection properties,
// such as timeout, headers
.connectionConfigurator(connection -> {
// set timeout
connection.setConnectTimeout(30000);
connection.setReadTimeout(50000);
//set customs headers
connection.setRequestProperty("TestHeader", "TestValue");
connection.setRequestProperty("TestHeader2", "TestValue2");
// ... set others properties of connection
})
.build();
Answer answer = bardClient.getAnswer("누구세요");
Assertions.assertNotNull(answer.getAnswer());
Answer answer2 = bardClient.getAnswer(Question.builder().question("あなたの名前は何ですか").build());
Assertions.assertNotNull(answer2.getAnswer());
}
/**
* Advanced usage: set translator to support languages other than English, Japanese or Korean
*/
@Test
public void testGetAnswer_withTranslator() {
IBardClient bardClient = BardClient.builder(token)
// Default middleLanguage is 'en'
.translator(GoogleTranslatorAdaptor.builder().build())
.build();
Answer answer = bardClient.getAnswer("누구세요");
Assertions.assertNotNull(answer.getAnswer());
// Korean is supported by Bard, so it should not use translator even set
Assertions.assertFalse(answer.isUsedTranslator());
Answer answer2 = bardClient.getAnswer(Question.builder().question("あなたの名前は何ですか").build());
Assertions.assertNotNull(answer2.getAnswer());
// Japanese is supported by Bard, so it should not use translator even set
Assertions.assertFalse(answer2.isUsedTranslator());
Answer answer3 = bardClient.getAnswer(Question.builder().question("你是谁?").build());
Assertions.assertNotNull(answer3.getAnswer());
// Chinese is not supported by Bard, so it should use the translator set, which middleLanguage is English
// This means the question is translated to English before interact with Bard, thus the answer is also in English from Bard
// And it will also translate the answer to Chinese before return
Assertions.assertTrue(answer3.isUsedTranslator());
IBardClient bardClient2 = BardClient.builder(token)
// You can set other middleLanguage which supported by Bard, such as 'ja'
.translator(GoogleTranslatorAdaptor.builder().middleLanguage("ja").build())
.build();
Answer answer4 = bardClient2.getAnswer("How are you?");
Assertions.assertNotNull(answer4.getAnswer());
// English is supported by Bard, so it should not use translator even set
Assertions.assertFalse(answer4.isUsedTranslator());
Answer answer5 = bardClient2.getAnswer(Question.builder().question("你是谁?").build());
Assertions.assertNotNull(answer5.getAnswer());
// Chinese is not supported by Bard, so it should use the translator set, which middleLanguage is Japanese
// This means the question is translated to Japanese before interact with Bard, thus the answer is also in Japanese from Bard
// And it will also translate the answer to Chinese before return
Assertions.assertTrue(answer5.isUsedTranslator());
}
/**
* Advanced usage: use advanced fields to get more information
* such as images, sources, relatedTopics, and raw response from google bard
*/
@Test
public void testGetAnswer_withAdvancedFields() {
IBardClient bardClient = BardClient.builder(token).build();
Answer answer = bardClient.getAnswer("Give me a picture of White House");
Assertions.assertNotNull(answer.getAnswer());
// Korean is supported by Bard, so it should not use translator even set
Assertions.assertFalse(answer.isUsedTranslator());
// verification of images/sources/relatedTopics in response
Assertions.assertEquals(answer.getImages().size(), 1);
Assertions.assertFalse(answer.getSources().isEmpty());
Assertions.assertFalse(answer.getRelatedTopics().isEmpty());
// raw response from google bard, you can parse it by yourself
Assertions.assertNotNull(answer.getRawResponse());
// If images are available, get the decorated answer with images in markdown format
String markdownAnswer = answer.getMarkdownAnswer();
Assertions.assertNotNull(markdownAnswer);
}
/**
* Advanced usage: set proxy if you can not access bard.google.com directly
* Tested in China, http/socks5 proxy is supported. Others are not tested.
*/
@Test
public void test_withProxy() {
String hostname = "192.168.31.1";
int port = 7890;
// Set Http proxy
IBardClient bardClient = BardClient.builder(token)
.proxy(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(hostname, port)))
// Set authUser and authPassword if proxy needs authentication
.auth(authUser, authPassword)
.build();
Answer answer = bardClient.getAnswer("Give me a picture of White House");
Assertions.assertNotNull(answer.getAnswer());
Assertions.assertFalse(answer.isUsedTranslator());
// Set Socks5 proxy
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(hostname, port));
IBardClient bardClient2 = BardClient.builder(token)
// Set authUser and authPassword if proxy needs authentication
.proxy(proxy)
.auth(authUser, authPassword)
// Note that if you need to set translator, you should set proxy for translator as well
.translator(GoogleTranslatorAdaptor.builder()
.proxy(proxy)
.auth(authUser, authPassword)
.build())
.build();
Answer answer2 = bardClient2.getAnswer("今天是星期几?");
Assertions.assertNotNull(answer2.getAnswer());
Assertions.assertTrue(answer2.isUsedTranslator());
}
}