PRSHNTKUMR commited on
Commit
20236af
·
verified ·
1 Parent(s): d828ccb

Create agent.py

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