|
1 | 1 | # Accept rich user input |
2 | 2 |
|
3 | | -This guide explains how a ChatKit server accepts user input beyond plain text—such as attachments and @-mentions—and makes it available to your inference pipeline. |
| 3 | +This guide explains how a ChatKit server accepts user input beyond plain text, such as attachments, structured follow-up answers, and @-mentions, and makes it available to your inference pipeline. |
4 | 4 |
|
5 | 5 | At a high level: |
6 | 6 |
|
7 | 7 | - Attachments let users upload files that your model can read. |
| 8 | +- Structured input lets the assistant ask for specific follow-up answers. |
8 | 9 | - @-mentions let users tag entities so the model does not have to guess from free text. |
9 | 10 |
|
10 | 11 | ## Attachments: let users upload files |
@@ -228,6 +229,59 @@ async def transcribe(self, audio_input: AudioInput, context: RequestContext) -> |
228 | 229 |
|
229 | 230 | Return a `TranscriptionResult` that includes the final `text` that should appear in the composer. |
230 | 231 |
|
| 232 | +## Structured input: ask for specific answers |
| 233 | + |
| 234 | +Structured input lets your assistant ask the user for small, typed follow-up answers as part of the conversation. The prompt takes over the composer with focused controls, plus a skip option. Use structured input when the next step depends on specific choices or short fields, and free-text back-and-forth would be slower or easier to misread. |
| 235 | + |
| 236 | +The server streams a [`StructuredInputItem`](../api/chatkit/types.md#chatkit.types.StructuredInputItem) during `respond`. ChatKit renders the questions in the composer area instead of the normal free-text input, then records the result on the same thread item when the user answers or skips. |
| 237 | + |
| 238 | +### Stream a structured input item |
| 239 | + |
| 240 | +Yield a `StructuredInputItem` from `respond` when you need the user to answer before continuing. |
| 241 | + |
| 242 | +```python |
| 243 | +from datetime import datetime |
| 244 | + |
| 245 | +from chatkit.types import ( |
| 246 | + StructuredInputFreeform, |
| 247 | + StructuredInputItem, |
| 248 | + StructuredInputMultipleChoice, |
| 249 | + ThreadItemDoneEvent, |
| 250 | +) |
| 251 | + |
| 252 | + |
| 253 | +yield ThreadItemDoneEvent( |
| 254 | + item=StructuredInputItem( |
| 255 | + id=self.store.generate_item_id("message", thread, context), |
| 256 | + thread_id=thread.id, |
| 257 | + created_at=datetime.now(), |
| 258 | + inputs=[ |
| 259 | + StructuredInputMultipleChoice( |
| 260 | + id="priority", |
| 261 | + question="What priority should I use?", |
| 262 | + options=["Low", "Medium", "High"], |
| 263 | + ), |
| 264 | + StructuredInputFreeform( |
| 265 | + id="notes", |
| 266 | + question="Any extra context?", |
| 267 | + description="Optional details to include.", |
| 268 | + ), |
| 269 | + ], |
| 270 | + ) |
| 271 | +) |
| 272 | +return |
| 273 | +``` |
| 274 | + |
| 275 | +Use [`StructuredInputMultipleChoice`](../api/chatkit/types.md#chatkit.types.StructuredInputMultipleChoice) for choice prompts and [`StructuredInputFreeform`](../api/chatkit/types.md#chatkit.types.StructuredInputFreeform) for short text answers. Set `multiple=True` on multiple-choice input when the user may submit more than one value. |
| 276 | + |
| 277 | +After the user submits or skips, ChatKit records the result on the structured input item. |
| 278 | + |
| 279 | +### Convert structured input submissions into model input |
| 280 | + |
| 281 | +The default [`ThreadItemConverter`](../api/chatkit/agents.md#chatkit.agents.ThreadItemConverter) includes structured input items in model input. It describes the prompt status and each answer as answered, skipped, or unanswered. |
| 282 | + |
| 283 | +If your model needs a different format, override `ThreadItemConverter.structured_input_to_input` before calling `Runner.run_streamed`. |
| 284 | + |
231 | 285 | ## @-mentions: tag entities in user messages |
232 | 286 |
|
233 | 287 | Enable @-mentions so users can tag entities (like documents, tickets, or users) instead of pasting raw identifiers. Mentions travel through ChatKit as structured tags so the model can resolve entities instead of guessing from free text. |
@@ -361,4 +415,3 @@ Clicks and hover previews apply to the tagged entities shown in past user messag |
361 | 415 |
|
362 | 416 | - `entities.onClick` fires when a user clicks a tag in the transcript. Handle navigation or open a detail view. See the [onClick option](https://openai.github.io/chatkit-js/api/openai/chatkit/type-aliases/entitiesoption/#onclick). |
363 | 417 | - `entities.onRequestPreview` runs when the user hovers or taps a tag that has `interactive: true`. Return a `BasicRoot` widget; you can build one with `WidgetTemplate.build_basic(...)` if you are building the preview widgets server-side. See the [onRequestPreview option](https://openai.github.io/chatkit-js/api/openai/chatkit/type-aliases/entitiesoption/#onrequestpreview). |
364 | | - |
|
0 commit comments