66
77def combine_documents (docs , document_separator = "\n \n " ):
88 chunks_array = []
9- for index , document in enumerate (docs ["documents" ][0 ]):
10- chunks_array .append (f"[source:{ index } ] { document } " )
9+ documents = docs .get ("documents" )
10+ if not documents or not documents [0 ]:
11+ return ""
12+ for index , document in enumerate (documents [0 ]):
13+ if document :
14+ chunks_array .append (f"[source:{ index } ] { document } " )
1115
1216 return document_separator .join (chunks_array )
1317
@@ -21,10 +25,10 @@ def get_documents(message: str):
2125 ]
2226 result = {"documents" : [[]], "metadatas" : [[]], "distances" : [[]]}
2327
24- for collection in collections :
28+ for collection_name in collections :
2529 try :
2630 collection = chromadb_client .get_collection (
27- collection
31+ collection_name
2832 )
2933 embeddings = openai_embeddings .embed_documents ([message ])
3034
@@ -39,15 +43,23 @@ def get_documents(message: str):
3943 )
4044 if (
4145 tmp_result
42- and tmp_result [ "documents" ]
43- and tmp_result [ "distances" ]
44- and tmp_result [ "metadatas" ]
46+ and tmp_result . get ( "documents" )
47+ and tmp_result . get ( "distances" )
48+ and tmp_result . get ( "metadatas" )
4549 ):
46- result ["distances" ][0 ] += tmp_result ["distances" ][0 ]
47- result ["documents" ][0 ] += tmp_result ["documents" ][0 ]
48- result ["metadatas" ][0 ] += tmp_result ["metadatas" ][0 ]
50+ documents = tmp_result ["documents" ]
51+ distances = tmp_result ["distances" ]
52+ metadatas = tmp_result ["metadatas" ]
53+ if documents and documents [0 ]:
54+ result ["documents" ][0 ] += documents [0 ]
55+ if distances and distances [0 ]:
56+ result ["distances" ][0 ] += distances [0 ]
57+ if metadatas and metadatas [0 ]:
58+ result ["metadatas" ][0 ] += metadatas [0 ]
4959 except Exception as e :
50- logger .error (f"Failed to retrieve documents from collection: { collection } " )
60+ logger .error (
61+ f"Failed to retrieve documents from collection: { collection_name } "
62+ )
5163 logger .exception (e )
5264
5365 return result
0 commit comments