dev.langchain4j.model.ollama.OllamaStreamingChatModel Maven / Gradle / Ivy
package dev.langchain4j.model.ollama;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.ollama.spi.OllamaStreamingChatModelBuilderFactory;
import lombok.Builder;
import java.time.Duration;
import java.util.List;
import static dev.langchain4j.internal.Utils.getOrDefault;
import static dev.langchain4j.internal.ValidationUtils.ensureNotBlank;
import static dev.langchain4j.internal.ValidationUtils.ensureNotEmpty;
import static dev.langchain4j.model.ollama.OllamaMessagesUtils.toOllamaMessages;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;
import static java.time.Duration.ofSeconds;
/**
* Ollama API reference
*
* Ollama API parameters.
*/
public class OllamaStreamingChatModel implements StreamingChatLanguageModel {
private final OllamaClient client;
private final String modelName;
private final Options options;
private final String format;
@Builder
public OllamaStreamingChatModel(String baseUrl,
String modelName,
Double temperature,
Integer topK,
Double topP,
Double repeatPenalty,
Integer seed,
Integer numPredict,
List stop,
String format,
Duration timeout) {
this.client = OllamaClient.builder()
.baseUrl(baseUrl)
.timeout(getOrDefault(timeout, ofSeconds(60)))
.build();
this.modelName = ensureNotBlank(modelName, "modelName");
this.options = Options.builder()
.temperature(temperature)
.topK(topK)
.topP(topP)
.repeatPenalty(repeatPenalty)
.seed(seed)
.numPredict(numPredict)
.stop(stop)
.build();
this.format = format;
}
@Override
public void generate(List messages, StreamingResponseHandler handler) {
ensureNotEmpty(messages, "messages");
ChatRequest request = ChatRequest.builder()
.model(modelName)
.messages(toOllamaMessages(messages))
.options(options)
.format(format)
.stream(true)
.build();
client.streamingChat(request, handler);
}
public static OllamaStreamingChatModelBuilder builder() {
for (OllamaStreamingChatModelBuilderFactory factory : loadFactories(OllamaStreamingChatModelBuilderFactory.class)) {
return factory.get();
}
return new OllamaStreamingChatModelBuilder();
}
public static class OllamaStreamingChatModelBuilder {
public OllamaStreamingChatModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy