Added support for OpenAI demo proxy (chat, embedding and moderation models) (#33)

Now, developers don't need their own OpenAI API key to try out
LangChain4j.
This commit is contained in:
LangChain4j 2023-07-17 20:44:55 +02:00 committed by GitHub
parent 14185653c7
commit b2028262b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 0 deletions

View File

@ -46,8 +46,14 @@ public class OpenAiChatModel implements ChatLanguageModel, TokenCountEstimator {
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)

View File

@ -13,6 +13,7 @@ import java.time.Duration;
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;
@ -37,8 +38,14 @@ public class OpenAiEmbeddingModel implements EmbeddingModel, TokenCountEstimator
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)

View File

@ -19,6 +19,11 @@ import static java.util.stream.Collectors.toList;
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);

View File

@ -16,6 +16,7 @@ import java.util.List;
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;
@ -39,8 +40,14 @@ public class OpenAiModerationModel implements ModerationModel {
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)