File size: 4,977 Bytes
bef8270
ad535b7
 
 
 
 
 
 
 
 
 
 
 
 
1fa2efd
ad535b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adb4004
 
ad535b7
 
1fa2efd
ad535b7
 
 
 
adb4004
 
ad535b7
 
1fa2efd
ad535b7
 
 
 
adb4004
 
ad535b7
 
73e8255
ad535b7
 
 
 
adb4004
 
ad535b7
e1c1bb7
ad535b7
 
 
74cc1b7
ad535b7
74cc1b7
ad535b7
 
 
 
 
74cc1b7
ad535b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fa2efd
ad535b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from langchain_tavily import TavilySearch
from langchain_community.document_loaders import WikipediaLoader
from langchain_ollama import ChatOllama
from langgraph.prebuilt import create_react_agent
from langgraph_supervisor import create_supervisor

llm_model=ChatOllama(model="qwen2:7b", temperature=0.0, max_tokens=1000, base_url="http://localhost:11434")

# Tool for searching wikipedia and returning the content
def search_wikipedia(query: str) -> str:
    """Searches Wikipedia for the given query and returns the first result's content.
    Args:
        query (str): The search query to use for Wikipedia. Should be a simple keyword or phrase. Do not enter the URL or any other complex query.
    """

    loader = WikipediaLoader(query=query, load_max_docs=1)
    docs = loader.load()
    if docs:
        return "From: " + docs[0].metadata['source'] + "\n\nContent: " + docs[0].page_content
    return "No content found."

# Tool for searching the web using Tavily
def search_web(query: str) -> str:
    """Searches the web for the given query and returns the first result's content.
    Args:
        query (str): The search query to use for web search. Should be a simple keyword or phrase. Do not enter the URL or any other complex query.
    """
    results = TavilySearch(max_results=1).invoke(input=query)
    if results:
        return "From: " + results['results'][0]['url'] + "\n\nContent: " + results['results'][0]['content']
    return "No content found."

research_agent = create_react_agent(
    model=llm_model,
    tools=[search_wikipedia, search_web],
    prompt=(
        "You are a research agent.\n\n"
        "INSTRUCTIONS:\n"
        "- Assist ONLY with research-related tasks, DO NOT do any math\n"
        "- After you're done with your tasks, respond to the supervisor directly\n"
        "- Respond ONLY with the results of your work, do NOT include ANY other text."
    ),
    name="research_agent",
)

def add(a: int, b: int) -> int:
    """
        Add two integers and return the result.
        Inputs:
            a: first integer
            b: second integer
    """
    return a + b

def subtract(a: int, b: int) -> int:
    """
        Subtract two integers and return the result.
        Inputs:
            a: first integer
            b: second integer
    """
    return a - b

def multiply(a: int, b: int) -> int:
    """
        Multiply two integers and return the result.
        Inputs:
            a: first integer
            b: second integer
    """
    return a * b

def divide(a: int, b: int) -> float:
    """
        Divide two integers and return the result.
        Inputs:
            a: first integer
            b: second integer
        Note: If b is 0, return "Error: Division by zero is not allowed."
    """
    if b == 0:
        return "Error: Division by zero is not allowed."
    return a / b

def modulo(a: int, b: int) -> int:
    """
        Calculate the modulo of two integers and return the result.
        Inputs:
            a: first integer
            b: second integer
        Note: If b is 0, return "Error: Division by zero is not allowed."
    """
    if b == 0:
        return "Error: Division by zero is not allowed."
    return a % b

math_agent = create_react_agent(
    model=llm_model,
    tools=[add, subtract, multiply, divide, modulo],
    prompt=(
        "You are a math agent.\n\n"
        "INSTRUCTIONS:\n"
        "- Assist ONLY with math-related tasks\n"
        "- After you're done with your tasks, respond to the supervisor directly\n"
        "- Respond ONLY with the results of your work, do NOT include ANY other text."
    ),
    name="math_agent",
)
def create_agent():
    supervisor = create_supervisor(
        model=llm_model,
        agents=[research_agent, math_agent],
        prompt=(
            "You are a supervisor managing two agents:\n"
            "- a research agent. Assign research-related tasks to this agent\n"
            "- a math agent. Assign math-related tasks to this agent\n"
            "Assign work to one agent at a time, do not call agents in parallel.\n"
            "Do not do any work yourself. When you are ready to give the final answer, do so as simply as possible.If the answer is a number, write only the number do not use commas or any special characters or any other text. \n"
            "If the answer is a string, write only the string without any additional text.\n"
            "If the answer is a list, write the items in the list separated by commas while following the above rules for numbers and string where appropriate.\n"
            "Preface your answer with FINAL ANSWER: {answer}\n"
            "Example: How many planets are in the solar system?\n Answer: 8\n"
            "Example: What is the capital of France?\n Answer: Paris\n"
            "Example: What is 2 + 2?\n Answer: 4\n"
        ),
        add_handoff_back_messages=True,
        output_mode="full_history",
    ).compile()

    return supervisor