📖
【Azure AI Agent Service】- Function callingエージェントの作り方
執筆日
2025/4/7
やること
Azure AI Agent SeriviceでFunction Callingエージェントを作ってみる。
OpenWeatherAPIで天気の情報を取得してみる。
前提
- Azure AI Agent Serviceを構築済みであること
- OpenWeatherAPIを発行済みであること
Function callingエージェントを作る(GUI編)
Azure AI Agent Seriviceは、GUIベースでFunction Callingエージェントが作ることはできません!!(現時点では)
Function callingエージェントを作る(コード編)
MicrosoftからコードのサンプルがGithub上にあります。こちらをベースにします。
天気予報を取得する
import os, time, json, requests, math
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.projects.models import FunctionTool, RequiredFunctionToolCall, SubmitToolOutputsAction, ToolOutput
project_client = AIProjectClient.from_connection_string(
credential=DefaultAzureCredential(), conn_str=<"projectの接続文字列">
)
OPEN_WEATHER_API_KEY = <"OpenWeatherAPIKEY">
# 天気予報を取得するAPI
def get_current_weather(city_name: str) -> str:
geocoding_response = requests.get(
"http://api.openweathermap.org/geo/1.0/direct",
params={
"q": f"{city_name},jp",
"limit": 1,
"appid": OPEN_WEATHER_API_KEY
}
)
geocodings = geocoding_response.json()
if not geocodings:
return json.dumps({"error": "Geocoding data not found"})
geocoding = geocodings[0]
lat, lon = geocoding["lat"], geocoding["lon"]
current_weather_response = requests.get(
"https://api.openweathermap.org/data/2.5/weather",
params={
"lat": lat,
"lon": lon,
"units": "metric",
"lang": "ja",
"appid": OPEN_WEATHER_API_KEY
}
)
current_weather = current_weather_response.json()
result = {
"city_name": city_name,
"description": current_weather["weather"][0]["description"],
"temperature": math.floor(current_weather["main"]["temp"])
}
return json.dumps(result)
# Initialize function tool with user functions
functions = FunctionTool(functions=[get_current_weather])
with project_client:
# Create an agent and run user's request with function calls
agent = project_client.agents.create_agent(
model=<"model名">,
name="my-assistant",
instructions="You are a helpful assistant",
tools=functions.definitions,
)
print(f"Created agent, ID: {agent.id}")
thread = project_client.agents.create_thread()
print(f"Created thread, ID: {thread.id}")
message = project_client.agents.create_message(
thread_id=thread.id,
role="user",
content="大阪の天気を教えて",
)
print(f"Created message, ID: {message.id}")
run = project_client.agents.create_run(thread_id=thread.id, agent_id=agent.id)
print(f"Created run, ID: {run.id}")
while run.status in ["queued", "in_progress", "requires_action"]:
time.sleep(1)
run = project_client.agents.get_run(thread_id=thread.id, run_id=run.id)
if run.status == "requires_action" and isinstance(run.required_action, SubmitToolOutputsAction):
tool_calls = run.required_action.submit_tool_outputs.tool_calls
if not tool_calls:
print("No tool calls provided - cancelling run")
project_client.agents.cancel_run(thread_id=thread.id, run_id=run.id)
break
tool_outputs = []
for tool_call in tool_calls:
if isinstance(tool_call, RequiredFunctionToolCall):
try:
print(f"Executing tool call: {tool_call}")
output = functions.execute(tool_call)
tool_outputs.append(
ToolOutput(
tool_call_id=tool_call.id,
output=output,
)
)
except Exception as e:
print(f"Error executing tool_call {tool_call.id}: {e}")
print(f"Tool outputs: {tool_outputs}")
if tool_outputs:
project_client.agents.submit_tool_outputs_to_run(
thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs
)
print(f"Current run status: {run.status}")
print(f"Run completed with status: {run.status}")
# Delete the agent when done
project_client.agents.delete_agent(agent.id)
print("Deleted agent")
# Fetch and log all messages
messages = project_client.agents.list_messages(thread_id=thread.id)
print(f"Messages: {messages}")
結果
Created agent, ID: asst_2tneh4tO9eWw3QA88ZFoRqfQ
Created thread, ID: thread_7vRznHqFXmL6E5gxbF2G39R1
Created message, ID: msg_KxIhtChBoLpAJDKGKFU4j7ml
Created thread, ID: thread_7vRznHqFXmL6E5gxbF2G39R1
Created message, ID: msg_KxIhtChBoLpAJDKGKFU4j7ml
Created message, ID: msg_KxIhtChBoLpAJDKGKFU4j7ml
Created run, ID: run_I7j3BmFNSGRt8o1xSqq9bSXQ
Created run, ID: run_I7j3BmFNSGRt8o1xSqq9bSXQ
Executing tool call: {'id': 'call_hSUdvFAhfk38AN802Wqi6xdJ', 'type': 'function', 'function': {'name': 'get_current_weather', 'arguments': '{"city_name":"大阪"}'}}
Tool outputs: [{'tool_call_id': 'call_hSUdvFAhfk38AN802Wqi6xdJ', 'output': '{"city_name": "\\u5927\\u962a", "description": "\\u6674\\u5929", "temperature": 17}'}]
Current run status: RunStatus.REQUIRES_ACTION
Current run status: RunStatus.COMPLETED
Run completed with status: RunStatus.COMPLETED
Deleted agent
Messages: {'object': 'list', 'data': [{'id': 'msg_B2qW0VgZegO1gRDM3RXHWeyb', 'object': 'thread.message', 'created_at': 1743827689, 'assistant_id': 'asst_2tneh4tO9eWw3QA88ZFoRqfQ', 'thread_id': 'thread_7vRznHqFXmL6E5gxbF2G39R1', 'run_id': 'run_I7j3BmFNSGRt8o1xSqq9bSXQ', 'role': 'assistant', 'content': [{'type': 'text', 'text': {'value': '大阪の現在の天気は晴れで、気温は17℃です。', 'annotations': []}}], 'attachments': [], 'metadata': {}}, {'id': 'msg_KxIhtChBoLpAJDKGKFU4j7ml', 'object': 'thread.message', 'created_at': 1743827682, 'assistant_id': None, 'thread_id': 'thread_7vRznHqFXmL6E5gxbF2G39R1', 'run_id': None, 'role': 'user', 'content': [{'type': 'text', 'text': {'value': '大阪の天気を教えて', 'annotations': []}}], 'attachments': [], 'metadata': {}}], 'first_id': 'msg_B2qW0VgZegO1gRDM3RXHWeyb', 'last_id': 'msg_KxIhtChBoLpAJDKGKFU4j7ml', 'has_more': False}
Discussion