xintongsong commented on code in PR #250:
URL: https://github.com/apache/flink-agents/pull/250#discussion_r2446653123


##########
api/src/main/java/org/apache/flink/agents/api/embedding/model/BaseEmbeddingModelConnection.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.api.embedding.model;
+
+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.util.List;
+import java.util.Map;
+import java.util.function.BiFunction;
+
+/**
+ * Abstraction of embedding model connection.
+ *
+ * <p>Responsible for managing embedding model service connection 
configurations, such as Service
+ * address, API key, Connection timeout, Model name, Authentication 
information, etc.
+ *
+ * <p>This class follows the parameter pattern where additional configuration 
options can be passed
+ * through a Map&lt;String, Object&gt; parameters argument. Common parameters 
include:
+ *
+ * <ul>
+ *   <li>model - The model name to use for embeddings
+ *   <li>encoding_format - The format for encoding (e.g., "float", "base64")
+ *   <li>timeout - Request timeout in milliseconds
+ *   <li>batch_size - Maximum number of texts to process in a single request
+ * </ul>
+ */
+public abstract class BaseEmbeddingModelConnection extends Resource {
+
+    public BaseEmbeddingModelConnection(
+            ResourceDescriptor descriptor, BiFunction<String, ResourceType, 
Resource> getResource) {
+        super(descriptor, getResource);
+    }
+
+    @Override
+    public ResourceType getResourceType() {
+        return ResourceType.EMBEDDING_MODEL_CONNECTION;
+    }
+
+    /**
+     * Generate embeddings for a single text input.
+     *
+     * @param text The input text to generate embeddings for
+     * @param parameters Additional parameters to configure the embedding 
request
+     * @return An array of floating-point values representing the text 
embeddings
+     */
+    public abstract float[] embed(String text, Map<String, Object> parameters);
+
+    /**
+     * Generate embeddings for multiple text inputs.
+     *
+     * @param texts The list of input texts to generate embeddings for
+     * @param parameters Additional parameters to configure the embedding 
request
+     * @return A list of arrays, each containing floating-point values 
representing the text
+     *     embeddings
+     */
+    public abstract List<float[]> embed(List<String> texts, Map<String, 
Object> parameters);
+
+    /**
+     * Get the dimension of the embeddings produced by this model.
+     *
+     * @return The embedding dimension
+     */
+    public abstract int getEmbeddingDimension();

Review Comment:
   Why do we need this? I think the dimension should be a parameter that you 
can set differently for each embedding request.



##########
integrations/embedding-models/ollama/src/main/java/org/apache/flink/agents/integrations/embeddingmodels/ollama/OllamaEmbeddingModelConnection.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * 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 to 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.HashMap;
+import java.util.List;
+import java.util.Map;
+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("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";;
+        this.defaultModel =
+                descriptor.getArgument("model") != null
+                        ? descriptor.getArgument("model")
+                        : "nomic-embed-text";
+
+        this.ollamaAPI = new OllamaAPI(host);
+    }
+
+    @Override
+    public float[] embed(String text, Map<String, Object> parameters) {
+        String model = (String) parameters.getOrDefault("model", defaultModel);
+        return embedSingle(text, model);
+    }
+
+    @Override
+    public List<float[]> embed(List<String> texts, Map<String, Object> 
parameters) {
+        String model = (String) parameters.getOrDefault("model", defaultModel);
+        return embedBatch(texts, model);
+    }
+
+    private float[] embedSingle(String text, String model) {
+        if (text == null || text.trim().isEmpty()) {
+            throw new IllegalArgumentException("Text cannot be null or empty");
+        }
+
+        try {
+            List<Double> embeddings = ollamaAPI.generateEmbeddings(model, 
text);
+
+            if (embeddings == null || embeddings.isEmpty()) {
+                throw new RuntimeException(
+                        "Received empty embeddings from Ollama for model: " + 
model);
+            }
+
+            float[] result = new float[embeddings.size()];
+            for (int i = 0; i < embeddings.size(); i++) {
+                result[i] = embeddings.get(i).floatValue();
+            }
+
+            if (cachedDimension == null) {
+                cachedDimension = result.length;
+            }
+
+            return result;
+        } catch (OllamaBaseException e) {
+            throw new RuntimeException(
+                    "Ollama API error while generating embeddings for text 
with model '"
+                            + model
+                            + "': "
+                            + e.getMessage(),
+                    e);
+        } catch (IOException | InterruptedException e) {
+            throw new RuntimeException(
+                    "Communication error with Ollama server at "
+                            + host
+                            + " while generating embeddings: "
+                            + e.getMessage(),
+                    e);
+        } catch (Exception e) {
+            throw new RuntimeException(
+                    "Unexpected error while generating embeddings for text 
with model '"
+                            + model
+                            + "': "
+                            + e.getMessage(),
+                    e);
+        }
+    }
+
+    private List<float[]> embedBatch(List<String> texts, String model) {
+        if (texts == null || texts.isEmpty()) {
+            throw new IllegalArgumentException("Texts list cannot be null or 
empty");
+        }
+
+        List<float[]> results = new ArrayList<>();
+        for (String text : texts) {
+            if (text != null && !text.trim().isEmpty()) {
+                results.add(embedSingle(text, model));
+            } else {
+                throw new IllegalArgumentException("Text in list cannot be 
null or empty");
+            }
+        }
+        return results;
+    }
+
+    @Override
+    public int getEmbeddingDimension() {
+        if (cachedDimension != null) {
+            return cachedDimension;
+        }
+
+        try {
+            Map<String, Object> testParams = new HashMap<>();
+            testParams.put("model", defaultModel);
+            float[] testEmbedding = embed("test", testParams);
+            cachedDimension = testEmbedding.length;
+            return cachedDimension;
+        } catch (Exception e) {
+            switch (defaultModel.toLowerCase()) {
+                case "nomic-embed-text":
+                    return 768;
+                case "mxbai-embed-large":
+                    return 1024;
+                case "all-minilm":
+                    return 384;
+                default:
+                    throw new RuntimeException(
+                            "Could not determine embedding dimension for 
model: "
+                                    + defaultModel
+                                    + ". Cause: "
+                                    + e.getMessage(),
+                            e);
+            }
+        }
+    }
+
+    /** Check if the specified model is available on the Ollama server. */
+    public boolean isModelAvailable(String model) {
+        try {
+            return ollamaAPI.listModels().stream()
+                    .anyMatch(modelInfo -> modelInfo.getName().equals(model));
+        } catch (Exception e) {
+            try {
+                Map<String, Object> testParams = new HashMap<>();
+                testParams.put("model", model);
+                embed("test", testParams);
+                return true;
+            } catch (Exception testException) {
+                return false;
+            }
+        }
+    }
+
+    /** Get the default embedding model name. */
+    public String getDefaultModel() {

Review Comment:
   Same here. Never used.



##########
examples/src/main/java/org/apache/flink/agents/examples/agents/EmbeddingsAgent.java:
##########
@@ -0,0 +1,378 @@
+/*
+ * 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 to 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.examples.agents;
+
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.flink.agents.api.Agent;
+import org.apache.flink.agents.api.InputEvent;
+import org.apache.flink.agents.api.OutputEvent;
+import org.apache.flink.agents.api.annotation.Action;
+import org.apache.flink.agents.api.annotation.EmbeddingModelConnection;
+import org.apache.flink.agents.api.annotation.EmbeddingModelSetup;
+import org.apache.flink.agents.api.annotation.Tool;
+import org.apache.flink.agents.api.annotation.ToolParam;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import 
org.apache.flink.agents.integrations.embeddingmodels.ollama.OllamaEmbeddingModelConnection;
+import 
org.apache.flink.agents.integrations.embeddingmodels.ollama.OllamaEmbeddingModelSetup;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * An agent that generates embeddings for each row of data using Ollama 
embedding models.
+ *
+ * <p>This agent receives text data, processes it to generate high-dimensional 
vector embeddings,
+ * and outputs the results with metadata. It demonstrates how to integrate 
embedding models into
+ * Flink Agents workflows for vector-based processing and similarity search 
applications.
+ *
+ * <p>The agent supports various embedding models available in Ollama such as: 
- nomic-embed-text
+ * (768 dimensions) - mxbai-embed-large (1024 dimensions) - all-minilm (384 
dimensions)
+ */
+public class EmbeddingsAgent extends Agent {

Review Comment:
   I think `EmbeddingAgent` and `WorkflowEmbeddingsAgentExampleJob` are more 
like e2e test for verification purposes, rather than user-facing examples. We 
probably don't want to recommend users to directly call the embedding models in 
their actions. Therefore, I'd suggest to move these to the `e2e-test` module.



##########
integrations/embedding-models/ollama/src/main/java/org/apache/flink/agents/integrations/embeddingmodels/ollama/OllamaEmbeddingModelConnection.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * 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 to 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.HashMap;
+import java.util.List;
+import java.util.Map;
+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("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";;
+        this.defaultModel =
+                descriptor.getArgument("model") != null
+                        ? descriptor.getArgument("model")
+                        : "nomic-embed-text";
+
+        this.ollamaAPI = new OllamaAPI(host);
+    }
+
+    @Override
+    public float[] embed(String text, Map<String, Object> parameters) {
+        String model = (String) parameters.getOrDefault("model", defaultModel);
+        return embedSingle(text, model);
+    }
+
+    @Override
+    public List<float[]> embed(List<String> texts, Map<String, Object> 
parameters) {
+        String model = (String) parameters.getOrDefault("model", defaultModel);
+        return embedBatch(texts, model);
+    }
+
+    private float[] embedSingle(String text, String model) {
+        if (text == null || text.trim().isEmpty()) {
+            throw new IllegalArgumentException("Text cannot be null or empty");
+        }
+
+        try {
+            List<Double> embeddings = ollamaAPI.generateEmbeddings(model, 
text);
+
+            if (embeddings == null || embeddings.isEmpty()) {
+                throw new RuntimeException(
+                        "Received empty embeddings from Ollama for model: " + 
model);
+            }
+
+            float[] result = new float[embeddings.size()];
+            for (int i = 0; i < embeddings.size(); i++) {
+                result[i] = embeddings.get(i).floatValue();
+            }
+
+            if (cachedDimension == null) {
+                cachedDimension = result.length;
+            }
+
+            return result;
+        } catch (OllamaBaseException e) {
+            throw new RuntimeException(
+                    "Ollama API error while generating embeddings for text 
with model '"
+                            + model
+                            + "': "
+                            + e.getMessage(),
+                    e);
+        } catch (IOException | InterruptedException e) {
+            throw new RuntimeException(
+                    "Communication error with Ollama server at "
+                            + host
+                            + " while generating embeddings: "
+                            + e.getMessage(),
+                    e);
+        } catch (Exception e) {
+            throw new RuntimeException(
+                    "Unexpected error while generating embeddings for text 
with model '"
+                            + model
+                            + "': "
+                            + e.getMessage(),
+                    e);
+        }
+    }
+
+    private List<float[]> embedBatch(List<String> texts, String model) {
+        if (texts == null || texts.isEmpty()) {
+            throw new IllegalArgumentException("Texts list cannot be null or 
empty");
+        }
+
+        List<float[]> results = new ArrayList<>();
+        for (String text : texts) {
+            if (text != null && !text.trim().isEmpty()) {
+                results.add(embedSingle(text, model));
+            } else {
+                throw new IllegalArgumentException("Text in list cannot be 
null or empty");
+            }
+        }
+        return results;
+    }
+
+    @Override
+    public int getEmbeddingDimension() {
+        if (cachedDimension != null) {
+            return cachedDimension;
+        }
+
+        try {
+            Map<String, Object> testParams = new HashMap<>();
+            testParams.put("model", defaultModel);
+            float[] testEmbedding = embed("test", testParams);
+            cachedDimension = testEmbedding.length;
+            return cachedDimension;
+        } catch (Exception e) {
+            switch (defaultModel.toLowerCase()) {
+                case "nomic-embed-text":
+                    return 768;
+                case "mxbai-embed-large":
+                    return 1024;
+                case "all-minilm":
+                    return 384;
+                default:
+                    throw new RuntimeException(
+                            "Could not determine embedding dimension for 
model: "
+                                    + defaultModel
+                                    + ". Cause: "
+                                    + e.getMessage(),
+                            e);
+            }
+        }
+    }
+
+    /** Check if the specified model is available on the Ollama server. */
+    public boolean isModelAvailable(String model) {

Review Comment:
   This method is never used. I think we should avoid introducing public 
interfaces unless we see a concrete demand for it.



-- 
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]

Reply via email to