Skip to content

Commit

Permalink
Merge pull request #265 from edeandrea/auth-header-not-masked
Browse files Browse the repository at this point in the history
Only pass Authorization/api-key headers when necessary
  • Loading branch information
geoand authored Feb 2, 2024
2 parents c9bf7a9 + 2e8f138 commit 6e4c0de
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String chatSync() {
return restApi.blockingChatCompletion(
createChatCompletionRequest("Write a short 1 paragraph funny poem about segmentation fault"),
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.openAiApiKey(token)
.organizationId(organizationId)
.build())
.content();
Expand All @@ -78,7 +78,7 @@ public Uni<String> chatAsync() {
return restApi
.createChatCompletion(createChatCompletionRequest("Write a short 1 paragraph funny poem about Unicode"),
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.openAiApiKey(token)
.organizationId(organizationId)
.build())
.map(ChatCompletionResponse::content);
Expand All @@ -91,7 +91,7 @@ public Multi<String> chatStreaming() {
return restApi.streamingChatCompletion(
createChatCompletionRequest("Write a short 1 paragraph funny poem about Enterprise Java"),
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.openAiApiKey(token)
.organizationId(organizationId)
.build())
.map(r -> {
Expand Down Expand Up @@ -124,7 +124,7 @@ public String languageSync() {
return restApi.blockingCompletion(
createCompletionRequest("Write a short 1 paragraph funny poem about segmentation fault"),
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.openAiApiKey(token)
.organizationId(organizationId)
.build())
.text();
Expand All @@ -136,7 +136,7 @@ public Uni<String> languageAsync() {
return restApi
.completion(createCompletionRequest("Write a short 1 paragraph funny poem about Unicode"),
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.openAiApiKey(token)
.organizationId(organizationId)
.build())
.map(CompletionResponse::text);
Expand All @@ -149,7 +149,7 @@ public Multi<String> languageStreaming() {
return restApi.streamingCompletion(
createCompletionRequest("Write a short 1 paragraph funny poem about Enterprise Java"),
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.openAiApiKey(token)
.organizationId(organizationId)
.build())
.map(r -> {
Expand All @@ -171,7 +171,7 @@ public Multi<String> languageStreaming() {
public List<Float> embeddingSync() {
return restApi.blockingEmbedding(createEmbeddingRequest("Your text string goes here"),
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.openAiApiKey(token)
.organizationId(organizationId)
.build())
.embedding();
Expand All @@ -183,7 +183,7 @@ public Uni<List<Float>> embeddingAsync() {
return restApi
.embedding(createEmbeddingRequest("Your text string goes here"),
OpenAiRestApi.ApiMetadata.builder()
.apiKey(token)
.openAiApiKey(token)
.organizationId(organizationId)
.build())
.map(EmbeddingResponse::embedding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,17 @@ class ApiMetadata {
public final String authorization;

@HeaderParam("api-key")
public final String apiKey;
public final String azureApiKey;
@QueryParam("api-version")
public final String apiVersion;

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

private ApiMetadata(String authorization, String apiKey,
private ApiMetadata(String openaiApiKey, String azureApiKey,
String apiVersion, String organizationId) {
this.authorization = authorization;
this.apiKey = apiKey;
this.authorization = (openaiApiKey != null) ? "Bearer " + openaiApiKey : null;
this.azureApiKey = azureApiKey;
this.apiVersion = apiVersion;
this.organizationId = organizationId;
}
Expand All @@ -436,20 +436,30 @@ public static ApiMetadata.Builder builder() {
}

public static class Builder {
private String apiKey;
private String azureApiKey;
private String openAiApiKey;
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);
if ((azureApiKey != null) && (openAiApiKey != null)) {
return new ApiMetadata(openAiApiKey, azureApiKey, apiVersion, organizationId);
} else if (azureApiKey != null) {
return new ApiMetadata(null, azureApiKey, apiVersion, organizationId);
} else if (openAiApiKey != null) {
return new ApiMetadata(openAiApiKey, null, apiVersion, organizationId);
}

return new ApiMetadata(null, null, apiVersion, organizationId);
}

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

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

Expand Down
Loading

0 comments on commit 6e4c0de

Please sign in to comment.