Skip to content

Commit 160c846

Browse files
authored
Python: Added more examples (#208)
* Small cleanup * Added Azure and Foundry chat client samples * Added Azure chat client agent example
1 parent d964b05 commit 160c846

9 files changed

Lines changed: 100 additions & 13 deletions
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
from random import randint
5+
from typing import Annotated
6+
7+
from agent_framework import ChatClientAgent
8+
from agent_framework.azure import AzureChatClient
9+
from pydantic import Field
10+
11+
12+
def get_weather(
13+
location: Annotated[str, Field(description="The location to get the weather for.")],
14+
) -> str:
15+
"""Get the weather for a given location."""
16+
conditions = ["sunny", "cloudy", "rainy", "stormy"]
17+
return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C."
18+
19+
20+
async def main() -> None:
21+
instructions = "You are a helpful assistant, you can help the user with weather information."
22+
agent = ChatClientAgent(AzureChatClient(), instructions=instructions, tools=get_weather)
23+
print(str(await agent.run("What's the weather in Amsterdam?")))
24+
25+
26+
if __name__ == "__main__":
27+
asyncio.run(main())

python/samples/getting_started/agents/foundry/foundry_basic.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from agent_framework import ChatClientAgent
88
from agent_framework.foundry import FoundryChatClient
9-
from dotenv import load_dotenv
109
from pydantic import Field
1110

1211

@@ -33,5 +32,4 @@ async def main() -> None:
3332

3433

3534
if __name__ == "__main__":
36-
load_dotenv()
3735
asyncio.run(main())

python/samples/getting_started/agents/foundry/foundry_with_existing_agent.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from agent_framework.foundry import FoundryChatClient
1010
from azure.ai.projects.aio import AIProjectClient
1111
from azure.identity.aio import AzureCliCredential
12-
from dotenv import load_dotenv
1312
from pydantic import Field
1413

1514

@@ -46,5 +45,4 @@ async def main() -> None:
4645

4746

4847
if __name__ == "__main__":
49-
load_dotenv()
5048
asyncio.run(main())

python/samples/getting_started/agents/foundry/foundry_with_existing_client.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from agent_framework.foundry import FoundryChatClient
1010
from azure.ai.projects.aio import AIProjectClient
1111
from azure.identity.aio import AzureCliCredential
12-
from dotenv import load_dotenv
1312
from pydantic import Field
1413

1514

@@ -43,5 +42,4 @@ async def main() -> None:
4342

4443

4544
if __name__ == "__main__":
46-
load_dotenv()
4745
asyncio.run(main())

python/samples/getting_started/agents/foundry/foundry_with_explicit_settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from agent_framework import ChatClientAgent
99
from agent_framework.foundry import FoundryChatClient
1010
from azure.identity.aio import AzureCliCredential
11-
from dotenv import load_dotenv
1211
from pydantic import Field
1312

1413

@@ -40,5 +39,4 @@ async def main() -> None:
4039

4140

4241
if __name__ == "__main__":
43-
load_dotenv()
4442
asyncio.run(main())

python/samples/getting_started/agents/chat_client_agent.py renamed to python/samples/getting_started/agents/openai/openai_chat_client_agent.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from agent_framework import ChatClientAgent
88
from agent_framework.openai import OpenAIChatClient
9-
from dotenv import load_dotenv
109
from pydantic import Field
1110

1211

@@ -25,5 +24,4 @@ async def main() -> None:
2524

2625

2726
if __name__ == "__main__":
28-
load_dotenv()
2927
asyncio.run(main())
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
from random import randint
5+
from typing import Annotated
6+
7+
from agent_framework.azure import AzureChatClient
8+
from pydantic import Field
9+
10+
11+
def get_weather(
12+
location: Annotated[str, Field(description="The location to get the weather for.")],
13+
) -> str:
14+
"""Get the weather for a given location."""
15+
conditions = ["sunny", "cloudy", "rainy", "stormy"]
16+
return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C."
17+
18+
19+
async def main() -> None:
20+
client = AzureChatClient()
21+
message = "What's the weather in Amsterdam and in Paris?"
22+
stream = False
23+
print(f"User: {message}")
24+
if stream:
25+
print("Assistant: ", end="")
26+
async for chunk in client.get_streaming_response(message, tools=get_weather):
27+
if str(chunk):
28+
print(str(chunk), end="")
29+
print("")
30+
else:
31+
response = await client.get_response(message, tools=get_weather)
32+
print(f"Assistant: {response}")
33+
34+
35+
if __name__ == "__main__":
36+
asyncio.run(main())
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
from random import randint
5+
from typing import Annotated
6+
7+
from agent_framework.foundry import FoundryChatClient
8+
from pydantic import Field
9+
10+
11+
def get_weather(
12+
location: Annotated[str, Field(description="The location to get the weather for.")],
13+
) -> str:
14+
"""Get the weather for a given location."""
15+
conditions = ["sunny", "cloudy", "rainy", "stormy"]
16+
return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C."
17+
18+
19+
async def main() -> None:
20+
client = FoundryChatClient()
21+
message = "What's the weather in Amsterdam and in Paris?"
22+
stream = False
23+
print(f"User: {message}")
24+
if stream:
25+
print("Assistant: ", end="")
26+
async for chunk in client.get_streaming_response(message, tools=get_weather):
27+
if str(chunk):
28+
print(str(chunk), end="")
29+
print("")
30+
else:
31+
response = await client.get_response(message, tools=get_weather)
32+
print(f"Assistant: {response}")
33+
34+
35+
if __name__ == "__main__":
36+
asyncio.run(main())

python/samples/getting_started/chat_client/openai_chat_client.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
from random import randint
55
from typing import Annotated
66

7-
from agent_framework import ai_function
87
from agent_framework.openai import OpenAIChatClient
98
from pydantic import Field
109

1110

12-
@ai_function
1311
def get_weather(
1412
location: Annotated[str, Field(description="The location to get the weather for.")],
1513
) -> str:
@@ -18,7 +16,7 @@ def get_weather(
1816
return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C."
1917

2018

21-
async def main():
19+
async def main() -> None:
2220
client = OpenAIChatClient()
2321
message = "What's the weather in Amsterdam and in Paris?"
2422
stream = False

0 commit comments

Comments
 (0)