The Swarms FastAPI module is designed to manage and interact with multiple language models through a RESTful API interface. This documentation will cover the classes, functions, and endpoints provided by this module, and provide comprehensive examples on how to use them effectively.
The Swarms API provides endpoints to interact with various language models, manage agent configurations, and handle token counting. This documentation covers the available endpoints, input and output models, and detailed examples for each endpoint.
### Purpose
## Endpoints
The purpose of this module is to create a flexible, scalable API service that can interface with various language models including OpenAIChat, GPT4o, GPT4VisionAPI, and Anthropic models. This allows for dynamic model selection, efficient token counting, and handling user requests for AI-generated content.
### Key Features
- **Dynamic Model Switching**: Easily switch between different language models based on the user input.
- **Token Counting**: Efficiently count tokens using the `tiktoken` library.
- **Agent Configuration**: Configure and run agents with detailed settings for various tasks.
- **CORS Handling**: Support for Cross-Origin Resource Sharing (CORS) to allow web-based clients to interact with the API.
## Class Definitions
### `AgentInput`
The `AgentInput` class defines the structure of the input data required to configure and run an agent.
This endpoint handles the completion request for an agent configured with the given input parameters.
**Method:** `POST`
**Request Model:** `AgentInput`
**Response Model:** `AgentOutput`
**Description:**
Receives an `AgentInput` configuration, sets up the agent, processes the request, and returns the completion results.
**Description:**
This endpoint handles the completion request for an agent configured with the given input parameters. It processes the request and returns the completion results.
**Request Example:**
```json
{
"agent_name": "Swarm Agent",
"system_prompt": "Summarize the following text",
"agent_description": "An agent that summarizes text",
"model_name": "OpenAIChat",
"max_loops": 1,
"autosave": false,
"dynamic_temperature_enabled": false,
"dashboard": false,
"verbose": false,
"streaming_on": true,
"saved_state_path": null,
"sop": null,
"sop_list": null,
"user_name": "User",
"retry_attempts": 3,
"context_length": 8192,
"task": "This is a sample text that needs to be summarized."
}
```
**Response Example:**
```json
{
"agent": {
"agent_name": "Swarm Agent",
"system_prompt": "Summarize the following text",
"agent_description": "An agent that summarizes text",
"model_name": "OpenAIChat",
"max_loops": 1,
"autosave": false,
"dynamic_temperature_enabled": false,
"dashboard": false,
"verbose": false,
"streaming_on": true,
"saved_state_path": null,
"sop": null,
"sop_list": null,
"user_name": "User",
"retry_attempts": 3,
"context_length": 8192,
"task": "This is a sample text that needs to be summarized."
},
"completions": {
"choices": [
{
"index": 0,
"message": {
"role": "Swarm Agent",
"content": "The sample text summarizes how to perform text summarization using an agent.",
"name": null
}
}
],
"stream_choices": null,
"usage_info": {
"prompt_tokens": 10,
"completion_tokens": 15,
"total_tokens": 25
}
}
}
```
**Example Usage:**
@ -146,75 +139,88 @@ class AgentInput(BaseModel):
task: str = None
agent_input = AgentInput(task="Generate a summary of the provided text.")
The `AgentInput` class defines the structure of the input data required to configure and run an agent.
The FastAPI application is initialized with CORS middleware to allow cross-origin requests. This is essential for enabling web-based clients to interact with the API without facing CORS issues.
| `agent` | `AgentInput` | The input configuration used to create the agent.|
| `completions` | `ChatCompletionResponse` | The response generated by the agent. |
## Functions
### count_tokens
The `count_tokens` function counts the number of tokens in a given text using the `tiktoken` library.
**Parameters:**
- `text` (`str`): The text to be tokenized and counted.
### Model Switching Logic
**Returns:**
- `int`: The number of tokens in the text.
The `model_router` function encapsulates the logic for switching between different language models based on the input model name. This is crucial for dynamic model selection based on user preferences.
The `model_router` function switches to the specified language model based on the provided model name.
**Parameters:**
The `count_tokens` function uses the `tiktoken` library to encode and count the number of tokens in a given text. This is essential for managing token limits and understanding the cost implications of API requests.
- `model_name` (`str`): The name of the model to switch to.
- [OpenAI API Documentation](https://beta.openai.com/docs/)
This documentation provides a comprehensive guide to using the Swarms FastAPI module, with detailed descriptions, examples, and implementation insights to help developers effectively utilize the provided functionalities.
- **Token Management**: Keep track of token usage to optimize API costs and manage rate limits effectively.