Question

Assistants API: replacing model generated substrings with annotations

  • 5 January 2024
  • 0 replies
  • 61 views

Userlevel 1

When using the Conversation with Assistant ChatGPT action with retrieval turned on, the response often contains annotations that are illegible, e.g. 【13†source】.

 

According to Open AI’s docs, there is a way to convert them to legible text in the annotations. But to see them, there’s a python code snippet they recommend:

 

# 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)

 

Now, I’m not a coder (though ChatGPT helps me with any coding work I might come across). My question specifically within Zapier is: is there a way to use the above information (taken from the documentation here) to convert the illegible annotations to readable text annotations in our responses?


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.