Add anthropic docs (#5882)

<!-- Thank you for your contribution! Please review
https://microsoft.github.io/autogen/docs/Contribute before opening a
pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

Add anthropic docs

- Add api docs 
- Add sample code + usage in agent chat user guide

<!-- Please give a short summary of the change and the problem this
solves. -->

## Related issue number

<!-- For example: "Closes #1234" -->

Closes #5856 

## Checks

- [ ] I've included any doc changes needed for
<https://microsoft.github.io/autogen/>. See
<https://github.com/microsoft/autogen/blob/main/CONTRIBUTING.md> to
build and test documentation locally.
- [ ] I've added tests (if relevant) corresponding to the changes
introduced in this PR.
- [ ] I've made sure all auto checks have passed.
This commit is contained in:
Victor Dibia 2025-03-08 19:35:28 -08:00 committed by GitHub
parent 704dab1018
commit 134a8c71ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 64 additions and 26 deletions

View File

@ -51,6 +51,7 @@ python/autogen_ext.models.cache
python/autogen_ext.models.openai
python/autogen_ext.models.replay
python/autogen_ext.models.azure
python/autogen_ext.models.anthropic
python/autogen_ext.models.semantic_kernel
python/autogen_ext.models.ollama
python/autogen_ext.tools.code_execution

View File

@ -0,0 +1,8 @@
autogen\_ext.models.anthropic
=============================
.. automodule:: autogen_ext.models.anthropic
:members:
:undoc-members:
:show-inheritance:

View File

@ -228,6 +228,47 @@
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Anthropic (experimental)\n",
"\n",
"To use the {py:class}`~autogen_ext.models.anthropic.AnthropicChatCompletionClient`, you need to install the `anthropic` extra. Underneath, it uses the `anthropic` python sdk to access the models.\n",
"You will also need to obtain an [API key](https://console.anthropic.com) from Anthropic."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !pip install -U \"autogen-ext[anthropic]\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"finish_reason='stop' content=\"The capital of France is Paris. It's not only the political and administrative capital but also a major global center for art, fashion, gastronomy, and culture. Paris is known for landmarks such as the Eiffel Tower, the Louvre Museum, Notre-Dame Cathedral, and the Champs-Élysées.\" usage=RequestUsage(prompt_tokens=14, completion_tokens=73) cached=False logprobs=None thought=None\n"
]
}
],
"source": [
"from autogen_core.models import UserMessage\n",
"from autogen_ext.models.anthropic import AnthropicChatCompletionClient\n",
"\n",
"anthropic_client = AnthropicChatCompletionClient(model=\"claude-3-7-sonnet-20250219\")\n",
"result = await anthropic_client.create([UserMessage(content=\"What is the capital of France?\", source=\"user\")])\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -434,7 +475,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.11.9"
}
},
"nbformat": 4,

View File

@ -910,16 +910,23 @@ class AnthropicChatCompletionClient(
.. code-block:: python
import asyncio
from autogen_ext.models.anthropic import AnthropicChatCompletionClient
from autogen_core.models import UserMessage
anthropic_client = AnthropicChatCompletionClient(
model="claude-3-sonnet-20240229",
api_key="your-api-key", # Optional if ANTHROPIC_API_KEY is set in environment
)
result = await anthropic_client.create([UserMessage(content="What is the capital of France?", source="user")])
print(result)
async def main():
anthropic_client = AnthropicChatCompletionClient(
model="claude-3-sonnet-20240229",
api_key="your-api-key", # Optional if ANTHROPIC_API_KEY is set in environment
)
result = await anthropic_client.create([UserMessage(content="What is the capital of France?", source="user")]) # type: ignore
print(result)
if __name__ == "__main__":
asyncio.run(main())
To load the client from a configuration:
@ -933,25 +940,6 @@ class AnthropicChatCompletionClient(
}
client = ChatCompletionClient.load_component(config)
The client supports function calling with Claude models that have the capability:
.. code-block:: python
from autogen_core.tools import FunctionTool
def get_weather(location: str) -> str:
'''Get the weather for a location'''
return f"The weather in {location} is sunny."
tool = FunctionTool(get_weather)
result = await anthropic_client.create(
[UserMessage(content="What's the weather in Paris?", source="user")],
tools=[tool],
)
"""
component_type = "model"