repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import subprocess |
| #2 | import sys |
| #3 | from typing import Literal, Optional |
| #4 | |
| #5 | from mem0.configs.embeddings.base import BaseEmbedderConfig |
| #6 | from mem0.embeddings.base import EmbeddingBase |
| #7 | |
| #8 | try: |
| #9 | from ollama import Client |
| #10 | except ImportError: |
| #11 | user_input = input("The 'ollama' library is required. Install it now? [y/N]: ") |
| #12 | if user_input.lower() == "y": |
| #13 | try: |
| #14 | subprocess.check_call([sys.executable, "-m", "pip", "install", "ollama"]) |
| #15 | from ollama import Client |
| #16 | except subprocess.CalledProcessError: |
| #17 | print("Failed to install 'ollama'. Please install it manually using 'pip install ollama'.") |
| #18 | sys.exit(1) |
| #19 | else: |
| #20 | print("The required 'ollama' library is not installed.") |
| #21 | sys.exit(1) |
| #22 | |
| #23 | |
| #24 | class OllamaEmbedding(EmbeddingBase): |
| #25 | def __init__(self, config: Optional[BaseEmbedderConfig] = None): |
| #26 | super().__init__(config) |
| #27 | |
| #28 | self.config.model = self.config.model or "nomic-embed-text" |
| #29 | self.config.embedding_dims = self.config.embedding_dims or 512 |
| #30 | |
| #31 | self.client = Client(host=self.config.ollama_base_url) |
| #32 | self._ensure_model_exists() |
| #33 | |
| #34 | def _ensure_model_exists(self): |
| #35 | """ |
| #36 | Ensure the specified model exists locally. If not, pull it from Ollama. |
| #37 | """ |
| #38 | local_models = self.client.list()["models"] |
| #39 | if not any(model.get("name") == self.config.model or model.get("model") == self.config.model for model in local_models): |
| #40 | self.client.pull(self.config.model) |
| #41 | |
| #42 | def embed(self, text, memory_action: Optional[Literal["add", "search", "update"]] = None): |
| #43 | """ |
| #44 | Get the embedding for the given text using Ollama. |
| #45 | |
| #46 | Args: |
| #47 | text (str): The text to embed. |
| #48 | memory_action (optional): The type of embedding to use. Must be one of "add", "search", or "update". Defaults to None. |
| #49 | Returns: |
| #50 | list: The embedding vector. |
| #51 | """ |
| #52 | response = self.client.embeddings(model=self.config.model, prompt=text) |
| #53 | return response["embedding"] |
| #54 |