Update main.py
Browse files
main.py
CHANGED
@@ -36,14 +36,107 @@ def auth_callback(username: str, password: str):
|
|
36 |
identifier=ident + " : 🧑🎓 User Datapcc", metadata={"role": "user", "provider": "credentials"}
|
37 |
)
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.environ['HUGGINGFACEHUB_API_TOKEN']
|
42 |
repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
43 |
llm = HuggingFaceEndpoint(
|
44 |
repo_id=repo_id, max_new_tokens=5300, temperature=0.1, task="text2text-generation", streaming=True
|
45 |
)
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
@cl.set_chat_profiles
|
49 |
async def chat_profile():
|
@@ -64,26 +157,13 @@ async def set_starters():
|
|
64 |
@cl.on_message
|
65 |
async def on_message(message: cl.Message):
|
66 |
await cl.Message(f"> SURVEYIA").send()
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
res = await agent.acall("Réponds en langue française à la question suivante :\n" + message.content + "\nDétaille la réponse en faisant une analyse complète en 2000 mots minimum.", callbacks=[cb])
|
78 |
-
await cl.Message(author="COPILOT",content=GoogleTranslator(source='auto', target='fr').translate(res['output'])).send()
|
79 |
-
except ValueError as e:
|
80 |
-
res = str(e)
|
81 |
-
resArray = res.split(":")
|
82 |
-
ans = ''
|
83 |
-
if str(res).find('parsing') != -1:
|
84 |
-
for i in range(2,len(resArray)):
|
85 |
-
ans += resArray[i]
|
86 |
-
await cl.Message(author="COPILOT",content=ans.replace("`","")).send()
|
87 |
-
else:
|
88 |
-
await cl.Message(author="COPILOT",content="Reformulez votre requête, s'il vous plait 😃").send()
|
89 |
-
|
|
|
36 |
identifier=ident + " : 🧑🎓 User Datapcc", metadata={"role": "user", "provider": "credentials"}
|
37 |
)
|
38 |
|
39 |
+
def create_agent(filename: str):
|
40 |
+
"""
|
41 |
+
Create an agent that can access and use a large language model (LLM).
|
42 |
+
|
43 |
+
Args:
|
44 |
+
filename: The path to the CSV file that contains the data.
|
45 |
+
|
46 |
+
Returns:
|
47 |
+
An agent that can access and use the LLM.
|
48 |
+
"""
|
49 |
+
|
50 |
+
# Create an OpenAI object.
|
51 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.environ['HUGGINGFACEHUB_API_TOKEN']
|
52 |
repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
53 |
llm = HuggingFaceEndpoint(
|
54 |
repo_id=repo_id, max_new_tokens=5300, temperature=0.1, task="text2text-generation", streaming=True
|
55 |
)
|
56 |
+
|
57 |
+
# Read the CSV file into a Pandas DataFrame.
|
58 |
+
df = pd.read_csv(filename)
|
59 |
+
|
60 |
+
# Create a Pandas DataFrame agent.
|
61 |
+
return create_pandas_dataframe_agent(llm, df, verbose=False)
|
62 |
+
|
63 |
+
def query_agent(agent, query):
|
64 |
+
"""
|
65 |
+
Query an agent and return the response as a string.
|
66 |
+
|
67 |
+
Args:
|
68 |
+
agent: The agent to query.
|
69 |
+
query: The query to ask the agent.
|
70 |
+
|
71 |
+
Returns:
|
72 |
+
The response from the agent as a string.
|
73 |
+
"""
|
74 |
+
|
75 |
+
prompt = (
|
76 |
+
"""
|
77 |
+
For the following query, if it requires drawing a table, reply as follows:
|
78 |
+
{"table": {"columns": ["column1", "column2", ...], "data": [[value1, value2, ...], [value1, value2, ...], ...]}}
|
79 |
+
|
80 |
+
If the query requires creating a bar chart, reply as follows:
|
81 |
+
{"bar": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}
|
82 |
+
|
83 |
+
If the query requires creating a line chart, reply as follows:
|
84 |
+
{"line": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}
|
85 |
+
|
86 |
+
There can only be two types of chart, "bar" and "line".
|
87 |
+
|
88 |
+
If it is just asking a question that requires neither, reply as follows:
|
89 |
+
{"answer": "answer"}
|
90 |
+
Example:
|
91 |
+
{"answer": "The title with the highest rating is 'Gilead'"}
|
92 |
+
|
93 |
+
If you do not know the answer, reply as follows:
|
94 |
+
{"answer": "I do not know."}
|
95 |
+
|
96 |
+
Return all output as a string.
|
97 |
+
|
98 |
+
All strings in "columns" list and data list, should be in double quotes,
|
99 |
+
|
100 |
+
For example: {"columns": ["title", "ratings_count"], "data": [["Gilead", 361], ["Spider's Web", 5164]]}
|
101 |
+
|
102 |
+
Lets think step by step.
|
103 |
+
|
104 |
+
Below is the query.
|
105 |
+
Query:
|
106 |
+
"""
|
107 |
+
+ query
|
108 |
+
)
|
109 |
+
|
110 |
+
# Run the prompt through the agent.
|
111 |
+
response = agent.run(prompt)
|
112 |
+
|
113 |
+
# Convert the response to a string.
|
114 |
+
return response.__str__()
|
115 |
+
|
116 |
+
def decode_response(response: str) -> dict:
|
117 |
+
"""This function converts the string response from the model to a dictionary object.
|
118 |
+
|
119 |
+
Args:
|
120 |
+
response (str): response from the model
|
121 |
+
|
122 |
+
Returns:
|
123 |
+
dict: dictionary with response data
|
124 |
+
"""
|
125 |
+
return json.loads(response)
|
126 |
+
|
127 |
+
def write_response(response_dict: dict):
|
128 |
+
"""
|
129 |
+
Write a response from an agent to a Streamlit app.
|
130 |
+
|
131 |
+
Args:
|
132 |
+
response_dict: The response from the agent.
|
133 |
+
|
134 |
+
Returns:
|
135 |
+
None.
|
136 |
+
"""
|
137 |
+
|
138 |
+
# Check if the response is an answer.
|
139 |
+
await cl.Message(author="COPILOT",content=GoogleTranslator(source='auto', target='fr').translate(response_dict["answer"])).send()
|
140 |
|
141 |
@cl.set_chat_profiles
|
142 |
async def chat_profile():
|
|
|
157 |
@cl.on_message
|
158 |
async def on_message(message: cl.Message):
|
159 |
await cl.Message(f"> SURVEYIA").send()
|
160 |
+
agent = create_agent("./public/ExpeCFA_LP_CAA.csv")
|
161 |
+
|
162 |
+
# Query the agent.
|
163 |
+
response = query_agent(agent=agent, query=message.content)
|
164 |
+
|
165 |
+
# Decode the response.
|
166 |
+
decoded_response = decode_response(response)
|
167 |
+
|
168 |
+
# Write the response to the Streamlit app.
|
169 |
+
write_response(decoded_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|