Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix tool creation to accept dev portal POST request #1656

Merged
merged 4 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion memgpt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.3.22"
__version__ = "0.3.23"

from memgpt.client.admin import Admin
from memgpt.client.client import create_client
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pymemgpt"
version = "0.3.22"
version = "0.3.23"
packages = [
{include = "memgpt"}
]
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
Loading