datacipen commited on
Commit
7b79c1f
1 Parent(s): ec0eefe

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +44 -2
main.py CHANGED
@@ -21,7 +21,6 @@ from deep_translator import GoogleTranslator
21
  from IPython.display import display
22
 
23
  from surveycaa import surveyCaa
24
- from csvanswer import LLMAnswer
25
 
26
  @cl.password_auth_callback
27
  def auth_callback(username: str, password: str):
@@ -40,6 +39,27 @@ def auth_callback(username: str, password: str):
40
  identifier=ident + " : 🧑‍🎓 User Datapcc", metadata={"role": "user", "provider": "credentials"}
41
  )
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  def query_agent(agent, query):
44
  """
45
  Query an agent and return the response as a string.
@@ -154,4 +174,26 @@ async def on_chat_start():
154
  @cl.on_message
155
  async def on_message(message: cl.Message):
156
  await cl.Message(f"> SURVEYIA").send()
157
- await LLMAnswer(message.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  from IPython.display import display
22
 
23
  from surveycaa import surveyCaa
 
24
 
25
  @cl.password_auth_callback
26
  def auth_callback(username: str, password: str):
 
39
  identifier=ident + " : 🧑‍🎓 User Datapcc", metadata={"role": "user", "provider": "credentials"}
40
  )
41
 
42
+ def create_agent(filename: str):
43
+ """
44
+ Create an agent that can access and use a large language model (LLM).
45
+
46
+ Args:
47
+ filename: The path to the CSV file that contains the data.
48
+
49
+ Returns:
50
+ An agent that can access and use the LLM.
51
+ """
52
+
53
+ # Create an OpenAI object.
54
+ os.environ['OPENAI_API_KEY'] = os.environ['OPENAI_API_KEY']
55
+ llm = ChatOpenAI(temperature=0, model="gpt-4o-2024-05-13")
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_csv_agent(llm, filename, verbose=False, allow_dangerous_code=True, handle_parsing_errors=True, agent_type=AgentType.OPENAI_FUNCTIONS)
62
+
63
  def query_agent(agent, query):
64
  """
65
  Query an agent and return the response as a string.
 
174
  @cl.on_message
175
  async def on_message(message: cl.Message):
176
  await cl.Message(f"> SURVEYIA").send()
177
+ agent = create_agent("./public/surveyia.csv")
178
+ cb = cl.AsyncLangchainCallbackHandler()
179
+ try:
180
+ res = await agent.acall("Réponds en langue française à la question suivante : " + message.content, callbacks=[cb])
181
+ await cl.Message(author="COPILOT",content=GoogleTranslator(source='auto', target='fr').translate(res['output'])).send()
182
+ except ValueError as e:
183
+ res = str(e)
184
+ resArray = res.split(":")
185
+ ans = ''
186
+ if str(res).find('parsing') != -1:
187
+ for i in range(2,len(resArray)):
188
+ ans += resArray[i]
189
+ await cl.Message(author="COPILOT",content=ans.replace("`","")).send()
190
+ else:
191
+ await cl.Message(author="COPILOT",content="Reformulez votre requête, s'il vous plait 😃").send()
192
+ # Query the agent.
193
+ #response = query_agent(agent=agent, query=message.content)
194
+ # Decode the response.
195
+ #decoded_response = decode_response(response)
196
+
197
+ # Write the response to the Streamlit app.
198
+ #result = write_response(decoded_response)
199
+ #await cl.Message(author="COPILOT",content=GoogleTranslator(source='auto', target='fr').translate(result)).send()