JulsdL commited on
Commit
bb2dcc7
Β·
1 Parent(s): cea0ce1

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
Files changed (4) hide show
  1. README.md +6 -1
  2. app.py +7 -13
  3. chainlit.md +15 -0
  4. prompt_templates.py +14 -1
README.md CHANGED
@@ -1 +1,6 @@
1
- # SmartQuery
 
 
 
 
 
 
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
- result = await agent.ainvoke(message.content, config=config)
20
-
21
- msg = cl.Message(content="")
22
-
23
- async for chunk in result:
24
- await msg.stream_token(chunk)
25
 
26
- await msg.send()
 
27
 
28
- # Run the app
29
- if __name__ == "__main__":
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 don't know" as the answer.
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\% \]"