Skip to content

Commit

Permalink
[PARALLEL SWARM_API]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye Gomez authored and Kye Gomez committed Aug 16, 2024
1 parent 0109abd commit 7be3ce4
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 34 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Infrastructure for scalable, reliable, and economical Multi-Modal Model API serv
- Swarms Memory API
- OES API
- Swarms Multi-Agent API
- Parallel API
- Sequential API
- Hiearchical API Etc


# License
MIT
Expand Down
4 changes: 0 additions & 4 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ async def create_agent(request: Request, agent_input: AgentInput):
max_loops=agent_input.max_loops,
autosave=agent_input.autosave,
dynamic_temperature_enabled=agent_input.dynamic_temperature_enabled,
dashboard=agent_input.dashboard,
verbose=agent_input.verbose,
streaming_on=agent_input.streaming_on,
saved_state_path=agent_input.saved_state_path,
sop=agent_input.sop,
Expand Down Expand Up @@ -200,8 +198,6 @@ async def agent_completions(agent_input: AgentInput):
max_loops=max_loops,
autosave=agent_input.autosave,
dynamic_temperature_enabled=agent_input.dynamic_temperature_enabled,
dashboard=agent_input.dashboard,
verbose=agent_input.verbose,
streaming_on=agent_input.streaming_on,
saved_state_path=agent_input.saved_state_path,
sop=agent_input.sop,
Expand Down
131 changes: 131 additions & 0 deletions parallel_swarm_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import os

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from swarms_cloud.schema.agent_api_schemas import (
ParallelSwarmAPIInput,
ParallelSwarmAPIOutput,
)
from swarms_cloud.schema.swarm_schema import SwarmAPISchema, AllSwarmsSchema
from swarms_cloud.utils.create_agent import create_agent_sync

# Create a FastAPI app
app = FastAPI(
debug=True,
title="Parallel Swarm API",
version="0.1.0",
)

# Load the middleware to handle CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


@app.get("/")
def read_root():
return {"Hello": "World"}


@app.get("/health")
def health():
return {"status": "ok"}


@app.get("/version")
def version():
return {"version": "0.1.0"}


@app.post("v1/swarms/parallel/create/{swarm_id}", response_model=SwarmAPISchema)
def create_parallel_swarm(request: Request, swarm_input: ParallelSwarmAPIInput):
config = swarm_input.config
task = swarm_input.task

created_agents = []

# Parse the schema for all the agents
for agent in swarm_input.agents:
created_agents.append(create_agent_sync(agent))

# Now execute all the agents in parallel
import concurrent.futures

with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(agent.run, task) for agent in created_agents]

# Wait for all the tasks to complete
results = [
future.result() for future in concurrent.futures.as_completed(futures)
]

#


@app.post(
"v1/swarms/parallel/{swarm_id}/completions", response_model=ParallelSwarmAPIOutput
)
def run_parallel_swarm_completions(
request: Request, swarm_input: ParallelSwarmAPIInput
):
config = swarm_input.config
task = swarm_input.task

created_agents = []

# Parse the schema for all the agents
for agent in swarm_input.agents:
created_agents.append(create_agent_sync(agent))

# Now execute all the agents in parallel
import concurrent.futures

with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(agent.run, task) for agent in created_agents]

# Wait for all the tasks to complete
results = [
future.result() for future in concurrent.futures.as_completed(futures)
]

# log_entry = ParallelSwarmAPIOutput(
# completions=MultipleAgentOutputs(
# agents =
# )
# )


@app.post("v1/swarms", response_model=AllSwarmsSchema)
def get_all_swarms(request: Request):
return AllSwarmsSchema(
swarms=[
SwarmAPISchema(
id="1",
swarm_name="Swarm API",
swarm_description="Swarm API description",
created_at=1628584185,
owned_by="TGSC",
tags=["tag_1", "agent"],
use_cases={
"use_case_1": "Use case 1 description",
"use_case_2": "Use case 2 description",
},
)
]
)


if __name__ == "__main__":
import uvicorn

uvicorn.run(
app,
host="0.0.0.0",
port=os.getenv("AGENT_PORT"),
use_colors=True,
log_level="info",
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "swarms-cloud"
version = "0.3.4"
version = "0.3.7"
description = "Swarms Cloud - Pytorch"
license = "MIT"
authors = ["Kye Gomez <[email protected]>"]
Expand Down
1 change: 0 additions & 1 deletion swarms_cloud/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
GenerateResponse,
GenerationConfig,
ModelCard,
ModelList,
ModelPermission,
UsageInfo,
)
Expand Down
62 changes: 39 additions & 23 deletions swarms_cloud/schema/agent_api_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@
from swarms_cloud.schema.cog_vlm_schemas import (
AgentChatCompletionResponse,
)
from swarms_cloud.schema.swarm_schema import SwarmAPISchema


# Define the input model using Pydantic
class AgentInput(BaseModel):
id: str = (uuid.uuid4().hex,)
id: str = uuid.uuid4().hex
created_at: int = time.time()
owned_by: Optional[str] = Field(None, description="The owner of the agent.")
agent_name: str = "Swarm Agent"
system_prompt: str = None
agent_description: str = None
model_name: str = "OpenAIChat"
max_loops: int = 1
autosave: bool = False
dynamic_temperature_enabled: bool = False
dashboard: bool = False
verbose: bool = False
streaming_on: bool = False
saved_state_path: str = "agent_saved_state.json"
sop: str = None
sop_list: List[str] = None
user_name: str = "User"
Expand Down Expand Up @@ -80,20 +77,14 @@ class ModelSchema(BaseModel):

class ModelList(BaseModel):
object: str = "list"
data: List[ModelSchema] = []
data: List[ModelSchema] = Field(..., description="The list of models available.")


# Define the output model using Pydantic
class AgentOutput(BaseModel):
completions: AgentChatCompletionResponse


class MultipleAgentOutputs(BaseModel):
agents: List[AgentOutput] = Field(
..., description="The list of agents and their completions."
)


class ParallelSwarmAPIInput(BaseModel):
"""
Represents a parallel swarm API.
Expand All @@ -105,11 +96,13 @@ class ParallelSwarmAPIInput(BaseModel):
owned_by (str): The owner of the API.
"""

id: str = (uuid.uuid4().hex,)
swarm_name: str = "Swarm API"
agents: List[AgentInput] = []
created_at: int = time.time()
owned_by: str = "TGSC"
config: SwarmAPISchema = Field(
..., description="The configuration for the swarm API."
)
agents: List[AgentInput] = Field(
..., description="The list of agents in the swarm."
)
task: str = Field(..., description="The task to be performed by the agents.,")


class ParallelSwarmAPIOutput(BaseModel):
Expand All @@ -123,10 +116,33 @@ class ParallelSwarmAPIOutput(BaseModel):
owned_by (str): The owner of the API.
"""

id: str = uuid.uuid4().hex
swarm_name: str = "Swarm API"
completions: MultipleAgentOutputs = Field(
..., description="The list of agents in the swarm."
config: SwarmAPISchema = Field(
..., description="The configuration for the swarm API."
)
created_at: int = time.time()
owned_by: str = "TGSC"
completions: List[AgentOutput] = Field(
..., description="The list of agents and their completions."
)


# full_example = ParallelSwarmAPIOutput(
# completions=[
# AgentOutput(
# completions=AgentChatCompletionResponse(
# agent_name="Agent 1",
# completion="Completion 1",
# created_at=1628584185,
# owned_by="TGSC",
# )
# ),
# AgentOutput(
# completions=AgentChatCompletionResponse(
# agent_name="Agent 2",
# completion="Completion 2",
# created_at=1628584185,
# owned_by="TGSC",
# )
# ),
# ]
# )

# print(full_example.dict())
4 changes: 2 additions & 2 deletions swarms_cloud/schema/cog_vlm_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class UsageInfo(BaseModel):
prompt_tokens: int = 0
total_tokens: int = 0
completion_tokens: Optional[int] = 0
tokens_per_second: Optional[float] = Field(default_factory=lambda: 0.0)


class ChatCompletionResponse(BaseModel):
Expand All @@ -104,7 +105,7 @@ class ChatCompletionResponse(BaseModel):

class AgentChatCompletionResponse(BaseModel):
id: str = f"agent-{uuid.uuid4().hex}"
agent: str = Field(
agent_name: str = Field(
...,
description="The name of the agent that generated the completion response.",
)
Expand All @@ -115,7 +116,6 @@ class AgentChatCompletionResponse(BaseModel):
created: Optional[int] = Field(default_factory=lambda: int(time.time()))
usage: Optional[UsageInfo] = None
completion_time: Optional[float] = Field(default_factory=lambda: 0.0)
tokens_per_second: Optional[float] = Field(default_factory=lambda: 0.0)


# out = AgentChatCompletionResponse(
Expand Down
55 changes: 55 additions & 0 deletions swarms_cloud/schema/swarm_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pydantic import BaseModel, Field
from typing import Dict, Optional, List
import time
import uuid


class SwarmAPISchema(BaseModel):

id: str = Field(default_factory=lambda: uuid.uuid4().hex)
swarm_name: Optional[str] = Field(default="Swarm API")
swarm_description: Optional[str] = Field(default="Swarm API description")
created_at: Optional[int] = Field(default_factory=lambda: int(time.time()))
owned_by: Optional[str] = Field(
default="TGSC",
description="The owner of the API.",
examples="TGSC",
)
tags: Optional[list] = Field(
default=...,
description="The tags for the API.",
examples=["tag_1", "agent"],
)
use_cases: Optional[Dict[str, str]] = Field(
default=...,
description="The use cases for the API.",
examples={
"use_case_1": "Use case 1 description",
"use_case_2": "Use case 2 description",
},
)


class AllSwarmsSchema(BaseModel):
swarms: Optional[List[SwarmAPISchema]] = Field(
default=...,
description="The list of all swarms.",
examples=[],
)


# example = {
# "swarm_name": "Swarm API",
# "swarm_description": "Swarm API description",
# "created_at": 1628584185,
# "owned_by": "TGSC",
# "tags": ["tag_1", "agent"],
# "use_cases": {
# "use_case_1": "Use case 1 description",
# "use_case_2": "Use case 2 description",
# },
# }

# # Define the input model using Pydantic
# out = SwarmAPISchema(**example)
# print(out)
7 changes: 5 additions & 2 deletions swarms_cloud/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from swarms_cloud.utils.api_key_generator import generate_api_key
from swarms_cloud.utils.calculate_pricing import calculate_pricing, count_tokens
from swarms_cloud.utils.rate_limiter import rate_limiter
from swarms_cloud.utils.calculate_pricing import calculate_pricing, count_tokens_hf
from swarms_cloud.utils.check_model_list import (
create_error_response,
)
from swarms_cloud.utils.count_tokens import count_tokens, count_tokens_async
from swarms_cloud.utils.rate_limiter import rate_limiter

__all__ = [
"generate_api_key",
"calculate_pricing",
"count_tokens",
"rate_limiter",
"create_error_response",
"count_tokens_async",
"count_tokens_hf",
]
4 changes: 3 additions & 1 deletion swarms_cloud/utils/calculate_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from transformers import AutoTokenizer, PreTrainedTokenizer


def count_tokens(texts: List[str], tokenizer: PreTrainedTokenizer, model: str) -> int:
def count_tokens_hf(
texts: List[str], tokenizer: PreTrainedTokenizer, model: str
) -> int:
"""
Counts the total number of tokens in a list of texts using a tokenizer.
Expand Down
Loading

0 comments on commit 7be3ce4

Please sign in to comment.