-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
329 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Completions | ||
--- | ||
|
||
!!! note | ||
|
||
Support the google palm, product address: [https://developers.generativeai.google/products/palm](https://developers.generativeai.google/products/palm) | ||
|
||
### Create completion | ||
|
||
--- | ||
|
||
Creates a completion for the provided prompt and parameters. | ||
|
||
```java | ||
// Automatic resource release | ||
try(OpenAiClient client=OpenAiClient.builder() | ||
.provider(ProviderModel.GOOGLE_PALM) | ||
.model(CompletionModel.TEXT_BISON_001) | ||
.apiKey(System.getProperty("google.token")) | ||
.build()) | ||
{ | ||
PromptEntity prompt = PromptEntity.builder() | ||
.text("How to create a completion") | ||
.build(); | ||
CompletionEntity configure = CompletionEntity.builder() | ||
.prompt(prompt) | ||
.build(); | ||
client.createPaLMCompletion(configure).getCandidates(); | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: Completions | ||
--- | ||
|
||
!!! note | ||
|
||
支持 google palm,产品地址: [https://developers.generativeai.google/products/palm](https://developers.generativeai.google/products/palm) | ||
|
||
### Create completion | ||
|
||
--- | ||
|
||
为提供的提示和参数创建补全。 | ||
|
||
```java | ||
try(OpenAiClient client=OpenAiClient.builder() | ||
.provider(ProviderModel.GOOGLE_PALM) | ||
.model(CompletionModel.TEXT_BISON_001) | ||
.apiKey(System.getProperty("google.token")) | ||
.build()) | ||
{ | ||
PromptEntity prompt = PromptEntity.builder() | ||
.text("How to create a completion") | ||
.build(); | ||
CompletionEntity configure = CompletionEntity.builder() | ||
.prompt(prompt) | ||
.build(); | ||
client.createPaLMCompletion(configure).getCandidates(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/org/devlive/sdk/openai/entity/google/CompletionEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.devlive.sdk.openai.entity.google; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.ToString; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
@ToString | ||
@AllArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class CompletionEntity | ||
{ | ||
@JsonProperty(value = "prompt") | ||
private PromptEntity prompt; | ||
|
||
@JsonProperty(value = "temperature") | ||
@Builder.Default | ||
private Double temperature = 0.25; | ||
|
||
@JsonProperty(value = "top_k") | ||
@Builder.Default | ||
private Integer topK = 40; | ||
|
||
@JsonProperty(value = "top_p") | ||
@Builder.Default | ||
private Double topP = 1.0; | ||
|
||
@JsonProperty(value = "candidate_count") | ||
@Builder.Default | ||
private Integer candidateCount = 1; | ||
|
||
@JsonProperty(value = "max_output_tokens") | ||
@Builder.Default | ||
private Integer maxOutputTokens = 1024; | ||
|
||
@JsonProperty(value = "stop_sequences") | ||
private List<String> stop; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/devlive/sdk/openai/entity/google/PromptEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.devlive.sdk.openai.entity.google; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@Builder | ||
@ToString | ||
@AllArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class PromptEntity | ||
{ | ||
@JsonProperty(value = "text") | ||
private String text; | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/devlive/sdk/openai/interceptor/GooglePaLMInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.devlive.sdk.openai.interceptor; | ||
|
||
import com.google.common.collect.Lists; | ||
import lombok.extern.slf4j.Slf4j; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.Request; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.devlive.sdk.openai.exception.ParamException; | ||
import org.devlive.sdk.openai.utils.HttpUrlUtils; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
public class GooglePaLMInterceptor | ||
extends DefaultInterceptor | ||
{ | ||
public GooglePaLMInterceptor() | ||
{ | ||
log.debug("Google PaLM Interceptor"); | ||
} | ||
|
||
@Override | ||
protected Request prepared(Request original) | ||
{ | ||
if (StringUtils.isEmpty(this.getApiKey())) { | ||
throw new ParamException("Invalid Google PaLM token, must be non-empty"); | ||
} | ||
HttpUrl httpUrl = original.url(); | ||
List<String> pathSegments = Lists.newArrayList(); | ||
httpUrl = HttpUrlUtils.removePathSegment(httpUrl); | ||
// https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=YOUR_KEY | ||
pathSegments.add(0, String.join(":", this.getModel(), "generateText")); | ||
pathSegments.add(0, "models"); | ||
pathSegments.add(0, "v1beta2"); | ||
httpUrl = httpUrl.newBuilder() | ||
.host(httpUrl.host()) | ||
.port(httpUrl.port()) | ||
.addPathSegments(String.join("/", pathSegments)) | ||
.addQueryParameter("key", this.getApiKey()) | ||
.build(); | ||
log.debug("Google PaLM interceptor request url {}", httpUrl); | ||
return original.newBuilder() | ||
.header("Content-Type", "application/json") | ||
.url(httpUrl) | ||
.method(original.method(), original.body()) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/org/devlive/sdk/openai/response/CandidateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.devlive.sdk.openai.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class CandidateResponse | ||
{ | ||
@JsonProperty(value = "output") | ||
private String output; | ||
|
||
@JsonProperty(value = "safetyRatings") | ||
private List<SafetyResponse> safetyRatings; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/devlive/sdk/openai/response/SafetyResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.devlive.sdk.openai.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class SafetyResponse | ||
{ | ||
@JsonProperty(value = "category") | ||
private String category; | ||
|
||
@JsonProperty(value = "probability") | ||
private String probability; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/devlive/sdk/openai/utils/HttpUrlUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.devlive.sdk.openai.utils; | ||
|
||
import okhttp3.HttpUrl; | ||
|
||
import java.util.List; | ||
|
||
public class HttpUrlUtils | ||
{ | ||
private HttpUrlUtils() | ||
{} | ||
|
||
/** | ||
* Removes all path segments from the given HttpUrl. | ||
* | ||
* @param httpUrl the HttpUrl from which to remove path segments | ||
* @return the modified HttpUrl with all path segments removed | ||
*/ | ||
public static HttpUrl removePathSegment(HttpUrl httpUrl) | ||
{ | ||
List<String> pathSegments = httpUrl.pathSegments(); | ||
for (int i = 0; i < pathSegments.size(); i++) { | ||
httpUrl = httpUrl.newBuilder() | ||
.removePathSegment(0) | ||
.build(); | ||
} | ||
return httpUrl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.