Class OpenAIChatBuilderImpl
- java.lang.Object
-
- net.stoerr.ai.aigenpipeline.framework.chat.OpenAIChatBuilderImpl
-
- All Implemented Interfaces:
AIChatBuilder
public class OpenAIChatBuilderImpl extends Object implements 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 Classes Modifier and Type Class Description protected static classOpenAIChatBuilderImpl.ChatCompletionRequestprotected static classOpenAIChatBuilderImpl.ChatCompletionResponseprotected static classOpenAIChatBuilderImpl.Message
-
Field Summary
Fields Modifier and Type Field Description static StringANTHROPIC_DEFAULT_VERSIONDefault version for the Anthropic API, of not overridden with environment variableENV_ANTHROPIC_VERSION.protected StringapiKeyprotected PatternCODEBLOCK_PATTERNstatic intDEFAULT_MAX_TOKENSstatic StringENV_ANTHROPIC_API_KEYEnvironment variable for the Anthropic API key.static StringENV_ANTHROPIC_VERSIONEnvironment variable for the Anthropic API version.static StringENV_OPENAI_API_KEYEnvironment variable for the OpenAI API key.protected static com.google.gson.Gsongsonprotected intmaxTokensprotected List<OpenAIChatBuilderImpl.Message>messagesprotected Stringmodelstatic StringMODEL_OPENAIJSONPseudo model that outputs the json that would be sent to OpenAI and writes that to the output, instead of actually calling OpenAI.protected StringorganizationIdstatic StringROLE_ASSISTANTstatic StringROLE_SYSTEMstatic StringROLE_USERprotected Stringurl
-
Constructor Summary
Constructors Constructor Description OpenAIChatBuilderImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AIChatBuilderassistantMsg(String text)Adds an assistant message to the chat.protected StringdetermineApiKey()Stringexecute()Executes the chat completion request and returns the response.protected StringextractResponse(String json)protected booleanisClaude()protected booleanisOpenAI()AIChatBuilderkey(String key)Sets the API key for the chat completion request.AIChatBuildermaxTokens(int maxTokens)Sets the maximum number of tokens the completion can use.AIChatBuildermodel(String model)Sets the AI model to be used for the chat completion.AIChatBuilderorganizationId(String organizationId)Sets the organization ID for the chat completion request - if applicable (OpenAI).AIChatBuildersystemMsg(String text)Adds a system message to the chat.StringtoJson()Converts the chat completion request to a JSON string.AIChatBuilderurl(String url)Sets the URL for the chat completion request.AIChatBuilderuserMsg(String text)Adds a user message to the chat.
-
-
-
Field Detail
-
MODEL_OPENAIJSON
public static final String MODEL_OPENAIJSON
Pseudo model that outputs the json that would be sent to OpenAI and writes that to the output, instead of actually calling OpenAI. Mostly for debugging.- See Also:
- Constant Field Values
-
ENV_ANTHROPIC_VERSION
public static final String ENV_ANTHROPIC_VERSION
Environment variable for the Anthropic API version.- See Also:
- Constant Field Values
-
ENV_OPENAI_API_KEY
public static final String ENV_OPENAI_API_KEY
Environment variable for the OpenAI API key.- See Also:
- Constant Field Values
-
ENV_ANTHROPIC_API_KEY
public static final String ENV_ANTHROPIC_API_KEY
Environment variable for the Anthropic API key.- See Also:
- Constant Field Values
-
ANTHROPIC_DEFAULT_VERSION
public static final String ANTHROPIC_DEFAULT_VERSION
Default version for the Anthropic API, of not overridden with environment variableENV_ANTHROPIC_VERSION.- See Also:
- Constant Field Values
-
DEFAULT_MAX_TOKENS
public static final int DEFAULT_MAX_TOKENS
- See Also:
- Constant Field Values
-
gson
protected static final com.google.gson.Gson gson
-
ROLE_SYSTEM
public static final String ROLE_SYSTEM
- See Also:
- Constant Field Values
-
ROLE_USER
public static final String ROLE_USER
- See Also:
- Constant Field Values
-
ROLE_ASSISTANT
public static final String ROLE_ASSISTANT
- See Also:
- Constant Field Values
-
CODEBLOCK_PATTERN
protected final Pattern CODEBLOCK_PATTERN
-
model
protected String model
-
messages
protected final List<OpenAIChatBuilderImpl.Message> messages
-
apiKey
protected String apiKey
-
organizationId
protected String organizationId
-
maxTokens
protected int maxTokens
-
url
protected String url
-
-
Method Detail
-
url
public AIChatBuilder url(String 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
public AIChatBuilder key(String 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.
-
organizationId
public AIChatBuilder organizationId(String organizationId)
Description copied from interface:AIChatBuilderSets the organization ID for the chat completion request - if applicable (OpenAI).- Specified by:
organizationIdin interfaceAIChatBuilder- Parameters:
organizationId- The organization ID.- Returns:
- The builder instance for chaining.
-
maxTokens
public AIChatBuilder maxTokens(int 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
public AIChatBuilder model(String 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
public AIChatBuilder systemMsg(String text)
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
public AIChatBuilder userMsg(String text)
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
public AIChatBuilder assistantMsg(String text)
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
protected String determineApiKey()
-
isClaude
protected boolean isClaude()
-
isOpenAI
protected boolean isOpenAI()
-
execute
public String execute()
Description copied from interface:AIChatBuilderExecutes the chat completion request and returns the response.- Specified by:
executein interfaceAIChatBuilder- Returns:
- The chat completion response.
-
toJson
public String toJson()
Description copied from interface:AIChatBuilderConverts the chat completion request to a JSON string.- Specified by:
toJsonin interfaceAIChatBuilder- Returns:
- The JSON string.
-
-