Skip to content

Commit

Permalink
fixes for #45, #46
Browse files Browse the repository at this point in the history
  • Loading branch information
renehonig committed Jan 19, 2024
1 parent 550ffc3 commit f523948
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
13 changes: 10 additions & 3 deletions ai_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain.schema import format_document
from langchain_core.messages import get_buffer_string
from langchain_core.messages.ai import AIMessage
from langchain_core.runnables import RunnableBranch

from operator import itemgetter
Expand Down Expand Up @@ -254,6 +255,12 @@ async def query_chain(question, language, chat_history):
loaded_memory | standalone_question | retrieved_documents_sa | answer,
)

logger.debug(f"final chain {final_chain}\n")
result = await final_chain.ainvoke(question)
return {'answer': result['answer'], 'source_documents': result['docs']}
try:
logger.debug(f"final chain {final_chain}\n")
result = await final_chain.ainvoke(question)
except Exception as e:
logger.error(f"An error occurred while generating a response: {str(e)}")
# Handle the error appropriately here
return {'answer': AIMessage(content='An error occurred while generating a response.'), 'source_documents': []}
else:
return {'answer': result['answer'], 'source_documents': result['docs'] if result['docs'] else []}
10 changes: 5 additions & 5 deletions guidance_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ async def query(user_id, query, language_code):
logger.info(f"\nQuery from user {user_id}: {query}\n")

if user_id not in user_data:
user_data[user_id] = {}
user_data[user_id]['chat_history'] = ConversationBufferWindowMemory(k=3, return_messages=True, output_key="answer", input_key="question")
# user_chain[user_id]=ai_utils.setup_chain()
reset(user_id)
# chat_history=[]

user_data[user_id]['language'] = ai_adapter.get_language_by_code(language_code)

Expand Down Expand Up @@ -116,8 +112,10 @@ async def query(user_id, query, language_code):
return response

def reset(user_id):
if user_id not in user_data:
user_data[user_id] = {}
user_data[user_id]['chat_history'] = ConversationBufferWindowMemory(k=3, return_messages=True, output_key="answer", input_key="question")
user_data[user_id]['chat_history'].clear()

return "Reset function executed"

async def ingest(source_url, website_repo, destination_path, source_path, source_url2, website_repo2, destination_path2, source_path2):
Expand Down Expand Up @@ -182,8 +180,10 @@ async def process_message(message: aio_pika.IncomingMessage):
else:
response = "Query parameter(s) not provided"
elif operation == 'reset':
logger.info(f"reset user id: {user_id}\n\n")
response = reset(user_id)
else:
logger.error(f"unknown function for user: {user_id}\n\n")
response = "Unknown function"

try:
Expand Down
61 changes: 32 additions & 29 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "guidance-engine"
version = "0.6.0"
version = "0.6.1"
description = "Alkemio Generative AI Guidance Engine"
authors = ["Alkemio BV <[email protected]>"]
license = "EUPL-1.2"
Expand Down

0 comments on commit f523948

Please sign in to comment.