Spaces:
Sleeping
Sleeping
feat: Add multi-provider support for agent generation with model validation, new templates, and security checks.
84cf689
Configure Model Options
Goal Description
Update the agent generation form to include the latest Anthropic models and add support for Hugging Face models. Additionally, implement backend validation, streaming support, security measures, a sandbox executor, and observability.
User Review Required
I am adding
claude-3-5-haiku-20241022to the list. I am also adding support for Hugging Face models (viahuggingface_hub). I am adding backend validation using Pydantic. I am adding a streaming response toggle. I am adding security measures (pre-commit hook). I am adding a Sandbox Executor for safe agent execution. I am adding Prometheus metrics and a/healthzendpoint.
Proposed Changes
Frontend
[MODIFY] AgentForm.jsx
- Add a "Provider" dropdown (Anthropic, Hugging Face).
- Update "Model" dropdown options based on the selected provider.
- Add
claude-3-5-haiku-20241022for Anthropic. - Add
meta-llama/Meta-Llama-3-8B-Instructandmistralai/Mistral-7B-Instruct-v0.3for Hugging Face. - Add a "Stream Response" checkbox (default: false).
- Add a "Test Agent" button to execute the generated code in the sandbox.
- Display execution results (output/errors) in the UI.
Backend
[NEW] models.py
- Define
ProviderEnum(Anthropic, HuggingFace). - Define
GenerateRequestPydantic model with validation:provider: ProviderEnummodel: Validated against an allowlist per provider.stream: bool (default: False)
[MODIFY] main.py
- Import
GenerateRequestfrommodels.py. - Update
generate_agentendpoint to use the new validation model. - Add
/api/executeendpoint. - Add
prometheus-fastapi-instrumentator. - Add
/metricsand/healthzendpoints.
[MODIFY] agent_builder.py
- Update
build_agentto acceptproviderandstreamarguments. - Select the appropriate template (
agent_template.py.j2oragent_template_hf.py.j2) based on the provider. - Pass
streamto the template context.
[NEW] agent_template_hf.py.j2
- Create a new Jinja2 template for agents using
huggingface_hub.InferenceClient. - Use
HUGGINGFACEHUB_API_TOKENfor authentication. - Implement conditional logic for
stream=Truevsstream=False.
[MODIFY] agent_template.py.j2
- Fix the environment variable name from
GEMINI_API_KEYtoANTHROPIC_API_KEY.
Sandbox Executor
[NEW] sandbox.py
- Implement
run_in_sandbox(code: str, task: str) -> str:- Write code to a temporary file.
- Use
subprocess.Popento execute the file. - Use
preexec_fnto setresource.setrlimit:RLIMIT_CPU: Limit CPU time (e.g., 30 seconds).RLIMIT_AS: Limit address space (memory) (e.g., 512MB).
- Capture
stdoutandstderr. - Handle timeouts and errors.
Security & Misc
[NEW] .env.example
- Add
ANTHROPIC_API_KEYandHUGGINGFACEHUB_API_TOKENplaceholders.
[NEW] pre-commit-check.sh
- Simple script to grep for potential API keys in staged files.
[MODIFY] requirements.txt
- Add
prometheus-fastapi-instrumentator.
Verification Plan
Automated Tests
- Run
pytestto ensure no regressions inagent_builder.
Manual Verification
- Start the frontend (
npm run devinfrontend/). - Verify the new provider and model options.
- Verify the stream toggle works.
- Generate an agent with streaming enabled and check the code.
- Test the "Test Agent" button with a simple task.
- Verify
/metricsand/healthzendpoints. - Try to commit a file with a fake API key to test the hook.