-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_agent_2.py
More file actions
46 lines (37 loc) · 1.58 KB
/
6_agent_2.py
File metadata and controls
46 lines (37 loc) · 1.58 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
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
from typing import Union, Dict, TypedDict, List
from langchain_core.messages import HumanMessage, AIMessage
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, START, END
from dotenv import load_dotenv
load_dotenv() # Load environment variables from .env file
class AgentState(TypedDict):
messages: List[Union[HumanMessage, AIMessage]]
llm = ChatOpenAI(model="gpt-4o")
def process(state:AgentState) -> AgentState:
"""This node will solve the request and append the response to the messages."""
response = llm.invoke(state["messages"])
state["messages"].append(AIMessage(content = response.content))
print(f"\n AI: {response.content}")
return state
graph = StateGraph(AgentState)
graph.add_node("processor", process)
graph.add_edge(START, "processor")
graph.add_edge("processor", END)
agent = graph.compile()
converstatin_history = []
user_input = input("Enter:")
while user_input!= "exit":
converstatin_history.append(HumanMessage(content=user_input))
result = agent.invoke({"messages": converstatin_history})
converstatin_history = result["messages"] # Update the conversation history with the latest messages
user_input = input("Enter:")
with open("logging.txt", "w") as file:
file.write("Your conversation log:\n")
for message in converstatin_history:
if isinstance(message, HumanMessage):
file.write(f"You: {message.content}\n")
elif isinstance(message, AIMessage):
file.write(f"AI: {message.content}")
file.write("End of conversation")
print("Convesation saved to logging.txt")