The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.
Python agentic-memoryagentic-memory-systemagentsaiai-agentschatgptgenaillmlong-term-memorymemorymemory-managementpythonragstate-management
Download Ollama model if not present (#1397)
Dev Khant73e53aaff192
diff --git a/Makefile b/Makefileindex 571d4eb8b..8ddb58e7b 100644--- a/Makefile+++ b/Makefile@@ -11,7 +11,7 @@ install:install_all:poetry install --all-extras- poetry run pip install pinecone-text pinecone-client langchain-anthropic "unstructured[local-inference, all-docs]"+ poetry run pip install pinecone-text pinecone-client langchain-anthropic "unstructured[local-inference, all-docs]" ollamainstall_es:poetry install --extras elasticsearchdiff --git a/embedchain/llm/ollama.py b/embedchain/llm/ollama.pyindex 221753d3a..7de675cd6 100644--- a/embedchain/llm/ollama.py+++ b/embedchain/llm/ollama.py@@ -1,3 +1,4 @@+import loggingfrom collections.abc import Iterablefrom typing import Optional, Union@@ -5,11 +6,14 @@ from langchain.callbacks.manager import CallbackManagerfrom langchain.callbacks.stdout import StdOutCallbackHandlerfrom langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandlerfrom langchain_community.llms.ollama import Ollama+from ollama import Clientfrom embedchain.config import BaseLlmConfigfrom embedchain.helpers.json_serializable import register_deserializablefrom embedchain.llm.base import BaseLlm+logger = logging.getLogger(__name__)+@register_deserializableclass OllamaLlm(BaseLlm):@@ -18,6 +22,12 @@ class OllamaLlm(BaseLlm):if self.config.model is None:self.config.model = "llama2"+ client = Client(host=config.base_url)+ local_models = client.list()["models"]+ if not any(model.get("name") == self.config.model for model in local_models):+ logger.info(f"Pulling {self.config.model} from Ollama!")+ client.pull(self.config.model)+def get_llm_model_answer(self, prompt):return self._get_answer(prompt=prompt, config=self.config)diff --git a/tests/llm/test_ollama.py b/tests/llm/test_ollama.pyindex 62252783c..ff16d1263 100644--- a/tests/llm/test_ollama.py+++ b/tests/llm/test_ollama.py@@ -11,6 +11,7 @@ def ollama_llm_config():def test_get_llm_model_answer(ollama_llm_config, mocker):+ mocker.patch("embedchain.llm.ollama.Client.list", return_value={"models": [{"name": "llama2"}]})mocker.patch("embedchain.llm.ollama.OllamaLlm._get_answer", return_value="Test answer")llm = OllamaLlm(ollama_llm_config)@@ -20,6 +21,7 @@ def test_get_llm_model_answer(ollama_llm_config, mocker):def test_get_answer_mocked_ollama(ollama_llm_config, mocker):+ mocker.patch("embedchain.llm.ollama.Client.list", return_value={"models": [{"name": "llama2"}]})mocked_ollama = mocker.patch("embedchain.llm.ollama.Ollama")mock_instance = mocked_ollama.return_valuemock_instance.invoke.return_value = "Mocked answer"