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 os |
| #2 | import warnings |
| #3 | from typing import Literal, Optional |
| #4 | |
| #5 | from openai import OpenAI |
| #6 | |
| #7 | from mem0.configs.embeddings.base import BaseEmbedderConfig |
| #8 | from mem0.embeddings.base import EmbeddingBase |
| #9 | |
| #10 | |
| #11 | class OpenAIEmbedding(EmbeddingBase): |
| #12 | def __init__(self, config: Optional[BaseEmbedderConfig] = None): |
| #13 | super().__init__(config) |
| #14 | |
| #15 | self.config.model = self.config.model or "text-embedding-3-small" |
| #16 | self.config.embedding_dims = self.config.embedding_dims or 1536 |
| #17 | |
| #18 | api_key = self.config.api_key or os.getenv("OPENAI_API_KEY") |
| #19 | base_url = ( |
| #20 | self.config.openai_base_url |
| #21 | or os.getenv("OPENAI_API_BASE") |
| #22 | or os.getenv("OPENAI_BASE_URL") |
| #23 | or "https://api.openai.com/v1" |
| #24 | ) |
| #25 | if os.environ.get("OPENAI_API_BASE"): |
| #26 | warnings.warn( |
| #27 | "The environment variable 'OPENAI_API_BASE' is deprecated and will be removed in the 0.1.80. " |
| #28 | "Please use 'OPENAI_BASE_URL' instead.", |
| #29 | DeprecationWarning, |
| #30 | ) |
| #31 | |
| #32 | self.client = OpenAI(api_key=api_key, base_url=base_url) |
| #33 | |
| #34 | def embed(self, text, memory_action: Optional[Literal["add", "search", "update"]] = None): |
| #35 | """ |
| #36 | Get the embedding for the given text using OpenAI. |
| #37 | |
| #38 | Args: |
| #39 | text (str): The text to embed. |
| #40 | memory_action (optional): The type of embedding to use. Must be one of "add", "search", or "update". Defaults to None. |
| #41 | Returns: |
| #42 | list: The embedding vector. |
| #43 | """ |
| #44 | text = text.replace("\n", " ") |
| #45 | return ( |
| #46 | self.client.embeddings.create(input=[text], model=self.config.model, dimensions=self.config.embedding_dims) |
| #47 | .data[0] |
| #48 | .embedding |
| #49 | ) |
| #50 |