Skip to content

Commit 6207599

Browse files
authored
Merge pull request #88 from alkem-io/chore/upgrade-langchain-and-sync-env
migrate to langchain_core, add error handling, and bump version
2 parents 8daf8e4 + 4ad45ec commit 6207599

File tree

4 files changed

+1149
-1088
lines changed

4 files changed

+1149
-1088
lines changed

ai_adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from config import env
3-
from langchain.schema import HumanMessage, SystemMessage
3+
from langchain_core.messages import HumanMessage, SystemMessage
44
from langchain_core.prompts import ChatPromptTemplate
55
from alkemio_virtual_contributor_engine import (
66
Response,

create_context.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
def 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

Comments
 (0)