implementing chroma

memory
Sashin 1 year ago
parent 1911d8db94
commit e09531131d

@ -18,13 +18,13 @@ class VectorDatabase(ABC):
@abstractmethod
def query(
self, vector: Dict[str, Any], num_results: int
self, text: str, num_results: int
) -> Dict[str, Any]:
"""
Query the database for vectors similar to the given vector.
Args:
vector (Dict[str, Any]): The vector to compare against.
text (Dict[str, Any]): The vector to compare against.
num_results (int): The number of similar vectors to return.
Returns:

@ -1,10 +1,13 @@
from typing import List
from abc import ABC, abstractmethod
from typing import Any, Dict, List
from chromadb.utils import embedding_functions
from httpx import RequestError
import chromadb
from swarms.memory.base_vector_db import VectorDatabase
class ChromaClient:
class ChromaClient(VectorDatabase):
def __init__(
self,
collection_name: str = "chromadb-collection",
@ -90,23 +93,14 @@ class ChromaClient:
print(f"Error searching vectors: {e}")
return None
def search_vectors_formatted(self, query: str, limit: int = 2):
"""
Searches the collection for vectors similar to the query vector.
def add(self, vector: Dict[str, Any], metadata: Dict[str, Any]) -> None:
pass
Args:
query (str): The query string to be converted into a vector and used for searching.
limit (int): The number of search results to return. Defaults to 3.
def query(self, vector: Dict[str, Any], num_results: int) -> Dict[str, Any]:
pass
Returns:
SearchResult or None: Returns the search results if successful, otherwise None.
"""
try:
search_result = self.collection.query(
query_texts=query,
n_results=limit,
)
return search_result
except Exception as e:
print(f"Error searching vectors: {e}")
return None
def delete(self, vector_id: str) -> None:
pass
def update(self, vector_id: str, vector: Dict[str, Any], metadata: Dict[str, Any]) -> None:
pass

@ -635,6 +635,10 @@ class Agent:
AGENT_SYSTEM_PROMPT_3, response
)
# Retreiving long term memory
if self.memory:
task = self.agent_memory_prompt(response, task)
attempt = 0
while attempt < self.retry_attempts:
try:
@ -755,6 +759,33 @@ class Agent:
"""
return agent_history_prompt
def agent_memory_prompt(
self,
query,
prompt
):
"""
Generate the agent long term memory prompt
Args:
system_prompt (str): The system prompt
history (List[str]): The history of the conversation
Returns:
str: The agent history prompt
"""
context_injected_prompt = prompt
if self.memory:
ltr = self.memory.query(query)
context_injected_prompt = f"""{prompt}
################ CONTEXT ####################
{ltr}
"""
return context_injected_prompt
async def run_concurrent(self, tasks: List[str], **kwargs):
"""
Run a batch of tasks concurrently and handle an infinite level of task inputs.

Loading…
Cancel
Save