เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การกำหนด Prompt ใน MCP Server

เราจะเพิ่ม prompt ที่นำกลับมาใช้ซ้ำได้ใน MCP server สำหรับแปลงสกุลเงิน เพื่อให้ LLM รู้วิธีจัดการคำขอแปลงสกุลเงิน และรู้ว่าควรถามข้อมูลเพิ่มเติมในกรณีใด (เช่น ไม่ระบุจำนวนเงินหรือรหัสสกุลเงิน) ซึ่งช่วยลดภาระของผู้ใช้และทำให้ assistant ทำงานได้อย่างน่าเชื่อถือมากขึ้น

สร้าง instance ของ MCP server ไว้แล้ว ให้เพิ่ม prompt โดยใช้ decorator @mcp.prompt() และเขียนฟังก์ชันที่ return template ของ prompt พร้อมแนบคำขอของผู้ใช้ไว้ด้วย

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Model Context Protocol (MCP) เบื้องต้น

ดูคอร์ส

คำแนะนำการฝึกหัด

  • ใช้ @mcp.prompt() ตกแต่งฟังก์ชัน และตั้งค่า title เป็น "Currency Conversion"
  • กำหนดฟังก์ชันที่รับ currency_request: str และ return string ที่มี template ของ prompt พร้อมคำขอของผู้ใช้
  • เรียกใช้ฟังก์ชัน prompt ด้วยคำขอตัวอย่าง แล้ว print ผลลัพธ์เพื่อตรวจสอบว่าทำงานได้ถูกต้อง

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Define a prompt for currency conversion
____
def ____(currency_request: str) -> str:
    return f"""You are a currency conversion assistant.

Your task is to:
1. Extract the amount and source currency from the user's natural language input.
2. Identify the target currency.
3. Use the conversion tool to convert the amount.

Rules:
- If the amount or currencies are ambiguous or missing, ask the user for clarification.
- Use only supported currency codes (e.g., USD, EUR, GBP).

User's currency conversion request: {currency_request}"""

# Test the prompt function
print(____("100 USD to EUR"))
แก้ไขและรันโค้ด