Skip to content

Commit

Permalink
fix: fix tool creation to accept dev portal POST request (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahwooders authored Aug 17, 2024
1 parent 69a1ad2 commit 88eb58a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
40 changes: 38 additions & 2 deletions memgpt/server/rest_api/tools/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ListToolsResponse(BaseModel):


class CreateToolRequest(BaseModel):
json_schema: dict = Field(..., description="JSON schema of the tool.")
json_schema: dict = Field(..., description="JSON schema of the tool.") # NOT OpenAI - just has `name`
source_code: str = Field(..., description="The source code of the function.")
source_type: Optional[Literal["python"]] = Field(None, description="The type of the source code.")
tags: Optional[List[str]] = Field(None, description="Metadata tags.")
Expand Down Expand Up @@ -81,9 +81,45 @@ async def create_tool(
"""
Create a new tool
"""
# NOTE: horrifying code, should be replaced when we migrate dev portal
from memgpt.agent import Agent # nasty: need agent to be defined
from memgpt.functions.schema_generator import generate_schema

name = request.json_schema["name"]

import ast

parsed_code = ast.parse(request.source_code)
function_names = []

# Function to find and print function names
def find_function_names(node):
for child in ast.iter_child_nodes(node):
if isinstance(child, ast.FunctionDef):
# Print the name of the function
function_names.append(child.name)
# Recurse into child nodes
find_function_names(child)

# Find and print function names
find_function_names(parsed_code)
assert len(function_names) == 1, f"Expected 1 function, found {len(function_names)}: {function_names}"

# generate JSON schema
env = {}
env.update(globals())
exec(request.source_code, env)
func = env.get(function_names[0])
json_schema = generate_schema(func, name=name)
from pprint import pprint

pprint(json_schema)

try:

return server.create_tool(
json_schema=request.json_schema,
# json_schema=request.json_schema, # TODO: add back
json_schema=json_schema,
source_code=request.source_code,
source_type=request.source_type,
tags=request.tags,
Expand Down
3 changes: 1 addition & 2 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from dotenv import load_dotenv

from memgpt import Admin, create_client
from memgpt.agent import Agent
from memgpt.config import MemGPTConfig
from memgpt.constants import DEFAULT_PRESET
from memgpt.credentials import MemGPTCredentials
Expand Down Expand Up @@ -171,7 +170,7 @@ def print_tool(message: str):
def test_create_agent_tool(client):
"""Test creation of a agent tool"""

def core_memory_clear(self: Agent):
def core_memory_clear(self):
"""
Args:
agent (Agent): The agent to delete from memory.
Expand Down

0 comments on commit 88eb58a

Please sign in to comment.