דילוג לתוכן
NotHolyתיעוד

מדריכים

קריאה לפונקציות

אפשר לתת למודל כלים משלך. הוא מחזיר קריאות עם ארגומנטים ב-JSON, הקוד שלך מריץ אותן ושולח בחזרה את התוצאות.

להציג כ-Markdown

מגדירים כלים עם JSON Schema. המודל יכול לענות בטקסט או בקריאה אחת או יותר לכלים. את הכלי מריצים בצד שלך, ואת התוצאה שולחים בהודעת tool.

import json
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.notholy.com/v1", api_key=os.environ["NOTHOLY_API_KEY"])
tools = [{
    "type": "function",
    "function": {
        "name": "get_cvss",
        "description": "Return the CVSS base score of a CVE",
        "parameters": {"type": "object", "properties": {"id": {"type": "string"}}, "required": ["id"]},
    },
}]
messages = [{"role": "user", "content": "What is the CVSS score of CVE-2021-44228?"}]
r = client.chat.completions.create(model="notholy-specter", messages=messages, tools=tools)
msg = r.choices[0].message
if msg.tool_calls:
    messages.append(msg)
    for call in msg.tool_calls:
        args = json.loads(call.function.arguments)
        messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps({"id": args["id"], "cvss": 10.0})})
    r = client.chat.completions.create(model="notholy-specter", messages=messages, tools=tools)
print(r.choices[0].message.content)

הערות

  • נתמכים רק כלים מסוג function. כלי שרת מובנים לא מורצים.
  • tool_choice מקבל auto, none, required או שם של פונקציה.
  • בסטרימינג, קריאה לכלי מגיעה שלמה בכל פורמט: delta אחד עם כל הארגומנטים.
  • תוצאות הכלים הן תוכן שבשליטתך. למה שכלי מביא מהעולם החיצוני יש להתייחס כאל נתונים.

הבא