Skip to content

Commit

Permalink
Allow specifying the organizationId in the configuration
Browse files Browse the repository at this point in the history
Fixes #131

# Conflicts:
#	openai/openai-vanilla/runtime/src/main/java/io/quarkiverse/langchain4j/openai/runtime/OpenAiRecorder.java
  • Loading branch information
edeandrea committed Jan 3, 2024
1 parent cd127b7 commit 73e477a
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 56 deletions.
17 changes: 17 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-langchain4j-openai.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ endif::add-copy-button-to-env-var[]
|


a| [[quarkus-langchain4j-openai_quarkus.langchain4j.openai.organization-id]]`link:#quarkus-langchain4j-openai_quarkus.langchain4j.openai.organization-id[quarkus.langchain4j.openai.organization-id]`


[.description]
--
OpenAI Organization ID (https://platform.openai.com/docs/api-reference/organization-optional)

ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_OPENAI_ORGANIZATION_ID+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_OPENAI_ORGANIZATION_ID+++`
endif::add-copy-button-to-env-var[]
--|string
|


a| [[quarkus-langchain4j-openai_quarkus.langchain4j.openai.timeout]]`link:#quarkus-langchain4j-openai_quarkus.langchain4j.openai.timeout[quarkus.langchain4j.openai.timeout]`


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
*/
package org.acme.example.openai;

import static org.acme.example.openai.MessageUtil.createChatCompletionRequest;
import static org.acme.example.openai.MessageUtil.createCompletionRequest;
import static org.acme.example.openai.MessageUtil.createEmbeddingRequest;
import static org.acme.example.openai.MessageUtil.*;

import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -50,6 +48,7 @@ public class QuarkusRestApiResource {

private final OpenAiRestApi restApi;
private final String token;
private final String organizationId;

public QuarkusRestApiResource(Langchain4jOpenAiConfig runtimeConfig)
throws URISyntaxException {
Expand All @@ -58,15 +57,18 @@ public QuarkusRestApiResource(Langchain4jOpenAiConfig runtimeConfig)
.build(OpenAiRestApi.class);
this.token = runtimeConfig.apiKey()
.orElseThrow(() -> new IllegalArgumentException("quarkus.langchain4j.openai.api-key must be provided"));

this.organizationId = runtimeConfig.organizationId().orElse(null);
}

@GET
@Path("chat/sync")
public String chatSync() {
return restApi.blockingChatCompletion(
createChatCompletionRequest("Write a short 1 paragraph funny poem about segmentation fault"),
OpenAiRestApi.ApiMetadata.of(token, null))
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.organizationId(organizationId)
.build())
.content();
}

Expand All @@ -75,7 +77,10 @@ public String chatSync() {
public Uni<String> chatAsync() {
return restApi
.createChatCompletion(createChatCompletionRequest("Write a short 1 paragraph funny poem about Unicode"),
OpenAiRestApi.ApiMetadata.of(token, null))
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.organizationId(organizationId)
.build())
.map(ChatCompletionResponse::content);
}

Expand All @@ -85,7 +90,10 @@ public Uni<String> chatAsync() {
public Multi<String> chatStreaming() {
return restApi.streamingChatCompletion(
createChatCompletionRequest("Write a short 1 paragraph funny poem about Enterprise Java"),
OpenAiRestApi.ApiMetadata.of(token, null))
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.organizationId(organizationId)
.build())
.map(r -> {
if (r.choices() != null) {
if (r.choices().size() == 1) {
Expand Down Expand Up @@ -115,7 +123,10 @@ public Multi<String> chatStreaming() {
public String languageSync() {
return restApi.blockingCompletion(
createCompletionRequest("Write a short 1 paragraph funny poem about segmentation fault"),
OpenAiRestApi.ApiMetadata.of(token, null))
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.organizationId(organizationId)
.build())
.text();
}

Expand All @@ -124,7 +135,10 @@ public String languageSync() {
public Uni<String> languageAsync() {
return restApi
.completion(createCompletionRequest("Write a short 1 paragraph funny poem about Unicode"),
OpenAiRestApi.ApiMetadata.of(token, null))
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.organizationId(organizationId)
.build())
.map(CompletionResponse::text);
}

Expand All @@ -134,7 +148,10 @@ public Uni<String> languageAsync() {
public Multi<String> languageStreaming() {
return restApi.streamingCompletion(
createCompletionRequest("Write a short 1 paragraph funny poem about Enterprise Java"),
OpenAiRestApi.ApiMetadata.of(token, null))
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.organizationId(organizationId)
.build())
.map(r -> {
if (r.choices() != null) {
if (r.choices().size() == 1) {
Expand All @@ -153,14 +170,22 @@ public Multi<String> languageStreaming() {
@Path("embedding/sync")
public List<Float> embeddingSync() {
return restApi.blockingEmbedding(createEmbeddingRequest("Your text string goes here"),
OpenAiRestApi.ApiMetadata.of(token, null)).embedding();
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.organizationId(organizationId)
.build())
.embedding();
}

@GET
@Path("embedding/async")
public Uni<List<Float>> embeddingAsync() {
return restApi
.embedding(createEmbeddingRequest("Your text string goes here"), OpenAiRestApi.ApiMetadata.of(token, null))
.embedding(createEmbeddingRequest("Your text string goes here"),
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.organizationId(organizationId)
.build())
.map(EmbeddingResponse::embedding);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,48 @@ class ApiMetadata {
@QueryParam("api-version")
public final String apiVersion;

@HeaderParam("OpenAI-Organization")
public final String organizationId;

private ApiMetadata(String authorization, String apiKey,
String apiVersion) {
String apiVersion, String organizationId) {
this.authorization = authorization;
this.apiKey = apiKey;
this.apiVersion = apiVersion;
this.organizationId = organizationId;
}

public static ApiMetadata.Builder builder() {
return new Builder();
}

public static ApiMetadata of(String apiKey, String apiVersion) {
if (apiKey == null) {
return new ApiMetadata(null, null, apiVersion);
public static class Builder {
private String apiKey;
private String apiVersion;
private String organizationId;

public ApiMetadata build() {
return (apiKey == null) ? new ApiMetadata(null, null, apiVersion, organizationId)
: new ApiMetadata(
"Bearer " + apiKey, // typical OpenAI authentication
apiKey, // used by AzureAI
apiVersion, organizationId);
}

public ApiMetadata.Builder apiKey(String apiKey) {
this.apiKey = apiKey;
return this;
}

public ApiMetadata.Builder apiVersion(String apiVersion) {
this.apiVersion = apiVersion;
return this;
}

public ApiMetadata.Builder organizationId(String organizationId) {
this.organizationId = organizationId;
return this;
}
return new ApiMetadata(
"Bearer " + apiKey, // typical OpenAI authentication
apiKey, // used by AzureAI
apiVersion);
}
}
}
Loading

0 comments on commit 73e477a

Please sign in to comment.