Skip to content

Commit 3ae9e45

Browse files
committed
1 parent d9c6f40 commit 3ae9e45

File tree

6 files changed

+711
-0
lines changed

6 files changed

+711
-0
lines changed

examples/azure.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# /// script
2+
# requires-python = ">=3.9"
3+
# dependencies = [
4+
# "anthropic",
5+
# ]
6+
#
7+
# [tool.uv.sources]
8+
# anthropic = { path = "../", editable = true }
9+
# ///
10+
11+
from anthropic import AnthropicFoundry
12+
13+
cl = AnthropicFoundry(
14+
resource="your-resource-name",
15+
api_key="your-foundry-anthropic-api-key",
16+
)
17+
18+
response = cl.messages.create(
19+
model="claude-haiku-4-5",
20+
messages=[
21+
{"role": "user", "content": "Hello!"},
22+
],
23+
max_tokens=1024,
24+
)
25+
26+
print(response)

src/anthropic/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
from .lib.tools import beta_tool, beta_async_tool
102102
from .lib.vertex import *
103103
from .lib.bedrock import *
104+
from .lib.foundry import AnthropicFoundry as AnthropicFoundry, AsyncAnthropicFoundry as AsyncAnthropicFoundry
104105
from .lib.streaming import *
105106

106107
_setup_logging()

src/anthropic/_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ def default_headers(self) -> dict[str, str | Omit]:
183183

184184
@override
185185
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
186+
if headers.get("Authorization") or headers.get("X-Api-Key"):
187+
# valid
188+
return
189+
186190
if self.api_key and headers.get("X-Api-Key"):
187191
return
188192
if isinstance(custom_headers.get("X-Api-Key"), Omit):
@@ -423,6 +427,10 @@ def default_headers(self) -> dict[str, str | Omit]:
423427

424428
@override
425429
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
430+
if headers.get("Authorization") or headers.get("X-Api-Key"):
431+
# valid
432+
return
433+
426434
if self.api_key and headers.get("X-Api-Key"):
427435
return
428436
if isinstance(custom_headers.get("X-Api-Key"), Omit):

src/anthropic/lib/foundry.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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-3-5-sonnet-20241022",
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-3-5-sonnet-20241022",
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-3-5-sonnet-20241022",
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-3-5-sonnet-20241022",
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-3-5-sonnet-20241022",
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+
```

0 commit comments

Comments
 (0)