Api Agent
This example will walk you through creating an Api agent and integrating it into your Multi-Agent Orchestrator System. Let’s dive in!
📚Prerequisites:
- Basic knowledge of TypeScript or Python
- Familiarity with the Multi-Agent Orchestrator System
🧬 1. Create the Api Agent class:
Let’s create our ApiAgent
class. This class extends the Agent
abstract class from the Multi-Agent Orchestrator.
The process_request method must be implemented by the ApiAgent
import { Tabs, TabItem } from ‘@astrojs/starlight/components’;
This ApiAgent class provides flexibility for users to customize how input is encoded before sending to the API, how output is decoded after receiving from the API, and how headers are generated. This is done through three optional callbacks in the ApiAgentOptions interface:
- input_payload_encoder
- output_payload_decoder
- headers_callback
Let’s break these down:
1. input_payload_encoder: This function allows users to customize how the input is formatted before sending it to the API.
- Default behavior: If not provided, it uses the default_input_payload_encoder, which creates a payload with
input
andhistory
fields. - Custom behavior: Users can provide their own function to format the input however their API expects it. This function receives the input text, chat history, and other parameters, allowing for flexible payload creation.
Example usage:
2. output_payload_decoder: This function allows users to customize how the API response is processed.
- Default behavior: If not provided, it uses the default_output_payload_decoder, which simply returns the
output
field from the response. - Custom behavior: Users can provide their own function to extract and process the relevant data from the API response.
Example usage:
3. headers_callback: This function allows users to add custom headers to the API request.
- Default behavior: If not provided, it only sets the ‘Content-Type’ header to ‘application/json’.
- Custom behavior: Users can provide their own function to return additional headers, which will be merged with the default headers.
Example usage:
To use these custom functions, you would include them in the options when creating a new ApiAgent. This design allows users to adapt the ApiAgent to work with a wide variety of APIs without having to modify the core ApiAgent class. It provides a flexible way to handle different API specifications and requirements.
Now that we have our ApiAgent
, let’s add it to the Multi-Agent Orchestrator:
🔗 2. Add ApiAgent to the orchestrator:
If you have used the quickstarter sample program, you can add the Api agent and run it:
🎉And You’re All Set!
3.💡 Next Steps:
- Experiment with different Api endpoints
- Create specialized agents for various tasks using ApiAgent
- Include your existing Api with the Multi-agent orchestrator
Happy coding! 🚀