wenjin272 commented on code in PR #250: URL: https://github.com/apache/flink-agents/pull/250#discussion_r2438136157
########## integrations/embedding-models/ollama/src/main/java/org/apache/flink/agents/integrations/embeddingmodels/ollama/OllamaEmbeddingModelConnection.java: ########## @@ -0,0 +1,255 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.agents.integrations.embeddingmodels.ollama; + +import io.github.ollama4j.OllamaAPI; +import io.github.ollama4j.exceptions.OllamaBaseException; +import org.apache.flink.agents.api.embedding.model.BaseEmbeddingModelConnection; +import org.apache.flink.agents.api.resource.Resource; +import org.apache.flink.agents.api.resource.ResourceDescriptor; +import org.apache.flink.agents.api.resource.ResourceType; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiFunction; + +/** + * An embedding model integration for Ollama powered by the ollama4j client. + * + * <p>This implementation adapts the generic Flink Agents embedding model interface to Ollama's + * embedding API. It supports various embedding models available in Ollama such as: + * + * <ul> + * <li>nomic-embed-text + * <li>mxbai-embed-large + * <li>all-minilm + * <li>And other embedding models supported by Ollama + * </ul> + * + * <p>See also {@link BaseEmbeddingModelConnection} for the common resource abstractions and + * lifecycle. + * + * <p>Example usage: + * + * <pre>{@code + * public class MyAgent extends Agent { + * // Register the embedding model connection via @EmbeddingModelConnection metadata. + * @EmbeddingModelConnection + * public static ResourceDescriptor ollama() { + * return ResourceDescriptor.Builder.newBuilder(OllamaEmbeddingModelConnection.class.getName()) + * .addInitialArgument("host", "http://localhost:11434") // the Ollama server URL + * .addInitialArgument("timeout", 60) // optional timeout in seconds + * .addInitialArgument("model", "nomic-embed-text") // the embedding model name + * .build(); + * } + * } + * }</pre> + */ +public class OllamaEmbeddingModelConnection extends BaseEmbeddingModelConnection { + + private final OllamaAPI ollamaAPI; + private final String host; + private final String defaultModel; + private Integer cachedDimension; + + public OllamaEmbeddingModelConnection( + ResourceDescriptor descriptor, BiFunction<String, ResourceType, Resource> getResource) { + super(descriptor, getResource); + this.host = + descriptor.getArgument("host") != null + ? descriptor.getArgument("host") + : "http://localhost:11434"; + long timeout = + descriptor.getArgument("timeout") != null + ? Long.parseLong(descriptor.getArgument("timeout").toString()) + : 60L; + this.defaultModel = + descriptor.getArgument("model") != null + ? descriptor.getArgument("model") + : "nomic-embed-text"; + + this.ollamaAPI = new OllamaAPI(host); + // Note: OllamaAPI timeout configuration may vary by version + // For ollama4j 1.1.0, timeout is typically configured at request level + } + + @Override + public float[] embed(String text) { Review Comment: Maybe we could add an additional parameter `Map<String, Object> parameters` here, like `chat` method of `BaseChatModelConnection`. ``` /** * Generate embeddings for a single text input. * * @param text The input text to generate embeddings for * @param parameters The additional arguments to configure the embed request. * @return An array of floating-point values representing the text embeddings */ public abstract float[] embed(String text, Map<String, Object> parameters); ``` Then we can remove the `embed(String text, String model)` and get `model` name from `parameters`. I recommend this implementation because, in our design, different instances of `EmbeddingModelSetup` can be initialized with different configuration parameters, yet they can share the same `EmbeddingModelConnection`. When an `EmbeddingModelSetup` instance calls the embed method of `EmbeddingModelConnection`, it can pass its own unique configuration parameters, including `model_name`, `encoding_format`, and others. You can take a look at python implementation as a reference. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
