-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathcsv_qa.py
81 lines (58 loc) · 1.94 KB
/
csv_qa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from langchain.agents import create_pandas_dataframe_agent
from langchain.llms import OpenAI
import pandas as pd
import chainlit as cl
import os
import io
# Chainlit fetches env variables from .env automatically
""" from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
OPENAI_API_KEY= os.getenv("OPENAI_API_KEY")
"""
# Create an OpenAI object.
llm = OpenAI()
def create_agent(data: str, llm):
"""Create a Pandas DataFrame agent."""
return create_pandas_dataframe_agent(llm, data, verbose=False)
@cl.on_chat_start
async def on_chat_start():
# Sending an image with the local file path
elements = [
cl.Image(name="image1", display="inline", path="./robot.jpeg")
]
await cl.Message(content="Hello there, Welcome to AskAnyQuery related to Data!", elements=elements).send()
files = None
# Wait for user to upload csv data
while files is None:
files = await cl.AskFileMessage(
content="Please upload a csv file to begin!",
accept=["text/csv"],
max_size_mb= 100,
timeout = 180,
).send()
# load the csv data and store in user_session
file = files[0]
msg = cl.Message(content=f"Processing `{file.name}`...")
await msg.send()
# Read csv file with pandas
csv_file = io.BytesIO(file.content)
df = pd.read_csv(csv_file, encoding="utf-8")
# creating user session to store data
cl.user_session.set('data', df)
# Send response back to user
# Let the user know that the system is ready
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
await msg.update()
@cl.on_message
async def main(message: str):
# Get data
df = cl.user_session.get('data')
# Agent creation
agent = create_agent(df, llm)
# Run model
response = agent.run(message)
# Send a response back to the user
await cl.Message(
content=response,
).send()