You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -346,3 +345,171 @@ This creates a chatbot that:
346
345
347
346
That's it! You now have a chatbot that not only responds to users but also shows its thinking process, creating a more transparent and engaging interaction. See our finished Gemini 2.0 Flash Thinking demo [here](https://huggingface.co/spaces/ysharma/Gemini2-Flash-Thinking).
348
347
348
+
349
+
## Building with Citations
350
+
351
+
The Gradio Chatbot can display citations fromLLM responses, making it perfect for creating UIs that show source documentation and references. This guide will show you how to build a chatbot that displays Claude's citations in real-time.
352
+
353
+
### A real example using Anthropic's Citations API
354
+
Let's create a complete chatbot that shows both responses and their supporting citations. We'll use Anthropic's Claude API with citations enabled and Gradio for the UI.
355
+
356
+
We'll begin with imports and setting up the Anthropic client. Note that you'll need an `ANTHROPIC_API_KEY` environment variable set:
357
+
358
+
```python
359
+
import gradio as gr
360
+
import anthropic
361
+
import base64
362
+
from typing import List, Dict, Any
363
+
364
+
client = anthropic.Anthropic()
365
+
```
366
+
367
+
First, let's set up our message formatting functions that handle document preparation:
368
+
369
+
```python
370
+
def encode_pdf_to_base64(file_obj) ->str:
371
+
"""Convert uploaded PDF file to base64 string."""
372
+
if file_obj isNone:
373
+
returnNone
374
+
withopen(file_obj.name, 'rb') as f:
375
+
return base64.b64encode(f.read()).decode('utf-8')
376
+
377
+
def format_message_history(
378
+
history: list,
379
+
enable_citations: bool,
380
+
doc_type: str,
381
+
text_input: str,
382
+
pdf_file: str
383
+
) -> List[Dict]:
384
+
"""Convert Gradio chat history to Anthropic message format."""
- Supports both plain text andPDF documents for Claude to cite from
509
+
- Displays Citations in collapsible sections using our `metadata` feature
510
+
- Shows source quotes directly from the given documents
511
+
512
+
The citations feature works particularly well with the Gradio Chatbot's `metadata` support, allowing us to create collapsible sections that keep the chat interface clean while still providing easy access to source documentation.
513
+
514
+
That's it! You now have a chatbot that not only responds to users but also shows its sources, creating a more transparent and trustworthy interaction. See our finished Citations demo [here](https://huggingface.co/spaces/ysharma/anthropic-citations-with-gradio-metadata-key).
0 commit comments