بدء استخدام مساعدي Azure OpenAI (معاينة)

يسمح لك مساعدو Azure OpenAI (معاينة) بإنشاء مساعدين الذكاء الاصطناعي مصممين خصيصا لاحتياجاتك من خلال إرشادات مخصصة ومعززة بأدوات متقدمة مثل مترجم التعليمات البرمجية والوظائف المخصصة. في هذه المقالة، نقدم معاينة متعمقة لبدء استخدام واجهة برمجة تطبيقات المساعدين.

إشعار

  • يمكن للبحث عن الملفات استيعاب ما يصل إلى 10000 ملف لكل مساعد - 500 مرة أكثر من ذي قبل. وهو سريع، ويدعم الاستعلامات المتوازية من خلال عمليات البحث متعددة مؤشرات الترابط، وميزات محسنة لإعادة الإرسال وإعادة كتابة الاستعلام.
    • مخزن المتجهات هو كائن جديد في واجهة برمجة التطبيقات. بمجرد إضافة ملف إلى مخزن متجهات، يتم تحليله تلقائيا، وتقسيمه، ودمجه، ويكون جاهزا للبحث فيه. يمكن استخدام مخازن المتجهات عبر المساعدين ومؤشرات الترابط، مما يبسط إدارة الملفات والفوترة.
  • لقد أضفنا دعما للمعلمة tool_choice التي يمكن استخدامها لفرض استخدام أداة معينة (مثل البحث في الملفات أو مترجم التعليمات البرمجية أو وظيفة) في تشغيل معين.

دعم المساعدين

دعم المنطقة والنموذج

يتوفر مترجم التعليمات البرمجية في جميع المناطق التي يدعمها مساعدو Azure OpenAI. تحتوي صفحة النماذج على أحدث المعلومات حول المناطق/النماذج حيث يتم دعم المساعدين حاليا.

إصدارات واجهة برمجة التطبيقات

  • 2024-02-15-preview
  • 2024-05-01-preview

أنواع الملفات المدعومة

تنسيق الملف نوع الملفات مترجم التعليمات البرمجية
جـ. text/x-c
.حزب الشعب الكمبودي text/x-c++
.Csv application/csv
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.html text/html
.جافا text/x-java
.Json تطبيق/json
md. text/markdown
pdf. تطبيق/pdf
.Php text/x-php
pptx. application/vnd.openxmlformats-officedocument.presentationml.presentation
.Py text/x-python
.Py text/x-script.python
.Rb text/x-ruby
.تكس text/x-tex
.txt رسالة نصية/نص عادي
.المغلق نص/css
.Jpeg image/jpeg
‎.jpg image/jpeg
.شبيبه text/javascript
.Gif صورة/gif
‎.png image/png
.القطران application/x-tar
.اتس application/typescript
xlsx. application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xml application/xml أو "text/xml"
.zip تطبيق/مضغوط

الأدوات

تلميح

لقد أضفنا دعما للمعلمة tool_choice التي يمكن استخدامها لفرض استخدام أداة معينة (مثل file_searchأو code_interpreterأو function) في تشغيل معين.

يمكن لمساعد فردي الوصول إلى ما يصل إلى 128 أدوات بما في ذلك مترجم التعليمات البرمجية والبحث في الملفات، ولكن يمكنك أيضا تحديد الأدوات المخصصة الخاصة بك عبر الوظائف.

الملفات

يمكن تحميل الملفات عبر Studio أو برمجيا. المعلمة file_ids مطلوبة لمنح أدوات مثل code_interpreter الوصول إلى الملفات. عند استخدام نقطة نهاية تحميل الملف، يجب أن يكون لديك purpose تعيين إلى المساعدين لاستخدامها مع واجهة برمجة تطبيقات المساعدين.

ملعب المساعدين

نحن نقدم معاينة لملعب المساعدين في دليل التشغيل السريع. يوفر هذا بيئة بدون تعليمات برمجية لاختبار قدرات المساعدين.

مكونات المساعدين

المكون الوصف
المساعد الذكاء الاصطناعي المخصصة التي تستخدم نماذج Azure OpenAI بالاقتران مع الأدوات.
الموضوع جلسة محادثة بين مساعد ومستخدم. تقوم مؤشرات الترابط بتخزين الرسائل ومعالجة الاقتطاع تلقائيا لاحتواء المحتوى في سياق النموذج.
رسالة رسالة تم إنشاؤها بواسطة مساعد أو مستخدم. يمكن أن تتضمن الرسائل نصا وصورا وملفات أخرى. يتم تخزين الرسائل كقوائم على مؤشر الترابط.
تشغيل تنشيط مساعد لبدء التشغيل استنادا إلى محتويات مؤشر الترابط. يستخدم المساعد التكوين الخاص به ورسائل مؤشر الترابط لتنفيذ المهام عن طريق استدعاء النماذج والأدوات. كجزء من Run، يقوم المساعد بإلحاق الرسائل إلى مؤشر الترابط.
خطوة التشغيل قائمة مفصلة بالخطوات التي اتخذها المساعد كجزء من Run. يمكن للمساعد الاتصال بالأدوات أو إنشاء رسائل أثناء تشغيله. يتيح لك فحص خطوات التشغيل فهم كيفية حصول المساعد على نتائجه النهائية.

إعداد مساعدك الأول

إنشاء مساعد

في هذا المثال، سننشئ مساعدا يكتب التعليمات البرمجية لإنشاء مرئيات باستخدام قدرات code_interpreter الأداة. تهدف الأمثلة أدناه إلى التشغيل بشكل تسلسلي في بيئة مثل Jupyter Notebooks.

import os
import json
from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-05-01-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )

# Create an assistant
assistant = client.beta.assistants.create(
    name="Data Visualization",
    instructions=f"You are a helpful AI assistant who makes interesting visualizations based on data." 
    f"You have access to a sandboxed environment for writing and testing code."
    f"When you are asked to create a visualization you should follow these steps:"
    f"1. Write the code."
    f"2. Anytime you write new code display a preview of the code to show your work."
    f"3. Run the code to confirm that it runs."
    f"4. If the code is successful display the visualization."
    f"5. If the code is unsuccessful display the error message and try to revise the code and rerun going through the steps from above again.",
    tools=[{"type": "code_interpreter"}],
    model="gpt-4-1106-preview" #You must replace this value with the deployment name for your model.
)

هناك بعض التفاصيل التي يجب أن تلاحظها من التكوين أعلاه:

  • نقوم بتمكين هذا المساعد للوصول إلى مترجم التعليمات البرمجية باستخدام السطر tools=[{"type": "code_interpreter"}],. وهذا يعطي النموذج حق الوصول إلى بيئة python ذات بيئة الاختبار المعزولة لتشغيل وتنفيذ التعليمات البرمجية للمساعدة في صياغة الاستجابات لسؤال المستخدم.
  • في الإرشادات، نذكر النموذج بأنه يمكنه تنفيذ التعليمات البرمجية. في بعض الأحيان يحتاج النموذج إلى مساعدة في توجيهه نحو الأداة الصحيحة لحل استعلام معين. إذا كنت تعرف أنك تريد استخدام مكتبة معينة لإنشاء استجابة معينة تعرف أنها جزء من مترجم التعليمات البرمجية، فيمكن أن تساعد في توفير إرشادات من خلال قول شيء مثل "استخدام Matplotlib للقيام ب x."
  • نظرا لأن هذا هو Azure OpenAI، يجب أن تتطابق القيمة التي تدخلها مع model=اسم النشر.

بعد ذلك سنقوم بطباعة محتويات المساعد الذي أنشأناه للتو للتأكد من نجاح الإنشاء:

print(assistant.model_dump_json(indent=2))
{
  "id": "asst_7AZSrv5I3XzjUqWS40X5UgRr",
  "created_at": 1705972454,
  "description": null,
  "file_ids": [],
  "instructions": "You are a helpful AI assistant who makes interesting visualizations based on data.You have access to a sandboxed environment for writing and testing code.When you are asked to create a visualization you should follow these steps:1. Write the code.2. Anytime you write new code display a preview of the code to show your work.3. Run the code to confirm that it runs.4. If the code is successful display the visualization.5. If the code is unsuccessful display the error message and try to revise the code and rerun going through the steps from above again.",
  "metadata": {},
  "model": "gpt-4-1106-preview",
  "name": "Data Visualization",
  "object": "assistant",
  "tools": [
    {
      "type": "code_interpreter"
    }
  ]
}

إنشاء مؤشر ترابط

الآن دعونا ننشئ مؤشر ترابط.

# Create a thread
thread = client.beta.threads.create()
print(thread)
Thread(id='thread_6bunpoBRZwNhovwzYo7fhNVd', created_at=1705972465, metadata={}, object='thread')

مؤشر الترابط هو في الأساس سجل جلسة المحادثة بين المساعد والمستخدم. إنه مشابه لصفيف/قائمة الرسائل في استدعاء واجهة برمجة تطبيقات إكمال الدردشة النموذجي. أحد الاختلافات الرئيسية، هو على عكس صفيف رسائل إكمال الدردشة، لا تحتاج إلى تعقب الرموز المميزة مع كل استدعاء للتأكد من أنك متبقية تحت طول سياق النموذج. تقوم مؤشرات الترابط بتجريد تفاصيل الإدارة هذه وستضغط محفوظات مؤشر الترابط حسب الحاجة للسماح للمحادثة بالمتابعة. يتم تحسين قدرة مؤشرات الترابط على تحقيق ذلك من خلال محادثات أكبر عند استخدام أحدث النماذج، والتي لها أطوال سياق أكبر ودعم لأحدث الميزات.

بعد ذلك، قم بإنشاء سؤال المستخدم الأول لإضافته إلى مؤشر الترابط.

# Add a user question to the thread
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Create a visualization of a sinewave"
)

سرد رسائل مؤشر الترابط

thread_messages = client.beta.threads.messages.list(thread.id)
print(thread_messages.model_dump_json(indent=2))
{
  "data": [
    {
      "id": "msg_JnkmWPo805Ft8NQ0gZF6vA2W",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Create a visualization of a sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705972476,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_6bunpoBRZwNhovwzYo7fhNVd"
    }
  ],
  "object": "list",
  "first_id": "msg_JnkmWPo805Ft8NQ0gZF6vA2W",
  "last_id": "msg_JnkmWPo805Ft8NQ0gZF6vA2W",
  "has_more": false
}

تشغيل مؤشر الترابط

run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
  #instructions="New instructions" #You can optionally provide new instructions but these will override the default instructions
)

يمكننا أيضا تمرير معلمة instructions هنا، ولكن هذا من شأنه أن يتجاوز التعليمات الموجودة التي قدمناها بالفعل للمساعد.

استرداد حالة مؤشر الترابط

# Retrieve the status of the run
run = client.beta.threads.runs.retrieve(
  thread_id=thread.id,
  run_id=run.id
)

status = run.status
print(status)
completed

اعتمادا على تعقيد الاستعلام الذي تقوم بتشغيله، قد يستغرق تنفيذ مؤشر الترابط وقتا أطول. في هذه الحالة يمكنك إنشاء حلقة لمراقبة حالة تشغيل مؤشر الترابط مع التعليمات البرمجية مثل المثال أدناه:

import time
from IPython.display import clear_output

start_time = time.time()

status = run.status

while status not in ["completed", "cancelled", "expired", "failed"]:
    time.sleep(5)
    run = client.beta.threads.runs.retrieve(thread_id=thread.id,run_id=run.id)
    print("Elapsed time: {} minutes {} seconds".format(int((time.time() - start_time) // 60), int((time.time() - start_time) % 60)))
    status = run.status
    print(f'Status: {status}')
    clear_output(wait=True)

messages = client.beta.threads.messages.list(
  thread_id=thread.id
) 

print(f'Status: {status}')
print("Elapsed time: {} minutes {} seconds".format(int((time.time() - start_time) // 60), int((time.time() - start_time) % 60)))
print(messages.model_dump_json(indent=2))

عندما يكون in_progress Run أو في حالات أخرى غير محددة، يتم تأمين مؤشر الترابط. عندما يكون مؤشر ترابط مؤمنا، لا يمكن إضافة رسائل جديدة، ولا يمكن إنشاء عمليات تشغيل جديدة.

قائمة رسائل مؤشر الترابط بعد التشغيل

بمجرد أن تشير حالة التشغيل إلى اكتمال ناجح، يمكنك سرد محتويات مؤشر الترابط مرة أخرى لاسترداد استجابة النموذج وأي أدوات:

messages = client.beta.threads.messages.list(
  thread_id=thread.id
)

print(messages.model_dump_json(indent=2))
{
  "data": [
    {
      "id": "msg_M5pz73YFsJPNBbWvtVs5ZY3U",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Is there anything else you would like to visualize or any additional features you'd like to add to the sine wave plot?"
          },
          "type": "text"
        }
      ],
      "created_at": 1705967782,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_AGQHJrrfV3eM0eI9T3arKgYY",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_oJbUanImBRpRran5HSa4Duy4",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "image_file": {
            "file_id": "assistant-1YGVTvNzc2JXajI5JU9F0HMD"
          },
          "type": "image_file"
        },
        {
          "text": {
            "annotations": [],
            "value": "Here is the visualization of a sine wave: \n\nThe wave is plotted using values from 0 to \\( 4\\pi \\) on the x-axis, and the corresponding sine values on the y-axis. I've also added grid lines for easier reading of the plot."
          },
          "type": "text"
        }
      ],
      "created_at": 1705967044,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_8PsweDFn6gftUd91H87K0Yts",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Create a visualization of a sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705966634,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    }
  ],
  "object": "list",
  "first_id": "msg_M5pz73YFsJPNBbWvtVs5ZY3U",
  "last_id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
  "has_more": false
}

استرداد معرف الملف

لقد طلبنا أن يقوم النموذج بإنشاء صورة لموجة جيبية. لتنزيل الصورة، نحتاج أولا إلى استرداد معرف ملف الصور.

data = json.loads(messages.model_dump_json(indent=2))  # Load JSON data into a Python object
image_file_id = data['data'][0]['content'][0]['image_file']['file_id']

print(image_file_id)  # Outputs: assistant-1YGVTvNzc2JXajI5JU9F0HMD

تحميل الصورة

content = client.files.content(image_file_id)

image= content.write_to_file("sinewave.png")

افتح الصورة محليا بمجرد تنزيلها:

from PIL import Image

# Display the image in the default image viewer
image = Image.open("sinewave.png")
image.show()

لقطة شاشة لمترجم التعليمات البرمجية الذي أنشأ موجة جيبية.

طرح سؤال متابعة حول مؤشر الترابط

نظرا لأن المساعد لم يتبع تعليماتنا تماما، وقم بتضمين التعليمات البرمجية التي تم تشغيلها في جزء النص من استجابته، فلنطلب هذه المعلومات بشكل صريح.

# Add a new user question to the thread
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Show me the code you used to generate the sinewave"
)

مرة أخرى سنحتاج إلى تشغيل واسترداد حالة مؤشر الترابط:

run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
  #instructions="New instructions" #You can optionally provide new instructions  but these will override the default instructions
)

# Retrieve the status of the run
run = client.beta.threads.runs.retrieve(
  thread_id=thread.id,
  run_id=run.id
)

status = run.status
print(status)

completed

بمجرد اكتمال حالة التشغيل، سنقوم بإدراج الرسائل في مؤشر الترابط مرة أخرى والتي يجب أن تتضمن الآن الرد على سؤالنا الأخير.

messages = client.beta.threads.messages.list(
  thread_id=thread.id
)

print(messages.model_dump_json(indent=2))
{
  "data": [
    {
      "id": "msg_oaF1PUeozAvj3KrNnbKSy4LQ",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Certainly, here is the code I used to generate the sine wave visualization:\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Generating data for the sinewave\nx = np.linspace(0, 4 * np.pi, 1000)  # Generate values from 0 to 4*pi\ny = np.sin(x)  # Compute the sine of these values\n\n# Plotting the sine wave\nplt.plot(x, y)\nplt.title('Sine Wave')\nplt.xlabel('x')\nplt.ylabel('sin(x)')\nplt.grid(True)\nplt.show()\n```\n\nThis code snippet uses `numpy` to generate an array of x values and then computes the sine for each x value. It then uses `matplotlib` to plot these values and display the resulting graph."
          },
          "type": "text"
        }
      ],
      "created_at": 1705969710,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_oDS3fH7NorCUVwROTZejKcZN",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_moYE3aNwFYuRq2aXpxpt2Wb0",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Show me the code you used to generate the sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705969678,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_M5pz73YFsJPNBbWvtVs5ZY3U",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Is there anything else you would like to visualize or any additional features you'd like to add to the sine wave plot?"
          },
          "type": "text"
        }
      ],
      "created_at": 1705967782,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_AGQHJrrfV3eM0eI9T3arKgYY",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_oJbUanImBRpRran5HSa4Duy4",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "image_file": {
            "file_id": "assistant-1YGVTvNzc2JXajI5JU9F0HMD"
          },
          "type": "image_file"
        },
        {
          "text": {
            "annotations": [],
            "value": "Here is the visualization of a sine wave: \n\nThe wave is plotted using values from 0 to \\( 4\\pi \\) on the x-axis, and the corresponding sine values on the y-axis. I've also added grid lines for easier reading of the plot."
          },
          "type": "text"
        }
      ],
      "created_at": 1705967044,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_8PsweDFn6gftUd91H87K0Yts",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Create a visualization of a sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705966634,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    }
  ],
  "object": "list",
  "first_id": "msg_oaF1PUeozAvj3KrNnbKSy4LQ",
  "last_id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
  "has_more": false
}

لاستخراج الإجابة على سؤالنا الأخير فقط:

data = json.loads(messages.model_dump_json(indent=2))
code = data['data'][0]['content'][0]['text']['value']
print(code)

بالتأكيد، هنا هو التعليمات البرمجية التي استخدمتها لإنشاء تصور موجة جيبية:

import numpy as np
import matplotlib.pyplot as plt

# Generating data for the sinewave
x = np.linspace(0, 4 * np.pi, 1000)  # Generate values from 0 to 4*pi
y = np.sin(x)  # Compute the sine of these values

# Plotting the sine wave
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.grid(True)
plt.show()

الوضع الداكن

دعونا نضيف سؤالا أخيرا إلى مؤشر الترابط لمعرفة ما إذا كان مترجم التعليمات البرمجية يمكنه تبديل المخطط إلى الوضع الداكن بالنسبة لنا.

# Add a user question to the thread
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="I prefer visualizations in darkmode can you change the colors to make a darkmode version of this visualization."
)

# Run the thread
run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
)

# Retrieve the status of the run
run = client.beta.threads.runs.retrieve(
  thread_id=thread.id,
  run_id=run.id
)

status = run.status
print(status)
completed
messages = client.beta.threads.messages.list(
  thread_id=thread.id
)

print(messages.model_dump_json(indent=2))
{
  "data": [
    {
      "id": "msg_KKzOHCArWGvGpuPo0pVZTHgV",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "You're viewing the dark mode version of the sine wave visualization in the image above. The plot is set against a dark background with a cyan colored sine wave for better contrast and visibility. If there's anything else you'd like to adjust or any other assistance you need, feel free to let me know!"
          },
          "type": "text"
        }
      ],
      "created_at": 1705971199,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_izZFyTVB1AlFM1VVMItggRn4",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_30pXFVYNgP38qNEMS4Zbozfk",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "I prefer visualizations in darkmode can you change the colors to make a darkmode version of this visualization."
          },
          "type": "text"
        }
      ],
      "created_at": 1705971194,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_3j31M0PaJLqO612HLKVsRhlw",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "image_file": {
            "file_id": "assistant-kfqzMAKN1KivQXaEJuU0u9YS"
          },
          "type": "image_file"
        },
        {
          "text": {
            "annotations": [],
            "value": "Here is the dark mode version of the sine wave visualization. I've used the 'dark_background' style in Matplotlib and chosen a cyan color for the plot line to ensure it stands out against the dark background."
          },
          "type": "text"
        }
      ],
      "created_at": 1705971123,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_B91erEPWro4bZIfryQeIDDlx",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_FgDZhBvvM1CLTTFXwgeJLdua",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "I prefer visualizations in darkmode can you change the colors to make a darkmode version of this visualization."
          },
          "type": "text"
        }
      ],
      "created_at": 1705971052,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_oaF1PUeozAvj3KrNnbKSy4LQ",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Certainly, here is the code I used to generate the sine wave visualization:\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Generating data for the sinewave\nx = np.linspace(0, 4 * np.pi, 1000)  # Generate values from 0 to 4*pi\ny = np.sin(x)  # Compute the sine of these values\n\n# Plotting the sine wave\nplt.plot(x, y)\nplt.title('Sine Wave')\nplt.xlabel('x')\nplt.ylabel('sin(x)')\nplt.grid(True)\nplt.show()\n```\n\nThis code snippet uses `numpy` to generate an array of x values and then computes the sine for each x value. It then uses `matplotlib` to plot these values and display the resulting graph."
          },
          "type": "text"
        }
      ],
      "created_at": 1705969710,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_oDS3fH7NorCUVwROTZejKcZN",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_moYE3aNwFYuRq2aXpxpt2Wb0",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Show me the code you used to generate the sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705969678,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_M5pz73YFsJPNBbWvtVs5ZY3U",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Is there anything else you would like to visualize or any additional features you'd like to add to the sine wave plot?"
          },
          "type": "text"
        }
      ],
      "created_at": 1705967782,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_AGQHJrrfV3eM0eI9T3arKgYY",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_oJbUanImBRpRran5HSa4Duy4",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "image_file": {
            "file_id": "assistant-1YGVTvNzc2JXajI5JU9F0HMD"
          },
          "type": "image_file"
        },
        {
          "text": {
            "annotations": [],
            "value": "Here is the visualization of a sine wave: \n\nThe wave is plotted using values from 0 to \\( 4\\pi \\) on the x-axis, and the corresponding sine values on the y-axis. I've also added grid lines for easier reading of the plot."
          },
          "type": "text"
        }
      ],
      "created_at": 1705967044,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_8PsweDFn6gftUd91H87K0Yts",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Create a visualization of a sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705966634,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    }
  ],
  "object": "list",
  "first_id": "msg_KKzOHCArWGvGpuPo0pVZTHgV",
  "last_id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
  "has_more": false
}

استخراج معرف ملف الصورة الجديد وتنزيل الصورة وعرضها:

data = json.loads(messages.model_dump_json(indent=2))  # Load JSON data into a Python object
image_file_id = data['data'][0]['content'][0]['image_file']['file_id'] # index numbers can vary if you have had a different conversation over the course of the thread.

print(image_file_id)

content = client.files.content(image_file_id)
image= content.write_to_file("dark_sine.png")

# Display the image in the default image viewer
image = Image.open("dark_sine.png")
image.show()

لقطة شاشة لمترجم التعليمات البرمجية الذي أنشأ موجة جيبية في الوضع الداكن.

مرجع إضافي

تشغيل تعريفات الحالة

الحالة التعريف
queued عند إنشاء عمليات التشغيل لأول مرة أو عند إكمال required_action، يتم نقلها إلى حالة في قائمة الانتظار. يجب أن ينتقلوا على الفور تقريبا إلى in_progress.
in_progress أثناء in_progress، يستخدم المساعد النموذج والأدوات لتنفيذ الخطوات. يمكنك عرض التقدم الذي يتم إحرازه بواسطة Run عن طريق فحص Run Steps.
completed اكتمل التشغيل بنجاح! يمكنك الآن عرض كافة الرسائل التي أضافها المساعد إلى مؤشر الترابط، وجميع الخطوات التي اتخذها التشغيل. يمكنك أيضا متابعة المحادثة عن طريق إضافة المزيد من رسائل المستخدم إلى مؤشر الترابط وإنشاء تشغيل آخر.
requires_action عند استخدام أداة استدعاء الدالة، سينتقل تشغيل إلى حالة required_action بمجرد أن يحدد النموذج أسماء الدالات التي سيتم استدعاؤها ووسيطاتها. يجب عليك بعد ذلك تشغيل هذه الدالات وإرسال المخرجات قبل متابعة التشغيل. إذا لم يتم توفير المخرجات قبل مرور الطابع الزمني expires_at (ما يقرب من 10 دقائق بعد الإنشاء)، فسينتقل التشغيل إلى حالة منتهية الصلاحية.
expired يحدث هذا عندما لا يتم إرسال مخرجات استدعاء الدالة قبل expires_at وتنتهي صلاحية التشغيل. بالإضافة إلى ذلك، إذا استغرقت عمليات التشغيل وقتا طويلا للتنفيذ وتتجاوز الوقت المذكور في expires_at، فستنتهي صلاحية أنظمتنا للتشغيل.
cancelling يمكنك محاولة إلغاء تشغيل in_progress باستخدام نقطة نهاية إلغاء التشغيل. بمجرد نجاح محاولة الإلغاء، تنتقل حالة Run إلى ملغاة. تتم محاولة الإلغاء ولكن غير مضمونة.
cancelled تم إلغاء التشغيل بنجاح.
failed يمكنك عرض سبب الفشل من خلال النظر إلى last_error العنصر في Run. سيتم تسجيل الطابع الزمني للفشل ضمن failed_at.

التعليقات التوضيحية

تختلف التعليقات التوضيحية لرسالة المساعد عن التعليقات التوضيحية لتصفية المحتوى الموجودة في استجابات واجهة برمجة تطبيقات إكمال الدردشة والإكمال. يمكن أن تحدث التعليقات التوضيحية المساعدة داخل صفيف المحتوى للكائن. توفر التعليقات التوضيحية معلومات حول كيفية إضافة تعليق توضيحي للنص في الاستجابات للمستخدم.

عند وجود التعليقات التوضيحية في صفيف محتوى الرسالة، سترى سلاسل فرعية غير مقروءة تم إنشاؤها بواسطة نموذج في النص الذي تحتاج إلى استبداله بالتعليقات التوضيحية الصحيحة. قد تبدو هذه السلاسل شيئا مثل 【13†source】 أو sandbox:/mnt/data/file.csv. فيما يلي قصاصة برمجية Python من OpenAI تستبدل هذه السلاسل بالمعلومات الموجودة في التعليقات التوضيحية.


from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-05-01-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )

# Retrieve the message object
message = client.beta.threads.messages.retrieve(
  thread_id="...",
  message_id="..."
)

# Extract the message content
message_content = message.content[0].text
annotations = message_content.annotations
citations = []

# Iterate over the annotations and add footnotes
for index, annotation in enumerate(annotations):
    # Replace the text with a footnote
    message_content.value = message_content.value.replace(annotation.text, f' [{index}]')

    # Gather citations based on annotation attributes
    if (file_citation := getattr(annotation, 'file_citation', None)):
        cited_file = client.files.retrieve(file_citation.file_id)
        citations.append(f'[{index}] {file_citation.quote} from {cited_file.filename}')
    elif (file_path := getattr(annotation, 'file_path', None)):
        cited_file = client.files.retrieve(file_path.file_id)
        citations.append(f'[{index}] Click <here> to download {cited_file.filename}')
        # Note: File download functionality not implemented above for brevity

# Add footnotes to the end of the message before displaying to user
message_content.value += '\n' + '\n'.join(citations)

التعليق التوضيحي للرسالة ‏‏الوصف
file_citation يتم إنشاء اقتباسات الملفات بواسطة أداة الاسترداد وتحديد المراجع إلى عرض أسعار معين في ملف معين تم تحميله واستخدامه من قبل المساعد لإنشاء الاستجابة.
file_path يتم إنشاء التعليقات التوضيحية لمسار الملف بواسطة أداة code_interpreter وتحتوي على مراجع إلى الملفات التي تم إنشاؤها بواسطة الأداة.

(راجع أيضًا )