Skip to content

Commit 3a42951

Browse files
authored
Merge pull request #77 from alkem-io/vceg-61/ingest-user-guide-and-update-chroma
Cleanup a comment and add `create_context`
2 parents 7ba90a7 + 0fd24a3 commit 3a42951

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

ai_adapter.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ async def query_chain(input: Input) -> Response:
6767

6868
messages = [
6969
SystemMessage(content=bok_system_prompt.format(knowledge=context)),
70-
SystemMessage(
71-
content=response_system_prompt.format(
72-
context=context # , language=get_language_by_code(input.language)
73-
)
74-
),
70+
SystemMessage(content=response_system_prompt.format(context=context)),
7571
UserMessage(content=message),
7672
]
7773

create_context.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from chromadb.api.types import IncludeEnum
2+
from models import embed_func
3+
from logger import setup_logger
4+
5+
from alkemio_virtual_contributor_engine.chromadb_client import chromadb_client
6+
7+
8+
logger = setup_logger(__name__)
9+
10+
11+
def combine_documents(docs, document_separator="\n\n"):
12+
chunks_array = []
13+
for index, document in enumerate(docs["documents"][0]):
14+
chunks_array.append(f"[source:{index}] {document}")
15+
16+
return document_separator.join(chunks_array)
17+
18+
19+
def get_documents(message: str):
20+
21+
collections = [
22+
"alkem.io-knowledge",
23+
"welcome.alkem.io-knowledge",
24+
"www.alkemio.org-knowledge",
25+
]
26+
result = {"documents": [[]], "metadatas": [[]], "distances": [[]]}
27+
28+
for collection in collections:
29+
collection = chromadb_client.get_collection(
30+
collection, embedding_function=embed_func
31+
)
32+
tmp_result = collection.query(
33+
query_texts=[message],
34+
include=[
35+
IncludeEnum.documents,
36+
IncludeEnum.metadatas,
37+
IncludeEnum.distances,
38+
],
39+
n_results=3,
40+
)
41+
if (
42+
tmp_result
43+
and tmp_result["documents"]
44+
and tmp_result["distances"]
45+
and tmp_result["metadatas"]
46+
):
47+
result["distances"][0] += tmp_result["distances"][0]
48+
result["documents"][0] += tmp_result["documents"][0]
49+
result["metadatas"][0] += tmp_result["metadatas"][0]
50+
return result
51+
52+
53+
def create_context(message):
54+
documents = get_documents(message)
55+
logger.info("Context retrieved.")
56+
logger.debug(f"Context is {documents}")
57+
return documents, combine_documents(documents)

0 commit comments

Comments
 (0)