|
| 1 | +# Add annotations in assistant messages |
| 2 | + |
| 3 | +ChatKit renders clickable inline citations when assistant text includes `annotations` and rolls every reference into a collapsed **Sources** list beneath each message. You can let the model emit annotations directly or attach sources yourself before streaming the message. |
| 4 | + |
| 5 | +## Use model-emitted citations |
| 6 | + |
| 7 | +When you stream a Responses run through `stream_agent_response`, ChatKit automatically converts any `file_citation`, `container_file_citation`, and `url_citation` annotations returned by the OpenAI API into ChatKit `Annotation` objects and attaches them to streamed message content. |
| 8 | + |
| 9 | +Provide the model with citable evidence via tools to receive citation annotations, most commonly: |
| 10 | + |
| 11 | +- `FileSearchTool` for uploaded documents (emits `file_citation` / `container_file_citation`) |
| 12 | +- `WebSearchTool` for live URLs (emits `url_citation`) |
| 13 | + |
| 14 | +No additional server-side wiring is required beyond calling `stream_agent_response`. If the model emits citation annotations from tool usage, ChatKit will forward them automatically as `Annotation` objects on the corresponding content parts. |
| 15 | + |
| 16 | + |
| 17 | +## Attach sources manually |
| 18 | + |
| 19 | +If you build assistant messages yourself, include annotations on each `AssistantMessageContent` item. |
| 20 | + |
| 21 | +```python |
| 22 | +from datetime import datetime |
| 23 | +from chatkit.types import ( |
| 24 | + Annotation, |
| 25 | + AssistantMessageContent, |
| 26 | + AssistantMessageItem, |
| 27 | + FileSource, |
| 28 | + ThreadItemDoneEvent, |
| 29 | + URLSource, |
| 30 | +) |
| 31 | + |
| 32 | +text = "Quarterly revenue grew 12% year over year." |
| 33 | +annotations = [ |
| 34 | + Annotation( |
| 35 | + source=FileSource(filename="q1_report.pdf", title="Q1 Report"), |
| 36 | + index=len(text) - 1, # attach near the end of the sentence |
| 37 | + ), |
| 38 | + Annotation( |
| 39 | + source=URLSource( |
| 40 | + url="https://example.com/press-release", |
| 41 | + title="Press release", |
| 42 | + ), |
| 43 | + index=len(text) - 1, |
| 44 | + ), |
| 45 | +] |
| 46 | + |
| 47 | +yield ThreadItemDoneEvent( |
| 48 | + item=AssistantMessageItem( |
| 49 | + id=self.store.generate_item_id("message", thread, context), |
| 50 | + thread_id=thread.id, |
| 51 | + created_at=datetime.now(), |
| 52 | + content=[AssistantMessageContent(text=text, annotations=annotations)], |
| 53 | + ) |
| 54 | +) |
| 55 | +``` |
| 56 | + |
| 57 | +`index` is the character position to place the footnote marker; re-use the same index when multiple citations support the same claim so the footnote numbers stay grouped. |
| 58 | + |
| 59 | +## Annotating with custom entities |
| 60 | + |
| 61 | +Inline annotations are not yet supported for entity sources, but you can still attach `EntitySource` items as annotations so they appear in the Sources list below the message. |
| 62 | + |
| 63 | +```python |
| 64 | +from datetime import datetime |
| 65 | +from chatkit.types import ( |
| 66 | + Annotation, |
| 67 | + AssistantMessageContent, |
| 68 | + AssistantMessageItem, |
| 69 | + EntitySource, |
| 70 | + ThreadItemDoneEvent, |
| 71 | +) |
| 72 | + |
| 73 | +annotations = [ |
| 74 | + Annotation( |
| 75 | + source=EntitySource( |
| 76 | + id="customer_123", |
| 77 | + title="ACME Corp", |
| 78 | + description="Enterprise plan · 500 seats", |
| 79 | + icon="suitcase", |
| 80 | + data={"href": "https://crm.example.com/customers/123"}, |
| 81 | + ) |
| 82 | + ) |
| 83 | +] |
| 84 | + |
| 85 | +yield ThreadItemDoneEvent( |
| 86 | + item=AssistantMessageItem( |
| 87 | + id=self.store.generate_item_id("message", thread, context), |
| 88 | + thread_id=thread.id, |
| 89 | + created_at=datetime.now(), |
| 90 | + content=[ |
| 91 | + AssistantMessageContent( |
| 92 | + text="Here are the ACME account details for reference.", |
| 93 | + annotations=annotations, |
| 94 | + ) |
| 95 | + ], |
| 96 | + ) |
| 97 | +) |
| 98 | +``` |
| 99 | + |
| 100 | +Provide richer previews and navigation by handling [`entities.onRequestPreview`](https://openai.github.io/chatkit-js/api/openai/chatkit/type-aliases/entitiesoption/#onrequestpreview) and [`entities.onClick`](https://openai.github.io/chatkit-js/api/openai/chatkit/type-aliases/entitiesoption/#onclick) in ChatKit.js, using the `data` payload to pass entity information and deep link into your app. |
0 commit comments