how to get access to quote in file search annotation
Hi,
I am using an OpenAI assistant and can see the content of the citation in the Playground. However, I would also like to access it in Python. I have attached the picture, and when I hover over the files, I can see their content
I can also see that quote exists at the following link:
Can someone please help me access this in my Python code?
Thanks!
Azure OpenAI Service
-
Marj De 0 Reputation points
2024-12-16T16:09:22.75+00:00 there is no quote in the library under "FileCitation"
class FileCitation(BaseModel): file_id: str """The ID of the specific File the citation is from.""" class FileCitationAnnotation(BaseModel): end_index: int file_citation: FileCitation start_index: int text: str """The text in the message content that needs to be replaced.""" type: Literal["file_citation"] """Always `file_citation`."""
-
kothapally Snigdha 850 Reputation points • Microsoft Vendor
2024-12-16T20:58:05.4033333+00:00 Hi Marj De
Greetings & Welcome to the Microsoft Q&A forum! Thank you for sharing your query.
To access the quote in file search annotations using Python, you can utilize the File Citation Annotation class that you have defined. This class includes properties such as file citation, which contains the file-id, and the text property that represents the citation text.
To retrieve the quote, you would typically need to implement a method to fetch the file content using the file-id from the File Citation object.
- Use the file-id from the File Citation to locate the file.
- Implement a function that retrieves the content of the file associated with the file-id.
- Extract the specific quote from the file content based on the indices provided in the File Citation Annotation.
- Unfortunately, without specific methods or APIs outlined in the context for retrieving file content based on file-id. can you refer this https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/assistant#message-annotations I hope this helps you! Thankyou!
-
Marj De 0 Reputation points
2024-12-16T21:52:41.7633333+00:00 The code snippet I shared is from the OpenAI library, and I’m pointing out that it doesn't include the "quote" attribute . My question is how to retrieve the text that appears when I hover over the citations in the Playground, as shown in the picture below:
Running the code you provided, I’m encountering the following error:
AttributeError: 'FileCitation' object has no attribute 'quote'
-
kothapally Snigdha 850 Reputation points • Microsoft Vendor
2024-12-20T19:12:21.97+00:00 Hi Marj De
Assuming your completion object contains a structure with a message that includes text, here's how you could retrieve it:
completion = {
"choices": [ { "message": { "context": { "text": "This is the text you want to retrieve.", "citations": [ {"URL": "http://example.com/article1", "file path": "article1.pdf"}, {"URL": "http://example.com/article2", "file path": "article2.pdf"} ] } } } ] } # Retrieve the text from the completion object text = completion["choices"][0] ["message"] ["context"] ["text"] # Print the retrieved text print ("Retrieved Text:") print(text) # Optionally, print citations as well citations = completion["choices"][0] ["message"] ["context"] ["citations"] print("\citations:") for citation in citations: yaml print (citation: {citation['URL']} - Source: {citation ['file path']")
I hope this helps you! Thank you!
-
kothapally Snigdha 850 Reputation points • Microsoft Vendor
2024-12-23T16:12:51.1066667+00:00 Hi Marj De
following up to see if the above response was helpful.
-
kothapally Snigdha 850 Reputation points • Microsoft Vendor
2024-12-24T16:27:59.7933333+00:00 Hi Marj De
We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet. In case if you have any resolution, please do share that same with the community as it can be helpful to others. Otherwise, will respond with more details and we will try to help.
-
Marj De 0 Reputation points
2025-01-03T00:27:13.6833333+00:00 @kothapally Snigdha No, it's not in that format. I am using the file search assistant, and the message follows this structure:
Message(id='..', assistant_id='..', attachments=[], completed_at=None, content=[TextContentBlock(text=Text(annotations=[FileCitationAnnotation(end_index=469, file_citation=FileCitation(file_id='..'), start_index=456, text='【4:14†source】', type='file_citation'), FileCitationAnnotation(end_index=677, file_citation=FileCitation(file_id='...'), start_index=664, text='【4:17†source】', type='file_citation')Note: this is part of the message object
I will need to replace the source in brackets with the corresponding input text. Currently, in the Azure Playground Assistant, this is shown as shown in the picture I attached earlier.
-
Marj De 0 Reputation points
2025-01-03T00:29:34.84+00:00 -
kothapally Snigdha 850 Reputation points • Microsoft Vendor
2025-01-07T10:07:32.0233333+00:00 Hi Marj De
Sorry for the delay
- Identify the File Citation objects within the message. Each object contains a file _id that links to specific file content. You will need to fetch this file using the corresponding file _id or parse it if it's already available in your system.
- From each File Citation, extract the relevant source text. This could be a title, section name, or URL, depending on how your system is set up to handle citations.
- After obtaining the correct text, replace the citation placeholder in the original message with the extracted text.
- This involves parsing the message's content and performing a string replacement based on the position of the citation.
I hope this helps you. Thank you.
-
kothapally Snigdha 850 Reputation points • Microsoft Vendor
2025-01-08T08:30:30.1466667+00:00 Hi Marj De
Following up to see if the above response was helpful.
-
kothapally Snigdha 850 Reputation points • Microsoft Vendor
2025-01-09T08:44:44.5433333+00:00 Hi Marj De
We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet. In case if you have any resolution, please do share that same with the community as it can be helpful to others. Otherwise, will respond with more details and we will try to help.
Sign in to comment