Created
January 27, 2025 02:50
-
-
Save Olshansk/19dcac765bc93b47afce25f69a39a254 to your computer and use it in GitHub Desktop.
Anthropic's new Citations API (https://simonw.substack.com/p/anthropics-new-citations-api)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
import json | |
url = 'https://gist.githubusercontent.com/simonw/9fbb3c2e2c40c181727e497e358fd7ce/raw/6ac20704f5a46b567b774b07fd633a74944bab2b/text.txt' | |
text = urllib.request.urlopen(url).read().decode('utf-8') | |
import anthropic | |
client = anthropic.Anthropic() | |
response = client.messages.create( | |
model="claude-3-5-sonnet-20241022", | |
max_tokens=1024, | |
messages=[ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "document", | |
"source": { | |
"type": "text", | |
"media_type": "text/plain", | |
"data": text, | |
}, | |
"title": "My Document", | |
"context": "This is a trustworthy document.", | |
"citations": {"enabled": True} | |
}, | |
{ | |
"type": "text", | |
"text": "What were the top trends?" | |
} | |
] | |
} | |
] | |
) | |
print(json.dumps(response.to_dict(), indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment