mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
* server working with agent library * update readme * add messages to events * factor out steps * fix websocket messages * allow user to run arbitrary actions * allow user to run commands before a task is started * fix main.py * check JSON * handle errors in controller better * fix memory issue * better error handling and task cancellation * fix monologue len * fix imports * remove server from lint check * fix lint issues * fix lint errors
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
from . import json
|
|
|
|
import chromadb
|
|
|
|
from llama_index.core import Document
|
|
from llama_index.core.retrievers import VectorIndexRetriever
|
|
from llama_index.core import VectorStoreIndex
|
|
from llama_index.vector_stores.chroma import ChromaVectorStore
|
|
|
|
class LongTermMemory:
|
|
def __init__(self):
|
|
db = chromadb.Client()
|
|
self.collection = db.get_or_create_collection(name="memories")
|
|
vector_store = ChromaVectorStore(chroma_collection=self.collection)
|
|
self.index = VectorStoreIndex.from_vector_store(vector_store)
|
|
self.thought_idx = 0
|
|
|
|
def add_event(self, event):
|
|
doc = Document(
|
|
text=json.dumps(event),
|
|
doc_id=self.thought_idx,
|
|
extra_info={
|
|
"type": event.action,
|
|
"idx": self.thought_idx,
|
|
},
|
|
)
|
|
self.thought_idx += 1
|
|
self.index.insert(doc)
|
|
|
|
def search(self, query, k=10):
|
|
retriever = VectorIndexRetriever(
|
|
index=self.index,
|
|
similarity_top_k=k,
|
|
)
|
|
results = retriever.retrieve(query)
|
|
return [r.get_text() for r in results]
|
|
|
|
|