Spaces:
Sleeping
Sleeping
Commit
·
9d34f4b
1
Parent(s):
508df21
Refined business interaction with memory and summarizer
Browse files
my_agent/utils/business_interaction.py
CHANGED
|
@@ -2,11 +2,15 @@ import os
|
|
| 2 |
from langchain_groq import ChatGroq
|
| 3 |
from langgraph.graph import StateGraph, MessagesState, START, END
|
| 4 |
from langgraph.checkpoint.memory import MemorySaver
|
| 5 |
-
from langchain_core.messages import SystemMessage
|
| 6 |
from pydantic import BaseModel, ConfigDict, Field
|
| 7 |
from typing import Optional, List
|
| 8 |
from .models_loader import llm
|
| 9 |
-
from .prompts import business_interaction_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
|
|
@@ -18,32 +22,53 @@ class State(BaseModel):
|
|
| 18 |
# Global business state (shared)
|
| 19 |
business_state = State()
|
| 20 |
|
| 21 |
-
class
|
| 22 |
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
self.memory = MemorySaver()
|
| 24 |
# self.llm = ChatGroq(model_name="Gemma2-9b-It")
|
| 25 |
-
self.llm = llm
|
| 26 |
self.workflow = self._initialize_workflow()
|
| 27 |
self.interact_agent = self.workflow.compile(checkpointer=self.memory)
|
| 28 |
self.messages = []
|
| 29 |
|
|
|
|
| 30 |
def _initialize_workflow(self):
|
| 31 |
workflow = StateGraph(MessagesState)
|
| 32 |
workflow.add_node("chatbot", self._call_model)
|
| 33 |
-
workflow.
|
|
|
|
|
|
|
| 34 |
workflow.add_edge("chatbot", END)
|
| 35 |
return workflow
|
| 36 |
|
| 37 |
def _call_model(self, state):
|
|
|
|
| 38 |
template = business_interaction_prompt
|
| 39 |
-
messages = [SystemMessage(content=template)] + state["messages"]
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
return {"messages": [response]}
|
| 42 |
|
| 43 |
-
def chat(self, user_input: str
|
|
|
|
| 44 |
self.messages.append({"role": "user", "content": user_input})
|
| 45 |
-
config = {"configurable": {"thread_id": "
|
| 46 |
-
response = self.interact_agent.invoke({"messages":
|
|
|
|
| 47 |
self.messages.append({"role": "assistant", "content": response})
|
| 48 |
business_state.interactions.append({'user': user_input, 'agent_response': response})
|
| 49 |
-
return response
|
|
|
|
| 2 |
from langchain_groq import ChatGroq
|
| 3 |
from langgraph.graph import StateGraph, MessagesState, START, END
|
| 4 |
from langgraph.checkpoint.memory import MemorySaver
|
| 5 |
+
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
|
| 6 |
from pydantic import BaseModel, ConfigDict, Field
|
| 7 |
from typing import Optional, List
|
| 8 |
from .models_loader import llm
|
| 9 |
+
from .prompts import introduction_prompt , business_interaction_prompt
|
| 10 |
+
from .tools import retrieve_tool
|
| 11 |
+
from langgraph.prebuilt import create_react_agent
|
| 12 |
+
from langmem.short_term import SummarizationNode
|
| 13 |
+
from langchain_core.messages.utils import count_tokens_approximately
|
| 14 |
|
| 15 |
|
| 16 |
|
|
|
|
| 22 |
# Global business state (shared)
|
| 23 |
business_state = State()
|
| 24 |
|
| 25 |
+
class BusinessInteractionChatbot:
|
| 26 |
def __init__(self):
|
| 27 |
+
self.react_agent=create_react_agent(
|
| 28 |
+
model=llm.bind_tools([retrieve_tool]),
|
| 29 |
+
tools=[retrieve_tool]
|
| 30 |
+
)
|
| 31 |
+
self.summarization_model = llm.bind(max_tokens=400)
|
| 32 |
+
|
| 33 |
+
self.summarization_node = SummarizationNode(
|
| 34 |
+
token_counter=count_tokens_approximately,
|
| 35 |
+
model=self.summarization_model,
|
| 36 |
+
max_tokens=256,
|
| 37 |
+
max_tokens_before_summary=256,
|
| 38 |
+
max_summary_tokens=128,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
self.memory = MemorySaver()
|
| 42 |
# self.llm = ChatGroq(model_name="Gemma2-9b-It")
|
|
|
|
| 43 |
self.workflow = self._initialize_workflow()
|
| 44 |
self.interact_agent = self.workflow.compile(checkpointer=self.memory)
|
| 45 |
self.messages = []
|
| 46 |
|
| 47 |
+
|
| 48 |
def _initialize_workflow(self):
|
| 49 |
workflow = StateGraph(MessagesState)
|
| 50 |
workflow.add_node("chatbot", self._call_model)
|
| 51 |
+
workflow.add_node("summarize",self.summarization_node)
|
| 52 |
+
workflow.add_edge(START, "summarize")
|
| 53 |
+
workflow.add_edge("summarize", "chatbot")
|
| 54 |
workflow.add_edge("chatbot", END)
|
| 55 |
return workflow
|
| 56 |
|
| 57 |
def _call_model(self, state):
|
| 58 |
+
print('Entered into callmodel')
|
| 59 |
template = business_interaction_prompt
|
| 60 |
+
messages = [SystemMessage(content=template)] + state["messages"]
|
| 61 |
+
tool_response = self.react_agent.invoke({'messages':messages})['messages'][-2]
|
| 62 |
+
response = self.react_agent.invoke({'messages':messages})['messages'][-1]
|
| 63 |
+
print('Tool response:',tool_response)
|
| 64 |
return {"messages": [response]}
|
| 65 |
|
| 66 |
+
def chat(self, user_input: str):
|
| 67 |
+
print('Entered into chat')
|
| 68 |
self.messages.append({"role": "user", "content": user_input})
|
| 69 |
+
config = {"configurable": {"thread_id": "2"}}
|
| 70 |
+
response = self.interact_agent.invoke({"messages":self.messages}, config)['messages'][-1].content
|
| 71 |
+
print('The response:',response)
|
| 72 |
self.messages.append({"role": "assistant", "content": response})
|
| 73 |
business_state.interactions.append({'user': user_input, 'agent_response': response})
|
| 74 |
+
return response
|