Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_openai import ChatOpenAI
|
| 2 |
+
from langchain.agents import create_openai_functions_agent, AgentExecutor, Tool
|
| 3 |
+
from langchain.prompts import ChatPromptTemplate
|
| 4 |
+
from langchain.memory import ConversationBufferMemory
|
| 5 |
+
import json
|
| 6 |
+
from tools import rag_tool, support_ticket_tool, store_ticket_info
|
| 7 |
+
from config import llm, memory
|
| 8 |
+
|
| 9 |
+
tools = [
|
| 10 |
+
Tool(
|
| 11 |
+
name="KnowledgeBase",
|
| 12 |
+
func=rag_tool,
|
| 13 |
+
coroutine=rag_tool,
|
| 14 |
+
description="Use this to answer user questions about the documents."
|
| 15 |
+
),
|
| 16 |
+
Tool(
|
| 17 |
+
name="SupportTicket",
|
| 18 |
+
func=support_ticket_tool,
|
| 19 |
+
coroutine=support_ticket_tool,
|
| 20 |
+
description="This tool opens a support ticket in the system after you get information about the user"
|
| 21 |
+
),
|
| 22 |
+
Tool(
|
| 23 |
+
name="collect_data",
|
| 24 |
+
func=store_ticket_info,
|
| 25 |
+
coroutine=store_ticket_info,
|
| 26 |
+
description="""Use this to collect user info for support ticket.
|
| 27 |
+
Pass the full conversation text that contains issue details and email."""
|
| 28 |
+
)
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
# إنشاء الـ prompt بالطريقة الصحيحة
|
| 32 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 33 |
+
("system", """
|
| 34 |
+
أنت "آدم"، موظف دعم فني ودود لشركة Valetax.
|
| 35 |
+
عرف نفسك مع اول محادثة
|
| 36 |
+
شخصيتك: ودود، بشوش، وتكتب بأسلوب محادثة طبيعي.
|
| 37 |
+
|
| 38 |
+
دورك ينقسم إلى حالتين:
|
| 39 |
+
|
| 40 |
+
1. **استفسارات عامة:**
|
| 41 |
+
- استخدم KnowledgeBase للإجابة عن الأسئلة
|
| 42 |
+
|
| 43 |
+
2. **فتح شكوى / تذكرة دعم:**
|
| 44 |
+
- اجمع المعلومات (المشكلة + الإيميل + المستندات)
|
| 45 |
+
- استخدم collect_data مع كل ما قاله العميل
|
| 46 |
+
- ثم استخدم SupportTicket لفتح التذكرة
|
| 47 |
+
|
| 48 |
+
⚠️ استخدم الـ tools دائماً - لا ترد بدون tools
|
| 49 |
+
"""),
|
| 50 |
+
("placeholder", "{chat_history}"),
|
| 51 |
+
("human", "{input}"),
|
| 52 |
+
("placeholder", "{agent_scratchpad}")
|
| 53 |
+
])
|
| 54 |
+
|
| 55 |
+
# إنشاء الـ agent
|
| 56 |
+
agent = create_openai_functions_agent(llm, tools, prompt)
|
| 57 |
+
|
| 58 |
+
# إنشاء الـ executor
|
| 59 |
+
agent_executor = AgentExecutor(
|
| 60 |
+
agent=agent,
|
| 61 |
+
tools=tools,
|
| 62 |
+
memory=memory,
|
| 63 |
+
verbose=True,
|
| 64 |
+
handle_parsing_errors=True
|
| 65 |
+
)
|