Spaces:
Sleeping
Sleeping
Update src/agent.py
Browse files- src/agent.py +74 -57
src/agent.py
CHANGED
|
@@ -1,66 +1,83 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def decode_response(response: str) -> dict:
|
| 8 |
-
""" This function converts the string response from the model to a dictionary.
|
| 9 |
Args:
|
| 10 |
-
|
|
|
|
| 11 |
Returns:
|
| 12 |
-
|
| 13 |
"""
|
| 14 |
-
response_dict = json.loads(response)
|
| 15 |
-
return response_dict
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
Args:
|
| 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 |
-
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__()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|