Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor fixes #47

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 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 @@ -82,9 +83,10 @@ def get_language_by_code(language_code):
1 - If the question is in a different language than English, translate the question to English before answering.
2 - The text provided in the context delimited by triple pluses is retrieved from the Alkemio website is not part of the conversation with the user.
3 - Provide an answer of 250 words or less that is professional, engaging, accurate and exthausive, based on the context delimited by triple pluses. \
If the answer cannot be found within the context, write 'Hmm, I am not sure'.
4 - Only return the answer from step 3, do not show any code or additional information.
5 - Answer the question in the {language} language.
If the answer cannot be found within the context, write 'Hmm, I am not sure'.
4 - If the question is not specifically about Alkemio or if the question is not professional write 'Unfortunately, I cannot answer that question'.
5 - Only return the answer from step 3, do not show any code or additional information.
6 - Answer the question in the {language} language.
+++
context:
{context}
Expand All @@ -99,7 +101,8 @@ def get_language_by_code(language_code):
simply repeat it.
2. Otherwise, combine the chat history delimited by triple pluses and human input into a single standalone query that does \
justice to the human input.
3. Do only return the standalone query, do not return any other information. Never return the chat history delimited by triple pluses.
3. Do only return the standalone query, do not try to respond to the user query and do not return any other information. \
Never return the chat history delimited by triple pluses.

+++
chat history:
Expand Down Expand Up @@ -254,6 +257,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
Loading