Dokumen ini menyediakan contoh penggunaan agen.
Assistant API tidak lagi digunakan. Migrasikan ke Responses API sebagai alternatif. Responses API mencakup beberapa tool bawaan dan mendukung manajemen konteks multi-turn.
Untuk menjalankan contoh berikut, Anda memerlukan Python SDK versi 1.18.0 atau lebih baru dan Java SDK versi 2.14.2 atau lebih baru. Untuk memperbarui Python SDK, jalankan perintah pip install -U dashscope.
Contoh Assistant sederhana
import os
import json
import sys
from http import HTTPStatus
from dashscope import Assistants, Messages, Runs, Threads
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
def create_assistant():
# membuat assistant dengan informasi
assistant = Assistants.create(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Contoh ini menggunakan qwen-max. Anda dapat mengganti nama model sesuai kebutuhan. Untuk daftar model, lihat https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
model='qwen-max',
name='smart helper',
description='A tool helper.',
instructions='You are a helpful assistant.', # noqa E501
)
return assistant
def verify_status_code(res):
if res.status_code != HTTPStatus.OK:
print('Gagal: ')
print(res)
sys.exit(res.status_code)
if __name__ == '__main__':
# membuat assistant
assistant = create_assistant()
print(assistant)
verify_status_code(assistant)
# membuat thread.
thread = Threads.create(
messages=[{
'role': 'user',
'content': 'How to make delicious beef and potato stew?'
}])
print(thread)
verify_status_code(thread)
# membuat run
run = Runs.create(thread.id, assistant_id=assistant.id)
print(run)
verify_status_code(run)
# menunggu run selesai atau requires_action
run_status = Runs.wait(run.id, thread_id=thread.id)
print(run_status)
# mendapatkan pesan thread.
msgs = Messages.list(thread.id)
print(msgs)
print(json.dumps(msgs, ensure_ascii=False, default=lambda o: o.__dict__, sort_keys=True, indent=4))
Contoh Assistant sederhana (keluaran streaming)
Copy
import os
import json
import sys
from http import HTTPStatus
from dashscope import Assistants, Messages, Runs, Threads
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
def create_assistant():
# membuat assistant dengan informasi
assistant = Assistants.create(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Contoh ini menggunakan qwen-max. Anda dapat mengganti nama model sesuai kebutuhan. Untuk daftar model, lihat https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
model='qwen-max',
name='smart helper',
description='A tool helper.',
instructions='You are a helpful assistant.', # noqa E501
)
return assistant
def verify_status_code(res):
if res.status_code != HTTPStatus.OK:
print('Gagal: ')
print(res)
sys.exit(res.status_code)
if __name__ == '__main__':
# membuat assistant
assistant = create_assistant()
print(assistant)
verify_status_code(assistant)
# membuat thread.
thread = Threads.create(
messages=[{
'role': 'user',
'content': 'How to make delicious beef and potato stew?'
}])
print(thread)
verify_status_code(thread)
# membuat run dengan stream.
run_iterator = Runs.create(thread.id,
assistant_id=assistant.id,
stream=True)
# iterasi melalui event dan pesan.
for event, msg in run_iterator:
print(event)
print(msg)
# mendapatkan pesan thread.
msgs = Messages.list(thread.id)
print(msgs)
print(json.dumps(msgs, ensure_ascii=False, default=lambda o: o.__dict__, sort_keys=True, indent=4))
Lakukan pemanggilan fungsi menggunakan Assistant
import os
import json
import sys
from http import HTTPStatus
from dashscope import Assistants, Messages, Runs, Steps, Threads
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
def create_assistant_call_function():
# membuat assistant dengan informasi
assistant = Assistants.create(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Contoh ini menggunakan qwen-max. Anda dapat mengganti nama model sesuai kebutuhan. Untuk daftar model, lihat https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
model='qwen-max',
name='smart helper',
description='A tool helper.',
instructions='You are a helpful assistant. When asked a question, use tools wherever possible.', # noqa E501
tools=[{
'type': 'function',
'function': {
'name': 'big_add',
'description': 'Add to number',
'parameters': {
'type': 'object',
'properties': {
'left': {
'type': 'integer',
'description': 'The left operator'
},
'right': {
'type': 'integer',
'description': 'The right operator.'
}
},
'required': ['left', 'right']
}
}
}],
)
return assistant
def verify_status_code(res):
if res.status_code != HTTPStatus.OK:
print('Gagal: ')
print(res)
sys.exit(res.status_code)
if __name__ == '__main__':
# membuat assistant
assistant = create_assistant_call_function()
print(assistant)
verify_status_code(assistant)
# membuat thread.
thread = Threads.create()
print(thread)
verify_status_code(thread)
# membuat pesan.
message = Messages.create(thread.id, content='Add 87787 to 788988737.')
print(message)
verify_status_code(message)
# membuat run baru untuk menjalankan pesan
message_run = Runs.create(thread.id, assistant_id=assistant.id)
print(message_run)
verify_status_code(message_run)
# mendapatkan status run
run_status = Runs.get(message_run.id, thread_id=thread.id)
print(run_status)
verify_status_code(run_status)
# menunggu run selesai atau requires_action
run_status = Runs.wait(message_run.id, thread_id=thread.id)
print(run_status)
# jika prompt meminta hasil tool, kirimkan hasil tool.
# harus memanggil big_add
if run_status.required_action:
tool_outputs = [{
'output':
'789076524'
}]
run = Runs.submit_tool_outputs(message_run.id,
thread_id=thread.id,
tool_outputs=tool_outputs)
print(run)
verify_status_code(run)
# menunggu run selesai atau requires_action
run_status = Runs.wait(message_run.id, thread_id=thread.id)
print(run_status)
verify_status_code(run_status)
run_steps = Steps.list(run.id, thread_id=thread.id)
print(run_steps)
verify_status_code(run_steps)
# mendapatkan pesan thread.
msgs = Messages.list(thread.id)
print(msgs)
print(json.dumps(msgs, ensure_ascii=False, default=lambda o: o.__dict__, sort_keys=True, indent=4))
Lakukan pemanggilan fungsi menggunakan Assistant (keluaran streaming)
Contoh Java pada bagian ini menunjukkan cara menghasilkan skema JSON, termasuk implementasi bidang deskripsi.# yapf: disable
import os
import json
import sys
from http import HTTPStatus
import dashscope
from dashscope import Assistants, Messages, Runs, Steps, Threads
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
def create_assistant_call_function():
# membuat assistant dengan informasi
assistant = Assistants.create(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Contoh ini menggunakan qwen-max. Anda dapat mengganti nama model sesuai kebutuhan. Untuk daftar model, lihat https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
model='qwen-max',
name='smart helper',
description='A tool helper.',
instructions='You are a helpful assistant. When asked a question, use tools wherever possible.', # noqa E501
tools=[{
'type': 'function',
'function': {
'name': 'big_add',
'description': 'Add to number',
'parameters': {
'type': 'object',
'properties': {
'left': {
'type': 'integer',
'description': 'The left operator'
},
'right': {
'type': 'integer',
'description': 'The right operator.'
}
},
'required': ['left', 'right']
}
}
}],
)
return assistant
def verify_status_code(res):
if res.status_code != HTTPStatus.OK:
print('Gagal: ')
print(res)
sys.exit(res.status_code)
if __name__ == '__main__':
# membuat assistant
assistant = create_assistant_call_function()
print(assistant)
verify_status_code(assistant)
# membuat run
run_iterator = Runs.create_thread_and_run(thread={'messages': [{
'role': 'user',
'content': 'What is transformer? Explain it in simple terms.'
}]}, assistant_id=assistant.id, stream=True)
thread = None
for event, run in run_iterator:
print(event)
print(run)
if event == 'thread.created':
thread = run
verify_status_code(run)
run_status = Runs.get(run.id, thread_id=thread.id)
print(run_status)
verify_status_code(run_status)
# daftar langkah run
run_steps = Steps.list(run.id, thread_id=thread.id)
print(run_steps)
verify_status_code(run_steps)
# membuat pesan.
message = Messages.create(thread.id, content='Add 87787 to 788988737.')
print(message)
verify_status_code(message)
# membuat run baru untuk menjalankan pesan
responses = Runs.create(thread.id, assistant_id=assistant.id, stream=True)
for event, message_run in responses:
print(event)
print(message_run)
verify_status_code(message_run)
# mendapatkan status run
run_status = Runs.get(message_run.id, thread_id=thread.id)
print(run_status)
verify_status_code(run_status)
# jika prompt meminta hasil tool, kirimkan hasil tool.
# harus memanggil big_add
if run_status.required_action:
tool_outputs = [{
'output':
'789076524'
}]
# kirimkan output dengan stream.
responses = Runs.submit_tool_outputs(message_run.id,
thread_id=thread.id,
tool_outputs=tool_outputs,
stream=True)
for event, run in responses:
print(event)
print(run)
verify_status_code(run)
run_status = Runs.get(run.id, thread_id=thread.id)
print(run_status)
verify_status_code(run_status)
run_steps = Steps.list(run.id, thread_id=thread.id)
print(run_steps)
verify_status_code(run_steps)
# mendapatkan pesan thread.
msgs = Messages.list(thread.id)
print(msgs)
print(json.dumps(msgs, ensure_ascii=False, default=lambda o: o.__dict__, sort_keys=True, indent=4))
Panggil tool menggunakan Assistant
Anda mungkin perlu meminta izin untuk menggunakan tool tersebut.
Daftar tool yang didukung
Tool | Fitur |
|---|---|
text_to_image | Memanggil tool text-to-image untuk menghasilkan gambar berdasarkan prompt. |
code_interpreter | Plugin code interpreter. |
Contoh text-to-image
import os
import json
import sys
from http import HTTPStatus
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
def create_assistant():
# membuat assistant dengan informasi
assistant = dashscope.Assistants.create(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Contoh ini menggunakan qwen-max. Anda dapat mengganti nama model sesuai kebutuhan. Untuk daftar model, lihat https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
model='qwen-max',
name='smart helper',
description='A tool helper.',
instructions='You are a helpful assistant. When asked a question, use tools wherever possible.', # noqa E501
tools=[{
'type': 'text_to_image'
}],
)
return assistant
def verify_status_code(res):
if res.status_code != HTTPStatus.OK:
sys.exit(res.status_code)
if __name__ == '__main__':
# membuat assistant
assistant = create_assistant()
print(assistant)
verify_status_code(assistant)
# membuat thread.
thread = dashscope.Threads.create()
print(thread)
verify_status_code(thread)
# membuat pesan.
message = dashscope.Messages.create(thread.id, content='Draw a picture of a cute kitten lying on the sofa.')
print(message)
verify_status_code(message)
# membuat run baru untuk menjalankan pesan
message_run = dashscope.Runs.create(thread.id, assistant_id=assistant.id)
print(message_run)
verify_status_code(message_run)
# mendapatkan status run
run = dashscope.Runs.get(message_run.id, thread_id=thread.id)
print(run)
verify_status_code(run)
# mencetak status run, untuk memverifikasi run telah selesai.
print(run.status)
# menunggu run selesai atau requires_action
run = dashscope.Runs.wait(run.id, thread_id=thread.id)
print(run)
run = dashscope.Runs.get(run.id, thread_id=thread.id)
print(run)
verify_status_code(run)
run_steps = dashscope.Steps.list(run.id, thread_id=thread.id)
print(run_steps)
verify_status_code(run_steps)
# mendapatkan pesan thread.
msgs = dashscope.Messages.list(thread.id)
print(msgs)
print(json.dumps(msgs, default=lambda o: o.__dict__, sort_keys=True, indent=4, ensure_ascii=False))
Panggil plugin code interpreter
import os
import json
import sys
from http import HTTPStatus
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
def create_assistant():
# membuat assistant dengan informasi
assistant = dashscope.Assistants.create(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Contoh ini menggunakan qwen-max. Anda dapat mengganti nama model sesuai kebutuhan. Untuk daftar model, lihat https://www.alibabacloud.com/help/zh/model-studio/getting-started/models
model='qwen-max',
name='smart helper',
description='A tool helper.',
instructions='You are a helpful assistant. When asked a question, use tools wherever possible.', # noqa E501
tools=[{
'type': 'code_interpreter'
}],
)
return assistant
def verify_status_code(res):
if res.status_code != HTTPStatus.OK:
sys.exit(res.status_code)
if __name__ == '__main__':
# membuat assistant
assistant = create_assistant()
print(assistant)
verify_status_code(assistant)
# membuat thread.
thread = dashscope.Threads.create()
print(thread)
verify_status_code(thread)
# membuat pesan.
message = dashscope.Messages.create(thread.id, content='There are some chickens and rabbits in a cage. From the top, there are 100 heads. From the bottom, there are 334 feet. Use a tool to calculate how many chickens and rabbits are in the cage.')
print(message)
verify_status_code(message)
# membuat run baru untuk menjalankan pesan
message_run = dashscope.Runs.create(thread.id, assistant_id=assistant.id)
print(message_run)
verify_status_code(message_run)
# mendapatkan status run
run = dashscope.Runs.get(message_run.id, thread_id=thread.id)
print(run)
verify_status_code(run)
# mencetak status run, untuk memverifikasi run telah selesai.
print(run.status)
# menunggu run selesai atau requires_action
run = dashscope.Runs.wait(run.id, thread_id=thread.id)
print(run)
run = dashscope.Runs.get(run.id, thread_id=thread.id)
print(run)
verify_status_code(run)
run_steps = dashscope.Steps.list(run.id, thread_id=thread.id)
print(run_steps)
verify_status_code(run_steps)
# mendapatkan pesan thread.
msgs = dashscope.Messages.list(thread.id)
print(msgs)
print(json.dumps(msgs, default=lambda o: o.__dict__, sort_keys=True, indent=4, ensure_ascii=False))