|
| 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 | +``` |
0 commit comments