Skip to content

Commit

Permalink
Added support for OpenAI demo proxy (chat, embedding and moderation m…
Browse files Browse the repository at this point in the history
…odels) (langchain4j#33)

Now, developers don't need their own OpenAI API key to try out
LangChain4j.
  • Loading branch information
langchain4j authored Jul 17, 2023
1 parent 1418565 commit b202826
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ public OpenAiChatModel(String apiKey,
timeout = timeout == null ? defaultTimeoutFor(modelName) : timeout;
maxRetries = maxRetries == null ? 3 : maxRetries;

String url = OPENAI_URL;
if (OPENAI_DEMO_API_KEY.equals(apiKey)) {
url = OPENAI_DEMO_URL;
}

this.client = OpenAiClient.builder()
.apiKey(apiKey)
.url(url)
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;

import static dev.langchain4j.internal.RetryUtils.withRetry;
import static dev.langchain4j.model.openai.OpenAiHelper.*;
import static dev.langchain4j.model.openai.OpenAiModelName.TEXT_EMBEDDING_ADA_002;
import static java.time.Duration.ofSeconds;
import static java.util.Collections.singletonList;
Expand All @@ -37,8 +38,14 @@ public OpenAiEmbeddingModel(String apiKey,
timeout = timeout == null ? ofSeconds(15) : timeout;
maxRetries = maxRetries == null ? 3 : maxRetries;

String url = OPENAI_URL;
if (OPENAI_DEMO_API_KEY.equals(apiKey)) {
url = OPENAI_DEMO_URL;
}

this.client = OpenAiClient.builder()
.apiKey(apiKey)
.url(url)
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

class OpenAiHelper {

static final String OPENAI_URL = "https://api.openai.com";

static final String OPENAI_DEMO_API_KEY = "demo";
static final String OPENAI_DEMO_URL = "http://langchain4j.dev/demo/openai";

static Duration defaultTimeoutFor(String modelName) {
if (modelName.startsWith(GPT_3_5_TURBO)) {
return ofSeconds(7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static dev.langchain4j.internal.RetryUtils.withRetry;
import static dev.langchain4j.model.input.structured.StructuredPromptProcessor.toPrompt;
import static dev.langchain4j.model.openai.OpenAiHelper.*;
import static dev.langchain4j.model.openai.OpenAiModelName.TEXT_MODERATION_LATEST;
import static java.time.Duration.ofSeconds;
import static java.util.Collections.singletonList;
Expand All @@ -39,8 +40,14 @@ public OpenAiModerationModel(String apiKey,
timeout = timeout == null ? ofSeconds(15) : timeout;
maxRetries = maxRetries == null ? 3 : maxRetries;

String baseUrl = OPENAI_URL;
if (OPENAI_DEMO_API_KEY.equals(apiKey)) {
baseUrl = OPENAI_DEMO_URL;
}

this.client = OpenAiClient.builder()
.apiKey(apiKey)
.url(baseUrl)
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
Expand Down

0 comments on commit b202826

Please sign in to comment.