| import os
|
| from dotenv import load_dotenv
|
|
|
|
|
| from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
|
| from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| from langchain_classic.agents import create_tool_calling_agent, AgentExecutor
|
|
|
|
|
| from langchain_community.chat_message_histories import ChatMessageHistory
|
| from langchain_core.runnables.history import RunnableWithMessageHistory
|
|
|
|
|
| from backend import tool
|
|
|
| load_dotenv()
|
|
|
|
|
|
|
| llm_obj=HuggingFaceEndpoint(model="Qwen/Qwen3-Coder-30B-A3B-Instruct")
|
| chat_model=ChatHuggingFace(llm=llm_obj)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| tools=[
|
| tool.search_knowledge_base,
|
| tool.create_support_ticket,
|
| tool.escalate_to_human
|
| ]
|
|
|
|
|
| prompt = ChatPromptTemplate.from_messages([
|
| ("system", """You are a highly capable customer support agent for SmartSupport.
|
| Your main goal is to help customers quickly and accurately.
|
|
|
| RULES:
|
| 1. ALWAYS use the `search_knowledge_base` tool FIRST when a user asks a question about policies, shipping, or products.
|
| 2. Do NOT make up answers. If the knowledge base does not contain the answer, tell the user.
|
| 3. If the user asks to open a ticket, or if you cannot solve their issue, use the `create_support_ticket` tool.
|
| 4. If the user is angry or asks for a human, use the `escalate_to_human` tool.
|
| 5. Keep your final answers polite, professional, and concise.
|
| """),
|
|
|
| MessagesPlaceholder(variable_name="chat_history"),
|
| ("user", "{input}"),
|
|
|
| MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| ])
|
|
|
| agent_calling=create_tool_calling_agent(
|
| llm=chat_model,
|
| tools=tools,
|
| prompt=prompt
|
| )
|
|
|
|
|
| agent_executor=AgentExecutor(
|
| agent=agent_calling,
|
| tools=tools,
|
| verbose=True
|
| )
|
|
|
|
|
| store = {}
|
|
|
| def get_session_history(session_id: str):
|
| if session_id not in store:
|
| store[session_id] = ChatMessageHistory()
|
| return store[session_id]
|
|
|
|
|
| agent_with_memory = RunnableWithMessageHistory(
|
| agent_executor,
|
| get_session_history,
|
| input_messages_key="input",
|
| history_messages_key="chat_history",
|
| )
|
|
|
| def run_agent(message: str, session_id: str) -> str:
|
| """
|
| Main function to be called by your FastAPI backend or Streamlit UI.
|
| Takes a user message and a session ID, returns the AI's response.
|
| """
|
| response = agent_with_memory.invoke(
|
| {"input": message},
|
| config={"configurable": {"session_id": session_id}}
|
| )
|
| return response["output"]
|
|
|
|
|
|
|
|
|
|
|
| if __name__ == "__main__":
|
| print("π€ SmartSupport AI Agent Initialized! Type 'exit' to quit.")
|
| print("-" * 50)
|
|
|
| test_session_id = "terminal_user_1"
|
|
|
| while True:
|
| user_input = input("\nYou: ")
|
| if user_input.lower() in ['exit', 'quit']:
|
| break
|
|
|
| print("\nThinking...")
|
| answer = run_agent(user_input, test_session_id)
|
| print(f"\nAgent: {answer}") |