Merge pull request #974 from better629/feat_memory

Feat add rag
This commit is contained in:
Alexander Wu 2024-03-17 23:39:12 +08:00 committed by GitHub
commit e783e5b208
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
61 changed files with 2353 additions and 248 deletions

View file

@ -0,0 +1,22 @@
import asyncio
import threading
from typing import Any
def run_coroutine_in_new_loop(coroutine) -> Any:
"""Runs a coroutine in a new, separate event loop on a different thread.
This function is useful when try to execute an async function within a sync function, but encounter the error `RuntimeError: This event loop is already running`.
"""
new_loop = asyncio.new_event_loop()
t = threading.Thread(target=lambda: new_loop.run_forever())
t.start()
future = asyncio.run_coroutine_threadsafe(coroutine, new_loop)
try:
return future.result()
finally:
new_loop.call_soon_threadsafe(new_loop.stop)
t.join()
new_loop.close()

View file

@ -5,12 +5,15 @@
@Author : alexanderwu
@File : embedding.py
"""
from langchain_community.embeddings import OpenAIEmbeddings
from llama_index.embeddings.openai import OpenAIEmbedding
from metagpt.config2 import config
def get_embedding():
def get_embedding() -> OpenAIEmbedding:
llm = config.get_openai_llm()
embedding = OpenAIEmbeddings(openai_api_key=llm.api_key, openai_api_base=llm.base_url)
if llm is None:
raise ValueError("To use OpenAIEmbedding, please ensure that config.llm.api_type is correctly set to 'openai'.")
embedding = OpenAIEmbedding(api_key=llm.api_key, api_base=llm.base_url)
return embedding

View file

@ -0,0 +1,18 @@
"""class tools, including method inspection, class attributes, inheritance relationships, etc."""
def check_methods(C, *methods):
"""Check if the class has methods. borrow from _collections_abc.
Useful when implementing implicit interfaces, such as defining an abstract class, isinstance can be used for determination without inheritance.
"""
mro = C.__mro__
for method in methods:
for B in mro:
if method in B.__dict__:
if B.__dict__[method] is None:
return NotImplemented
break
else:
return NotImplemented
return True