Class OpenAIChatBuilderImpl
java.lang.Object
net.stoerr.ai.aigenpipeline.framework.chat.OpenAIChatBuilderImpl
- All Implemented Interfaces:
AIChatBuilder
Implementation of AIChatBuilder for creating and executing OpenAI chat completion requests.
That does work for interfaces similar to OpenAI chat completion - e.g. Anthropic Claude ,
local models with LM Studio , probably more.
- See Also:
-
- "https://platform.openai.com/docs/api-reference/chat"
- "https://docs.anthropic.com/claude/reference/messages_post"
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classprotected static classprotected static class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault version for the Anthropic API, of not overridden with environment variableENV_ANTHROPIC_VERSION.protected Stringprotected final Patternstatic final intstatic final StringEnvironment variable for the Anthropic API key.static final StringEnvironment variable for the Anthropic API version.static final StringEnvironment variable for the OpenAI API key.protected static final com.google.gson.Gsonprotected intprotected final List<OpenAIChatBuilderImpl.Message>protected Stringstatic final Stringstatic final Stringstatic final Stringprotected String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionassistantMsg(String text) Adds an assistant message to the chat.protected Stringexecute()Executes the chat completion request and returns the response.protected StringextractResponse(String json) protected booleanisClaude()protected booleanisOpenAI()Sets the API key for the chat completion request.maxTokens(int maxTokens) Sets the maximum number of tokens the completion can use.Sets the AI model to be used for the chat completion.Adds a system message to the chat.toJson()Converts the chat completion request to a JSON string.Sets the URL for the chat completion request.Adds a user message to the chat.
-
Field Details
-
ENV_ANTHROPIC_VERSION
Environment variable for the Anthropic API version.- See Also:
-
ENV_OPENAI_API_KEY
Environment variable for the OpenAI API key.- See Also:
-
ENV_ANTHROPIC_API_KEY
Environment variable for the Anthropic API key.- See Also:
-
ANTHROPIC_DEFAULT_VERSION
Default version for the Anthropic API, of not overridden with environment variableENV_ANTHROPIC_VERSION.- See Also:
-
DEFAULT_MAX_TOKENS
public static final int DEFAULT_MAX_TOKENS- See Also:
-
gson
protected static final com.google.gson.Gson gson -
ROLE_SYSTEM
- See Also:
-
ROLE_USER
- See Also:
-
ROLE_ASSISTANT
- See Also:
-
CODEBLOCK_PATTERN
-
model
-
messages
-
apiKey
-
maxTokens
protected int maxTokens -
url
-
-
Constructor Details
-
OpenAIChatBuilderImpl
public OpenAIChatBuilderImpl()
-
-
Method Details
-
url
Description copied from interface:AIChatBuilderSets the URL for the chat completion request.- Specified by:
urlin interfaceAIChatBuilder- Parameters:
url- The URL.- Returns:
- The builder instance for chaining.
-
key
Description copied from interface:AIChatBuilderSets the API key for the chat completion request.- Specified by:
keyin interfaceAIChatBuilder- Parameters:
key- The API key.- Returns:
- The builder instance for chaining.
-
maxTokens
Description copied from interface:AIChatBuilderSets the maximum number of tokens the completion can use.- Specified by:
maxTokensin interfaceAIChatBuilder- Parameters:
maxTokens- The maximum number of tokens.- Returns:
- The builder instance for chaining.
-
model
Description copied from interface:AIChatBuilderSets the AI model to be used for the chat completion.- Specified by:
modelin interfaceAIChatBuilder- Parameters:
model- The model name.- Returns:
- The builder instance for chaining.
-
systemMsg
Description copied from interface:AIChatBuilderAdds a system message to the chat.- Specified by:
systemMsgin interfaceAIChatBuilder- Parameters:
text- The text of the message.- Returns:
- The builder instance for chaining.
-
userMsg
Description copied from interface:AIChatBuilderAdds a user message to the chat.- Specified by:
userMsgin interfaceAIChatBuilder- Parameters:
text- The text of the message.- Returns:
- The builder instance for chaining.
-
assistantMsg
Description copied from interface:AIChatBuilderAdds an assistant message to the chat.- Specified by:
assistantMsgin interfaceAIChatBuilder- Parameters:
text- The text of the message.- Returns:
- The builder instance for chaining.
-
determineApiKey
-
isClaude
protected boolean isClaude() -
isOpenAI
protected boolean isOpenAI() -
execute
Description copied from interface:AIChatBuilderExecutes the chat completion request and returns the response.- Specified by:
executein interfaceAIChatBuilder- Returns:
- The chat completion response.
-
toJson
Description copied from interface:AIChatBuilderConverts the chat completion request to a JSON string.- Specified by:
toJsonin interfaceAIChatBuilder- Returns:
- The JSON string.
-
extractResponse
-