深入探讨GPTs和AI Assistant( 四 )

消息注解Message annotationsAI 助手返回的消息可能会包含 Message annotations,存储在content 的对象中 。注解(Annotations)提供了如何解析消息的信息;
目前支持两种不同的注解:

  1. file_citation: 该注解是 retrieval工具提供的,它定了了参考的内容的来源 。
  2. 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 )通常情况下,我们在创建 Assistant 对象的时候,已经指定了model和tools , 但是我们仍可以在创建执行对象(Run)的时候,进行重新指定 。
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对象有不同的状态
深入探讨GPTs和AI Assistant

文章插图

深入探讨GPTs和AI Assistant

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

深入探讨GPTs和AI Assistant

文章插图
执行步骤Run steps可能耗时比较长,为了能了解执行的细节 , 我们可以通过 step_details这个字段进行观察,包含了两种类型的内容:
  1. message_creation: 展示了产生了什么消息
  2. tool_calls: 展示了使用了什么tool
限制目前是beta 版本,将会持续解决后续这些如下问题
  • 支持流式输出
  • 支持通知的功能,可以在无需轮询的情况下共享对象状态更新
  • 支持 DALL·E 作为工具
  • 支持用户上传图片
Tools Code InterpreterCode Interpreter(代码解释器) 允许 Assistant API 去创建并且执行代码 。这个代码解释器能力,支持多种文件处理 , 以及代码执行 。
代码解释器能够通过代码运行,完成多种困难的任务,并且能解决很多GPT地薄弱能力 , 例如数学能力等 。Code Interpreter 支持如果发现自己的代码执行失败了,会通过多轮重试,直到执行成功 。
开启Code Interpreter
如果需要开启 Code Interpreter 能力,只需要在tools参数中加入 Code Interpreter, 如 tools=[{"type": "code_interpreter"}]即可 。


推荐阅读