-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from TFyre/refactor
refactor model builders
- Loading branch information
Showing
17 changed files
with
2,313 additions
and
470 deletions.
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
src/main/java/io/github/jeddict/ai/lang/ChatModelBaseBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package io.github.jeddict.ai.lang; | ||
|
||
import java.time.Duration; | ||
import java.util.Map; | ||
|
||
/** | ||
* Builder interface for configuring chat model parameters. This interface provides methods to set various configuration options for chat models. | ||
* | ||
* @author Francois Steyn | ||
* @param <T> The type of chat model being built | ||
*/ | ||
public interface ChatModelBaseBuilder<T> { | ||
|
||
/** | ||
* Sets the base URL for the chat model API. | ||
* | ||
* @param baseUrl The base URL of the API endpoint | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> baseUrl(final String baseUrl); | ||
|
||
/** | ||
* Sets the custom headers for accessing the service. | ||
* | ||
* @param customHeaders The custom headers for accessing the service | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> customHeaders(final Map<String, String> customHeaders); | ||
|
||
/** | ||
* Sets the API key for authentication. | ||
* | ||
* @param apiKey The API key for accessing the service | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> apiKey(final String apiKey); | ||
|
||
/** | ||
* Sets the name of the model to be used. | ||
* | ||
* @param modelName The name of the model | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> modelName(final String modelName); | ||
|
||
/** | ||
* Sets the temperature parameter for response generation. | ||
* | ||
* @param temperature Controls randomness in the output (0.0 to 1.0) | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> temperature(final Double temperature); | ||
|
||
/** | ||
* Sets the timeout duration for API calls. | ||
* | ||
* @param timeout The duration after which the request times out | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> timeout(final Duration timeout); | ||
|
||
/** | ||
* Sets the top P parameter for response generation. | ||
* | ||
* @param topP Controls diversity via nucleus sampling | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> topP(final Double topP); | ||
|
||
/** | ||
* Sets the maximum number of retry attempts for failed requests. | ||
* | ||
* @param maxRetries The maximum number of retries | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> maxRetries(final Integer maxRetries); | ||
|
||
/** | ||
* Sets the maximum number of tokens in the output. | ||
* | ||
* @param maxOutputTokens The maximum number of output tokens | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> maxOutputTokens(final Integer maxOutputTokens); | ||
|
||
/** | ||
* Sets the repeat penalty for response generation. | ||
* | ||
* @param repeatPenalty The penalty for repeated content | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> repeatPenalty(final Double repeatPenalty); | ||
|
||
/** | ||
* Sets the random seed for reproducible results. | ||
* | ||
* @param seed The random seed value | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> seed(final Integer seed); | ||
|
||
/** | ||
* Sets the maximum total tokens (input + output). | ||
* | ||
* @param maxTokens The maximum number of total tokens | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> maxTokens(final Integer maxTokens); | ||
|
||
/** | ||
* Sets the maximum number of tokens for completion. | ||
* | ||
* @param maxCompletionTokens The maximum completion tokens | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> maxCompletionTokens(final Integer maxCompletionTokens); | ||
|
||
/** | ||
* Sets the top K parameter for response generation. | ||
* | ||
* @param topK The number of highest probability vocabulary tokens | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> topK(final Integer topK); | ||
|
||
/** | ||
* Sets the presence penalty for response generation. | ||
* | ||
* @param presencePenalty The penalty for token presence | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> presencePenalty(final Double presencePenalty); | ||
|
||
/** | ||
* Sets the frequency penalty for response generation. | ||
* | ||
* @param frequencyPenalty The penalty for token frequency | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> frequencyPenalty(final Double frequencyPenalty); | ||
|
||
/** | ||
* Sets the organization ID for API requests. | ||
* | ||
* @param organizationId The organization identifier | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> organizationId(final String organizationId); | ||
|
||
/** | ||
* Configures logging for requests and responses. | ||
* | ||
* @param logRequests Whether to log requests | ||
* @param logResponses Whether to log responses | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> logRequestsResponses(final boolean logRequests, final boolean logResponses); | ||
|
||
/** | ||
* Sets whether to include code execution output. | ||
* | ||
* @param includeCodeExecutionOutput Whether to include code execution output | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> includeCodeExecutionOutput(final boolean includeCodeExecutionOutput); | ||
|
||
/** | ||
* Sets whether to allow code execution. | ||
* | ||
* @param allowCodeExecution Whether to allow code execution | ||
* @return The builder instance | ||
*/ | ||
ChatModelBaseBuilder<T> allowCodeExecution(final boolean allowCodeExecution); | ||
|
||
/** | ||
* Builds and returns the configured chat model instance. | ||
* | ||
* @return The configured chat model instance | ||
*/ | ||
T build(); | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/io/github/jeddict/ai/lang/ChatModelBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.github.jeddict.ai.lang; | ||
|
||
import dev.langchain4j.model.chat.ChatLanguageModel; | ||
import java.time.Duration; | ||
import java.util.Map; | ||
|
||
/** | ||
* A specialized builder interface for constructing ChatLanguageModel instances. This interface extends ChatModelBaseBuilder to provide specific building capabilities for | ||
* chat-based language models. It inherits all configuration methods from the base builder while specifically targeting ChatLanguageModel as the build output type. | ||
* | ||
* @author Francois Steyn | ||
* @see ChatLanguageModel | ||
* @see ChatModelBaseBuilder | ||
*/ | ||
public interface ChatModelBuilder extends ChatModelBaseBuilder<ChatLanguageModel> { | ||
|
||
@Override | ||
ChatModelBuilder baseUrl(final String baseUrl); | ||
|
||
@Override | ||
ChatModelBuilder customHeaders(final Map<String, String> customHeaders); | ||
|
||
@Override | ||
ChatModelBuilder apiKey(final String apiKey); | ||
|
||
@Override | ||
ChatModelBuilder modelName(final String modelName); | ||
|
||
@Override | ||
ChatModelBuilder temperature(final Double temperature); | ||
|
||
@Override | ||
ChatModelBuilder timeout(final Duration timeout); | ||
|
||
@Override | ||
ChatModelBuilder topP(final Double topP); | ||
|
||
@Override | ||
ChatModelBuilder maxRetries(final Integer maxRetries); | ||
|
||
@Override | ||
ChatModelBuilder maxOutputTokens(final Integer maxOutputTokens); | ||
|
||
@Override | ||
ChatModelBuilder repeatPenalty(final Double repeatPenalty); | ||
|
||
@Override | ||
ChatModelBuilder seed(final Integer seed); | ||
|
||
@Override | ||
ChatModelBuilder maxTokens(final Integer maxTokens); | ||
|
||
@Override | ||
ChatModelBuilder maxCompletionTokens(final Integer maxCompletionTokens); | ||
|
||
@Override | ||
ChatModelBuilder topK(final Integer topK); | ||
|
||
@Override | ||
ChatModelBuilder presencePenalty(final Double presencePenalty); | ||
|
||
@Override | ||
ChatModelBuilder frequencyPenalty(final Double frequencyPenalty); | ||
|
||
@Override | ||
ChatModelBuilder organizationId(final String organizationId); | ||
|
||
@Override | ||
ChatModelBuilder logRequestsResponses(final boolean logRequests, final boolean logResponses); | ||
|
||
@Override | ||
ChatModelBuilder includeCodeExecutionOutput(final boolean includeCodeExecutionOutput); | ||
|
||
@Override | ||
ChatModelBuilder allowCodeExecution(final boolean allowCodeExecution); | ||
|
||
@Override | ||
ChatLanguageModel build(); | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/io/github/jeddict/ai/lang/ChatModelStreamingBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.github.jeddict.ai.lang; | ||
|
||
import dev.langchain4j.model.chat.StreamingChatLanguageModel; | ||
import java.time.Duration; | ||
import java.util.Map; | ||
|
||
/** | ||
* A specialized builder interface for constructing StreamingChatLanguageModel instances. This interface extends ChatModelBaseBuilder to provide specific building capabilities for | ||
* streaming-enabled chat language models. It inherits all configuration methods from the base builder while targeting StreamingChatLanguageModel as the build output type. The | ||
* streaming functionality enables real-time, token-by-token response generation. | ||
* | ||
* @author Francois Steyn | ||
* @see StreamingChatLanguageModel | ||
* @see ChatModelBaseBuilder | ||
*/ | ||
public interface ChatModelStreamingBuilder extends ChatModelBaseBuilder<StreamingChatLanguageModel> { | ||
|
||
@Override | ||
ChatModelStreamingBuilder baseUrl(final String baseUrl); | ||
|
||
@Override | ||
ChatModelStreamingBuilder customHeaders(final Map<String, String> customHeaders); | ||
|
||
@Override | ||
ChatModelStreamingBuilder apiKey(final String apiKey); | ||
|
||
@Override | ||
ChatModelStreamingBuilder modelName(final String modelName); | ||
|
||
@Override | ||
ChatModelStreamingBuilder temperature(final Double temperature); | ||
|
||
@Override | ||
ChatModelStreamingBuilder timeout(final Duration timeout); | ||
|
||
@Override | ||
ChatModelStreamingBuilder topP(final Double topP); | ||
|
||
@Override | ||
ChatModelStreamingBuilder maxRetries(final Integer maxRetries); | ||
|
||
@Override | ||
ChatModelStreamingBuilder maxOutputTokens(final Integer maxOutputTokens); | ||
|
||
@Override | ||
ChatModelStreamingBuilder repeatPenalty(final Double repeatPenalty); | ||
|
||
@Override | ||
ChatModelStreamingBuilder seed(final Integer seed); | ||
|
||
@Override | ||
ChatModelStreamingBuilder maxTokens(final Integer maxTokens); | ||
|
||
@Override | ||
ChatModelStreamingBuilder maxCompletionTokens(final Integer maxCompletionTokens); | ||
|
||
@Override | ||
ChatModelStreamingBuilder topK(final Integer topK); | ||
|
||
@Override | ||
ChatModelStreamingBuilder presencePenalty(final Double presencePenalty); | ||
|
||
@Override | ||
ChatModelStreamingBuilder frequencyPenalty(final Double frequencyPenalty); | ||
|
||
@Override | ||
ChatModelStreamingBuilder organizationId(final String organizationId); | ||
|
||
@Override | ||
ChatModelStreamingBuilder logRequestsResponses(final boolean logRequests, final boolean logResponses); | ||
|
||
@Override | ||
ChatModelStreamingBuilder includeCodeExecutionOutput(final boolean includeCodeExecutionOutput); | ||
|
||
@Override | ||
ChatModelStreamingBuilder allowCodeExecution(final boolean allowCodeExecution); | ||
|
||
@Override | ||
StreamingChatLanguageModel build(); | ||
|
||
} |
Oops, something went wrong.