openai/chatkit-python

Public

mirrored fromhttps://github.com/openai/chatkit-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.5.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

docs/guides/add-annotations.md

114lines · modecode

1# Add annotations in assistant messages
2
3ChatKit 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
7When 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
9Provide 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
14No 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
19If you build assistant messages yourself, include annotations on each `AssistantMessageContent` item.
20
21```python
22from datetime import datetime
23from chatkit.types import (
24 Annotation,
25 AssistantMessageContent,
26 AssistantMessageItem,
27 FileSource,
28 ThreadItemDoneEvent,
29 URLSource,
30)
31
32text = "Quarterly revenue grew 12% year over year."
33annotations = [
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
47yield 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
61You can attach `EntitySource` items as annotations to show entity references inline in assistant text and in the **Sources** list below the message.
62
63Entity annotations support a few UI-focused fields:
64
65- `icon`: Controls the icon shown for the entity in the default inline/hover UI.
66- `label`: Customizes what's shown in the default entity hover header (when you are not rendering a custom preview).
67- `inline_label`: Shows a label inline instead of an icon.
68- `interactive=True`: Wires the annotation to client-side callbacks (`ChatKitOptions.entities.onClick` and `ChatKitOptions.entities.onRequestPreview`).
69
70```python
71from datetime import datetime
72from chatkit.types import (
73 Annotation,
74 AssistantMessageContent,
75 AssistantMessageItem,
76 EntitySource,
77 ThreadItemDoneEvent,
78)
79
80text = "Here are the ACME account details for reference."
81
82annotations = [
83 Annotation(
84 source=EntitySource(
85 id="customer_123",
86 title="ACME Corp",
87 description="Enterprise plan · 500 seats",
88 icon="suitcase",
89 label="Customer",
90 interactive=True,
91 # Free-form data object passed to your client-side entity callbacks
92 data={"href": "https://crm.example.com/customers/123"},
93 ),
94 # `index` controls where the inline marker is placed in the text.
95 index=text.index("ACME") + len("ACME"),
96 )
97]
98
99yield ThreadItemDoneEvent(
100 item=AssistantMessageItem(
101 id=self.store.generate_item_id("message", thread, context),
102 thread_id=thread.id,
103 created_at=datetime.now(),
104 content=[
105 AssistantMessageContent(
106 text=text,
107 annotations=annotations,
108 )
109 ],
110 )
111)
112```
113
114Provide 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. These callbacks are only invoked for entity annotations with `interactive=True`; use the `data` payload to pass entity information and deep link into your app.
115