Skip to content

[Google] [Gemini] Support google Gemini #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ pom.xml.versionsBackup

BaseTest.java
site/

application.properties
121 changes: 121 additions & 0 deletions docs/docs/reference/google/gemini/chat/home.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
title: Chat
---

> 支持 Google Gemini,产品地址: https://ai.google.dev/gemini-api

### 简单对话

---

处理纯文字输入。借助此功能,您可以执行自然语言处理 (NLP) 任务,例如文本补全和摘要。

使用示例

```java
try (GoogleClient client = GoogleClient.builder()
.apiKey(token)
.build()) {
PartEntity part = PartEntity.builder()
.text("Hello, Open AI Java SDK!")
.build();
ObjectEntity object = ObjectEntity.builder()
.parts(Lists.newArrayList(part))
.build();
ChatEntity chat = ChatEntity.builder()
.contents(Lists.newArrayList(object))
.build();

ChatResponse response = client.createChatCompletions(chat);
response.getCandidates()
.forEach(item -> item.getContent()
.getParts()
.forEach(value -> log.info(value.getText())));
}
```

### 多轮对话(聊天)

---

打造互动式聊天体验。 借助此 API 的聊天功能,您可以收集多轮问题和回复,让用户能够逐步找到答案或获得有关多部分问题的帮助。

使用示例

```java
List<ObjectEntity> contents = Lists.newArrayList();
PartEntity part = PartEntity.builder()
.text("你好,我叫小明")
.build();
ObjectEntity object = ObjectEntity.builder()
.parts(Lists.newArrayList(part))
.build();
contents.add(object);
ChatEntity chat = ChatEntity.builder()
.contents(contents)
.build();
ChatResponse response = client.createChatCompletions(chat);
response.getCandidates()
.forEach(item -> item.getContent()
.getParts()
.forEach(value -> {
log.info(value.getText());

contents.add(ObjectEntity.builder()
.role(RoleModel.MODEL)
.parts(Lists.newArrayList(PartEntity.builder()
.text(value.getText())
.build()))
.build());
}));

ObjectEntity newObject = ObjectEntity.builder()
.parts(Lists.newArrayList(PartEntity.builder()
.text("我刚刚说了什么")
.build()))
.build();
contents.add(newObject);
ChatEntity newChat = ChatEntity.builder()
.contents(contents)
.build();
client.createChatCompletions(newChat);
```

### 流式响应

---

以数据流的形式接收。流式响应会在模型生成增量数据时将这些数据发送回您的应用。

使用示例

```java
// 构建客户端
CountDownLatch countDownLatch = new CountDownLatch(1);
ConsoleEventSourceListener listener = ConsoleEventSourceListener.builder()
.countDownLatch(countDownLatch)
.build();
GoogleClient client = GoogleClient.builder()
.apiKey(ResourceUtils.getValue("google.token"))
.listener(listener)
.build();

List<ObjectEntity> contents = Lists.newArrayList();
PartEntity part = PartEntity.builder()
.text("帮我写一万字的作文")
.build();
ObjectEntity object = ObjectEntity.builder()
.parts(Lists.newArrayList(part))
.build();
contents.add(object);
ChatEntity chat = ChatEntity.builder()
.contents(contents)
.build();
client.createChatCompletions(chat);
try {
countDownLatch.await();
}
catch (InterruptedException e) {
log.error("Interrupted while waiting", e);
}
```
6 changes: 4 additions & 2 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ theme:
name: material
custom_dir: overrides

language: en
features:
- content.code.annotate
- content.tabs.link
Expand Down Expand Up @@ -64,7 +63,7 @@ markdown_extensions:
plugins:
- i18n:
reconfigure_material: true
default_language: en
default_language: zh
languages:
- locale: zh
name: Chinese (Simplified)
Expand Down Expand Up @@ -111,6 +110,9 @@ nav:
- Google PaLM:
- reference/google_palm/completions.md
- reference/google_palm/chat.md
- Google:
- Gemini:
- reference/google/gemini/chat/home.md
- NavReleaseNote:
- release/latest.md
- release/2024.01.1.md
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai;
package org.devlive.sdk.common;

import io.reactivex.Single;
import okhttp3.MultipartBody;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai;
package org.devlive.sdk.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
Expand All @@ -12,6 +12,7 @@
import okhttp3.sse.EventSourceListener;
import okhttp3.sse.EventSources;
import org.apache.commons.lang3.ObjectUtils;
import org.devlive.sdk.common.exception.RequestException;
import org.devlive.sdk.openai.entity.AudioEntity;
import org.devlive.sdk.openai.entity.ChatEntity;
import org.devlive.sdk.openai.entity.CompletionEntity;
Expand All @@ -28,7 +29,6 @@
import org.devlive.sdk.openai.entity.beta.QueryEntity;
import org.devlive.sdk.openai.entity.beta.ThreadEntity;
import org.devlive.sdk.openai.entity.google.MessageEntity;
import org.devlive.sdk.openai.exception.RequestException;
import org.devlive.sdk.openai.mixin.IgnoreUnknownMixin;
import org.devlive.sdk.openai.model.ProviderModel;
import org.devlive.sdk.openai.model.UrlModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai.exception;
package org.devlive.sdk.common.exception;

public class AuthorizedException
extends DefaultException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai.exception;
package org.devlive.sdk.common.exception;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai.exception;
package org.devlive.sdk.common.exception;

public class ParamException
extends DefaultException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai.exception;
package org.devlive.sdk.common.exception;

public class RequestException
extends DefaultException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai.interceptor;
package org.devlive.sdk.common.interceptor;

import com.google.common.base.Preconditions;
import lombok.Getter;
Expand All @@ -11,8 +11,8 @@
import okhttp3.ResponseBody;
import okio.Buffer;
import org.apache.commons.lang3.ObjectUtils;
import org.devlive.sdk.openai.exception.AuthorizedException;
import org.devlive.sdk.openai.exception.RequestException;
import org.devlive.sdk.common.exception.AuthorizedException;
import org.devlive.sdk.common.exception.RequestException;
import org.devlive.sdk.openai.response.DefaultResponse;
import org.devlive.sdk.openai.utils.JsonUtils;

Expand Down Expand Up @@ -44,7 +44,7 @@ public Response intercept(Chain chain)
if (ObjectUtils.isNotEmpty(requestBody)) {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
log.debug("Request body {}", buffer.readUtf8());
log.info("Request body {}", buffer.readUtf8());
}

Response response = chain.proceed(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai.listener;
package org.devlive.sdk.common.listener;

import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai.listener;
package org.devlive.sdk.common.listener;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -9,7 +9,7 @@
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;
import org.apache.commons.lang3.ObjectUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.response.CompleteResponse;
import org.devlive.sdk.openai.utils.JsonUtils;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.devlive.sdk.openai.utils;
package org.devlive.sdk.common.utils;

import okhttp3.HttpUrl;

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/devlive/sdk/common/utils/ValidateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.devlive.sdk.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.devlive.sdk.common.exception.ParamException;

public class ValidateUtils
{
private ValidateUtils()
{
}

/**
* Validate host
*
* @param host Original host
* @param defaultHost Default host
* @return host
*/
public static String validateHost(String host, String defaultHost)
{
if (StringUtils.isEmpty(host)) {
return defaultHost;
}
else {
boolean flag = host.startsWith("http") || host.startsWith("https");
if (!flag) {
throw new ParamException(String.format("Invalid apiHost <%s> must start with http or https", host));
}
}
return host;
}
}
6 changes: 4 additions & 2 deletions src/main/java/org/devlive/sdk/openai/OpenAiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import okhttp3.sse.EventSourceListener;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.DefaultApi;
import org.devlive.sdk.common.DefaultClient;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.common.interceptor.DefaultInterceptor;
import org.devlive.sdk.openai.interceptor.AzureInterceptor;
import org.devlive.sdk.openai.interceptor.ClaudeInterceptor;
import org.devlive.sdk.openai.interceptor.DefaultInterceptor;
import org.devlive.sdk.openai.interceptor.GooglePaLMInterceptor;
import org.devlive.sdk.openai.interceptor.OpenAiInterceptor;
import org.devlive.sdk.openai.model.CompletionModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.model.AudioFormatModel;
import org.devlive.sdk.openai.model.AudioModel;
import org.devlive.sdk.openai.utils.FileUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.ObjectUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.model.CompletionModel;
import org.devlive.sdk.openai.utils.EnumsUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.ToString;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.model.CompletionModel;
import org.devlive.sdk.openai.utils.EnumsUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.ToString;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.model.EditModel;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import lombok.ToString;
import okhttp3.RequestBody;
import org.apache.commons.lang3.ObjectUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.model.PurposeModel;
import org.devlive.sdk.openai.utils.MultipartBodyUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.model.CompletionModel;

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.model.ImageFormatModel;
import org.devlive.sdk.openai.model.ImageSizeModel;
import org.devlive.sdk.openai.utils.MultipartBodyUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.ToString;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.model.MessageModel;
import org.devlive.sdk.openai.utils.EnumsUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.ToString;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.devlive.sdk.openai.exception.ParamException;
import org.devlive.sdk.common.exception.ParamException;
import org.devlive.sdk.openai.model.ModerationModel;

import java.util.List;
Expand Down
Loading
Loading