File size: 1,523 Bytes
30b5e11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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