File size: 2,120 Bytes
4235119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langgraph.graph import StateGraph, START, END
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from state import AgentState
from tools import search_tool
from dotenv import load_dotenv

load_dotenv()

template = """Your name is Atom, you're an advance AI Agent powered by a powerful LLM. Your task is to answer the following questions as best you can. You have access to the following tools:

{tools}

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!

Question: {input}
Thought:{{agent_scratchpad}}"""

prompt_template = PromptTemplate.from_template(template)

llm = ChatOpenAI(temperature=0)


def analyze_question(state: AgentState):
    """Read the incoming question"""
    question = state["question"]
    tools = state["tools"]  # temp
    tool_names = state["tool_names"]  # temp
    # Create prompt template
    prompt = prompt_template.invoke(
        {"input": question, "tools": tools, "tool_names": tool_names}
    )
    response = llm.invoke(prompt)
    state["thought"] = response
    print("\n STATE", state)


def create_final_answer(state: AgentState):
    """Create the final answer"""


# Create graph
builder = StateGraph(AgentState)

# Add Nodes
builder.add_node("analyze_question", analyze_question)
builder.add_node("search_tool", search_tool)

# Add Edges
builder.add_edge(START, "analyze_question")
builder.add_edge("analyze_question", "search_tool")
builder.add_edge("search_tool", END)


agent = builder.compile()
agent.invoke(
    {
        "input": "whats the current weather in Orlando?",
        "question": "whats the current weather in Orlando?",
        "tools": "search_tool",
        "agent_scratchpad": "",
        "tool_names": "search_tool",
    }
)