Spaces:
Sleeping
Sleeping
Commit
·
3f79d7a
1
Parent(s):
4aa04b3
Implemented the business interaction after introduction
Browse files
my_agent/utils/business_interaction.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 |
+
|
| 13 |
+
# State model
|
| 14 |
+
class State(BaseModel):
|
| 15 |
+
interactions: Optional[list] = []
|
| 16 |
+
model_config = ConfigDict(arbitrary_types_allowed=True)
|
| 17 |
+
|
| 18 |
+
# Global business state (shared)
|
| 19 |
+
business_state = State()
|
| 20 |
+
|
| 21 |
+
class BusinessInteractionChatbot2:
|
| 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.add_edge(START, "chatbot")
|
| 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 |
+
response = self.llm.invoke(messages)
|
| 41 |
+
return {"messages": [response]}
|
| 42 |
+
|
| 43 |
+
def chat(self, user_input: str , business_details: dict={}):
|
| 44 |
+
self.messages.append({"role": "user", "content": user_input})
|
| 45 |
+
config = {"configurable": {"thread_id": "1"}}
|
| 46 |
+
response = self.interact_agent.invoke({"messages": [user_input + str(business_details)]}, config)['messages'][-1].content
|
| 47 |
+
self.messages.append({"role": "assistant", "content": response})
|
| 48 |
+
business_state.interactions.append({'user': user_input, 'agent_response': response})
|
| 49 |
+
return response
|