Skip to content

Commit aa45af9

Browse files
authored
Merge pull request #78 from alkem-io/develop
Release v0.9.0
2 parents 8dfbfaa + 3a42951 commit aa45af9

File tree

10 files changed

+621
-232
lines changed

10 files changed

+621
-232
lines changed

ai_adapter.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
import json
12
from azure.ai.inference.models import SystemMessage, UserMessage
23
from alkemio_virtual_contributor_engine.events.response import Response
34
from alkemio_virtual_contributor_engine.events.input import Input
5+
from create_context import create_context
46
from logger import setup_logger
5-
from logger import setup_logger
6-
from models import get_retriever, invoke_model
7+
from models import invoke_model
78
from alkemio_virtual_contributor_engine.utils import (
8-
combine_documents,
9-
get_language_by_code,
109
history_as_text,
1110
)
12-
from prompts import chat_system_prompt, condense_prompt
11+
from prompts import (
12+
condense_prompt,
13+
bok_system_prompt,
14+
response_system_prompt,
15+
)
1316
from config import env
1417

1518
logger = setup_logger(__name__)
@@ -60,32 +63,36 @@ async def query_chain(input: Input) -> Response:
6063
else:
6164
logger.info("No history to handle, initial interaction")
6265

63-
documents = get_retriever().invoke(message)
64-
logger.info("Context retrieved.")
65-
logger.debug(f"Context is {documents}")
66-
context = combine_documents(documents)
66+
documents, context = create_context(message)
6767

6868
messages = [
69-
SystemMessage(
70-
content=chat_system_prompt.format(
71-
context=context, language=get_language_by_code(input.language)
72-
)
73-
),
69+
SystemMessage(content=bok_system_prompt.format(knowledge=context)),
70+
SystemMessage(content=response_system_prompt.format(context=context)),
7471
UserMessage(content=message),
7572
]
7673

7774
logger.info("Invoking LLM.")
78-
result = invoke_model(messages)
75+
response = json.loads(invoke_model(messages))
7976
logger.info("LLM invocation completed.")
80-
logger.info(f"LLM message is: {result}")
77+
logger.info(f"LLM message is: {response}")
78+
79+
sources = []
80+
for index, metadata in enumerate(documents["metadatas"][0]):
81+
index = str(index)
82+
if (
83+
{"uri": metadata["source"]} not in sources
84+
and index in response["source_scores"]
85+
and response["source_scores"][index] > 0
86+
):
87+
sources.append({"uri": metadata["source"]})
8188

8289
return Response(
8390
{
84-
"result": result,
85-
"original_result": result,
91+
"result": response["result"],
92+
"original_result": response["result"],
8693
"human_language": input.language,
8794
"result_language": input.language,
8895
"knowledge_language": "en",
89-
"sources": [{"uri": document.metadata["source"]} for document in documents],
96+
"sources": sources,
9097
}
9198
)

config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
class Env:
1212
model_name: str
1313
embeddings_model_name: str
14+
openai_api_key: str
15+
openai_endpoint: str
1416
openai_api_version: str
1517
site_url: str
1618
welcome_site_url: str
@@ -41,6 +43,8 @@ def __init__(self):
4143

4244
self.model_name = os.getenv("LLM_DEPLOYMENT_NAME", "")
4345
self.embeddings_model_name = os.getenv("EMBEDDINGS_DEPLOYMENT_NAME", "")
46+
self.openai_api_key = os.getenv("AZURE_OPENAI_API_KEY", "")
47+
self.openai_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT", "")
4448
self.openai_api_version = os.getenv("OPENAI_API_VERSION", "")
4549

4650
self.site_url = os.getenv("AI_SOURCE_WEBSITE", "")

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)

ingest.py

Lines changed: 0 additions & 177 deletions
This file was deleted.

main.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
1-
import ai_adapter
21
import asyncio
2+
3+
import ai_adapter
34
from alkemio_virtual_contributor_engine.alkemio_vc_engine import (
45
AlkemioVirtualContributorEngine,
56
)
6-
from alkemio_virtual_contributor_engine.events.input import Input, InvocationOperation
7+
from alkemio_virtual_contributor_engine.events import Input
78
from alkemio_virtual_contributor_engine.events.response import Response
89

9-
from ingest import ensure_ingested
1010
from logger import setup_logger
1111

1212
logger = setup_logger(__name__)
1313

14-
# Lock to prevent multiple ingestions from happening at the same time
15-
ingestion_lock = asyncio.Lock()
14+
engine = AlkemioVirtualContributorEngine()
1615

1716

1817
async def query(input: Input) -> Response:
1918
logger.info("Query method invoked.")
20-
if input.operation is InvocationOperation.INGEST:
21-
logger.info("Operation is INGEST.")
22-
async with ingestion_lock:
23-
await ensure_ingested(True)
24-
# return empty response - we can extend this to give valid feedback
25-
return Response()
26-
27-
logger.info("Operation is QUERY.")
2819
result = await ai_adapter.invoke(input)
2920
logger.info("Query method completed.")
3021
return result
3122

3223

33-
engine = AlkemioVirtualContributorEngine()
3424
engine.register_handler(query)
3525
asyncio.run(engine.start())

manifests/25-virtual-contributor-engine-guidance-deployment-dev.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ spec:
4040
secretKeyRef:
4141
name: alkemio-rabbitmq-cluster-default-user
4242
key: password
43+
- name: RABBITMQ_QUEUE
44+
value: virtual-contributor-engine-guidance
4345
envFrom:
4446
- secretRef:
4547
name: alkemio-secrets

0 commit comments

Comments
 (0)