Dependencies
Introduction
When writing agents, you often rely on common resources, whether data for running your agent or for recording the output. Instead of manually setting up these resources with extensive boilerplate code, we use dependency injection to declaratively acquire them, allowing you to focus on developing your agent.
What is Dependency Injection
We follow dependencies as defined in: Fastapi Dependencies. More examples can be found in the linked documentation.
“Dependency Injection” means, in programming, that there is a way for your code (in this case, your path operation functions) to declare things that it requires to work and use: “dependencies”.
And then, that system(e.g. FastAPI or encord-agents) will take care of doing whatever is needed to provide your code with those needed dependencies (“inject” the dependencies)
Practice
When defining your agents, you can easily inject essential dependencies, such as the path to the underlying asset or frame iterators. You can also add custom dependencies if needed.
To inject dependencies, simply type-annotate your agent function variables using the Depends
class.
The {module}
depends on which type of agent you’re building.
See the references section for more details on available dependencies.
Custom Dependencies
To add a custom dependencies:
- Define a function to load the dependencies.
- Use that function as a dependency.
The function itself can also rely on other dependencies if needed, allowing more complicated resource acquisition. See the internals of dep_video_iterator
for an example of this.