-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
33 lines (31 loc) · 1.18 KB
/
main.py
File metadata and controls
33 lines (31 loc) · 1.18 KB
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
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
from langchain.tools import tool
from langgraph.prebuilt import create_react_agent
load_dotenv()
@tool
def addition(a: int, b: int) -> int:
"""Returns the sum of two numbers."""
return "The sum is: " + str(a + b)
def main():
model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
tools = [addition] # You can add tools later
# Create the agent
agent = create_react_agent(model=model, tools=tools)
print("Hello! I am your chatbot. How can I assist you today?")
print("Type 'exit' to quit.\n")
while True:
user_input = input("You: ").strip()
if user_input.lower() in ["exit", "quit"]:
print("Goodbye!")
break
print("Bot: ", end="", flush=True)
# STREAMING RESPONSE (LATEST CORRECT FORMAT)
for event in agent.stream({"messages": [HumanMessage(content=user_input)]}):
if "agent" in event:
msg = event["agent"]["messages"][-1]
print(msg.content, end="", flush=True)
print() # new line after bot response
if __name__ == "__main__":
main()