| from langchain.agents import create_react_agent, AgentExecutor | |
| from langchain.memory import ConversationBufferMemory | |
| from langchain_community.vectorstores import FAISS | |
| from langchain_community.embeddings import HuggingFaceEmbeddings | |
| from langchain.prompts import PromptTemplate | |
| from langchain_openai import ChatOpenAI | |
| from tools.web import web_search_tool | |
| from tools.local_docs import local_knowledge_tool | |
| import os | |
| def create_agent(): | |
| llm = ChatOpenAI( | |
| model="gpt-4o-mini", | |
| temperature=0.2 | |
| ) | |
| tools = [ | |
| local_knowledge_tool, | |
| web_search_tool, | |
| ] | |
| prompt = PromptTemplate.from_template( | |
| """Tu es Kibali, un agent IA avancé. | |
| CAPACITÉS : | |
| - Base de connaissances locale | |
| - Recherche web Tavily | |
| RÈGLES : | |
| 1. Base locale TOUJOURS en premier | |
| 2. Web seulement si nécessaire | |
| 3. Cite les sources | |
| 4. Mentionne les contradictions | |
| FORMAT ReAct STRICT : | |
| Question: {input} | |
| Thought: raisonnement | |
| Action: outil | |
| Action Input: entrée | |
| Observation: résultat | |
| ... | |
| Final Answer: réponse claire | |
| Commence ! | |
| Question: {input} | |
| Thought: {agent_scratchpad} | |
| """ | |
| ) | |
| agent = create_react_agent( | |
| llm=llm, | |
| tools=tools, | |
| prompt=prompt | |
| ) | |
| memory = ConversationBufferMemory( | |
| memory_key="chat_history", | |
| return_messages=True | |
| ) | |
| executor = AgentExecutor( | |
| agent=agent, | |
| tools=tools, | |
| memory=memory, | |
| verbose=True, | |
| max_iterations=5, | |
| handle_parsing_errors=True | |
| ) | |
| return executor | |