消息注解Message annotationsAI 助手返回的消息可能会包含 Message annotations,存储在content 的对象中 。注解(Annotations)提供了如何解析消息的信息;
目前支持两种不同的注解:
- file_citation: 该注解是 retrieval工具提供的,它定了了参考的内容的来源 。
- file_path:该注解是 code_interpreter工具提供,指定了参考文件的地址目录 。
# Retrieve the message object import openai as client 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 = https://www.isolves.com/it/ai/2024-01-03/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 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) 执行(Run)和执行步骤(Run Steps)
当我们需要AI Assistant 对用户问题进行回复,,需要创建一个Run对象,该对象包含了两个参数:
- thread_id: 之前创建的Thread的id
- assistant_id: 该AI Assistant 的id
run = client.beta.threads.runs.create( thread_id=thread.id, assistant_id=assistant.id, model="gpt-4-1106-preview", instructions="additional instructions", tools=[{"type": "code_interpreter"}, {"type": "retrieval"}] )
注意:file_ids不可以在执行中进行修改,需要使用修改Assistant的API进行修改
执行的生命周期(Run lifecycle)
Run对象有不同的状态

文章插图

文章插图
获取进度 Polling for updates
为了可以及时获取执行的进度,可以设置定时获取 retrieve the Run 执行状态 。你可以获取每次 Run 的执行状态,从而决定下一步该做什么 。目前还不支持 streaming 的输出(2023-11-12日)
对话锁 Thread locks当执行对象 Run处于进行中 in_progress的状态的时候 , 对话Thread 对象会被锁上,这意味着:
- 新消息不能加到对话中
- 新的执行Run 不能被创建
当执行进入 in_progress后,会有下面四种可能的状态,分别是
- 完成
- 失败
- 取消
- 超时

文章插图
执行步骤Run steps可能耗时比较长,为了能了解执行的细节 , 我们可以通过 step_details这个字段进行观察,包含了两种类型的内容:
- message_creation: 展示了产生了什么消息
- tool_calls: 展示了使用了什么tool
- 支持流式输出
- 支持通知的功能,可以在无需轮询的情况下共享对象状态更新
- 支持 DALL·E 作为工具
- 支持用户上传图片
代码解释器能够通过代码运行,完成多种困难的任务,并且能解决很多GPT地薄弱能力 , 例如数学能力等 。Code Interpreter 支持如果发现自己的代码执行失败了,会通过多轮重试,直到执行成功 。
开启Code Interpreter
如果需要开启 Code Interpreter 能力,只需要在tools参数中加入 Code Interpreter, 如 tools=[{"type": "code_interpreter"}]即可 。
推荐阅读
- C++的面向对象编程:深入解析与理解
- 深入掌握Java线程池调度策略,优化任务执行
- 探讨网站转化率与SEO优化之间的关系及提升策略
- 深入浅出Kafka:高可用、顺序消费及幂等性
- 人脸识别技术:应用与隐私挑战的探讨
- 62岁黄日华黄昏恋修成正果?手牵手甜蜜现身,与友人探讨婚礼细节
- 深入了解手机芯片:架构与性能的权衡
- 深入了解Linux中常见的五种文件类型
- 深入理解Java微服务架构与容器化部署
- 深入Rust的模式匹配与枚举类型
