All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.langchain4j.model.ollama.OllamaStreamingLanguageModel Maven / Gradle / Ivy

package dev.langchain4j.model.ollama;

import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.language.StreamingLanguageModel;
import dev.langchain4j.model.ollama.spi.OllamaStreamingLanguageModelBuilderFactory;
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.spi.ServiceHelper.loadFactories;
import static java.time.Duration.ofSeconds;

/**
 * Ollama API reference
 * 
* Ollama API parameters. */ public class OllamaStreamingLanguageModel implements StreamingLanguageModel { private final OllamaClient client; private final String modelName; private final Options options; private final String format; @Builder public OllamaStreamingLanguageModel(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(String prompt, StreamingResponseHandler handler) { CompletionRequest request = CompletionRequest.builder() .model(modelName) .prompt(prompt) .options(options) .format(format) .stream(true) .build(); client.streamingCompletion(request, handler); } public static OllamaStreamingLanguageModelBuilder builder() { for (OllamaStreamingLanguageModelBuilderFactory factory : loadFactories(OllamaStreamingLanguageModelBuilderFactory.class)) { return factory.get(); } return new OllamaStreamingLanguageModelBuilder(); } public static class OllamaStreamingLanguageModelBuilder { public OllamaStreamingLanguageModelBuilder() { // This is public so it can be extended // By default with Lombok it becomes package private } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy