File size: 2,597 Bytes
f28182b
 
 
 
 
 
 
 
 
 
 
 
c07521b
 
 
 
 
 
 
 
 
 
f28182b
 
db9c22d
f28182b
 
 
 
 
e2fe7bb
f28182b
 
 
 
 
 
 
 
 
 
c07521b
f28182b
 
 
 
 
 
 
 
 
 
 
 
 
e2fe7bb
f28182b
 
cabe1ba
f28182b
6db96bb
f28182b
 
 
 
 
 
 
 
c07521b
 
 
 
 
 
f28182b
 
06c5681
0462614
c07521b
 
0462614
e7a5418
f28182b
06c5681
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from langchain.prompts import StringPromptTemplate
import re
import langchain
from qa_txt import conversation_chain
from key_extract import chain
from bs4 import BeautifulSoup
import requests
from data_process import *
from langchain.tools.base import StructuredTool
from langchain.agents import initialize_agent
from qa_txt import llm
import gradio as gr
from langchain.agents import (
    create_react_agent,
    AgentExecutor,
    tool,
)
from langchain import hub



prompt = hub.pull("hwchase17/react")

def faq(query: str) -> str:
    reponse = conversation_chain.invoke({"question": query, "chat_history": []})
    return reponse['answer']

qa_faq = StructuredTool.from_function(
    func = faq ,
    description="""
    Respond to general and FAQ questions about the website .

    Parameters :
    - query (string) : the same input as the user input no more no less and dont translate it even if it is in another language.

    Returns :
    - string : the output as returned from the function in french.
    """
)

def request_data(query: str) -> str:
    request = chain.invoke({"input": query})['text']
    mot_cle = nettoyer_string(request)
    mots = mot_cle.split()
    ui = mots[0]
    rg = chercher_data(ui)
    if len(rg[0]):
      reponse_final = format_reponse(rg)
      return reponse_final
    else:
      return "Désolé, il semble que nous n'ayons pas de données correspondant à votre demande pour le moment. Avez-vous une autre question ou avez-vous besoin d'aide sur quelque chose d'autre?"

fetch_data = StructuredTool.from_function(
    func=request_data,
    description="""
    Request and fetch data using a search keyword.

    Parameters :
    - query (string) : the same input as the user input no more no less and always it must be in french if it isn't already. For example : "give me data about health" the input is health in french which is santé, same for other languages.
    Returns :
    - string : the output as returned from the function in french , includes the link to all the data about the keyword along with an example.
    """,
)

tools_add = [
    qa_faq,
    fetch_data,
]

agent = create_react_agent(llm=llm, tools=tools_add, prompt=prompt)

agent_executor = AgentExecutor(
    agent=agent,
    tools=tools_add,
    verbose=True,
)

def data_gov_ma(message, history):
  try:
    response = agent_executor.invoke({"input": message})
    return response['output']
  except ValueError as e:
    return "Je suis désolé, je n'ai pas compris votre question.Pourriez-vous la reformuler s'il vous plaît ?"

gr.ChatInterface(data_gov_ma).launch()