Skip to content

Commit 922b5af

Browse files
committed
simplify docker with automatic framework detection
1 parent 47400f8 commit 922b5af

File tree

2 files changed

+6
-27
lines changed

2 files changed

+6
-27
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,18 @@ Here’s how to get started with a basic `PydanticAI` agent:
5757
```python
5858
from fastapi import FastAPI
5959
from fastapi_agents import FastAPIAgents
60-
from fastapi_agents.pydantic_ai import PydanticAIAgent
6160
from pydantic_ai import Agent
6261

62+
# Create a FastAPI application and FastAPIAgents instance.
6363
app = FastAPI()
6464
agents = FastAPIAgents(path_prefix="/agents")
6565

66-
# Initialize and register the agent
66+
# Initialize and register the agent.
67+
# The framework is automatically detected.
6768
agent = Agent("openai:gpt-4o-mini")
68-
agents.register("pydanticai", PydanticAIAgent(agent))
69+
agents.register("pydanticai", agent)
6970

70-
# Include the router
71+
# Include the router in your FastAPI application.
7172
app.include_router(agents)
7273
```
7374

@@ -141,7 +142,6 @@ The pre-built images support the following environment variables for customisati
141142

142143
| Variable | Example Value | Description |
143144
|--------------------|--------------------|------------------------------------------------------------|
144-
| `AGENT_FRAMEWORK` | `pydantic-ai` | Specifies the agent framework to use. |
145145
| `AGENT_MODULE` | `agent.pydantic_ai` | Path to the agent module. |
146146
| `AGENT_CLASS` | `agent` | Class name for the agent. |
147147
| `SECURITY_MODULE` | `agent.pydantic_ai` | Specifies the security module for the agent. |

docker/main.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import importlib
55

66
# get environment variables
7-
agent_framework = os.getenv("AGENT_FRAMEWORK", "pydantic_ai")
87
agent_module = os.getenv("AGENT_MODULE", "agent")
98
agent_class = os.getenv("AGENT_CLASS", "agent")
109
security_module = os.getenv("SECURITY_MODULE", None)
@@ -19,22 +18,6 @@
1918
if security_class == '':
2019
security_class = None
2120

22-
# dynamically import agent framework
23-
if agent_framework == "pydantic-ai":
24-
from fastapi_agents.pydantic_ai import PydanticAIAgent as AgentWrapper
25-
from pydantic_ai import Agent as BaseAgentClass
26-
elif agent_framework == "smolagents":
27-
from fastapi_agents.smolagents import SmolagentsAgent as AgentWrapper
28-
from smolagents import MultiStepAgent as BaseAgentClass
29-
elif agent_framework == "llama-index":
30-
from fastapi_agents.llama_index import LlamaIndexAgent as AgentWrapper
31-
from llama_index.core.agent.types import BaseAgent as BaseAgentClass
32-
elif agent_framework == "crewai":
33-
from fastapi_agents.crewai import CrewAIAgent as AgentWrapper
34-
from crewai import Crew as BaseAgentClass
35-
else:
36-
raise ValueError(f"Unknown agent framework: {agent_framework}")
37-
3821
# create FastAPI app and FastAPIAgents instance
3922
app = FastAPI()
4023
agents = FastAPIAgents(path_prefix=api_prefix, mode=api_mode)
@@ -43,10 +26,6 @@
4326
import_agent_module = importlib.import_module(agent_module)
4427
agent = getattr(import_agent_module, agent_class)
4528

46-
# check type of agent
47-
if not issubclass(type(agent), BaseAgentClass):
48-
raise ValueError(f"Agent must be a subclass of {type(BaseAgentClass)}, not {agent}")
49-
5029
# dynamically import security module
5130
if security_class and security_module:
5231
import_security_module = importlib.import_module(security_module)
@@ -59,7 +38,7 @@
5938
# register agent with FastAPIAgents
6039
agents.register(
6140
api_endpoint,
62-
AgentWrapper(agent),
41+
agent,
6342
security_dependency=security
6443
)
6544
app.include_router(agents)

0 commit comments

Comments
 (0)