Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
88 lines (53 loc) · 2.08 KB
/
Copy pathcode.py
File metadata and controls
88 lines (53 loc) · 2.08 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
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
77
78
79
80
81
82
83
84
85
86
87
88
## agent 2
## chatbot with some memory
## objectives :
# 1. use different message types - HumanMessage and ai message
# 2. maintain full conversation history using both message types
# 3. use gpt-4o model using langchain chatopenai , or langchain-google-genai
# main goal : create a form of memory for our agent
import os
from typing import TypedDict, List , Union
from langchain_core.messages import HumanMessage, AIMessage
from langchain_google_genai import ChatGoogleGenerativeAI
from langgraph.graph import StateGraph , START, END
from dotenv import load_dotenv
load_dotenv()
class AgentState(TypedDict):
messages : List[Union[HumanMessage,AIMessage]]
llm = ChatGoogleGenerativeAI(
model="gemini-pro",
google_api_key=os.getenv("GEMINI_API_KEY")
)
def process(state:AgentState) -> AgentState :
"""this node will solve the request you input"""
response = llm.invoke(state['messages'])
state["messages"].append(AIMessage(content=response.content)) # only extract contents
# this is our memory
print(f"\nAI : {response.content}")
print("CUURENT 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\n")
file.write("End of Conversation")
print("Conversation saved to logging.txt")
### here we have 2 big problems here
# solution for you data being get wiped out ?
# is , you can store it in DB , textfiles , vector db