update rag example

This commit is contained in:
seehi 2024-03-08 11:18:53 +08:00
parent 5cbb3f5170
commit 63a3a760e9
4 changed files with 19 additions and 20 deletions

View file

@ -92,7 +92,7 @@ class RAGExample:
self.engine.add_docs([travel_filepath])
await self.rag_pipeline(question=travel_question, print_title=False)
async def rag_add_objs(self):
async def rag_add_objs(self, print_title=True):
"""This example show how to add objs, before add docs engine retrieve nothing, after add objs engine give the correct answer, will print something like:
[Before add objs]
Retrieve Result:
@ -104,8 +104,8 @@ class RAGExample:
[Object Detail]
{'name': 'Mike', 'goal': 'Win The 100-meter Sprint', 'tool': 'Red Bull Energy Drink'}
"""
self._print_title("RAG Add Objs")
if print_title:
self._print_title("RAG Add Objs")
player = Player(name="Mike")
question = f"{player.rag_key()}"
@ -118,17 +118,22 @@ class RAGExample:
nodes = await self._retrieve_and_print(question)
print("[Object Detail]")
player: Player = nodes[0].metadata["obj"]
print(player.name)
try:
player: Player = nodes[0].metadata["obj"]
print(player.name)
except Exception as e:
print(f"ERROR: nodes is empty, llm don't answer correctly, exception: {e}")
async def rag_ini_objs(self):
"""This example show how to from objs, will print something like:
Same as rag_add_objs
"""
self._print_title("RAG Ini Objs")
pre_engine = self.engine
self.engine = SimpleEngine.from_objs(retriever_configs=[FAISSRetrieverConfig()])
await self.rag_add_objs()
await self.rag_add_objs(print_title=False)
self.engine = pre_engine
async def rag_chromadb(self):

View file

@ -170,6 +170,7 @@ class SimpleEngine(RetrieverQueryEngine):
documents = SimpleDirectoryReader(input_files=input_files).load_data()
self._fix_document_metadata(documents)
nodes = run_transformations(documents, transformations=self.index._transformations)
self._save_nodes(nodes)
@ -220,11 +221,6 @@ class SimpleEngine(RetrieverQueryEngine):
@staticmethod
def _fix_document_metadata(documents: list[Document]):
"""LlamaIndex bug, maybe deleted in the near future.
Metadata in doc has `file_path`, but excluded_embed_metadata_keys is missing.
"""
"""LlamaIndex keep metadata['file_path'], which is unnecessary, maybe deleted in the near future."""
for doc in documents:
keys_set = set(doc.excluded_embed_metadata_keys)
keys_set.add("file_path")
doc.excluded_embed_metadata_keys = list(keys_set)
doc.excluded_embed_metadata_keys.append("file_path")

View file

@ -36,8 +36,8 @@ class RAGIndexFactory(ConfigFactory):
def _create_chroma(self, config: ChromaIndexConfig, **kwargs) -> VectorStoreIndex:
embed_model = self.extract_embed_model(config, **kwargs)
db2 = chromadb.PersistentClient(str(config.persist_path))
chroma_collection = db2.get_or_create_collection(config.collection_name)
db = chromadb.PersistentClient(str(config.persist_path))
chroma_collection = db.get_or_create_collection(config.collection_name)
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
index = VectorStoreIndex.from_vector_store(
vector_store,

View file

@ -1,11 +1,9 @@
"""
class tools, including method inspection, class attributes, inheritance relationships, etc.
"""
"""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.
"""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__