Skip to content

Commit

Permalink
Added more request parameters to OpenAi models (langchain4j#36)
Browse files Browse the repository at this point in the history
- top_p
- max_tokens
- presence_penalty
- frequency_penalty
  • Loading branch information
langchain4j authored Jul 18, 2023
1 parent e439f96 commit 99c4621
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ public class OpenAiChatModel implements ChatLanguageModel, TokenCountEstimator {
private final OpenAiClient client;
private final String modelName;
private final Double temperature;
private final Double topP;
private final Integer maxTokens;
private final Double presencePenalty;
private final Double frequencyPenalty;
private final Integer maxRetries;
private final OpenAiTokenizer tokenizer;

@Builder
public OpenAiChatModel(String apiKey,
String modelName,
Double temperature,
Double topP,
Integer maxTokens,
Double presencePenalty,
Double frequencyPenalty,
Duration timeout,
Integer maxRetries,
Boolean logRequests,
Expand Down Expand Up @@ -63,6 +71,10 @@ public OpenAiChatModel(String apiKey,
.build();
this.modelName = modelName;
this.temperature = temperature;
this.topP = topP;
this.maxTokens = maxTokens;
this.presencePenalty = presencePenalty;
this.frequencyPenalty = frequencyPenalty;
this.maxRetries = maxRetries;
this.tokenizer = new OpenAiTokenizer(this.modelName);
}
Expand Down Expand Up @@ -101,6 +113,10 @@ public AiMessage sendMessages(List<ChatMessage> messages, List<ToolSpecification
.messages(toOpenAiMessages(messages))
.functions(toFunctions(toolSpecifications))
.temperature(temperature)
.topP(topP)
.maxTokens(maxTokens)
.presencePenalty(presencePenalty)
.frequencyPenalty(frequencyPenalty)
.build();

ChatCompletionResponse response = withRetry(() -> client.chatCompletion(request).execute(), maxRetries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ public class OpenAiStreamingChatModel implements StreamingChatLanguageModel, Tok
private final OpenAiClient client;
private final String modelName;
private final Double temperature;
private final Double topP;
private final Integer maxTokens;
private final Double presencePenalty;
private final Double frequencyPenalty;
private final OpenAiTokenizer tokenizer;

@Builder
public OpenAiStreamingChatModel(String apiKey,
String modelName,
Double temperature,
Double topP,
Integer maxTokens,
Double presencePenalty,
Double frequencyPenalty,
Duration timeout,
Boolean logRequests,
Boolean logResponses) {
Expand All @@ -51,6 +59,10 @@ public OpenAiStreamingChatModel(String apiKey,
.build();
this.modelName = modelName;
this.temperature = temperature;
this.topP = topP;
this.maxTokens = maxTokens;
this.presencePenalty = presencePenalty;
this.frequencyPenalty = frequencyPenalty;
this.tokenizer = new OpenAiTokenizer(this.modelName);
}

Expand All @@ -74,10 +86,14 @@ public void sendUserMessage(Object structuredPrompt, StreamingResultHandler hand
public void sendMessages(List<ChatMessage> messages, StreamingResultHandler handler) {

ChatCompletionRequest request = ChatCompletionRequest.builder()
.stream(true)
.model(modelName)
.messages(toOpenAiMessages(messages))
.temperature(temperature)
.stream(true)
.topP(topP)
.maxTokens(maxTokens)
.presencePenalty(presencePenalty)
.frequencyPenalty(frequencyPenalty)
.build();

client.chatCompletion(request)
Expand Down

0 comments on commit 99c4621

Please sign in to comment.