diff --git a/docs/examples/llm/anthropic.ipynb b/docs/examples/llm/anthropic.ipynb index b4bc70c55886d..2980ba5bca7a3 100644 --- a/docs/examples/llm/anthropic.ipynb +++ b/docs/examples/llm/anthropic.ipynb @@ -9,11 +9,18 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "72ed6f61-28a7-4f90-8a45-e3f452f95dbd", "metadata": {}, "source": [ - "# Anthropic" + "# Anthropic\n", + "\n", + "Anthropic has recently released its latest models: `Claude 3 Opus`, `Claude 3 Sonnet`, and `Claude 3 Haiku` (which will be available soon). By default, the `claude-2.1 model` is used. This notebook provides guidance on how to utilize these new models.\n", + "\n", + "1. Claude 3 Opus - claude-3-opus-20240229\n", + "2. Claude 3 Sonnet\t- claude-3-sonnet-20240229\n", + "3. Claude 3 Haiku - Coming soon" ] }, { @@ -52,6 +59,18 @@ "#### Call `complete` with a prompt" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "85fbba23", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "os.environ[\"ANTHROPIC_API_KEY\"] = \"YOUR ANTHROPIC API KEY\"" + ] + }, { "cell_type": "code", "execution_count": null, @@ -64,7 +83,7 @@ "# To customize your API key, do this\n", "# otherwise it will lookup ANTHROPIC_API_KEY from your env variable\n", "# llm = Anthropic(api_key=\"\")\n", - "llm = Anthropic()\n", + "llm = Anthropic(model=\"claude-3-opus-20240229\")\n", "\n", "resp = llm.complete(\"Paul Graham is \")" ] @@ -79,21 +98,21 @@ "name": "stdout", "output_type": "stream", "text": [ - " Here are some key facts about Paul Graham:\n", + "Paul Graham is a well-known entrepreneur, programmer, venture capitalist, and essayist. He is best known for co-founding Viaweb, one of the first web application companies, which was later sold to Yahoo! in 1998 and became Yahoo! Store. Graham is also the co-founder of Y Combinator, a highly successful startup accelerator that has helped launch numerous successful companies, such as Dropbox, Airbnb, and Reddit.\n", "\n", - "- Paul Graham is an American computer scientist, venture capitalist, and essayist. He is known for co-founding Viaweb, one of the first web-based application companies, which was acquired by Yahoo in 1998.\n", + "Some key points about Paul Graham:\n", "\n", - "- In 1995, Graham co-founded Viaweb with Robert Morris, Trevor Blackwell, and Jessica Livingston. The company helped popularize the business model of applying software as a service.\n", + "1. Programming: Graham is a skilled programmer and has written extensively on the subject, including his book \"Hackers & Painters: Big Ideas from the Computer Age.\"\n", "\n", - "- After selling Viaweb to Yahoo, Graham became a venture capitalist. He co-founded Y Combinator in 2005 with Jessica Livingston, Trevor Blackwell, and Robert Morris. Y Combinator is an influential startup accelerator that provides seed funding and advice to startups.\n", + "2. Essays: He is a prolific essayist, writing on various topics related to technology, startups, and entrepreneurship. His essays have been influential in the tech startup community.\n", "\n", - "- Graham has written several influential essays on startups, technology, and programming. Some of his most well-known essays include \"How to Start a Startup\", \"Do Things that Don't Scale\", and \"Beating the Averages\" about Lisp programming. \n", + "3. Lisp: Graham is an advocate for the Lisp programming language and has written several essays on its advantages.\n", "\n", - "- He pioneered the concept of using online essays to attract startup founders to apply to Y Combinator's program. His essays are often required reading in Silicon Valley.\n", + "4. Y Combinator: As a co-founder of Y Combinator, Graham has played a significant role in shaping the startup ecosystem and has mentored and invested in numerous successful companies.\n", "\n", - "- Graham has a Bachelor's degree in philosophy from Cornell University and a PhD in computer science from Harvard University. His doctoral thesis focused on Lisp compilers.\n", + "5. Wealth and inequality: In recent years, Graham has written about income inequality and the concentration of wealth, sparking discussions and debates within the tech community.\n", "\n", - "- He is considered an influential figure in the tech and startup worlds, known for his insights on startups, programming languages, and technology trends. His writings have shaped the strategies of many founders building startups.\n" + "Overall, Paul Graham is a significant figure in the technology and startup world, known for his contributions as a programmer, investor, and thought leader.\n" ] } ], @@ -125,7 +144,7 @@ " ),\n", " ChatMessage(role=\"user\", content=\"Tell me a story\"),\n", "]\n", - "resp = Anthropic().chat(messages)" + "resp = Anthropic(model=\"claude-3-opus-20240229\").chat(messages)" ] }, { @@ -138,19 +157,19 @@ "name": "stdout", "output_type": "stream", "text": [ - "assistant: Here is a fun pirate story for you:\n", + "assistant: *clears throat and speaks in a pirate accent* Aye, gather 'round me hearties and I'll spin ye a yarn of adventure on the high seas!\n", "\n", - "Yarrr matey! Me name be Captain Redbeard, the most fearsome pirate to sail the seven seas. I be the captain of the good ship Salty Dog, and we be lookin' fer treasure! \n", + "T'was a dark and stormy night when the Black Pearl set sail from Tortuga. The salty sea spray stung me eyes as I stood at the helm, guidin' me beloved ship through the roilin' waves. Me loyal crew scurried about, securin' the riggin' and battening down the hatches. \n", "\n", - "I lost me leg in a battle with the evil Captain Bluebeard years ago. That scallywag got the better of me that time, but I'll have me revenge! Now I got me a peg leg that I can use to stomp the deck or kick me enemies right in the rear! \n", + "Suddenly, the lookout cried \"Ship ahoy!\" and pointed off the starboard bow. I raised me spyglass and spied a Spanish galleon, her decks heavily laden with treasure. The crew gave a hearty cheer - we'd be feastin' and drinkin' well tonight!\n", "\n", - "Me first mate Scurvy Sam be my best friend. We go way back to when we were just lads dreamin' of a pirate's life. He may only have one good eye after losin' the other one to a seagull, but he can still spot treasure from a league away! \n", + "I ordered the crew to ready the cannons as we drew alongside the galleon. \"Fire all!\" I bellowed and the Pearl shook as the guns unleashed a barrage. The Spaniards returned fire but they were no match for me skilled gunners.\n", "\n", - "Today we be sailin' for the fabled Treasure Island, in search of the loot buried long ago by the notorious Captain Flint. Flint was the most ruthless pirate ever to live, but he buried his treasure and no one ever found it. But I have a map, given to me by a dying sailor. I just know it'll lead us right to Flint's trove of rubies, diamonds and mountains of gold! \n", + "We boarded the galleon, swords flashin' and pistols blazin'. The fight was fast and bloody but in the end, the Pearl was victorious! We claimed the treasure as our own - mountains of gold and jewels glintin' in the moonlight.\n", "\n", - "It won't be easy. We may have to fight off Flint's ghost, or deal with tribes of cannibals, or outwit double-crossing thieves. But that's all part of a pirate's life! And when we finally get our hands on that treasure, we'll live like kings. We'll party all night and sleep all day in our fancy pirate cove. \n", + "As we sailed away, I couldn't help but grin. T'was a fine night of piratin' and I knew many more adventures lay ahead for me and me crew. No matter the danger, the Black Pearl would always prevail! Yo ho ho!\n", "\n", - "So hoist the mainsail me hearties, and let's set sail for adventure! Keep a weather eye on the horizon, mateys. Treasure awaits!\n" + "*laughs heartily* And that, me friends, is a taste of the pirate's life. May yer sails always be full and yer horizons bright. Fare thee well!\n" ] } ], @@ -183,7 +202,7 @@ "source": [ "from llama_index.llms.anthropic import Anthropic\n", "\n", - "llm = Anthropic()\n", + "llm = Anthropic(model=\"claude-3-opus-20240229\", max_tokens=100)\n", "resp = llm.stream_complete(\"Paul Graham is \")" ] }, @@ -197,21 +216,9 @@ "name": "stdout", "output_type": "stream", "text": [ - " Here are some key points about Paul Graham:\n", - "\n", - "- Paul Graham is an American computer scientist, venture capitalist, and essayist. He is known for co-founding Viaweb, one of the first web-based applications, which was acquired by Yahoo in 1998.\n", + "Paul Graham is a well-known entrepreneur, programmer, venture capitalist, and essayist. He is best known for co-founding Viaweb, one of the first web application companies, which was later sold to Yahoo! in 1998 and became Yahoo! Store. \n", "\n", - "- In 2005, Graham co-founded Y Combinator, a startup accelerator that provides seed funding and advice to startups. Y Combinator has backed over 2000 companies including Dropbox, Airbnb, Stripe, and Reddit. \n", - "\n", - "- Graham has written extensively about startups, programming, and technology. Some of his most popular essays include \"How to Start a Startup\", \"The Age of the Essay\", and \"Beating the Averages\" about his experiences with Viaweb.\n", - "\n", - "- As an essayist, Graham has a very analytical and insightful writing style. He is skilled at breaking down complex concepts and explaining ideas clearly. His essays cover a wide range of topics including startups, programming, economics, and philosophy.\n", - "\n", - "- In addition to his work with startups, Graham previously worked as a programmer at Yahoo and was also a professor of computer science at Harvard University. He studied mathematics at Cornell University and obtained a PhD in Computer Science from Harvard.\n", - "\n", - "- Graham has advocated for funding and supporting startup founders who may lack traditional credentials like college degrees. He has argued that intelligence, determination, and flexibility are more important than formal education for succeeding in startups.\n", - "\n", - "In summary, Paul Graham is a prominent figure in the tech industry known for his work with startups, programming, and influential writing and perspectives on technology. His ideas have had a major impact on the startup ecosystem." + "After the sale of Viaweb, Graham and his wife Jessica Livingston co-founded Y Combinator in 2005, a highly successful startup accelerator that has helped launch" ] } ], @@ -229,7 +236,7 @@ "source": [ "from llama_index.llms.anthropic import Anthropic\n", "\n", - "llm = Anthropic()\n", + "llm = Anthropic(model=\"claude-3-opus-20240229\")\n", "messages = [\n", " ChatMessage(\n", " role=\"system\", content=\"You are a pirate with a colorful personality\"\n", @@ -249,15 +256,23 @@ "name": "stdout", "output_type": "stream", "text": [ - " Here is a fun pirate story for you:\n", + "*clears throat and speaks in a gruff, piratey voice* \n", + "\n", + "Aye, gather 'round me hearties and I'll spin ye a yarn of adventure on the high seas! \n", "\n", - "Yarrr matey! Me name be Captain Redbeard, the most fearsome pirate to sail the seven seas. I be the captain of the good ship Salty Dog, and we be lookin' fer treasure! \n", + "'Twas a dark and stormy night, the kind where the wind howls like a banshee and the waves crash over the deck. Me and me crew were sailin' the Caribbean, searchin' for treasure and glory.\n", "\n", - "I lost me leg in a battle with the evil Captain Bluebeard years ago. That scallywag got the better of me that time, but I'll have me revenge! Now I got me a peg leg that I can use to kick me enemies right in the behind! Har har!\n", + "Suddenly, the lookout cried \"Ship ahoy!\" and sure enough, a Spanish galleon was bearin' down on us, her decks bristlin' with cannons. The scurvy dogs wanted our gold, but I'd sooner walk the plank than surrender!\n", "\n", - "Just last week me crew and I found a map leading to the lost treasure of the island of Rundoon. We set sail right away, braving storms and sea creatures the size of ships! When we got to the island, it were guarded by angry natives with spears and poison darts. Me crew fought 'em off while I snuck into the temple and grabbed the treasure chest.\n", + "\"All hands to battle stations!\" I bellowed. \"Ready the cannons and prepare to board!\" \n", "\n", - "Now we be rich with dubloons and jewels! I plan to stash me loot on a remote island, then find a tavern and drink grog until I can't stand up straight. Being a pirate captain be a tough life, but someone's got to sail the high seas in search of adventure! Maybe one day I'll get enough treasure to retire and open up a little beach shack...but probably not, cause I love me pirate life too much! Har har har!" + "A mighty battle erupted, cannons boomin' and swords clashin'. We swung over on ropes and fought the Spaniards hand-to-hand on the pitchin' and rollin' deck. Me cutlass was a blur as I dueled their captain, a big brute with a wicked scar.\n", + "\n", + "Finally, I drove me blade into that bilge rat's black heart and he fell dead at me feet. His crew surrendered and we took their ship as a prize. In the hold, we found chests overflowing with gold doubloons and jewels - a king's ransom! \n", + "\n", + "We sailed off into the sunset, our pirate flag snappin' in the breeze, flush with coin and the thrill of victory. And that, me buckos, is a taste of the pirate life! Now who wants some grog?\n", + "\n", + "*laughs heartily*" ] } ], @@ -267,6 +282,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "d4b6ea50-d777-4174-a326-6e4e57b9ea8b", "metadata": {}, @@ -283,7 +299,7 @@ "source": [ "from llama_index.llms.anthropic import Anthropic\n", "\n", - "llm = Anthropic(model=\"claude-instant-1\")" + "llm = Anthropic(model=\"claude-3-sonnet-20240229\")" ] }, { @@ -306,23 +322,21 @@ "name": "stdout", "output_type": "stream", "text": [ - " Here are a few key facts about Paul Graham:\n", + "Paul Graham is a computer scientist, entrepreneur, venture capitalist, and author. He is best known for the following:\n", "\n", - "- Paul Graham is an American computer scientist, venture capitalist, and essayist. He is known for co-founding Viaweb, one of the first web-based application companies, which was acquired by Yahoo in 1998.\n", + "1. Co-founding Y Combinator: Y Combinator is a prominent startup accelerator based in Silicon Valley. It has funded and helped launch thousands of startups, including Airbnb, Dropbox, Stripe, and Reddit.\n", "\n", - "- In 2005, Graham co-founded Y Combinator, a startup accelerator that provides seed funding and advice to startups. Y Combinator has backed over 3,000 startups including Dropbox, Airbnb, Stripe, and Reddit. \n", + "2. Writing essays on startups and technology: Graham has written numerous influential essays on topics related to startups, programming, and entrepreneurship. His essays are widely read and have helped shape the thinking of many entrepreneurs and technologists.\n", "\n", - "- Graham has written several influential essays on startups, programming languages, and other technology topics. Some of his most well-known essays include \"Beating the Averages\", \"The Refragmentation\", and \"How to Start a Startup\".\n", + "3. Developing the programming language Arc: In the early 2000s, Graham developed a new programming language called Arc, which was designed to be a more powerful and expressive dialect of Lisp.\n", "\n", - "- He pioneered and popularized the idea of using Lisp as a web programming language via his company Viaweb. This helped inspire interest in functional programming languages for web development.\n", + "4. Advocating for the use of Lisp and functional programming: Graham is a strong proponent of the Lisp programming language and functional programming paradigms. He has written extensively about the benefits of these approaches and has influenced many programmers to explore them.\n", "\n", - "- Graham has a Bachelor's degree in philosophy from Cornell University and a PhD in computer science from Harvard University. \n", + "5. Authoring books: Graham has authored several books, including \"Hackers & Painters: Big Ideas from the Computer Age\" (2004), \"On Lisp\" (1993), and \"ANSI Common Lisp\" (1995).\n", "\n", - "- He was inducted into the American Academy of Arts and Sciences in 2020 for his contributions to computer science and entrepreneurship.\n", + "6. Investing in startups: Through Y Combinator and his own investments, Graham has invested in and advised numerous successful startups, helping to shape the technology industry.\n", "\n", - "- In addition to his work in technology and startups, Graham is also known for his essays on topics like education, productivity, and economics. Many consider him an influential writer and thinker in the tech industry.\n", - "\n", - "In summary, Paul Graham is a prominent computer scientist, entrepreneur, investor and writer who has made significant contributions to the web, startups and programming languages. He continues to share his insights through his writings and his work with Y Combinator." + "Overall, Paul Graham is widely respected in the technology and startup communities for his contributions as a programmer, writer, investor, and advocate for innovative ideas and approaches." ] } ], @@ -348,7 +362,7 @@ "source": [ "from llama_index.llms.anthropic import Anthropic\n", "\n", - "llm = Anthropic()\n", + "llm = Anthropic(\"claude-3-sonnet-20240229\")\n", "resp = await llm.acomplete(\"Paul Graham is \")" ] }, @@ -362,21 +376,19 @@ "name": "stdout", "output_type": "stream", "text": [ - " Here are some key facts about Paul Graham:\n", - "\n", - "- Paul Graham is an American computer scientist, venture capitalist, and essayist. He is known for co-founding Viaweb, one of the first web-based application companies, which was acquired by Yahoo in 1998.\n", + "Paul Graham is a computer scientist, entrepreneur, venture capitalist, and author. He is best known for the following:\n", "\n", - "- In 1995, Graham co-founded Viaweb with Robert Morris, Trevor Blackwell, and Jessica Livingston. The company helped popularize the business model of applying software as a service.\n", + "1. Co-founding Y Combinator: Y Combinator is a prominent startup accelerator based in Silicon Valley. It has funded and helped launch many successful startups, including Airbnb, Dropbox, Stripe, and Reddit.\n", "\n", - "- After selling Viaweb to Yahoo, Graham became a venture capitalist. He co-founded Y Combinator in 2005 with Jessica Livingston, Trevor Blackwell, and Robert Morris. Y Combinator is an influential startup accelerator that provides seed funding and advice to startups.\n", + "2. Writing essays on startups and technology: Graham has written numerous influential essays on topics related to startups, programming, and entrepreneurship. His essays are widely read and have helped shape the thinking of many entrepreneurs and technologists.\n", "\n", - "- Graham has written several influential essays on startups, technology, and programming. Some of his most well-known essays include \"How to Start a Startup\", \"Do Things that Don't Scale\", and \"Beating the Averages\" about Lisp programming. \n", + "3. Developing the programming language Arc: Graham designed and developed the programming language Arc, which was intended to be a more powerful and expressive dialect of Lisp.\n", "\n", - "- He pioneered the concept of using online essays to attract startup founders to apply to Y Combinator's program. His essays are often required reading in Silicon Valley.\n", + "4. Authoring books: He has written several books, including \"Hackers & Painters: Big Ideas from the Computer Age,\" \"ANSI Common Lisp,\" and \"On Lisp.\"\n", "\n", - "- Graham has a Bachelor's degree in philosophy from Cornell University and a PhD in computer science from Harvard University. His doctoral thesis focused on Lisp compilers.\n", + "5. Founding Viaweb: In the 1990s, Graham co-founded Viaweb, one of the earliest web-based application software companies. Viaweb was later acquired by Yahoo! in 1998.\n", "\n", - "- He is considered an influential figure in the tech and startup worlds, known for his insights on startups, programming languages, and technology trends. His writings have shaped the strategies of many founders building startups.\n" + "Graham is widely respected in the technology and startup communities for his insights, writings, and contributions to the field of computer science and entrepreneurship.\n" ] } ], @@ -387,9 +399,9 @@ ], "metadata": { "kernelspec": { - "display_name": "llama_index_v2", + "display_name": "anthropic_env", "language": "python", - "name": "llama_index_v2" + "name": "anthropic_env" }, "language_info": { "codemirror_mode": { @@ -401,6 +413,11 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3" + }, + "vscode": { + "interpreter": { + "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e" + } } }, "nbformat": 4, diff --git a/llama-index-integrations/llms/llama-index-llms-anthropic/llama_index/llms/anthropic/base.py b/llama-index-integrations/llms/llama-index-llms-anthropic/llama_index/llms/anthropic/base.py index 7fc634cfa05a0..19373290362dc 100644 --- a/llama-index-integrations/llms/llama-index-llms-anthropic/llama_index/llms/anthropic/base.py +++ b/llama-index-integrations/llms/llama-index-llms-anthropic/llama_index/llms/anthropic/base.py @@ -1,5 +1,5 @@ from typing import Any, Callable, Dict, Optional, Sequence - +from anthropic.types import ContentBlockDeltaEvent from llama_index.core.base.llms.types import ( ChatMessage, ChatResponse, @@ -28,12 +28,12 @@ from llama_index.core.types import BaseOutputParser, PydanticProgramMode from llama_index.llms.anthropic.utils import ( anthropic_modelname_to_contextsize, - messages_to_anthropic_prompt, + messages_to_anthropic_messages, ) import anthropic -DEFAULT_ANTHROPIC_MODEL = "claude-2" +DEFAULT_ANTHROPIC_MODEL = "claude-2.1" DEFAULT_ANTHROPIC_MAX_TOKENS = 512 @@ -128,7 +128,7 @@ def _model_kwargs(self) -> Dict[str, Any]: base_kwargs = { "model": self.model, "temperature": self.temperature, - "max_tokens_to_sample": self.max_tokens, + "max_tokens": self.max_tokens, } return { **base_kwargs, @@ -143,15 +143,18 @@ def _get_all_kwargs(self, **kwargs: Any) -> Dict[str, Any]: @llm_chat_callback() def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse: - prompt = messages_to_anthropic_prompt(messages) + anthropic_messages, system_prompt = messages_to_anthropic_messages(messages) all_kwargs = self._get_all_kwargs(**kwargs) - response = self._client.completions.create( - prompt=prompt, stream=False, **all_kwargs + response = self._client.messages.create( + messages=anthropic_messages, + stream=False, + system=system_prompt, + **all_kwargs, ) return ChatResponse( message=ChatMessage( - role=MessageRole.ASSISTANT, content=response.completion + role=MessageRole.ASSISTANT, content=response.content[0].text ), raw=dict(response), ) @@ -167,24 +170,25 @@ def complete( def stream_chat( self, messages: Sequence[ChatMessage], **kwargs: Any ) -> ChatResponseGen: - prompt = messages_to_anthropic_prompt(messages) + anthropic_messages, system_prompt = messages_to_anthropic_messages(messages) all_kwargs = self._get_all_kwargs(**kwargs) - response = self._client.completions.create( - prompt=prompt, stream=True, **all_kwargs + response = self._client.messages.create( + messages=anthropic_messages, system=system_prompt, stream=True, **all_kwargs ) def gen() -> ChatResponseGen: content = "" role = MessageRole.ASSISTANT for r in response: - content_delta = r.completion - content += content_delta - yield ChatResponse( - message=ChatMessage(role=role, content=content), - delta=content_delta, - raw=r, - ) + if isinstance(r, ContentBlockDeltaEvent): + content_delta = r.delta.text + content += content_delta + yield ChatResponse( + message=ChatMessage(role=role, content=content), + delta=content_delta, + raw=r, + ) return gen() @@ -199,15 +203,18 @@ def stream_complete( async def achat( self, messages: Sequence[ChatMessage], **kwargs: Any ) -> ChatResponse: - prompt = messages_to_anthropic_prompt(messages) + anthropic_messages, system_prompt = messages_to_anthropic_messages(messages) all_kwargs = self._get_all_kwargs(**kwargs) - response = await self._aclient.completions.create( - prompt=prompt, stream=False, **all_kwargs + response = await self._aclient.messages.create( + messages=anthropic_messages, + system=system_prompt, + stream=False, + **all_kwargs, ) return ChatResponse( message=ChatMessage( - role=MessageRole.ASSISTANT, content=response.completion + role=MessageRole.ASSISTANT, content=response.content[0].text ), raw=dict(response), ) @@ -223,24 +230,25 @@ async def acomplete( async def astream_chat( self, messages: Sequence[ChatMessage], **kwargs: Any ) -> ChatResponseAsyncGen: - prompt = messages_to_anthropic_prompt(messages) + anthropic_messages, system_prompt = messages_to_anthropic_messages(messages) all_kwargs = self._get_all_kwargs(**kwargs) - response = await self._aclient.completions.create( - prompt=prompt, stream=True, **all_kwargs + response = await self._aclient.messages.create( + messages=anthropic_messages, system=system_prompt, stream=True, **all_kwargs ) async def gen() -> ChatResponseAsyncGen: content = "" role = MessageRole.ASSISTANT async for r in response: - content_delta = r.completion - content += content_delta - yield ChatResponse( - message=ChatMessage(role=role, content=content), - delta=content_delta, - raw=r, - ) + if isinstance(r, ContentBlockDeltaEvent): + content_delta = r.delta.text + content += content_delta + yield ChatResponse( + message=ChatMessage(role=role, content=content), + delta=content_delta, + raw=r, + ) return gen() diff --git a/llama-index-integrations/llms/llama-index-llms-anthropic/llama_index/llms/anthropic/utils.py b/llama-index-integrations/llms/llama-index-llms-anthropic/llama_index/llms/anthropic/utils.py index 3cfbd0177d0df..1a22b8cdbeea5 100644 --- a/llama-index-integrations/llms/llama-index-llms-anthropic/llama_index/llms/anthropic/utils.py +++ b/llama-index-integrations/llms/llama-index-llms-anthropic/llama_index/llms/anthropic/utils.py @@ -1,17 +1,18 @@ -from typing import Dict, Sequence +from typing import Dict, Sequence, Tuple from llama_index.core.base.llms.types import ChatMessage, MessageRole HUMAN_PREFIX = "\n\nHuman:" ASSISTANT_PREFIX = "\n\nAssistant:" - CLAUDE_MODELS: Dict[str, int] = { "claude-instant-1": 100000, "claude-instant-1.2": 100000, "claude-2": 100000, "claude-2.0": 100000, "claude-2.1": 200000, + "claude-3-opus-20240229": 200000, + "claude-3-sonnet-20240229": 200000, } @@ -25,6 +26,21 @@ def anthropic_modelname_to_contextsize(modelname: str) -> int: return CLAUDE_MODELS[modelname] +def messages_to_anthropic_messages( + messages: Sequence[ChatMessage], +) -> Tuple[Sequence[ChatMessage], str]: + anthropic_messages = [] + system_prompt = "" + for message in messages: + if message.role == MessageRole.SYSTEM: + system_prompt = message.content + else: + message = {"role": message.role.value, "content": message.content} + anthropic_messages.append(message) + return anthropic_messages, system_prompt + + +# Function used in bedrock def _message_to_anthropic_prompt(message: ChatMessage) -> str: if message.role == MessageRole.USER: prompt = f"{HUMAN_PREFIX} {message.content}" diff --git a/llama-index-integrations/llms/llama-index-llms-anthropic/pyproject.toml b/llama-index-integrations/llms/llama-index-llms-anthropic/pyproject.toml index 5e9ce75b791bd..2101bff443627 100644 --- a/llama-index-integrations/llms/llama-index-llms-anthropic/pyproject.toml +++ b/llama-index-integrations/llms/llama-index-llms-anthropic/pyproject.toml @@ -27,12 +27,12 @@ exclude = ["**/BUILD"] license = "MIT" name = "llama-index-llms-anthropic" readme = "README.md" -version = "0.1.3" +version = "0.1.4" [tool.poetry.dependencies] python = ">=3.8.1,<4.0" llama-index-core = "^0.10.1" -anthropic = "^0.11.0" +anthropic = "^0.17.0" [tool.poetry.group.dev.dependencies] ipython = "8.10.0"