File size: 1,654 Bytes
f8734b3
94bbd69
 
fdfde63
 
144f199
bcfaa84
 
b48a433
bcfaa84
94bbd69
fdfde63
 
94bbd69
 
c2d02ae
 
94bbd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcfaa84
 
b6b7990
c2d02ae
 
94bbd69
 
 
bcfaa84
 
94bbd69
 
bcfaa84
 
94bbd69
 
 
 
bcfaa84
 
 
 
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
from langgraph.graph import StateGraph
from langchain_core.tools import Tool
from langchain_community.chat_models import ChatOpenAI
from langchain_community.tools import TavilySearchResults



def build_graph():
    graph = StateGraph(dict)

    # Setup tools
    search = TavilySearchResults()


    def search_step(state):
        question = state.get("question")
        if not question:
            raise ValueError("Missing 'question' in state")
        result = search.run(question)
        state["search_result"] = result
        return state

    def llm_step(state):
        llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
        question = state.get("question")
        search_info = state.get("search_result", "")
        prompt = f"""
You are solving a GAIA benchmark question.
Here’s the question:
{question}

Here’s retrieved web info:
{search_info}

ONLY return the final exact answer (no explanation, no prefix).
"""
        response = llm.invoke(prompt)
        state["llm_output"] = response.content.strip()
        return state

    def formatter_step(state):
        llm_output = state.get("llm_output")
        if not llm_output:
            raise ValueError("Missing 'llm_output' in state")
        cleaned = llm_output.strip()
        state["final_answer"] = cleaned
        return state

    # Add nodes
    graph.add_node("search", search_step)
    graph.add_node("llm", llm_step)
    graph.add_node("formatter", formatter_step)

    # Define flow
    graph.set_entry_point("search")
    graph.add_edge("search", "llm")
    graph.add_edge("llm", "formatter")
    graph.set_finish_point("formatter")

    return graph