Spaces:
Paused
Paused
Refactor app.py to use async context for SmartQuery Agent, enhance error handling, and streamline message streaming. Add comprehensive documentation in chainlit.md and update prompt_templates.py to improve user query responses. Update README.md with project metadata.
Browse files- README.md +6 -1
- app.py +7 -13
- chainlit.md +15 -0
- prompt_templates.py +14 -1
README.md
CHANGED
@@ -1 +1,6 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
title: SmartQuery
|
2 |
+
emoji: π
|
3 |
+
colorFrom: blue
|
4 |
+
colorTo: yellow
|
5 |
+
sdk: docker
|
6 |
+
pinned: false
|
app.py
CHANGED
@@ -2,9 +2,6 @@ import chainlit as cl
|
|
2 |
from langchain.schema.runnable.config import RunnableConfig
|
3 |
from sql_agent import SQLAgent
|
4 |
|
5 |
-
# Test the agent
|
6 |
-
# agent.invoke({"input": "How many artists are there?"})
|
7 |
-
|
8 |
# ChainLit Integration
|
9 |
@cl.on_chat_start
|
10 |
async def on_chat_start():
|
@@ -16,15 +13,12 @@ async def on_message(message: cl.Message):
|
|
16 |
cb = cl.AsyncLangchainCallbackHandler(stream_final_answer=True)
|
17 |
config = RunnableConfig(callbacks=[cb])
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
async for chunk in result:
|
24 |
-
await msg.stream_token(chunk)
|
25 |
|
26 |
-
|
|
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
cl.run()
|
|
|
2 |
from langchain.schema.runnable.config import RunnableConfig
|
3 |
from sql_agent import SQLAgent
|
4 |
|
|
|
|
|
|
|
5 |
# ChainLit Integration
|
6 |
@cl.on_chat_start
|
7 |
async def on_chat_start():
|
|
|
13 |
cb = cl.AsyncLangchainCallbackHandler(stream_final_answer=True)
|
14 |
config = RunnableConfig(callbacks=[cb])
|
15 |
|
16 |
+
async with cl.Step(name="SmartQuery Agent", root=True) as step:
|
17 |
+
step.input = message.content
|
18 |
+
result = await agent.ainvoke(message.content, config=config)
|
|
|
|
|
|
|
19 |
|
20 |
+
# Assuming the result is a dictionary with a key 'output' containing the final answer
|
21 |
+
final_answer = result.get('output', 'No answer returned')
|
22 |
|
23 |
+
# Stream the final answer as a token to the step
|
24 |
+
await step.stream_token(final_answer)
|
|
chainlit.md
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Welcome to SmartQuery! ππ
|
2 |
+
|
3 |
+
Hello and welcome to SmartQuery, your intelligent assistant designed to help you interact with your database effortlessly. Whether you're a business analyst, data scientist, or a curious user, our tool will help you get answers from your database without writing a single line of SQL.
|
4 |
+
|
5 |
+
## Getting Started
|
6 |
+
|
7 |
+
## Sample Questions to Try:
|
8 |
+
|
9 |
+
## How It Works π§
|
10 |
+
|
11 |
+
## The Tech Behind It π‘π€
|
12 |
+
|
13 |
+
## Ready to Query?
|
14 |
+
|
15 |
+
With SmartQuery, transform your database interactions into a seamless experience. Dive into your data, uncover insights, and make data-driven decisions more effectively than ever before. Happy querying!
|
prompt_templates.py
CHANGED
@@ -55,6 +55,19 @@ You MUST double check your query before executing it. If you get an error while
|
|
55 |
|
56 |
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.
|
57 |
|
58 |
-
If the question does not seem related to the database, just return "I
|
59 |
|
60 |
Here are some examples of user inputs and their corresponding SQL queries:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.
|
57 |
|
58 |
+
If the question does not seem related to the database, just return "Sorry, the query is not related to the database, I can't answe" as the answer.
|
59 |
|
60 |
Here are some examples of user inputs and their corresponding SQL queries:"""
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
# DO NOT include code formatted text in FINAL response to the user. For example, avoid this: "\[ \text{Proportion} = \left( \frac{1233.54}{2328.60} \right) \times 100 \approx 52.98\% \]"
|