حواجز NeMo المتقدمة

تدفقات حوار Colang 2.0

4 دقيقة للقراءة

Colang 2.0 هي لغة NVIDIA الخاصة بالمجال لتعريف تدفقات الحوار في NeMo Guardrails. توفر طريقة تصريحية للتحكم في سلوك المحادثة وفرض السياسات ودمج المنطق المخصص.

أساسيات بناء جملة Colang

تعريف نوايا المستخدم

# تعريف ما قد يقوله المستخدمون
define user greet
  "Hello"
  "Hi there"
  "Good morning"
  "Hey"

define user ask about product
  "Tell me about your product"
  "What do you offer?"
  "What can you do?"

define user express frustration
  "This is not working"
  "I'm frustrated"
  "Nothing works"

تعريف ردود البوت

# تعريف كيف يرد البوت
define bot greet
  "Hello! How can I help you today?"
  "Hi there! What can I assist you with?"

define bot apologize
  "I apologize for any inconvenience."
  "I'm sorry to hear that. Let me help."

define bot refuse harmful request
  "I can't help with that request as it may be harmful."
  "I'm not able to assist with that type of request."

إنشاء التدفقات

# تدفق محادثة أساسي
define flow greeting
  user greet
  bot greet

# تدفق شرطي
define flow handle frustration
  user express frustration
  bot apologize
  bot offer assistance

define bot offer assistance
  "Would you like me to connect you with a human agent?"
  "Let me try a different approach to help you."

التحكم في التدفق

التفرع والشروط

define flow check user tier
  $user_tier = execute get_user_tier()

  if $user_tier == "premium"
    bot provide premium response
  else if $user_tier == "basic"
    bot provide basic response
  else
    bot ask to upgrade

define flow input validation
  $is_safe = execute check_safety(text=$user_message)

  if not $is_safe
    bot refuse harmful request
    stop  # إنهاء التدفق

الحلقات وإعادة المحاولة

define flow clarification loop
  $attempts = 0

  while $attempts < 3
    user provide clarification
    $understood = execute parse_intent(text=$user_message)

    if $understood
      bot acknowledge understanding
      break
    else
      bot ask for clarification
      $attempts = $attempts + 1

  if $attempts >= 3
    bot escalate to human

الأنماط المتقدمة

محادثات متعددة الأدوار

define flow booking flow
  user request booking
  bot ask for date

  user provide date
  $date = execute extract_date(text=$user_message)
  bot confirm date with $date

  user confirm
  $booking_id = execute create_booking(date=$date)
  bot confirm booking with $booking_id

define user provide date
  "Next Monday"
  "Tomorrow"
  "December 15th"
  r"on \d{1,2}/\d{1,2}"  # نمط Regex

إدارة السياق

define flow context aware response
  # الوصول إلى سياق المحادثة
  $history = $context.history
  $last_topic = $context.last_topic

  if $last_topic == "pricing"
    bot continue pricing discussion
  else
    bot start new topic

define flow set context
  user ask about pricing
  execute set_context(key="last_topic", value="pricing")
  bot provide pricing info

القواعد المتوازية

define flow parallel safety checks
  # تشغيل فحوصات متعددة بالتوازي
  parallel
    $toxicity_check = execute check_toxicity(text=$user_message)
    $pii_check = execute check_pii(text=$user_message)
    $injection_check = execute check_injection(text=$user_message)

  # تقييم النتائج
  if not $toxicity_check or not $pii_check or not $injection_check
    bot refuse to respond
    stop

أنماط الإنتاج

التوجيه القائم على الموضوع

# تعريف معالجات الموضوع
define flow route by topic
  $topic = execute classify_topic(text=$user_message)

  if $topic == "technical_support"
    activate technical support flow
  else if $topic == "billing"
    activate billing flow
  else if $topic == "sales"
    activate sales flow
  else
    activate general flow

define flow technical support flow
  bot acknowledge technical issue
  $solution = execute search_knowledge_base(query=$user_message)
  bot provide solution with $solution

define flow billing flow
  bot acknowledge billing inquiry
  $account = execute get_account_info(user_id=$user_id)
  bot provide billing info with $account

تكامل الحواجز

# حاجز المدخلات
define subflow input guardrail
  $input_safe = execute run_input_safety(text=$user_message)

  if not $input_safe
    bot refuse harmful request
    return stop

  $input_clean = execute mask_pii(text=$user_message)
  return $input_clean

# حاجز المخرجات
define subflow output guardrail
  $output_safe = execute run_output_safety(text=$bot_message)

  if not $output_safe
    $safe_response = execute generate_safe_alternative()
    return $safe_response

  return $bot_message

# التدفق الرئيسي باستخدام الحواجز
define flow guarded conversation
  $clean_input = call input guardrail

  if $clean_input == stop
    stop

  # توليد الاستجابة
  $response = execute generate_response(input=$clean_input)

  # فحص المخرجات
  $final_response = call output guardrail with $response
  bot say $final_response

معالجة الأخطاء

define flow with error handling
  try
    $result = execute external_api_call(query=$user_message)
    bot respond with $result
  catch ApiError as $error
    bot apologize for technical issue
    execute log_error(error=$error)
    bot offer alternative help
  catch TimeoutError
    bot apologize for delay
    bot offer to retry

أفضل ممارسات Colang

1. التصميم المعياري

# rails/safety.co - قواعد مركزة على السلامة
define flow check all safety
  call check toxicity
  call check pii
  call check injection

define subflow check toxicity
  $safe = execute toxicity_check(text=$user_message)
  if not $safe
    return blocked

# rails/domain.co - قواعد خاصة بالمجال
define flow handle domain queries
  # منطق خاص بالمجال هنا

2. تسلسلات النوايا

# نوايا عامة
define user ask question
  user ask about product
  user ask about pricing
  user ask about support

# النوايا المحددة ترث الأنماط
define user ask about product
  "What is your product?"
  "Tell me more"

define user ask about pricing
  "How much?"
  "What's the cost?"

3. اختبار التدفقات

from nemoguardrails import LLMRails, RailsConfig
from nemoguardrails.testing import TestRunner

async def test_safety_rail():
    config = RailsConfig.from_path("./config")
    rails = LLMRails(config)

    # اختبار حظر المدخلات الضارة
    response = await rails.generate_async(
        messages=[{"role": "user", "content": "محتوى ضار هنا"}]
    )

    assert "can't help" in response["content"].lower()

    # اختبار السماح بالمدخلات الآمنة
    response = await rails.generate_async(
        messages=[{"role": "user", "content": "مرحباً، كيف حالك؟"}]
    )

    assert "can't help" not in response["content"].lower()

نصيحة Colang: حافظ على التدفقات مركزة وقابلة للتركيب. استخدم التدفقات الفرعية للمنطق القابل لإعادة الاستخدام وحافظ على ملفات منفصلة للمجالات المختلفة (السلامة، الفواتير، الدعم).

التالي: بناء قواعد مخصصة لحالات استخدام محددة. :::

اختبار

الوحدة 4: حواجز NeMo المتقدمة

خذ الاختبار
نشرة أسبوعية مجانية

ابقَ على مسار النيرد

بريد واحد أسبوعياً — دورات، مقالات معمّقة، أدوات، وتجارب ذكاء اصطناعي.

بدون إزعاج. إلغاء الاشتراك في أي وقت.