PRSHNTKUMR commited on
Commit
c2ae22f
·
verified ·
1 Parent(s): 799c96f

Update src/agent.py

Browse files
Files changed (1) hide show
  1. src/agent.py +74 -57
src/agent.py CHANGED
@@ -1,66 +1,83 @@
1
- import streamlit as st
 
2
  import pandas as pd
3
- import json
4
 
5
- from agent import query_agent, create_agent
 
 
 
 
 
 
 
 
6
 
7
- def decode_response(response: str) -> dict:
8
- """ This function converts the string response from the model to a dictionary.
9
  Args:
10
- response (str): The string response from the model.
 
11
  Returns:
12
- dict: The dictionary representation of the response.
13
  """
14
- response_dict = json.loads(response)
15
- return response_dict
16
 
17
- def write_response(response_dict: dict):
18
- """ This function writes the response from the model to the Streamlit app.
 
 
 
 
 
 
 
 
 
 
 
 
19
  Args:
20
- response_dict (dict): The dictionary representation of the response.
 
 
 
 
21
  """
22
- # Check if the response is an answer.
23
- if "answer" in response_dict:
24
- st.write(response_dict["answer"])
25
-
26
- # Check if the response is a bar chart.
27
- if "bar" in response_dict:
28
- data = response_dict["bar"]
29
- df = pd.DataFrame(data)
30
- df.set_index("columns", inplace=True)
31
- st.bar_chart(df)
32
-
33
- # Check if the response is a line chart.
34
- if "line" in response_dict:
35
- data = response_dict["line"]
36
- df = pd.DataFrame(data)
37
- df.set_index("columns", inplace=True)
38
- st.line_chart(df)
39
-
40
- # Check if the response is a table.
41
- if "table" in response_dict:
42
- data = response_dict["table"]
43
- df = pd.DataFrame(data["data"], columns=data["columns"])
44
- st.table(df)
45
-
46
- st.title("👨‍💻 Chat with your CSV")
47
-
48
- st.write("Please upload your CSV file below.")
49
-
50
- data = st.file_uploader("Upload a CSV")
51
-
52
- query = st.text_area("Type your query here")
53
-
54
- if st.button("Submit Query", type="primary"):
55
- # Create an agent from the CSV file.
56
- agent = create_agent(data)
57
-
58
- # Query the agent.
59
- response = query_agent(agent=agent, query=query)
60
-
61
- # Decode the response.
62
- decoded_response = decode_response(response)
63
- print(decoded_response)
64
-
65
- # Write the response to the Streamlit app.
66
- write_response(decoded_response)
 
1
+ from langchain.llms import OpenAI
2
+ from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
3
  import pandas as pd
4
+ import environ
5
 
6
+ env = environ.Env()
7
+ environ.Env.read_env()
8
+
9
+ API_KEY = env("OPENAI_API_KEY")
10
+
11
+
12
+ def create_agent(filename: str):
13
+ """
14
+ Create an agent that can access and use a large language model (LLM).
15
 
 
 
16
  Args:
17
+ filename: The path to the CSV file that contains the data.
18
+
19
  Returns:
20
+ An agent that can access and use the LLM.
21
  """
 
 
22
 
23
+ # Create an OpenAI object.
24
+ llm = OpenAI(openai_api_key=API_KEY)
25
+
26
+ # Read the CSV file into a Pandas DataFrame.
27
+ df = pd.read_csv(filename)
28
+
29
+ # Create a Pandas DataFrame agent.
30
+ return create_pandas_dataframe_agent(llm, df, verbose=False)
31
+
32
+
33
+ def query_agent(agent, query):
34
+ """
35
+ Query an agent and return the response as a string.
36
+
37
  Args:
38
+ agent: The agent to query.
39
+ query: The query to ask the agent.
40
+
41
+ Returns:
42
+ The response from the agent as a string.
43
  """
44
+ prompt = (
45
+ """
46
+ For the following query, if it requires drawing a table, reply as follows:
47
+ {"table": {"columns": ["column1", "column2", ...], "data": [[value1, value2, ...], [value1, value2, ...], ...]}}
48
+
49
+ If the query requires creating a bar chart, reply as follows:
50
+ {"bar": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}
51
+
52
+ If the query requires creating a line chart, reply as follows:
53
+ {"line": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}
54
+
55
+ There can only be two types of chart, "bar" and "line".
56
+
57
+ If it is just asking a question that requires neither, reply as follows:
58
+ {"answer": "answer"}
59
+ Example:
60
+ {"answer": "I do not have sufficient information to provide an answer to your query."}
61
+
62
+ If you do not know the answer, reply as follows:
63
+ {"answer": "I do not know."}
64
+
65
+ Return all output as a string.
66
+
67
+ All strings in "columns" list and data list, should be in double quotes,
68
+
69
+ For example: {"columns": ["title", "ratings_count"], "data": [["Gilead", 361], ["Spider's Web", 5164]]}
70
+
71
+ Lets think step by step.
72
+
73
+ Below is the query.
74
+ Query:
75
+ """
76
+ + query
77
+ )
78
+
79
+ # Run the prompt through the agent.
80
+ response = agent.run(prompt)
81
+
82
+ # Convert the response to a string.
83
+ return response.__str__()