Skip to content

Commit

Permalink
added calling codex
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftyos committed May 10, 2024
1 parent 2c86ada commit 367e7d0
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 90 deletions.
111 changes: 110 additions & 1 deletion autogpts/autogpt/autogpt/commands/create_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@
from autogpt.command_decorator import command
from autogpt.core.utils.json_schema import JSONSchema
from autogpt.models.command import Command
from pydantic import BaseModel
import aiohttp
import logging
import asyncio

logger = logging.getLogger(__name__)


class FunctionSpecRequest(BaseModel):
"""
A request to generate a correctly formated function spec
"""

name: str
description: str
inputs: str
outputs: str


class FunctionResponse(BaseModel):
id: str
name: str
requirements: list[str]
code: str


class CreateCommandComponent(DirectiveProvider, CommandProvider):
Expand Down Expand Up @@ -36,7 +60,92 @@ def get_commands(self) -> Iterator["Command"]: # type: ignore
description="The description of what the command needs to do",
required=True,
),
"inputs": JSONSchema(
type=JSONSchema.Type.STRING,
description="The description of what inputs the command needs",
required=True,
),
"output": JSONSchema(
type=JSONSchema.Type.STRING,
description="The description of what the command needs to output",
required=True,
),
},
)
def execute_create_command(self, command_name: str, description: str) -> str:
def execute_create_command(
self, command_name: str, description: str, inputs: str, outputs: str
) -> str:
"""
Executes the create command with the given parameters.
Args:
command_name (str): The name of the command.
description (str): The description of the command.
inputs (str): The inputs of the command.
outputs (str): The outputs of the command.
Returns:
str: The generated code.
Raises:
Exception: If there is an error when calling Codex.
"""
req = FunctionSpecRequest(
name=command_name, description=description, inputs=inputs, outputs=outputs
)
try:
func_date = self.run_generate_command(req)
code = func_date.code
except Exception as e:
return f"Error when calling Codex: {e}"

return code

def create_command(self, func: FunctionResponse) -> str:
return ""

def run_generate_command(self, req: FunctionSpecRequest) -> FunctionResponse:
loop = asyncio.new_event_loop()
spec = loop.run_until_complete(self._create_spec(req))
func = loop.run_until_complete(self._write_function(spec)) # type: ignore
return func

async def _write_function(self, req) -> FunctionResponse: # type: ignore
headers: dict[str, str] = {"accept": "application/json"}

url = f"{self.codex_base_url}/function/spec/"
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=req) as response:
response.raise_for_status()

data = await response.json()
return FunctionResponse(**data)

except aiohttp.ClientError as e:
logger.exception(f"Error getting user: {e}")
raise e
except Exception as e:
logger.exception(f"Unknown Error when write function: {e}")
raise e

async def _create_spec(self, req: FunctionSpecRequest):
headers: dict[str, str] = {"accept": "application/json"}

url = f"{self.codex_base_url}/function/spec/"
try:
async with aiohttp.ClientSession() as session:
async with session.post(
url, headers=headers, json=req.json()
) as response:
response.raise_for_status()

data = await response.json()
return data

except aiohttp.ClientError as e:
logger.exception(f"Error getting user: {e}")
raise e
except Exception as e:
logger.exception(f"Unknown Error when write function: {e}")
raise e

0 comments on commit 367e7d0

Please sign in to comment.