|
78 | 78 | {
|
79 | 79 | "data": {
|
80 | 80 | "text/plain": [
|
81 |
| - "ChatCompletion(id='chatcmpl-AWXWdjz55mygQ0I8rOdnPj58BmM3E', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='Hello! How can I assist you today?', refusal=None, role='assistant', function_call=None, tool_calls=None))], created=1732318731, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier=None, system_fingerprint='fp_0705bf87c0', usage=CompletionUsage(completion_tokens=9, prompt_tokens=11, total_tokens=20, completion_tokens_details=CompletionTokensDetails(reasoning_tokens=0, audio_tokens=0, accepted_prediction_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details={'cached_tokens': 0, 'audio_tokens': 0}))" |
| 81 | + "ChatCompletion(id='chatcmpl-AwxutFvFrBvjSeejfih4WDfcIRNo1', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='Hello! How can I assist you today?', refusal=None, role='assistant', function_call=None, tool_calls=None))], created=1738616707, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_bd83329f63', usage=CompletionUsage(completion_tokens=10, prompt_tokens=11, total_tokens=21, completion_tokens_details=CompletionTokensDetails(reasoning_tokens=0, audio_tokens=0, accepted_prediction_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details={'cached_tokens': 0, 'audio_tokens': 0}))" |
82 | 82 | ]
|
83 | 83 | },
|
84 | 84 | "execution_count": null,
|
|
119 | 119 | {
|
120 | 120 | "data": {
|
121 | 121 | "text/plain": [
|
122 |
| - "Message(id='msg_01JAn3tJTJtuTL79eeDME2rr', content=[TextBlock(text=\"Hello! I'm Claude, an AI assistant created by Anthropic. It's nice to meet you. How can I help you today?\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=11, output_tokens=32))" |
| 122 | + "Message(id='msg_01MJhQFAyz8RdH9C9jkm7tdi', content=[TextBlock(text=\"Hello! I'm an AI assistant created by Anthropic. How can I assist you today?\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=11, output_tokens=23, cache_creation_input_tokens=0, cache_read_input_tokens=0))" |
123 | 123 | ]
|
124 | 124 | },
|
125 | 125 | "execution_count": null,
|
|
223 | 223 | {
|
224 | 224 | "data": {
|
225 | 225 | "text/plain": [
|
226 |
| - "\"Hello! I am Claude, an AI assistant created by Anthropic. It's nice to meet you. How can I assist you today?\"" |
| 226 | + "\"Hello! It's a pleasure to meet you. I'm an AI assistant created by Anthropic. I'm here to help with any questions or tasks you may have. Please let me know how I can be of assistance.\"" |
227 | 227 | ]
|
228 | 228 | },
|
229 | 229 | "execution_count": null,
|
|
330 | 330 | {
|
331 | 331 | "data": {
|
332 | 332 | "text/plain": [
|
333 |
| - "\"Hello! It's nice to meet you. I'm an AI assistant created by Anthropic. I'm here to help with a variety of tasks - feel free to ask me anything!\"" |
| 333 | + "\"Hello! I'm an AI assistant created by Anthropic. I'm here to help with any questions or tasks you may have. How can I assist you today?\"" |
334 | 334 | ]
|
335 | 335 | },
|
336 | 336 | "execution_count": null,
|
|
392 | 392 | {
|
393 | 393 | "data": {
|
394 | 394 | "text/plain": [
|
395 |
| - "ChatCompletion(id='chatcmpl-AWXWjNJlLrQS8hECvDsOca0p00vqc', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The image contains a small puppy resting on grass. The puppy has a brown and white coat and is positioned near some purple flowers. It looks curious and attentive.', refusal=None, role='assistant', function_call=None, tool_calls=None))], created=1732318737, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier=None, system_fingerprint='fp_3de1288069', usage=CompletionUsage(completion_tokens=32, prompt_tokens=8512, total_tokens=8544, completion_tokens_details=CompletionTokensDetails(reasoning_tokens=0, audio_tokens=0, accepted_prediction_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details={'cached_tokens': 0, 'audio_tokens': 0}))" |
| 395 | + "ChatCompletion(id='chatcmpl-Awxv1j0M9xACPlg1X5VnUF5w25i8F', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The image features a cute puppy with brown and white fur lying on green grass. The puppy is resting near a patch of purple flowers, adding to the colorful and serene outdoor setting.', refusal=None, role='assistant', function_call=None, tool_calls=None))], created=1738616715, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_bd83329f63', usage=CompletionUsage(completion_tokens=37, prompt_tokens=452, total_tokens=489, completion_tokens_details=CompletionTokensDetails(reasoning_tokens=0, audio_tokens=0, accepted_prediction_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details={'cached_tokens': 0, 'audio_tokens': 0}))" |
396 | 396 | ]
|
397 | 397 | },
|
398 | 398 | "execution_count": null,
|
|
434 | 434 | {
|
435 | 435 | "data": {
|
436 | 436 | "text/plain": [
|
437 |
| - "Message(id='msg_01A2qtvcrYZF6C22Jbujezuh', content=[TextBlock(text=\"This image shows a cute puppy lying in some grass and flowers. The puppy appears to be a Cavalier King Charles Spaniel, with its long, silky fur and floppy ears. The puppy has a friendly, sweet expression on its face as it looks directly at the camera. The background shows some purple flowers, creating a nice colorful contrast against the puppy's white and brown fur.\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=104, output_tokens=89))" |
| 437 | + "Message(id='msg_01YZrV1h5fkdT27pXaiff99W', content=[TextBlock(text=\"This image shows a cute puppy lying in a grassy area with some purple flowers in the background. The puppy appears to be a Cavalier King Charles Spaniel, with its soft-looking fur and large, expressive eyes. The puppy looks relaxed and content, gazing out at the viewer. The vibrant purple flowers provide a lovely natural setting for the puppy's portrait.\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=104, output_tokens=86, cache_creation_input_tokens=0, cache_read_input_tokens=0))" |
438 | 438 | ]
|
439 | 439 | },
|
440 | 440 | "execution_count": null,
|
|
711 | 711 | {
|
712 | 712 | "data": {
|
713 | 713 | "text/plain": [
|
714 |
| - "'The picture features an adorable puppy lying on the grass. It has a fluffy white coat with brown patches, particularly around its ears and eyes. The puppy has large, expressive eyes and a curious expression. In the background, there are purple flowers blooming, adding a vibrant touch to the scene. The overall setting looks peaceful and natural, conveying a sense of tranquility and playfulness.'" |
| 714 | + "'The image features a cute puppy laying in a grassy area, partially hidden by a cluster of purple flowers. The puppy has a mostly white coat with brown markings on its ears and around its eyes. Its large, expressive eyes give it an adorable appearance, and the overall setting looks peaceful and vibrant with the flowers surrounding the pup.'" |
715 | 715 | ]
|
716 | 716 | },
|
717 | 717 | "execution_count": null,
|
|
733 | 733 | {
|
734 | 734 | "data": {
|
735 | 735 | "text/plain": [
|
736 |
| - "'The image shows a young puppy lying in a grassy area surrounded by purple daisy-like flowers. The puppy has a white and brown coat, with floppy ears and a friendly, alert expression on its face. The puppy appears to be relaxed and content, enjoying the warm, sunny environment. The vibrant colors of the flowers and the lush green grass create a charming, natural setting that complements the endearing presence of the puppy.'" |
| 736 | + "'The image shows a young, fluffy puppy lying on a grassy lawn. The puppy has a white and brown coat, with large, expressive eyes and floppy ears. The puppy appears to be looking directly at the camera with a curious and friendly expression. In the background, there are several vibrant purple flowers, adding a lovely, natural setting to the scene. Overall, the image captures the adorable and endearing nature of this puppy, inviting the viewer to appreciate the charming beauty of this young canine companion.'" |
737 | 737 | ]
|
738 | 738 | },
|
739 | 739 | "execution_count": null,
|
|
1039 | 1039 | {
|
1040 | 1040 | "data": {
|
1041 | 1041 | "text/plain": [
|
1042 |
| - "'Let me calculate the average monthly revenue for Product D by adding all monthly values and dividing by 12.\\n\\nMonthly revenues for Product D:\\nJan: ~900\\nFeb: ~500\\nMar: ~400\\nApr: ~700\\nMay: ~800\\nJun: ~900\\nJul: ~1000\\nAug: ~1050\\nSep: ~1200\\nOct: ~1300\\nNov: ~1300\\nDec: ~1300\\n\\nTotal = 11,350 (approximate)\\nAverage = 11,350 ÷ 12 = 946\\n\\nThe average monthly revenue for Product D was approximately $946 during fiscal year 2023.'" |
| 1042 | + "'Let me calculate the average monthly revenue for Product D by summing up all monthly values and dividing by 12.\\n\\nMonthly revenues for Product D:\\nJan: ~900\\nFeb: ~500\\nMar: ~400\\nApr: ~700\\nMay: ~800\\nJun: ~900\\nJul: ~1000\\nAug: ~1050\\nSep: ~1200\\nOct: ~1300\\nNov: ~1300\\nDec: ~1300\\n\\nTotal = 11,350 (approximately)\\nAverage = 11,350 ÷ 12 = 945.83\\n\\nThe average monthly revenue for Product D in fiscal year 2023 was approximately $946.'" |
1043 | 1043 | ]
|
1044 | 1044 | },
|
1045 | 1045 | "execution_count": null,
|
|
1551 | 1551 | {
|
1552 | 1552 | "data": {
|
1553 | 1553 | "text/plain": [
|
1554 |
| - "'Why did the scarecrow win an award?\\n\\nBecause he was outstanding in his field!'" |
| 1554 | + "\"Why don't skeletons fight each other?\\n\\nThey don't have the guts!\"" |
1555 | 1555 | ]
|
1556 | 1556 | },
|
1557 | 1557 | "execution_count": null,
|
|
1573 | 1573 | {
|
1574 | 1574 | "data": {
|
1575 | 1575 | "text/plain": [
|
1576 |
| - "\"Why don't skeletons fight each other? \\n\\nThey don't have the guts!\"" |
| 1576 | + "'Why did the scarecrow win an award?\\n\\nBecause he was outstanding in his field!'" |
1577 | 1577 | ]
|
1578 | 1578 | },
|
1579 | 1579 | "execution_count": null,
|
|
1781 | 1781 | "mk_msg_anthropic(\"Please cache my message\", cache=True)"
|
1782 | 1782 | ]
|
1783 | 1783 | },
|
| 1784 | + { |
| 1785 | + "cell_type": "markdown", |
| 1786 | + "metadata": {}, |
| 1787 | + "source": [ |
| 1788 | + "### Citations" |
| 1789 | + ] |
| 1790 | + }, |
| 1791 | + { |
| 1792 | + "cell_type": "markdown", |
| 1793 | + "metadata": {}, |
| 1794 | + "source": [ |
| 1795 | + "The Anthropic API provides detailed [citations](https://docs.anthropic.com/en/docs/build-with-claude/citations) when answering questions about documents.\n", |
| 1796 | + "\n", |
| 1797 | + "When citations are enabled a citations block like the one below will be included in the response.\n", |
| 1798 | + "\n", |
| 1799 | + "```js\n", |
| 1800 | + "{\n", |
| 1801 | + " \"content\": [\n", |
| 1802 | + " {\n", |
| 1803 | + " \"type\": \"text\",\n", |
| 1804 | + " \"text\": \"According to the document, \"\n", |
| 1805 | + " },\n", |
| 1806 | + " {\n", |
| 1807 | + " \"type\": \"text\",\n", |
| 1808 | + " \"text\": \"the grass is green\",\n", |
| 1809 | + " \"citations\": [{\n", |
| 1810 | + " \"type\": \"char_location\",\n", |
| 1811 | + " \"cited_text\": \"The grass is green.\",\n", |
| 1812 | + " \"document_index\": 0,\n", |
| 1813 | + " \"document_title\": \"Example Document\",\n", |
| 1814 | + " \"start_char_index\": 0,\n", |
| 1815 | + " \"end_char_index\": 20\n", |
| 1816 | + " }]\n", |
| 1817 | + " }\n", |
| 1818 | + " ]\n", |
| 1819 | + "}\n", |
| 1820 | + "```" |
| 1821 | + ] |
| 1822 | + }, |
| 1823 | + { |
| 1824 | + "cell_type": "markdown", |
| 1825 | + "metadata": {}, |
| 1826 | + "source": [ |
| 1827 | + "To enable citations you need to create an Anthropic document with the following structure.\n", |
| 1828 | + "\n", |
| 1829 | + "```js\n", |
| 1830 | + "{\n", |
| 1831 | + " \"type\": \"document\",\n", |
| 1832 | + " \"source\": {...},\n", |
| 1833 | + " \"title\": \"Document Title\", # optional\n", |
| 1834 | + " \"context\": \"Context about the document that will not be cited from\", # optional\n", |
| 1835 | + " \"citations\": {\"enabled\": True}\n", |
| 1836 | + "}\n", |
| 1837 | + "```" |
| 1838 | + ] |
| 1839 | + }, |
| 1840 | + { |
| 1841 | + "cell_type": "markdown", |
| 1842 | + "metadata": {}, |
| 1843 | + "source": [ |
| 1844 | + "Currently Anthropic supports citations on 3 document types:\n", |
| 1845 | + "- text\n", |
| 1846 | + "- pdfs\n", |
| 1847 | + "- custom\n", |
| 1848 | + "\n", |
| 1849 | + "A **text** document has the following source structure.\n", |
| 1850 | + "\n", |
| 1851 | + "```js\n", |
| 1852 | + "{\"type\": \"text\", \"media_type\": \"text/plain\", \"data\": \"Plain text content...\"}\n", |
| 1853 | + "```\n", |
| 1854 | + "\n", |
| 1855 | + "Here's the source structure for a **pdf**.\n", |
| 1856 | + "\n", |
| 1857 | + "```js\n", |
| 1858 | + "{\"type\": \"base64\", \"media_type\": \"application/pdf\", \"data\": b64_enc_data}\n", |
| 1859 | + "```\n", |
| 1860 | + "\n", |
| 1861 | + "Finally, here's the source structure for a **custom** document.\n", |
| 1862 | + "\n", |
| 1863 | + "```js\n", |
| 1864 | + "{\n", |
| 1865 | + " \"type\": \"content\",\n", |
| 1866 | + " \"content\": [\n", |
| 1867 | + " {\"type\": \"text\", \"text\": \"First chunk\"},\n", |
| 1868 | + " {\"type\": \"text\", \"text\": \"Second chunk\"}\n", |
| 1869 | + " ]\n", |
| 1870 | + "}\n", |
| 1871 | + "```" |
| 1872 | + ] |
| 1873 | + }, |
| 1874 | + { |
| 1875 | + "cell_type": "code", |
| 1876 | + "execution_count": null, |
| 1877 | + "metadata": {}, |
| 1878 | + "outputs": [], |
| 1879 | + "source": [ |
| 1880 | + "#| export\n", |
| 1881 | + "def mk_ant_doc(content, title=None, context=None, citation=True, **kws):\n", |
| 1882 | + " \"Create an Anthropic document.\"\n", |
| 1883 | + " if _is_pdf(content): src = {\"type\":\"base64\", \"media_type\":\"application/pdf\", \"data\":_mk_pdf(content)}\n", |
| 1884 | + " elif isinstance(content,list): src = {\"type\":\"content\", \"content\":content}\n", |
| 1885 | + " else: src = {\"type\":\"text\", \"media_type\":\"text/plain\", \"data\":content}\n", |
| 1886 | + " return {\"type\":\"document\", \"source\":src, \"citations\":{\"enabled\":citation}, \"title\":title, \"context\":context, **kws}" |
| 1887 | + ] |
| 1888 | + }, |
| 1889 | + { |
| 1890 | + "cell_type": "markdown", |
| 1891 | + "metadata": {}, |
| 1892 | + "source": [ |
| 1893 | + "Here's how you would implement the example from the citation's [docs](https://docs.anthropic.com/en/docs/build-with-claude/citations)." |
| 1894 | + ] |
| 1895 | + }, |
| 1896 | + { |
| 1897 | + "cell_type": "code", |
| 1898 | + "execution_count": null, |
| 1899 | + "metadata": {}, |
| 1900 | + "outputs": [ |
| 1901 | + { |
| 1902 | + "data": { |
| 1903 | + "text/markdown": [ |
| 1904 | + "```json\n", |
| 1905 | + "{ 'content': [{'type': 'document', 'source': {'type': 'text', 'media_type': 'text/plain', 'data': 'The grass is green. The sky is blue.'}, 'citations': {'enabled': True}, 'title': 'My Document', 'context': 'This is a trustworthy document.'}, {'type': 'text', 'text': 'What color is the grass and sky?'}],\n", |
| 1906 | + " 'role': 'user'}\n", |
| 1907 | + "```" |
| 1908 | + ], |
| 1909 | + "text/plain": [ |
| 1910 | + "{'role': 'user',\n", |
| 1911 | + " 'content': (#2) [{'type': 'document', 'source': {'type': 'text', 'media_type': 'text/plain', 'data': 'The grass is green. The sky is blue.'}, 'citations': {'enabled': True}, 'title': 'My Document', 'context': 'This is a trustworthy document.'},{'type': 'text', 'text': 'What color is the grass and sky?'}]}" |
| 1912 | + ] |
| 1913 | + }, |
| 1914 | + "execution_count": null, |
| 1915 | + "metadata": {}, |
| 1916 | + "output_type": "execute_result" |
| 1917 | + } |
| 1918 | + ], |
| 1919 | + "source": [ |
| 1920 | + "doc = mk_ant_doc(\"The grass is green. The sky is blue.\", title=\"My Document\", context=\"This is a trustworthy document.\")\n", |
| 1921 | + "mk_msg([doc, \"What color is the grass and sky?\"])" |
| 1922 | + ] |
| 1923 | + }, |
1784 | 1924 | {
|
1785 | 1925 | "cell_type": "code",
|
1786 | 1926 | "execution_count": null,
|
|
0 commit comments