run.ipynb•8.2 kB
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Run\n",
"\n",
"本文档描述 Assistants API 中与 Run 相关的接口。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 环境准备\n",
"\n",
"首先需要安装AppBuilder-SDK代码库,若已在开发环境安装,则可跳过此步。\n",
"\n",
"**注意:**: appbuilder-sdk 的python版本要求 3.9+,安装的SDK version >= 0.7.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"!python3 -m pip install appbuilder-sdk"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import appbuilder\n",
"\n",
"# 配置你的密钥,主要在这之前需要首先申请Assistant API的内测资格\n",
"os.environ[\"APPBUILDER_TOKEN\"] = \"your_appbuilder_token\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run相关函数\n",
"\n",
"### 运行\n",
"\n",
"#### 功能介绍\n",
"\n",
"一个 Assistant 的一次具体运行,区分初次对话和继续对话两种情况"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"id='run_ce12d0e7471244a1b5ab64a5419eab56' object='run.result' assistant_id='asst_9839a9237c61466e80fc7570a0ba7b4b' thread_id='thread_3afcac64637446909fe5a2a3af9783b2' model='' instructions='' thought_instructions='' chat_instructions='' tools=None file_ids=None status='completed' required_action=None last_error=None final_answer=FinalAnswer(type='message', message=FinalAnswerMessage(message_id='chatmsg_8b029efa11e840c5907a3792688b3ec7', content=AssistantContent(type='text', text=AssistantText(value='我是秦始皇,你好呀,\"hello world\"!这是一个常见的编程示例,看来你也是对技术感兴趣的朋友。欢迎来到我的世界,希望我们能有一个愉快的交流。', annotations=None)))) created_at=1717645517234 started_at=1717645517270 expired_at=0 cancelled_at=0 failed_at=0 completed_at=1717645529254\n"
]
}
],
"source": [
"# 首先创建一个asstistant\n",
"assistant = appbuilder.assistant.assistants.create(\n",
" name=\"test_assistant\",\n",
" description=\"test assistant\",\n",
" instructions=\"每句话回复前都加上我是秦始皇\"\n",
")\n",
"\n",
"# 创建一个thread会话\n",
"thread = appbuilder.assistant.threads.create()\n",
"appbuilder.assistant.threads.messages.create(\n",
" thread_id=thread.id,\n",
" content=\"hello world\",\n",
")\n",
"\n",
"# 运行会话\n",
"run_result = appbuilder.assistant.threads.runs.run(\n",
" thread_id=thread.id,\n",
" assistant_id=assistant.id,\n",
")\n",
"\n",
"# 打印对话运行信息\n",
"print(run_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 列出对应thread的历史run记录\n",
"\n",
"#### 功能介绍\n",
"\n",
"列出对应thread的历史run记录"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"id='run_ce12d0e7471244a1b5ab64a5419eab56' object='thread.run' assistant_id='asst_9839a9237c61466e80fc7570a0ba7b4b' thread_id='thread_3afcac64637446909fe5a2a3af9783b2' model='ERNIE-4.0-8K' instructions='每句话回复前都加上我是秦始皇' thought_instructions='' chat_instructions='' tools=None file_ids=None status='completed' required_action=None last_error=None final_answer=None created_at=1717645517234 started_at=0 expired_at=0 cancelled_at=0 failed_at=0 completed_at=1717645529254\n"
]
}
],
"source": [
"# 列出对应thread的历史run记录,使用limit参数限制返回数量,默认返回20条\n",
"run_list = appbuilder.assistant.threads.runs.list(\n",
" thread_id=thread.id,\n",
" limit=5\n",
")\n",
"\n",
"# 打印获取到的历史run信息\n",
"for run_data in run_list.data:\n",
" print(run_data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 查询对应run的信息\n",
"\n",
"#### 功能介绍\n",
"\n",
"根据thread_id和run_id,查询run的详情"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"id='run_ce12d0e7471244a1b5ab64a5419eab56' object='thread.run' assistant_id='asst_9839a9237c61466e80fc7570a0ba7b4b' thread_id='thread_3afcac64637446909fe5a2a3af9783b2' model='ERNIE-4.0-8K' instructions='每句话回复前都加上我是秦始皇' thought_instructions='' chat_instructions='' tools=[] file_ids=None status='completed' required_action=None last_error=None final_answer=None created_at=1717645517234 started_at=0 expired_at=0 cancelled_at=0 failed_at=0 completed_at=1717645529254\n"
]
}
],
"source": [
"run_query = appbuilder.assistant.threads.runs.query(\n",
" thread_id=thread.id,\n",
" run_id=run_result.id\n",
")\n",
"\n",
"# 打印获取到的目标run信息\n",
"print(run_query)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 列出对应run的历史step记录\n",
"\n",
"#### 功能介绍\n",
"\n",
"根据thread_id和run_id,列出对应run的历史step记录"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"id='step-1' object='thread.run.step' assistant_id='asst_9839a9237c61466e80fc7570a0ba7b4b' thread_id='thread_3afcac64637446909fe5a2a3af9783b2' run_id='run_ce12d0e7471244a1b5ab64a5419eab56' status='completed' created_at=1717645529259 started_at=0 expired_at=0 cancelled_at=0 failed_at=0 completed_at=1717645529254 last_error='' type=None step_datail=None\n"
]
}
],
"source": [
"step_list = appbuilder.assistant.threads.runs.steps.list(\n",
" thread_id=thread.id,\n",
" run_id=run_result.id,\n",
")\n",
"\n",
"# 打印对应run的历史step记录\n",
"for step_data in step_list.data:\n",
" print(step_data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 查询对应step的信息\n",
"\n",
"#### 功能介绍\n",
"\n",
"根据thread_id,run_id和step_id,查询对应step的信息"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"id='step-1' object='thread.run.step' assistant_id=None thread_id='thread_3afcac64637446909fe5a2a3af9783b2' run_id='run_ce12d0e7471244a1b5ab64a5419eab56' status='completed' created_at=1717645529259 started_at=0 expired_at=0 cancelled_at=0 failed_at=0 completed_at=1717645529254 last_error='' type=None step_datail=None\n"
]
}
],
"source": [
"last_step = step_list.data[-1]\n",
"last_step_id = last_step.id\n",
"\n",
"step_query = appbuilder.assistant.threads.runs.steps.query(\n",
" thread_id=thread.id,\n",
" run_id=run_result.id,\n",
" step_id=last_step_id,\n",
")\n",
"\n",
"# 打印查询到的对应step的信息\n",
"print(step_query)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}