Spaces:
Sleeping
Sleeping
Commit
·
ff17e37
1
Parent(s):
06c1ccb
Added the state update in business interaction
Browse files
business_interaction_agent/agent.py
CHANGED
|
@@ -2,44 +2,15 @@ from langchain_groq import ChatGroq
|
|
| 2 |
from langgraph.graph import StateGraph, MessagesState, START, END
|
| 3 |
from langgraph.checkpoint.memory import MemorySaver
|
| 4 |
from langgraph.prebuilt import create_react_agent
|
| 5 |
-
from .utils.state import State
|
| 6 |
# from .utils.nodes import business_interaction_node, cleanup_messages
|
| 7 |
from utils.models_loader import llm
|
| 8 |
from langchain_core.messages import SystemMessage
|
| 9 |
-
from .utils.prompts import business_retrieval_prompt
|
| 10 |
from .utils.utils import manual_retrieval
|
| 11 |
|
| 12 |
business_state = State()
|
| 13 |
|
| 14 |
-
# class BusinessInteractionChatbot:
|
| 15 |
-
# def __init__(self):
|
| 16 |
-
# self.messages = []
|
| 17 |
-
# self.business_details = None
|
| 18 |
-
# self.react_agent = create_react_agent(model=llm, tools=[])
|
| 19 |
-
# self.memory = MemorySaver()
|
| 20 |
-
# self.workflow = self._initialize_workflow()
|
| 21 |
-
# self.interact_agent = self.workflow.compile(checkpointer=self.memory)
|
| 22 |
-
|
| 23 |
-
# def _initialize_workflow(self):
|
| 24 |
-
# workflow = StateGraph(MessagesState)
|
| 25 |
-
# workflow.add_node("chatbot", lambda state: business_interaction_node(
|
| 26 |
-
# state, llm, self.react_agent, self.messages, self.business_details))
|
| 27 |
-
# workflow.add_node("remove_message", lambda state: cleanup_messages(self.messages))
|
| 28 |
-
# workflow.add_edge(START, "chatbot")
|
| 29 |
-
# workflow.add_edge("chatbot", "remove_message")
|
| 30 |
-
# workflow.add_edge("chatbot", END)
|
| 31 |
-
# return workflow
|
| 32 |
-
|
| 33 |
-
# def chat(self, user_input: str, business_details: dict):
|
| 34 |
-
# self.business_details = business_details
|
| 35 |
-
# self.messages.append({"role": "user", "content": user_input})
|
| 36 |
-
# config = {"configurable": {"thread_id": "2"}}
|
| 37 |
-
# response = self.interact_agent.invoke({"messages": self.messages}, config)['messages'][-1].content
|
| 38 |
-
# self.messages.append({"role": "assistant", "content": response})
|
| 39 |
-
# business_state.interactions.append({'user': user_input, 'agent_response': response})
|
| 40 |
-
# return response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
class BusinessInteractionChatbot:
|
| 44 |
def __init__(self):
|
| 45 |
self.messages = []
|
|
@@ -65,25 +36,41 @@ class BusinessInteractionChatbot:
|
|
| 65 |
if len(self.messages) > 4:
|
| 66 |
print('satisfied...')
|
| 67 |
self.messages = self.messages[2:]
|
|
|
|
| 68 |
|
| 69 |
def _call_model(self, state):
|
| 70 |
print('Entered into callmodel')
|
| 71 |
-
retrievals = manual_retrieval(str([msg['content'] for msg in self.messages if msg['role'] == 'user']),
|
| 72 |
-
template = business_retrieval_prompt(str([msg['content'] for msg in self.messages if msg['role'] == 'user']),retrievals,str(
|
| 73 |
messages = [SystemMessage(content=template)] + state["messages"]
|
| 74 |
backup_response = self.react_agent.invoke({'messages':messages})['messages'][-1]
|
| 75 |
print('Backup response:',backup_response.content)
|
| 76 |
return {"messages": [backup_response.content]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
def chat(self, user_input: str, business_details:dict):
|
| 80 |
print('Entered into chat')
|
| 81 |
-
|
| 82 |
self.messages.append({"role": "user", "content": f'{user_input}'})
|
|
|
|
|
|
|
| 83 |
config = {"configurable": {"thread_id": "2"}}
|
| 84 |
response = self.interact_agent.invoke({"messages":self.messages}, config)['messages'][-1].content
|
| 85 |
print('The response:',response)
|
| 86 |
self.messages.append({"role": "assistant", "content": response})
|
| 87 |
print('The message_history:',self.messages)
|
| 88 |
business_state.interactions.append({'user': user_input, 'agent_response': response})
|
| 89 |
-
return response
|
|
|
|
| 2 |
from langgraph.graph import StateGraph, MessagesState, START, END
|
| 3 |
from langgraph.checkpoint.memory import MemorySaver
|
| 4 |
from langgraph.prebuilt import create_react_agent
|
| 5 |
+
from .utils.state import State,StateUpdateFormatter
|
| 6 |
# from .utils.nodes import business_interaction_node, cleanup_messages
|
| 7 |
from utils.models_loader import llm
|
| 8 |
from langchain_core.messages import SystemMessage
|
| 9 |
+
from .utils.prompts import business_retrieval_prompt, check_state_update_prompt
|
| 10 |
from .utils.utils import manual_retrieval
|
| 11 |
|
| 12 |
business_state = State()
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
class BusinessInteractionChatbot:
|
| 15 |
def __init__(self):
|
| 16 |
self.messages = []
|
|
|
|
| 36 |
if len(self.messages) > 4:
|
| 37 |
print('satisfied...')
|
| 38 |
self.messages = self.messages[2:]
|
| 39 |
+
|
| 40 |
|
| 41 |
def _call_model(self, state):
|
| 42 |
print('Entered into callmodel')
|
| 43 |
+
retrievals = manual_retrieval(str([msg['content'] for msg in self.messages if msg['role'] == 'user']),business_state.business_details)
|
| 44 |
+
template = business_retrieval_prompt(str([msg['content'] for msg in self.messages if msg['role'] == 'user']),retrievals,str(business_state.business_details))
|
| 45 |
messages = [SystemMessage(content=template)] + state["messages"]
|
| 46 |
backup_response = self.react_agent.invoke({'messages':messages})['messages'][-1]
|
| 47 |
print('Backup response:',backup_response.content)
|
| 48 |
return {"messages": [backup_response.content]}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def check_state_update(self):
|
| 52 |
+
business_state.business_details
|
| 53 |
+
messages = str([msg['content'] for msg in self.messages if msg['role'] == 'user'])
|
| 54 |
+
template = check_state_update_prompt(business_state.business_details,messages)
|
| 55 |
+
messages = [SystemMessage(content=template)]
|
| 56 |
+
response = llm.with_structured_output(StateUpdateFormatter).invoke(messages)
|
| 57 |
+
# response= llm.invoke(messages)
|
| 58 |
+
print('Response of state check:',response)
|
| 59 |
+
return response.model_dump()
|
| 60 |
+
|
| 61 |
+
|
| 62 |
|
| 63 |
|
| 64 |
def chat(self, user_input: str, business_details:dict):
|
| 65 |
print('Entered into chat')
|
| 66 |
+
business_state.business_details=business_details
|
| 67 |
self.messages.append({"role": "user", "content": f'{user_input}'})
|
| 68 |
+
business_state.business_details=self.check_state_update()
|
| 69 |
+
|
| 70 |
config = {"configurable": {"thread_id": "2"}}
|
| 71 |
response = self.interact_agent.invoke({"messages":self.messages}, config)['messages'][-1].content
|
| 72 |
print('The response:',response)
|
| 73 |
self.messages.append({"role": "assistant", "content": response})
|
| 74 |
print('The message_history:',self.messages)
|
| 75 |
business_state.interactions.append({'user': user_input, 'agent_response': response})
|
| 76 |
+
return response , business_state.business_details
|