generated from kyegomez/Python-Package-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kye Gomez
authored and
Kye Gomez
committed
Aug 16, 2024
1 parent
0109abd
commit 7be3ce4
Showing
12 changed files
with
407 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ | |
GenerateResponse, | ||
GenerationConfig, | ||
ModelCard, | ||
ModelList, | ||
ModelPermission, | ||
UsageInfo, | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.