Skip to content

Commit b83270d

Browse files
committed
create/delete/list agent
1 parent 2b1edf9 commit b83270d

File tree

3 files changed

+94
-19
lines changed

3 files changed

+94
-19
lines changed

docs/references.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
- [Azure Cosmos DB No SQL](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db_no_sql/)
3030
- [Azure AI Search](https://python.langchain.com/docs/integrations/vectorstores/azuresearch/)
3131

32+
### Azure AI Foundry
33+
34+
- [Quickstart: Get started with Azure AI Foundry](https://learn.microsoft.com/azure/ai-foundry/quickstarts/get-started-code?tabs=python&pivots=fdp-project)
35+
- [azure-rest-api-specs/specification/ai/data-plane/Azure.AI.Agents](https://github.com/Azure/azure-rest-api-specs/tree/main/specification/ai/data-plane/Azure.AI.Agents)
36+
3237
### Services
3338

3439
- [FastAPI](https://fastapi.tiangolo.com/)

scripts/azure_ai_foundry_operator.py

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,13 @@ def chat(
3636
logger.setLevel(logging.DEBUG)
3737

3838
logger.info("Running Azure AI Foundry chat...")
39-
# https://learn.microsoft.com/azure/ai-foundry/quickstarts/get-started-code?tabs=python&pivots=fdp-project
40-
from azure.ai.projects import AIProjectClient
41-
from azure.identity import DefaultAzureCredential
4239

43-
settings = AzureAiFoundryWrapper().settings
40+
wrapper = AzureAiFoundryWrapper()
4441

45-
project = AIProjectClient(
46-
endpoint=settings.azure_ai_foundry_inference_endpoint,
47-
credential=DefaultAzureCredential(),
48-
)
49-
models = project.get_openai_client(api_version=settings.azure_ai_foundry_inference_api_version)
50-
response = models.chat.completions.create(
51-
model=settings.azure_ai_foundry_inference_model_chat,
42+
openai_client = wrapper.get_openai_client()
43+
44+
response = openai_client.chat.completions.create(
45+
model=wrapper.settings.azure_ai_foundry_inference_model_chat,
5246
messages=[
5347
{"role": "user", "content": query},
5448
],
@@ -57,12 +51,18 @@ def chat(
5751

5852

5953
@app.command()
60-
def chat_langchain(
61-
query: str = typer.Option(
62-
"Hello",
63-
"--query",
64-
"-q",
65-
help="The query to send to the AI",
54+
def create_agent(
55+
name: str = typer.Option(
56+
"MyAgent",
57+
"--name",
58+
"-n",
59+
help="The name of the agent",
60+
),
61+
instructions: str = typer.Option(
62+
"This is my agent",
63+
"--instructions",
64+
"-i",
65+
help="The instructions for the agent",
6666
),
6767
verbose: bool = typer.Option(
6868
False,
@@ -75,8 +75,63 @@ def chat_langchain(
7575
if verbose:
7676
logger.setLevel(logging.DEBUG)
7777

78-
logger.info("Running Azure AI Foundry chat...")
79-
# FIXME: impl
78+
logger.info("Creating agent...")
79+
project_client = AzureAiFoundryWrapper().get_ai_project_client()
80+
with project_client.agents as agents_client:
81+
# Create a new agent
82+
agent = agents_client.create_agent(
83+
name=name,
84+
instructions=instructions,
85+
model=AzureAiFoundryWrapper().settings.azure_ai_foundry_inference_model_chat,
86+
)
87+
logger.info(f"Created agent: {agent.as_dict()}")
88+
89+
90+
@app.command()
91+
def delete_agent(
92+
agent_id: str = typer.Option(
93+
"asst_xxx",
94+
"--agent-id",
95+
"-a",
96+
help="The ID of the agent to delete",
97+
),
98+
verbose: bool = typer.Option(
99+
False,
100+
"--verbose",
101+
"-v",
102+
help="Enable verbose output",
103+
),
104+
):
105+
# Set up logging
106+
if verbose:
107+
logger.setLevel(logging.DEBUG)
108+
109+
logger.info("Deleting agent...")
110+
project_client = AzureAiFoundryWrapper().get_ai_project_client()
111+
with project_client.agents as agents_client:
112+
agents_client.delete_agent(agent_id=agent_id)
113+
114+
115+
@app.command()
116+
def list_agents(
117+
verbose: bool = typer.Option(
118+
False,
119+
"--verbose",
120+
"-v",
121+
help="Enable verbose output",
122+
),
123+
):
124+
# Set up logging
125+
if verbose:
126+
logger.setLevel(logging.DEBUG)
127+
128+
logger.info("Listing agents...")
129+
130+
project_client = AzureAiFoundryWrapper().get_ai_project_client()
131+
with project_client.agents as agents_client:
132+
agents = agents_client.list_agents()
133+
for agent in agents:
134+
logger.info(f"Agent ID: {agent.id}, Name: {agent.name}")
80135

81136

82137
if __name__ == "__main__":

template_langgraph/llms/azure_ai_foundrys.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from functools import lru_cache
22

3+
from azure.ai.projects import AIProjectClient
4+
from azure.identity import DefaultAzureCredential
35
from pydantic_settings import BaseSettings, SettingsConfigDict
46

57

@@ -26,3 +28,16 @@ def __init__(self, settings: Settings = None):
2628
if settings is None:
2729
settings = get_azure_ai_foundry_settings()
2830
self.settings = settings
31+
self.project_client = AIProjectClient(
32+
endpoint=self.settings.azure_ai_foundry_inference_endpoint,
33+
credential=DefaultAzureCredential(),
34+
)
35+
self.openai_client = self.project_client.get_openai_client(
36+
api_version=self.settings.azure_ai_foundry_inference_api_version
37+
)
38+
39+
def get_ai_project_client(self):
40+
return self.project_client
41+
42+
def get_openai_client(self):
43+
return self.openai_client

0 commit comments

Comments
 (0)