تنسيق الوكلاء: الروتينات والتسليمات

عند العمل مع نماذج اللغة، يتطلب تحقيق أداء قوي غالباً موجهاً جيداً والأدوات المناسبة. ومع ذلك، يمكن أن تصبح إدارة تدفقات فريدة متعددة معقدة. يقدم هذا الدليل مفاهيم الروتينات والتسليمات، موضحاً كيف يمكن استخدامها لتنسيق وكلاء متعددين بطريقة بسيطة وقوية وقابلة للتحكم.
إعداد البيئة
للبدء، تأكد من توفر الاستيرادات الضرورية وإعداد العميل.
from openai import OpenAI
from pydantic import BaseModel
from typing import Optional
import json
client = OpenAI()فهم الروتينات
"الروتين" هو مجموعة من الخطوات المحددة باللغة الطبيعية، ممثلة بموجه النظام، إلى جانب الأدوات اللازمة لإكمالها. على سبيل المثال، قد يتضمن روتين خدمة العملاء فرز مشكلة المستخدم، أو اقتراح حل، أو تقديم استرداد.
system_message = (
"You are a customer support agent for ACME Inc."
"Always answer in a sentence or less."
"Follow the following routine with the user:"
"1. First, ask probing questions and understand the user's problem deeper.
"
" - unless the user has already provided a reason.
"
"2. Propose a fix (make one up).
"
"3. ONLY if not satisfied, offer a refund.
"
"4. If accepted, search for the ID and then execute refund."
""
)تنفيذ الروتينات
لتنفيذ روتين، قم بتنفيذ حلقة تتعامل مع إدخال المستخدم، وتضيف الرسائل، وتستدعي النموذج.
def run_full_turn(system_message, messages):
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": system_message}] + messages,
)
message = response.choices[0].message
messages.append(message)
if message.content: print("Assistant:", message.content)
return messageالتعامل مع استدعاءات الدوال
تتطلب النماذج تنسيق الدوال كمخطط دالة. حدد دالة مساعدة لتحويل دوال Python إلى مخطط الدالة المقابل.
import inspect
def function_to_schema(func) -> dict:
type_map = {
str: "string",
int: "integer",
float: "number",
bool: "boolean",
list: "array",
dict: "object",
type(None): "null",
}
try:
signature = inspect.signature(func)
except ValueError as e:
raise ValueError(
f"Failed to get signature for function {func.__name__}: {str(e)}"
)
parameters = {}
for param in signature.parameters.values():
try:
param_type = type_map.get(param.annotation, "string")
except KeyError as e:
raise KeyError(
f"Unknown type annotation {param.annotation} for parameter {param.name}: {str(e)}"
)
parameters[param.name] = {"type": param_type}
required = [
param.name
for param in signature.parameters.values()
if param.default == inspect._empty
]
return {
"type": "function",
"function": {
"name": func.__name__,
"description": (func.__doc__ or "").strip(),
"parameters": {
"type": "object",
"properties": parameters,
"required": required,
},
},
}تنفيذ التسليمات
تتيح التسليمات لوكيل واحد نقل محادثة نشطة إلى وكيل آخر، على غرار التحويل أثناء مكالمة هاتفية. حدد فئة أساسية لوكيل وعدّل الكود لدعم تسليمات الوكلاء.
class Agent(BaseModel):
name: str = "Agent"
model: str = "gpt-4o-mini"
instructions: str = "You are a helpful Agent"
tools: list = []
def run_full_turn(agent, messages):
current_agent = agent
num_init_messages = len(messages)
messages = messages.copy()
while True:
tool_schemas = [function_to_schema(tool) for tool in current_agent.tools]
tools = {tool.__name__: tool for tool in current_agent.tools}
response = client.chat.completions.create(
model=agent.model,
messages=[{"role": "system", "content": current_agent.instructions}]
+ messages,
tools=tool_schemas or None,
)
message = response.choices[0].message
messages.append(message)
if message.content: # print agent response
print(f"{current_agent.name}:", message.content)
if not message.tool_calls: # if finished handling tool calls, break
break
for tool_call in message.tool_calls:
result = execute_tool_call(tool_call, tools, current_agent.name)
if type(result) is Agent: # if agent transfer, update current agent
current_agent = result
result = (
f"Transferred to {current_agent.name}. Adopt persona immediately."
)
result_message = {
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
}
messages.append(result_message)
return Response(agent=current_agent, messages=messages[num_init_messages:])الخلاصة
من خلال تنفيذ الروتينات والتسليمات، يمكنك إدارة وكلاء متعددين بفعالية، مما يسمح بالتعامل الديناميكي مع المهام وتحسين الكفاءة. يوفر هذا النهج إطاراً قوياً لتنسيق سير العمل المعقدة.
المرجع: Orchestrating Agents: Routines and Handoffs بواسطة Ilan Bigio، OpenAI.
ناقش مشروعك معنا
نحن هنا للمساعدة في احتياجات تطوير الويب الخاصة بك. حدد موعدًا لمناقشة مشروعك وكيف يمكننا مساعدتك.
دعنا نجد أفضل الحلول لاحتياجاتك.
مقالات ذات صلة

دمج نماذج التفكير من OpenAI في طلبات السحب على GitHub
تعلم كيفية دمج نماذج التفكير من OpenAI في سير عمل طلبات السحب على GitHub لمراجعة الكود تلقائياً للجودة والأمان والامتثال لمعايير المؤسسة.

بناء مفسر الأكواد الخاص بك مع توليد الأدوات الديناميكية
تعلم كيفية إنشاء مفسر أكواد مخصص باستخدام التوليد الديناميكي للأدوات والتنفيذ مع o3-mini، مما يتيح حل المشاكل بمرونة وقابلية للتكيف.

بناء مفسر أكواد مخصص لوكلاء نماذج اللغة الكبيرة
تعلم كيفية إنشاء مفسر أكواد مخصص لوكلاء نماذج اللغة الكبيرة (LLM)، مما يتيح استدعاء الأدوات الديناميكي وتنفيذ الأكواد المعزول لتعزيز المرونة والأمان.