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/faos-optimize/references/python-patterns.md
1# Python FAOS (Foundry Agent Optimization Service) Optimization Patterns23These patterns are framework-neutral. Use them to expose Python agent behavior knobs to FAOS while preserving the app's current runtime.45## Base Contract67Use this when there is one clear instructions/model surface.89```python10import os1112from agent_optimization import load_config1314SYSTEM_PROMPT = """You are a helpful assistant."""1516config = load_config(17default_instructions=SYSTEM_PROMPT,18default_model=os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1"),19default_skills_dir="skills",20)21```2223Then map the resolved values into the existing framework:2425```python26instructions = config.compose_instructions()27model = config.model or os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1")28```2930Only apply temperature if the framework supports it:3132```python33options = {}34if config.temperature is not None:35options["temperature"] = config.temperature36```3738## Multi-Agent Named Targets3940When a Python app has multiple agents, use names that match the architecture rather than one generic `config`.4142```python43orchestrator_config = load_config(44default_instructions=ORCHESTRATOR_PROMPT,45default_model=os.getenv("ORCHESTRATOR_MODEL_DEPLOYMENT_NAME", os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1")),46default_skills_dir="skills/orchestrator",47)4849tool_agent_config = load_config(50default_instructions=TOOL_AGENT_PROMPT,51default_model=os.getenv("TOOL_AGENT_MODEL_DEPLOYMENT_NAME", os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1")),52default_skills_dir="skills/tool-agent",53)54```5556Use the evaluator objective to choose which named target to add first. For example, `intent_resolution` usually points to `orchestrator_config`, while `builtin.tool_call_accuracy` often points to `tool_agent_config`.5758## Microsoft Agent Framework5960Keep the current hosting adapter and agent construction. Replace only the selected knobs.6162```python63agent = Agent(64client=client,65instructions=config.compose_instructions(),66tools=existing_tools,67default_options=default_options,68)69```7071For model selection:7273```python74client = FoundryChatClient(75project_endpoint=project_endpoint,76model=config.model or os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1"),77credential=credential,78)79```8081If the model client is shared by multiple agents, flag this as a global side effect in the review summary.8283## FastAPI or Custom Responses Runtime8485Keep the existing HTTP contract. Use config values where the model call is created.8687```python88instructions = body.get("instructions", config.compose_instructions())89model = body.get("model", config.model or os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1"))90```9192When the app already supports request-level overrides, preserve them and use FAOS config as the default.9394## LangGraph or Workflow Runtimes9596Do not rewrite the graph. Identify node-level prompts and model clients.9798- Router/planner nodes are good targets for `intent_resolution`.99- Tool nodes are good targets for `builtin.tool_call_accuracy`.100- Final synthesis nodes are good targets for `relevance`, style, and task adherence.101102Prefer node-specific config names:103104```python105router_config = load_config(106default_instructions=ROUTER_PROMPT,107default_model=default_model,108)109```110111## Optional Skill Support112113`default_skills_dir="skills"` records the default skill location. It does not automatically make the runtime load files or expose skill tools.114115Add file-based skill support only when the target framework has a safe tool-calling or plugin mechanism. If adding it, use progressive disclosure:1161171. Startup prompt contains skill name and description only1182. Model calls a tool such as `load_skill` to load full skill instructions1193. Model calls a file-reading tool only for deep skill assets when needed120121Do not append every `SKILL.md` body into every agent prompt by default, especially in multi-agent architectures.122123## Dependency Guidance124125Add dependencies only when needed:126127```text128python-dotenv>=1.0.0129azure-identity>=1.19.0130```131132Use `python-dotenv` when local `.env` support exists. Use `azure-identity` when the local resolver uses Entra tokens.133134## Environment Variables135136The canonical local `agent_optimization` package uses these optimization variables:137138| Variable | Purpose |139| -------- | ------- |140| `AGENT_OPTIMIZATION_CONFIG` | Inline JSON config from the optimization service |141| `OPTIMIZATION_CONFIG` | Non-reserved inline JSON fallback |142| `AGENT_OPTIMIZATION_CANDIDATE_ID` | Candidate identifier to resolve from a service |143| `AGENT_OPTIMIZATION_RESOLVE_ENDPOINT` | Resolver API base URL |144| `AGENT_OPTIMIZATION_SKILLS_DIR` | Download location for candidate skill files |145146Do not add all of these to `agent.yaml` by default. For hosted agent vNext, do not place `AGENT_*` variables in the user-authored deployment payload because they are platform-reserved there. Use the optimization service/runtime injection path or local development environment for `AGENT_OPTIMIZATION_*`, and use `OPTIMIZATION_CONFIG` only when an inline non-reserved fallback is explicitly needed.147148Do not generate `FAOS_OPTIMIZATION_*` aliases in the base package unless the user explicitly requests a compatibility adapter. The reference package and FAOS contract use `AGENT_OPTIMIZATION_*` plus `OPTIMIZATION_CONFIG`.149150## Canonical Local Package151152When the target repository does not already provide an optimization package, add this split-file package rather than a single-file loader:153154```text155agent_optimization/156__init__.py157_config.py158_resolver.py159```160161`__init__.py` should only re-export the public API:162163```python164"""Agent optimization config loader for hosted agents."""165166from agent_optimization._config import OptimizationConfig, Skill, load_config167168__all__ = ["OptimizationConfig", "Skill", "load_config"]169__version__ = "0.1.0"170```171172`_config.py` owns `Skill`, `OptimizationConfig`, `load_config`, default fallback behavior, inline config parsing, and candidate config handoff.173174`_resolver.py` owns candidate resolution, using `{endpoint}/candidates/{candidate_id}/config` for resolved config, optional skill-file download from the candidate manifest, and `DefaultAzureCredential` with the `https://ml.azure.com/.default` scope.175176## Verification Checklist177178- Changed Python files compile179- `from agent_optimization import load_config` succeeds from the agent root180- `load_config(default_instructions="x", default_model="m")` returns defaults when no optimization env vars are set181- Existing entrypoint, hosting adapter, and protocol remain unchanged182- Multi-agent targets are named and documented183- Evaluator objective influenced the target selection or was explicitly unavailable184- User is asked to review before deployment185