Debian Codegraph @

src/anthropic/lib/foundry.md

raw
1 # Anthropic Foundry
2
3 To use this library with Foundry, use the `AnthropicFoundry` class instead of the `Anthropic` class.
4
5
6 ## Installation
7
8 ```bash
9 pip install anthropic
10 ```
11
12 ## Usage
13
14 ### Basic Usage with API Key
15
16 ```python
17 from anthropic import AnthropicFoundry
18
19 client = AnthropicFoundry(
20 api_key="...", # defaults to ANTHROPIC_FOUNDRY_API_KEY environment variable
21 resource="my-resource", # your Foundry resource
22 )
23
24 message = client.messages.create(
25 model="claude-sonnet-5",
26 max_tokens=1024,
27 messages=[{"role": "user", "content": "Hello!"}],
28 )
29
30 print(message.content[0].text)
31 ```
32
33 ### Using Azure AD Token Provider
34
35 For enhanced security, you can use Azure AD (Microsoft Entra) authentication instead of an API key:
36
37 ```python
38 from anthropic import AnthropicFoundry
39 from azure.identity import DefaultAzureCredential
40 from azure.identity import get_bearer_token_provider
41
42 credential = DefaultAzureCredential()
43 token_provider = get_bearer_token_provider(
44 credential,
45 "https://ai.azure.com/.default"
46 )
47
48 client = AnthropicFoundry(
49 azure_ad_token_provider=token_provider,
50 resource="my-resource",
51 )
52
53 message = client.messages.create(
54 model="claude-sonnet-5",
55 max_tokens=1024,
56 messages=[{"role": "user", "content": "Hello!"}],
57 )
58
59 print(message.content[0].text)
60 ```
61
62 ## Examples
63
64 ### Streaming Messages
65
66 ```python
67 from anthropic import AnthropicFoundry
68
69 client = AnthropicFoundry(
70 api_key="...",
71 resource="my-resource",
72 )
73
74 with client.messages.stream(
75 model="claude-sonnet-5",
76 max_tokens=1024,
77 messages=[{"role": "user", "content": "Write a haiku about programming"}],
78 ) as stream:
79 for text in stream.text_stream:
80 print(text, end="", flush=True)
81 ```
82
83 ### Async Usage
84
85 ```python
86 from anthropic import AsyncAnthropicFoundry
87
88 async def main():
89 client = AsyncAnthropicFoundry(
90 api_key="...",
91 resource="my-resource",
92 )
93
94 message = await client.messages.create(
95 model="claude-sonnet-5",
96 max_tokens=1024,
97 messages=[{"role": "user", "content": "Hello!"}],
98 )
99
100 print(message.content[0].text)
101
102 import asyncio
103 asyncio.run(main())
104 ```
105
106 ### Async Streaming
107
108 ```python
109 from anthropic import AsyncAnthropicFoundry
110
111 async def main():
112 client = AsyncAnthropicFoundry(
113 api_key="...",
114 resource="my-resource",
115 )
116
117 async with client.messages.stream(
118 model="claude-sonnet-5",
119 max_tokens=1024,
120 messages=[{"role": "user", "content": "Write a haiku about programming"}],
121 ) as stream:
122 async for text in stream.text_stream:
123 print(text, end="", flush=True)
124
125 import asyncio
126 asyncio.run(main())
127 ```