io.quarkiverse.langchain4j.ollama.OllamaChatLanguageModel Maven / Gradle / Ivy
package io.quarkiverse.langchain4j.ollama;
import static dev.langchain4j.data.message.AiMessage.aiMessage;
import static dev.langchain4j.internal.ValidationUtils.ensureNotEmpty;
import static io.quarkiverse.langchain4j.ollama.MessageMapper.toOllamaMessages;
import static io.quarkiverse.langchain4j.ollama.MessageMapper.toTools;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jboss.logging.Logger;
import org.jetbrains.annotations.NotNull;
import com.fasterxml.jackson.core.JsonProcessingException;
import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.listener.ChatModelErrorContext;
import dev.langchain4j.model.chat.listener.ChatModelListener;
import dev.langchain4j.model.chat.listener.ChatModelRequest;
import dev.langchain4j.model.chat.listener.ChatModelRequestContext;
import dev.langchain4j.model.chat.listener.ChatModelResponse;
import dev.langchain4j.model.chat.listener.ChatModelResponseContext;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.output.TokenUsage;
import io.quarkiverse.langchain4j.QuarkusJsonCodecFactory;
public class OllamaChatLanguageModel implements ChatLanguageModel {
private static final Logger log = Logger.getLogger(OllamaChatLanguageModel.class);
private final OllamaClient client;
private final String model;
private final String format;
private final Options options;
private final List listeners;
private OllamaChatLanguageModel(Builder builder) {
client = new OllamaClient(builder.baseUrl, builder.timeout, builder.logRequests, builder.logResponses,
builder.configName);
model = builder.model;
format = builder.format;
options = builder.options;
this.listeners = builder.listeners;
}
public static Builder builder() {
return new Builder();
}
@Override
public Response generate(List messages) {
return generate(messages, Collections.emptyList());
}
@Override
public Response generate(List messages, ToolSpecification toolSpecification) {
return generate(messages,
toolSpecification != null ? Collections.singletonList(toolSpecification) : Collections.emptyList());
}
@Override
public Response generate(List messages, List toolSpecifications) {
ensureNotEmpty(messages, "messages");
ChatRequest request = ChatRequest.builder()
.model(model)
.messages(toOllamaMessages(messages))
.tools(toTools(toolSpecifications))
.options(options)
.format(format)
.stream(false)
.build();
ChatModelRequest modelListenerRequest = createModelListenerRequest(request, messages, toolSpecifications);
Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy