Skip to content

Commit 51c4e0f

Browse files
committed
[Google] [Gemini] 添加使用文档
1 parent 93781d9 commit 51c4e0f

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
title: Chat
3+
---
4+
5+
> 支持 Google Gemini,产品地址: https://ai.google.dev/gemini-api
6+
7+
### 简单对话
8+
9+
---
10+
11+
处理纯文字输入。借助此功能,您可以执行自然语言处理 (NLP) 任务,例如文本补全和摘要。
12+
13+
使用示例
14+
15+
```java
16+
try (GoogleClient client = GoogleClient.builder()
17+
.apiKey(token)
18+
.build()) {
19+
PartEntity part = PartEntity.builder()
20+
.text("Hello, Open AI Java SDK!")
21+
.build();
22+
ObjectEntity object = ObjectEntity.builder()
23+
.parts(Lists.newArrayList(part))
24+
.build();
25+
ChatEntity chat = ChatEntity.builder()
26+
.contents(Lists.newArrayList(object))
27+
.build();
28+
29+
ChatResponse response = client.createChatCompletions(chat);
30+
response.getCandidates()
31+
.forEach(item -> item.getContent()
32+
.getParts()
33+
.forEach(value -> log.info(value.getText())));
34+
}
35+
```
36+
37+
### 多轮对话(聊天)
38+
39+
---
40+
41+
打造互动式聊天体验。 借助此 API 的聊天功能,您可以收集多轮问题和回复,让用户能够逐步找到答案或获得有关多部分问题的帮助。
42+
43+
使用示例
44+
45+
```java
46+
List<ObjectEntity> contents = Lists.newArrayList();
47+
PartEntity part = PartEntity.builder()
48+
.text("你好,我叫小明")
49+
.build();
50+
ObjectEntity object = ObjectEntity.builder()
51+
.parts(Lists.newArrayList(part))
52+
.build();
53+
contents.add(object);
54+
ChatEntity chat = ChatEntity.builder()
55+
.contents(contents)
56+
.build();
57+
ChatResponse response = client.createChatCompletions(chat);
58+
response.getCandidates()
59+
.forEach(item -> item.getContent()
60+
.getParts()
61+
.forEach(value -> {
62+
log.info(value.getText());
63+
64+
contents.add(ObjectEntity.builder()
65+
.role(RoleModel.MODEL)
66+
.parts(Lists.newArrayList(PartEntity.builder()
67+
.text(value.getText())
68+
.build()))
69+
.build());
70+
}));
71+
72+
ObjectEntity newObject = ObjectEntity.builder()
73+
.parts(Lists.newArrayList(PartEntity.builder()
74+
.text("我刚刚说了什么")
75+
.build()))
76+
.build();
77+
contents.add(newObject);
78+
ChatEntity newChat = ChatEntity.builder()
79+
.contents(contents)
80+
.build();
81+
client.createChatCompletions(newChat);
82+
```
83+
84+
### 流式响应
85+
86+
---
87+
88+
以数据流的形式接收。流式响应会在模型生成增量数据时将这些数据发送回您的应用。
89+
90+
使用示例
91+
92+
```java
93+
// 构建客户端
94+
CountDownLatch countDownLatch = new CountDownLatch(1);
95+
ConsoleEventSourceListener listener = ConsoleEventSourceListener.builder()
96+
.countDownLatch(countDownLatch)
97+
.build();
98+
GoogleClient client = GoogleClient.builder()
99+
.apiKey(ResourceUtils.getValue("google.token"))
100+
.listener(listener)
101+
.build();
102+
103+
List<ObjectEntity> contents = Lists.newArrayList();
104+
PartEntity part = PartEntity.builder()
105+
.text("帮我写一万字的作文")
106+
.build();
107+
ObjectEntity object = ObjectEntity.builder()
108+
.parts(Lists.newArrayList(part))
109+
.build();
110+
contents.add(object);
111+
ChatEntity chat = ChatEntity.builder()
112+
.contents(contents)
113+
.build();
114+
client.createChatCompletions(chat);
115+
try {
116+
countDownLatch.await();
117+
}
118+
catch (InterruptedException e) {
119+
log.error("Interrupted while waiting", e);
120+
}
121+
```

docs/mkdocs.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ theme:
2121
name: material
2222
custom_dir: overrides
2323

24-
language: en
2524
features:
2625
- content.code.annotate
2726
- content.tabs.link
@@ -64,7 +63,7 @@ markdown_extensions:
6463
plugins:
6564
- i18n:
6665
reconfigure_material: true
67-
default_language: en
66+
default_language: zh
6867
languages:
6968
- locale: zh
7069
name: Chinese (Simplified)
@@ -111,6 +110,9 @@ nav:
111110
- Google PaLM:
112111
- reference/google_palm/completions.md
113112
- reference/google_palm/chat.md
113+
- Google:
114+
- Gemini:
115+
- reference/google/gemini/chat/home.md
114116
- NavReleaseNote:
115117
- release/latest.md
116118
- release/2024.01.1.md

src/test/java/org/devlive/sdk/platform/google/GoogleClientTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ public void testCreateChat()
5151
Assert.assertNotNull(client.createChatCompletions(chat));
5252
}
5353

54+
@Test
55+
public void testAutoClose()
56+
{
57+
try (GoogleClient client = GoogleClient.builder()
58+
.apiKey(token)
59+
.build()) {
60+
PartEntity part = PartEntity.builder()
61+
.text("Hello, Open AI Java SDK!")
62+
.build();
63+
ObjectEntity object = ObjectEntity.builder()
64+
.parts(Lists.newArrayList(part))
65+
.build();
66+
ChatEntity chat = ChatEntity.builder()
67+
.contents(Lists.newArrayList(object))
68+
.build();
69+
70+
ChatResponse response = client.createChatCompletions(chat);
71+
response.getCandidates()
72+
.forEach(item -> item.getContent()
73+
.getParts()
74+
.forEach(value -> log.info(value.getText())));
75+
76+
Assert.assertNotNull(response);
77+
}
78+
}
79+
5480
@Test
5581
public void testContinuousChat()
5682
{

0 commit comments

Comments
 (0)