diff --git a/ai_adapter.py b/ai_adapter.py index f786fc0..8ec0b65 100644 --- a/ai_adapter.py +++ b/ai_adapter.py @@ -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 @@ -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 []} diff --git a/guidance_engine.py b/guidance_engine.py index 13c7597..762a14f 100644 --- a/guidance_engine.py +++ b/guidance_engine.py @@ -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) @@ -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): @@ -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: diff --git a/poetry.lock b/poetry.lock index 88e3a99..94f88c6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2,17 +2,17 @@ [[package]] name = "aio-pika" -version = "9.3.1" +version = "9.4.0" description = "Wrapper around the aiormq for asyncio and humans" optional = false -python-versions = ">=3.7,<4.0" +python-versions = ">=3.8,<4.0" files = [ - {file = "aio_pika-9.3.1-py3-none-any.whl", hash = "sha256:62ea4859572daccfc1d0963785185fa984f41f9fde576f9cc123ec899c257260"}, - {file = "aio_pika-9.3.1.tar.gz", hash = "sha256:f199e5179ff9a362c1494ddd4d26f40834896726fea9b9d91de971b2ad304fc8"}, + {file = "aio_pika-9.4.0-py3-none-any.whl", hash = "sha256:06d3680ea8515aa6c02ac6f94ffe2dde3396f141fde92eef63beb98c7a143cfd"}, + {file = "aio_pika-9.4.0.tar.gz", hash = "sha256:5199be0f50bd0fb1338962390383bb83a3ce8e760bb603aa071e58b56afeeec1"}, ] [package.dependencies] -aiormq = ">=6.7.7,<6.8.0" +aiormq = ">=6.8.0,<6.9.0" yarl = "*" [[package]] @@ -112,17 +112,17 @@ speedups = ["Brotli", "aiodns", "brotlicffi"] [[package]] name = "aiormq" -version = "6.7.7" +version = "6.8.0" description = "Pure python AMQP asynchronous client library" optional = false -python-versions = ">=3.7,<4.0" +python-versions = ">=3.8,<4.0" files = [ - {file = "aiormq-6.7.7-py3-none-any.whl", hash = "sha256:f5efbfcd7d703f3c05c08d4e74cfaa66ca7199840e2969d75ad41b0810026b0a"}, - {file = "aiormq-6.7.7.tar.gz", hash = "sha256:3b93f612f56989b2757a9a7b299dd94dd3227ce28ba43e81d5fbcded6341dfab"}, + {file = "aiormq-6.8.0-py3-none-any.whl", hash = "sha256:9a16174dcae4078c957a773d2f02d3dfd6c2fcf12c909dc244333a458f2aeab0"}, + {file = "aiormq-6.8.0.tar.gz", hash = "sha256:198f9c7430feb7bc491016099a06266dc45880b6b1de3925d410fde6541a66fb"}, ] [package.dependencies] -pamqp = "3.2.1" +pamqp = "3.3.0" yarl = "*" [[package]] @@ -202,19 +202,22 @@ tests-no-zope = ["attrs[tests-mypy]", "cloudpickle", "hypothesis", "pympler", "p [[package]] name = "beautifulsoup4" -version = "4.12.2" +version = "4.12.3" description = "Screen-scraping library" optional = false python-versions = ">=3.6.0" files = [ - {file = "beautifulsoup4-4.12.2-py3-none-any.whl", hash = "sha256:bd2520ca0d9d7d12694a53d44ac482d181b4ec1888909b035a3dbf40d0f57d4a"}, - {file = "beautifulsoup4-4.12.2.tar.gz", hash = "sha256:492bbc69dca35d12daac71c4db1bfff0c876c00ef4a2ffacce226d4638eb72da"}, + {file = "beautifulsoup4-4.12.3-py3-none-any.whl", hash = "sha256:b80878c9f40111313e55da8ba20bdba06d8fa3969fc68304167741bbf9e082ed"}, + {file = "beautifulsoup4-4.12.3.tar.gz", hash = "sha256:74e3d1928edc070d21748185c46e3fb33490f22f52a3addee9aee0f4f7781051"}, ] [package.dependencies] soupsieve = ">1.2" [package.extras] +cchardet = ["cchardet"] +chardet = ["chardet"] +charset-normalizer = ["charset-normalizer"] html5lib = ["html5lib"] lxml = ["lxml"] @@ -705,19 +708,19 @@ text-helpers = ["chardet (>=5.1.0,<6.0.0)"] [[package]] name = "langchain-community" -version = "0.0.11" +version = "0.0.13" description = "Community contributed LangChain integrations." optional = false python-versions = ">=3.8.1,<4.0" files = [ - {file = "langchain_community-0.0.11-py3-none-any.whl", hash = "sha256:30ab1d7dbf35d0ebe684d8a1e8964e8dedd3d31a3703790436b39674cfa06f41"}, - {file = "langchain_community-0.0.11.tar.gz", hash = "sha256:eaeaa8d63427ecf0cb32fe2f1ba4d05ad6d5ef9f7019baf21dc2dde5b1403002"}, + {file = "langchain_community-0.0.13-py3-none-any.whl", hash = "sha256:655196e446e7f37f4882221b6f3f791d6add28ea596d521ccf6f4507386b9a13"}, + {file = "langchain_community-0.0.13.tar.gz", hash = "sha256:cf66c6ff7fcbeb582f5e88ee00decea7fdeca5ccddda725046f28efc697c41a7"}, ] [package.dependencies] aiohttp = ">=3.8.3,<4.0.0" dataclasses-json = ">=0.5.7,<0.7" -langchain-core = ">=0.1.8,<0.2" +langchain-core = ">=0.1.9,<0.2" langsmith = ">=0.0.63,<0.1.0" numpy = ">=1,<2" PyYAML = ">=5.3" @@ -731,13 +734,13 @@ extended-testing = ["aiosqlite (>=0.19.0,<0.20.0)", "aleph-alpha-client (>=2.15. [[package]] name = "langchain-core" -version = "0.1.10" +version = "0.1.12" description = "Building applications with LLMs through composability" optional = false python-versions = ">=3.8.1,<4.0" files = [ - {file = "langchain_core-0.1.10-py3-none-any.whl", hash = "sha256:d89952f6d0766cfc88d9f1e25b84d56f8d7bd63a45ad8ec1a9a038c9b49df16d"}, - {file = "langchain_core-0.1.10.tar.gz", hash = "sha256:3c9e1383264c102fcc6f865700dbb9416c4931a25d0ac2195f6311c6b867aa17"}, + {file = "langchain_core-0.1.12-py3-none-any.whl", hash = "sha256:d11c6262f7a9deff7de8fdf14498b8a951020dfed3a80f2358ab731ad04abef0"}, + {file = "langchain_core-0.1.12.tar.gz", hash = "sha256:f18e9300e9a07589b3e280e51befbc5a4513f535949406e55eb7a2dc40c3ce66"}, ] [package.dependencies] @@ -772,13 +775,13 @@ tiktoken = ">=0.5.2,<0.6.0" [[package]] name = "langsmith" -version = "0.0.80" +version = "0.0.83" description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform." optional = false python-versions = ">=3.8.1,<4.0" files = [ - {file = "langsmith-0.0.80-py3-none-any.whl", hash = "sha256:dee1c6ef9e8241b82a8851926624269954d0ff8e22d82e32e73455f387f4e245"}, - {file = "langsmith-0.0.80.tar.gz", hash = "sha256:6d22ee07eb41c65b3f5166b20041a026714952497d9e80d5be6879d3a5c14d84"}, + {file = "langsmith-0.0.83-py3-none-any.whl", hash = "sha256:a5bb7ac58c19a415a9d5f51db56dd32ee2cd7343a00825bbc2018312eb3d122a"}, + {file = "langsmith-0.0.83.tar.gz", hash = "sha256:94427846b334ad9bdbec3266fee12903fe9f5448f628667689d0412012aaf392"}, ] [package.dependencies] @@ -971,13 +974,13 @@ files = [ [[package]] name = "openai" -version = "1.7.2" +version = "1.8.0" description = "The official Python library for the openai API" optional = false python-versions = ">=3.7.1" files = [ - {file = "openai-1.7.2-py3-none-any.whl", hash = "sha256:8f41b90a762f5fd9d182b45851041386fed94c8ad240a70abefee61a68e0ef53"}, - {file = "openai-1.7.2.tar.gz", hash = "sha256:c73c78878258b07f1b468b0602c6591f25a1478f49ecb90b9bd44b7cc80bce73"}, + {file = "openai-1.8.0-py3-none-any.whl", hash = "sha256:0f8f53805826103fdd8adaf379ad3ec23f9d867e698cbc14caf34b778d150175"}, + {file = "openai-1.8.0.tar.gz", hash = "sha256:93366be27802f517e89328801913d2a5ede45e3b86fdcab420385b8a1b88c767"}, ] [package.dependencies] @@ -1005,13 +1008,13 @@ files = [ [[package]] name = "pamqp" -version = "3.2.1" +version = "3.3.0" description = "RabbitMQ Focused AMQP low-level library" optional = false python-versions = ">=3.7" files = [ - {file = "pamqp-3.2.1-py2.py3-none-any.whl", hash = "sha256:15acef752356593ca569d13dfedc8ada9f17deeeb8cec4f7b77825e2b6c7de3e"}, - {file = "pamqp-3.2.1.tar.gz", hash = "sha256:22550ceb1ca50aafda65873e77e8c1c1b139fb5975e1a09860fae940cf8e970a"}, + {file = "pamqp-3.3.0-py2.py3-none-any.whl", hash = "sha256:c901a684794157ae39b52cbf700db8c9aae7a470f13528b9d7b4e5f7202f8eb0"}, + {file = "pamqp-3.3.0.tar.gz", hash = "sha256:40b8795bd4efcf2b0f8821c1de83d12ca16d5760f4507836267fd7a02b06763b"}, ] [package.extras] diff --git a/pyproject.toml b/pyproject.toml index 0f92a8b..b81a7af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "EUPL-1.2"