Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build and deploy AI applications on Azure AI Foundry using Microsoft's model catalog and AI services
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
foundry-agent/agent-optimizer/references/python-patterns.md
1# Python Agent Optimizer in Foundry Patterns23Use the Azure SDK optimization package and a local baseline folder. The baseline is file-based; call `load_config()` without code-level fallback parameters.45## Install and Import67Add `azure-ai-agentserver-optimization` to `requirements.txt` or the project dependency file:89```text10azure-ai-agentserver-optimization11```1213Import from the SDK namespace:1415```python16from azure.ai.agentserver.optimization import load_config17```1819## Baseline Folder2021Create `.agent_configs/baseline/` beside `agent.yaml`:2223```text24<agent-root>/25agent.yaml26.agent_configs/27baseline/28metadata.yaml29instructions.md30tools.json31skills/<skill-name>/SKILL.md32```3334Example `metadata.yaml`:3536```yaml37model: <existing-chat-model-deployment-name>38temperature: 0.739instruction_file: instructions.md40skill_dir: skills41tool_file: tools.json42```4344`instructions.md` contains the selected baseline system/developer instructions. Include only skill folders relevant to the optimization goal.4546Choose a `model` value that already exists as a model deployment in the target Foundry project. Do not assume `gpt-4o` is available.4748## Tools File4950Use OpenAI function-calling tool objects under top-level `tools`. Currently, only function tool definition optimization is supported:5152```json53{54"tools": [55{56"type": "function",57"function": {58"name": "lookup_policy",59"description": "Look up the company travel policy.",60"parameters": {61"type": "object",62"properties": {63"dept": {64"type": "string",65"description": "Department name"66}67}68}69}70}71]72}73```7475## Runtime Wiring7677Call `load_config()` with no defaults:7879```python80config = load_config()81instructions = config.compose_instructions()82model = config.model83```8485For Microsoft Agent Framework:8687```python88client = FoundryChatClient(89project_endpoint=project_endpoint,90model=config.model,91credential=credential,92)9394agent = Agent(95client=client,96instructions=config.compose_instructions(),97tools=tools,98)99```100101Patch optimized function tool definitions through the public helper. It updates matching function docs, descriptions, and parameter descriptions:102103```python104config.apply_tool_descriptions(tools)105```106107Load skills on demand when the runtime has a safe skill/tool mechanism:108109```python110from pathlib import Path111from azure.ai.agentserver.optimization import load_skills_from_dir112113skills = load_skills_from_dir(Path(config.skills_dir)) if config.skills_dir else []114```115116## Target Selection117118Use evaluator and dataset goals to decide what belongs in the baseline:119120| Signal | Prefer |121| ------ | ------ |122| `relevance`, `task_adherence` | primary instructions and model |123| `intent_resolution` | router/orchestrator instructions |124| `builtin.tool_call_accuracy` | tool-calling instructions and OpenAI function tool definitions |125| safety/groundedness | safety, retrieval, citation, or answer-synthesis instructions |126127For multi-agent apps, scaffold the target role's instructions and related skills/tools. Do not merge unrelated role prompts into one baseline.128129## Runtime Config130131The SDK reads optimization context from supported runtime sources. Keep `.agent_configs/baseline/` present so default `load_config()` startup has a local baseline. Use `load_config(config_dir="my_configs")` only for non-default local config directories, and `load_config(required=False)` only when the app can intentionally run without optimization config.132133## Verification Checklist134135- Dependency file includes `azure-ai-agentserver-optimization`136- `from azure.ai.agentserver.optimization import load_config` succeeds137- `.agent_configs/baseline/metadata.yaml` exists and points to existing files138- `load_config()` is called without defaults unless using an intentional `config_dir` or `required=False`139- Changed Python files compile and preserve the hosting adapter/protocol140- User is asked to review before deployment141