-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9_Memory_agent.py
More file actions
49 lines (39 loc) · 1.53 KB
/
Copy path9_Memory_agent.py
File metadata and controls
49 lines (39 loc) · 1.53 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
47
48
49
import os
from typing import TypedDict, List, Union
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()
class AgentState(TypedDict):
messages: List[Union[HumanMessage, AIMessage]]
llm = ChatOpenAI(model='gpt-4o-mini')
def process(state: AgentState) -> AgentState:
"""This node will solve the request from the input"""
response = llm.invoke(state['messages'])
state['messages'].append(AIMessage(content=response.content))
print(f"\n AI: {response.content}")
print(f"Current state: {state['messages']}")
return state
graph = StateGraph(AgentState)
graph.add_node("process", process)
graph.add_edge(START, 'process')
graph.add_edge('process', END)
agent = graph.compile()
conversation_history = []
user_input = input("Enter:")
while user_input != "exit":
conversation_history.append(HumanMessage(content=user_input))
result = agent.invoke({'messages': conversation_history})
print(result['messages'])
conversation_history = result['messages']
user_input = input("Enter: ")
with open('logging.txt', 'w') as file:
file.write("Your conversation Log:\n")
for message in conversation_history:
if isinstance(message, HumanMessage):
file.write(f"You: {message.content}\n")
elif isinstance(message, AIMessage):
file.write(f"AI: {message.content}\n")
file.write("End of Conversation")
print("Conversation saved to logging.txt")