xintongsong commented on code in PR #90: URL: https://github.com/apache/flink-agents/pull/90#discussion_r2254193503
########## python/flink_agents/integrations/chat_models/ollama_chat_model.py: ########## @@ -0,0 +1,182 @@ +################################################################################ +# 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. +################################################################################# +import uuid +from typing import Any, Dict, List, Mapping, Optional, Sequence, Union + +from ollama import Client, Message +from pydantic import Field + +from flink_agents.api.chat_message import ChatMessage +from flink_agents.api.chat_models.chat_model import BaseChatModel +from flink_agents.api.resource import ResourceType + +DEFAULT_CONTEXT_WINDOW = 2048 +DEFAULT_REQUEST_TIMEOUT = 30.0 + + +class OllamaChatModel(BaseChatModel): + """Ollama ChatModel. + + Visit https://ollama.com/ to download and install Ollama. + + Run `ollama serve` to start a server. + + Run `ollama pull <name>` to download a model to run. + """ + + base_url: str = Field( + default="http://localhost:11434", + description="Base url the model is hosted under.", + ) + model: str = Field(description="Model name to use.") + temperature: float = Field( + default=0.75, + description="The temperature to use for sampling.", + ge=0.0, + le=1.0, + ) + num_ctx: int = Field( + default=DEFAULT_CONTEXT_WINDOW, + description="The maximum number of context tokens for the model.", + gt=0, + ) + request_timeout: float = Field( + default=DEFAULT_REQUEST_TIMEOUT, + description="The timeout for making http request to Ollama API server", + ) + additional_kwargs: Dict[str, Any] = Field( + default_factory=dict, + description="Additional model parameters for the Ollama API.", + ) + keep_alive: Optional[Union[float, str]] = Field( + default="5m", + description="controls how long the model will stay loaded into memory following the request(default: 5m)", + ) + + __client: Client + __tools: Sequence[Mapping[str, Any]] = [] + + def __init__( + self, + model: str, + base_url: str = "http://localhost:11434", + temperature: float = 0.75, + num_ctx: int = DEFAULT_CONTEXT_WINDOW, + request_timeout: Optional[float] = DEFAULT_REQUEST_TIMEOUT, + additional_kwargs: Optional[Dict[str, Any]] = None, + keep_alive: Optional[Union[float, str]] = None, + **kwargs: Any, + ) -> None: + """Init method.""" + if additional_kwargs is None: + additional_kwargs = {} + super().__init__( + model=model, + base_url=base_url, + temperature=temperature, + num_ctx=num_ctx, + request_timeout=request_timeout, + additional_kwargs=additional_kwargs, + keep_alive=keep_alive, + **kwargs, + ) + self.__client = Client(host=self.base_url, timeout=self.request_timeout) + # bind tools + if self.tools is not None: + tools = [ + self.get_resource(tool_name, ResourceType.TOOL) + for tool_name in self.tools + ] + self.__tools = [tool.metadata.to_openai_tool() for tool in tools] + # bind prompt + if self.prompt is not None and isinstance(self.prompt, str): + self.prompt = self.get_resource(self.prompt, ResourceType.PROMPT) + + @property + def client(self) -> Client: + """Return ollama client.""" + if self.__client is None: + self.__client = Client(host=self.base_url, timeout=self.request_timeout) Review Comment: Duplicated with line 98 ########## python/flink_agents/integrations/chat_models/tests/test_ollama_chat_model.py: ########## @@ -0,0 +1,94 @@ +################################################################################ +# 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. +################################################################################# +import os + +import pytest +from ollama import Client + +from flink_agents.api.chat_message import ChatMessage, MessageRole +from flink_agents.api.resource import ResourceType +from flink_agents.integrations.chat_models.ollama_chat_model import OllamaChatModel +from flink_agents.plan.tools.function_tool import FunctionTool, from_callable + +test_model = os.environ.get("OLLAMA_CHAT_MODEL", "qwen3:8b") +try: + client = Client() + models = client.list() + + model_found = False + for model in models["models"]: + if model.model == test_model: + model_found = True + break + + if not model_found: + client = None # type: ignore +except Exception: + client = None # type: ignore Review Comment: Does it require starting ollama server before running the test? -- 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]
