diff options
author | Jon Bratseth <bratseth@vespa.ai> | 2023-04-25 20:06:15 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@vespa.ai> | 2023-04-25 20:06:15 +0200 |
commit | b1c12e25e9698501440b46bca37ada23c5116239 (patch) | |
tree | b620b6da03bd81f8805733f626ebf375a5a25357 /openai-client | |
parent | d5f17d23f377776e85aa687be17b211b54423c59 (diff) |
Put the openai client in a separate component
Diffstat (limited to 'openai-client')
-rw-r--r-- | openai-client/abi-spec.json | 29 | ||||
-rw-r--r-- | openai-client/pom.xml | 67 | ||||
-rw-r--r-- | openai-client/src/main/java/ai/vespa/llm/client/openai/OpenAiClient.java | 84 | ||||
-rw-r--r-- | openai-client/src/main/java/ai/vespa/llm/client/openai/package-info.java | 11 |
4 files changed, 191 insertions, 0 deletions
diff --git a/openai-client/abi-spec.json b/openai-client/abi-spec.json new file mode 100644 index 00000000000..039ca57fc64 --- /dev/null +++ b/openai-client/abi-spec.json @@ -0,0 +1,29 @@ +{ + "ai.vespa.llm.client.openai.OpenAiClient$Builder" : { + "superClass" : "java.lang.Object", + "interfaces" : [ ], + "attributes" : [ + "public" + ], + "methods" : [ + "public void <init>(java.lang.String)", + "public ai.vespa.llm.client.openai.OpenAiClient$Builder model(java.lang.String)", + "public ai.vespa.llm.client.openai.OpenAiClient$Builder echo(boolean)", + "public ai.vespa.llm.client.openai.OpenAiClient build()" + ], + "fields" : [ ] + }, + "ai.vespa.llm.client.openai.OpenAiClient" : { + "superClass" : "java.lang.Object", + "interfaces" : [ + "ai.vespa.llm.LanguageModel" + ], + "attributes" : [ + "public" + ], + "methods" : [ + "public java.util.List complete(ai.vespa.llm.completion.Prompt)" + ], + "fields" : [ ] + } +}
\ No newline at end of file diff --git a/openai-client/pom.xml b/openai-client/pom.xml new file mode 100644 index 00000000000..f4362194b58 --- /dev/null +++ b/openai-client/pom.xml @@ -0,0 +1,67 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>com.yahoo.vespa</groupId> + <artifactId>parent</artifactId> + <version>8-SNAPSHOT</version> + <relativePath>../parent/pom.xml</relativePath> + </parent> + <artifactId>openai-client</artifactId> + <packaging>container-plugin</packaging> + <version>8-SNAPSHOT</version> + + <properties> + <openai-gpt3.version>0.12.0</openai-gpt3.version> + </properties> + + <dependencies> + <dependency> + <groupId>com.theokanning.openai-gpt3-java</groupId> + <artifactId>service</artifactId> + <version>${openai-gpt3.version}</version> + </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>annotations</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>vespajlib</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>com.yahoo.vespa</groupId> + <artifactId>bundle-plugin</artifactId> + <extensions>true</extensions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <!-- openai-gpt3-java produces warnings --> + <compilerArgs> + <arg>-Xlint:all</arg> + <arg>-Xlint:-rawtypes</arg> + <arg>-Xlint:-unchecked</arg> + <arg>-Xlint:-serial</arg> + </compilerArgs> + </configuration> + </plugin> + <plugin> + <groupId>com.yahoo.vespa</groupId> + <artifactId>abi-check-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project>
\ No newline at end of file diff --git a/openai-client/src/main/java/ai/vespa/llm/client/openai/OpenAiClient.java b/openai-client/src/main/java/ai/vespa/llm/client/openai/OpenAiClient.java new file mode 100644 index 00000000000..66be5ff1f69 --- /dev/null +++ b/openai-client/src/main/java/ai/vespa/llm/client/openai/OpenAiClient.java @@ -0,0 +1,84 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.llm.client.openai; + +import ai.vespa.llm.completion.Completion; +import ai.vespa.llm.LanguageModel; +import ai.vespa.llm.completion.Prompt; +import com.theokanning.openai.OpenAiHttpException; +import com.theokanning.openai.completion.CompletionRequest; +import com.theokanning.openai.service.OpenAiService; +import com.yahoo.api.annotations.Beta; +import com.yahoo.yolean.Exceptions; + +import java.util.List; + +/** + * A client to the OpenAI language model API. Refer to https://platform.openai.com/docs/api-reference/. + * + * @author bratseth + */ +@Beta +public class OpenAiClient implements LanguageModel { + + private final OpenAiService openAiService; + private final String model; + private final boolean echo; + + private OpenAiClient(Builder builder) { + openAiService = new OpenAiService(builder.token); + this.model = builder.model; + this.echo = builder.echo; + } + + @Override + public List<Completion> complete(Prompt prompt) { + try { + CompletionRequest completionRequest = CompletionRequest.builder() + .prompt(prompt.asString()) + .model(model) + .echo(echo) + .build(); + return openAiService.createCompletion(completionRequest).getChoices().stream() + .map(c -> new Completion(c.getText(), toFinishReason(c.getFinish_reason()))).toList(); + } + catch (OpenAiHttpException e) { + throw new RuntimeException(Exceptions.toMessageString(e)); + } + } + + private Completion.FinishReason toFinishReason(String finishReasonString) { + return switch(finishReasonString) { + case "length" -> Completion.FinishReason.length; + case "stop" -> Completion.FinishReason.stop; + default -> throw new IllegalStateException("Unknown OpenAi completion finish reason '" + finishReasonString + "'"); + }; + } + + public static class Builder { + + private final String token; + private String model = "text-davinci-003"; + private boolean echo = false; + + public Builder(String token) { + this.token = token; + } + + /** One of the language models listed at https://platform.openai.com/docs/models */ + public Builder model(String model) { + this.model = model; + return this; + } + + public Builder echo(boolean echo) { + this.echo = echo; + return this; + } + + public OpenAiClient build() { + return new OpenAiClient(this); + } + + } + +} diff --git a/openai-client/src/main/java/ai/vespa/llm/client/openai/package-info.java b/openai-client/src/main/java/ai/vespa/llm/client/openai/package-info.java new file mode 100644 index 00000000000..8b8b99308b0 --- /dev/null +++ b/openai-client/src/main/java/ai/vespa/llm/client/openai/package-info.java @@ -0,0 +1,11 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@ExportPackage +@PublicApi +package ai.vespa.llm.client.openai; + +import com.yahoo.api.annotations.PublicApi; +import com.yahoo.osgi.annotation.ExportPackage; + +/** + * Client to OpenAi's large language models. + */
\ No newline at end of file |