Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: allow admin to access all API routes & add back auth (#1047)
Robin Goetze0e031795a87
diff --git a/memgpt/server/rest_api/admin/users.py b/memgpt/server/rest_api/admin/users.pyindex 01a6383aa..49068d32f 100644--- a/memgpt/server/rest_api/admin/users.py+++ b/memgpt/server/rest_api/admin/users.py@@ -1,13 +1,12 @@import uuidfrom typing import List, Optional-from fastapi import APIRouter, Body, HTTPException, Depends, Query+from fastapi import APIRouter, Body, HTTPException, Queryfrom pydantic import BaseModel, Fieldfrom memgpt.data_types import Userfrom memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.server import SyncServer-from memgpt.server.rest_api.auth_token import get_current_userrouter = APIRouter()@@ -145,7 +144,7 @@ def setup_admin_router(server: SyncServer, interface: QueuingInterface):raiseexcept Exception as e:raise HTTPException(status_code=500, detail=f"{e}")- return GetAPIKeyResponse(api_key=processed_tokens)+ return GetAPIKeysResponse(api_key=processed_tokens)@router.delete("/users/keys", tags=["admin"], response_model=DeleteAPIKeyResponse)def delete_api_key(diff --git a/memgpt/server/rest_api/agents/command.py b/memgpt/server/rest_api/agents/command.pyindex b1ad92056..615c5ea32 100644--- a/memgpt/server/rest_api/agents/command.py+++ b/memgpt/server/rest_api/agents/command.py@@ -4,9 +4,9 @@ from functools import partialfrom fastapi import APIRouter, Body, HTTPException, Dependsfrom pydantic import BaseModel, Field+from memgpt.server.rest_api.auth_token import get_current_userfrom memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.server import SyncServer-from memgpt.server.rest_api.auth_token import get_current_userrouter = APIRouter()@@ -20,8 +20,8 @@ class CommandResponse(BaseModel):response: str = Field(..., description="The result of the executed command.")-def setup_agents_command_router(server: SyncServer, interface: QueuingInterface):- get_current_user_with_server = partial(get_current_user, server)+def setup_agents_command_router(server: SyncServer, interface: QueuingInterface, password: str):+ get_current_user_with_server = partial(partial(get_current_user, server), password)@router.post("/agents/command", tags=["agents"], response_model=CommandResponse)def run_command(request: CommandRequest = Body(...), user_id: uuid.UUID = Depends(get_current_user_with_server)):diff --git a/memgpt/server/rest_api/agents/config.py b/memgpt/server/rest_api/agents/config.pyindex 1134f50f4..bb46e363a 100644--- a/memgpt/server/rest_api/agents/config.py+++ b/memgpt/server/rest_api/agents/config.py@@ -1,15 +1,15 @@-import uuidimport re+import uuidfrom functools import partialfrom fastapi import APIRouter, Body, Depends, Query, HTTPException, statusfrom fastapi.responses import JSONResponsefrom pydantic import BaseModel, Field+from memgpt.models.pydantic_models import AgentStateModel+from memgpt.server.rest_api.auth_token import get_current_userfrom memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.server import SyncServer-from memgpt.server.rest_api.auth_token import get_current_user-from memgpt.models.pydantic_models import AgentStateModelrouter = APIRouter()@@ -45,8 +45,8 @@ def validate_agent_name(name: str) -> str:return name-def setup_agents_config_router(server: SyncServer, interface: QueuingInterface):- get_current_user_with_server = partial(get_current_user, server)+def setup_agents_config_router(server: SyncServer, interface: QueuingInterface, password: str):+ get_current_user_with_server = partial(partial(get_current_user, server), password)@router.get("/agents/config", tags=["agents"], response_model=AgentConfigResponse)def get_agent_config(diff --git a/memgpt/server/rest_api/agents/index.py b/memgpt/server/rest_api/agents/index.pyindex be82ef366..7ce602fec 100644--- a/memgpt/server/rest_api/agents/index.py+++ b/memgpt/server/rest_api/agents/index.py@@ -1,8 +1,8 @@import uuid-from typing import Listfrom functools import partial+from typing import List-from fastapi import APIRouter, Depends, Body, Query, HTTPException+from fastapi import APIRouter, Depends, Body, HTTPExceptionfrom pydantic import BaseModel, Fieldfrom memgpt.server.rest_api.interface import QueuingInterface@@ -27,8 +27,8 @@ class CreateAgentResponse(BaseModel):agent_state: AgentStateModel = Field(..., description="The state of the newly created agent.")-def setup_agents_index_router(server: SyncServer, interface: QueuingInterface):- get_current_user_with_server = partial(get_current_user, server)+def setup_agents_index_router(server: SyncServer, interface: QueuingInterface, password: str):+ get_current_user_with_server = partial(partial(get_current_user, server), password)@router.get("/agents", tags=["agents"], response_model=ListAgentsResponse)def list_agents(diff --git a/memgpt/server/rest_api/agents/memory.py b/memgpt/server/rest_api/agents/memory.pyindex fce115f3c..599c8c50f 100644--- a/memgpt/server/rest_api/agents/memory.py+++ b/memgpt/server/rest_api/agents/memory.py@@ -1,13 +1,12 @@import uuid-from typing import Optionalfrom functools import partialfrom fastapi import APIRouter, Depends, Body, Queryfrom pydantic import BaseModel, Field+from memgpt.server.rest_api.auth_token import get_current_userfrom memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.server import SyncServer-from memgpt.server.rest_api.auth_token import get_current_userrouter = APIRouter()@@ -39,8 +38,8 @@ class UpdateAgentMemoryResponse(BaseModel):new_core_memory: CoreMemory = Field(..., description="The updated state of the agent's core memory.")-def setup_agents_memory_router(server: SyncServer, interface: QueuingInterface):- get_current_user_with_server = partial(get_current_user, server)+def setup_agents_memory_router(server: SyncServer, interface: QueuingInterface, password: str):+ get_current_user_with_server = partial(partial(get_current_user, server), password)@router.get("/agents/memory", tags=["agents"], response_model=GetAgentMemoryResponse)def get_agent_memory(diff --git a/memgpt/server/rest_api/agents/message.py b/memgpt/server/rest_api/agents/message.pyindex dcab7d893..75941976c 100644--- a/memgpt/server/rest_api/agents/message.py+++ b/memgpt/server/rest_api/agents/message.py@@ -1,19 +1,19 @@import asyncio-from asyncio import AbstractEventLoop-from enum import Enumimport jsonimport uuid-from typing import List+from asyncio import AbstractEventLoop+from enum import Enumfrom functools import partial+from typing import Listfrom fastapi import APIRouter, Body, HTTPException, Query, Dependsfrom pydantic import BaseModel, Fieldfrom starlette.responses import StreamingResponsefrom memgpt.constants import JSON_ENSURE_ASCII+from memgpt.server.rest_api.auth_token import get_current_userfrom memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.server import SyncServer-from memgpt.server.rest_api.auth_token import get_current_userrouter = APIRouter()@@ -44,8 +44,8 @@ class GetAgentMessagesResponse(BaseModel):messages: list = Field(..., description="List of message objects.")-def setup_agents_message_router(server: SyncServer, interface: QueuingInterface):- get_current_user_with_server = partial(get_current_user, server)+def setup_agents_message_router(server: SyncServer, interface: QueuingInterface, password: str):+ get_current_user_with_server = partial(partial(get_current_user, server), password)@router.get("/agents/message", tags=["agents"], response_model=GetAgentMessagesResponse)def get_agent_messages(diff --git a/memgpt/server/rest_api/auth/index.py b/memgpt/server/rest_api/auth/index.pyindex f730c9840..ffa33075f 100644--- a/memgpt/server/rest_api/auth/index.py+++ b/memgpt/server/rest_api/auth/index.py@@ -13,9 +13,13 @@ class AuthResponse(BaseModel):uuid: UUID = Field(..., description="UUID of the user")-def setup_auth_router(server: SyncServer, interface: QueuingInterface):- @router.get("/auth", tags=["auth"], response_model=AuthResponse)- def authenticate_user():+class AuthRequest(BaseModel):+ password: str = Field(None, description="Admin password provided when starting the MemGPT server")+++def setup_auth_router(server: SyncServer, interface: QueuingInterface, password: str) -> APIRouter:+ @router.post("/auth", tags=["auth"], response_model=AuthResponse)+ def authenticate_user(request: AuthRequest) -> AuthResponse:"""Authenticates the user and sends response with User related data.@@ -23,6 +27,8 @@ def setup_auth_router(server: SyncServer, interface: QueuingInterface):"""interface.clear()try:+ if request.password != password:+ raise HTTPException(status_code=400, detail="Incorrect credentials")response = server.authenticate_user()except HTTPException:raisediff --git a/memgpt/server/rest_api/auth_token.py b/memgpt/server/rest_api/auth_token.pyindex 1e798ed43..7b3008497 100644--- a/memgpt/server/rest_api/auth_token.py+++ b/memgpt/server/rest_api/auth_token.py@@ -8,12 +8,15 @@ from memgpt.server.server import SyncServersecurity = HTTPBearer()-def get_current_user(server: SyncServer, auth: HTTPAuthorizationCredentials = Depends(security)) -> uuid.UUID:+def get_current_user(server: SyncServer, password: str, auth: HTTPAuthorizationCredentials = Depends(security)) -> uuid.UUID:try:- api_key = auth.credentials- user_id = server.api_key_to_user(api_key=api_key)+ api_key_or_password = auth.credentials+ if api_key_or_password == password:+ # user is admin so we just return the default uuid+ return server.authenticate_user()+ user_id = server.api_key_to_user(api_key=api_key_or_password)return user_idexcept HTTPException:raiseexcept Exception as e:- raise HTTPException(status_code=500, detail=f"Authentication error: {e}")+ raise HTTPException(status_code=403, detail=f"Authentication error: {e}")diff --git a/memgpt/server/rest_api/config/index.py b/memgpt/server/rest_api/config/index.pyindex d65836806..041953282 100644--- a/memgpt/server/rest_api/config/index.py+++ b/memgpt/server/rest_api/config/index.py@@ -1,12 +1,12 @@import uuidfrom functools import partial-from fastapi import APIRouter, Depends, Query, HTTPException+from fastapi import APIRouter, Dependsfrom pydantic import BaseModel, Field+from memgpt.server.rest_api.auth_token import get_current_userfrom memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.server import SyncServer-from memgpt.server.rest_api.auth_token import get_current_userrouter = APIRouter()@@ -16,8 +16,8 @@ class ConfigResponse(BaseModel):defaults: dict = Field(..., description="The defaults for the configuration.")-def setup_config_index_router(server: SyncServer, interface: QueuingInterface):- get_current_user_with_server = partial(get_current_user, server)+def setup_config_index_router(server: SyncServer, interface: QueuingInterface, password: str):+ get_current_user_with_server = partial(partial(get_current_user, server), password)@router.get("/config", tags=["config"], response_model=ConfigResponse)def get_server_config(user_id: uuid.UUID = Depends(get_current_user_with_server)):diff --git a/memgpt/server/rest_api/humans/index.py b/memgpt/server/rest_api/humans/index.pyindex 2d7907a0f..4d9393203 100644--- a/memgpt/server/rest_api/humans/index.py+++ b/memgpt/server/rest_api/humans/index.py@@ -1,12 +1,13 @@import uuid+from functools import partialfrom typing import List-from fastapi import APIRouter, Query, Depends++from fastapi import APIRouter, Dependsfrom pydantic import BaseModel, Field-from functools import partial-from memgpt.server.server import SyncServer-from memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.rest_api.auth_token import get_current_user+from memgpt.server.rest_api.interface import QueuingInterface+from memgpt.server.server import SyncServerrouter = APIRouter()@@ -15,8 +16,8 @@ class ListHumansResponse(BaseModel):humans: List[dict] = Field(..., description="List of human configurations.")-def setup_humans_index_router(server: SyncServer, interface: QueuingInterface):- get_current_user_with_server = partial(get_current_user, server)+def setup_humans_index_router(server: SyncServer, interface: QueuingInterface, password: str):+ get_current_user_with_server = partial(partial(get_current_user, server), password)@router.get("/humans", tags=["humans"], response_model=ListHumansResponse)async def list_humans(diff --git a/memgpt/server/rest_api/models/index.py b/memgpt/server/rest_api/models/index.pyindex 716a79ddb..3e71cf6d8 100644--- a/memgpt/server/rest_api/models/index.py+++ b/memgpt/server/rest_api/models/index.py@@ -1,12 +1,13 @@import uuid+from functools import partialfrom typing import List-from fastapi import APIRouter, Query, Depends++from fastapi import APIRouter, Dependsfrom pydantic import BaseModel, Field-from functools import partial-from memgpt.server.server import SyncServer-from memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.rest_api.auth_token import get_current_user+from memgpt.server.rest_api.interface import QueuingInterface+from memgpt.server.server import SyncServerrouter = APIRouter()@@ -23,8 +24,8 @@ class ListModelsResponse(BaseModel):models: List[Model] = Field(..., description="List of model configurations.")-def setup_models_index_router(server: SyncServer, interface: QueuingInterface):- get_current_user_with_server = partial(get_current_user, server)+def setup_models_index_router(server: SyncServer, interface: QueuingInterface, password: str):+ get_current_user_with_server = partial(partial(get_current_user, server), password)@router.get("/models", tags=["models"], response_model=ListModelsResponse)async def list_models(diff --git a/memgpt/server/rest_api/personas/index.py b/memgpt/server/rest_api/personas/index.pyindex ab205e7c9..88c22adb9 100644--- a/memgpt/server/rest_api/personas/index.py+++ b/memgpt/server/rest_api/personas/index.py@@ -1,12 +1,13 @@import uuid+from functools import partialfrom typing import List-from fastapi import APIRouter, Query, Depends++from fastapi import APIRouter, Dependsfrom pydantic import BaseModel, Field-from functools import partial-from memgpt.server.server import SyncServer-from memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.rest_api.auth_token import get_current_user+from memgpt.server.rest_api.interface import QueuingInterface+from memgpt.server.server import SyncServerrouter = APIRouter()@@ -15,8 +16,8 @@ class ListPersonasResponse(BaseModel):personas: List[dict] = Field(..., description="List of persona configurations.")-def setup_personas_index_router(server: SyncServer, interface: QueuingInterface):- get_current_user_with_server = partial(get_current_user, server)+def setup_personas_index_router(server: SyncServer, interface: QueuingInterface, password: str):+ get_current_user_with_server = partial(partial(get_current_user, server), password)@router.get("/personas", tags=["personas"], response_model=ListPersonasResponse)async def list_personas(diff --git a/memgpt/server/rest_api/server.py b/memgpt/server/rest_api/server.pyindex 776723c53..4fdfab8c1 100644--- a/memgpt/server/rest_api/server.py+++ b/memgpt/server/rest_api/server.py@@ -1,31 +1,26 @@-import osimport json+import osimport secrets-import uuid-from contextlib import asynccontextmanager-from fastapi import FastAPI, HTTPException, Depends, Header+from fastapi import FastAPI, HTTPException, Dependsfrom fastapi.security import HTTPBearer, HTTPAuthorizationCredentials--from starlette.middleware.cors import CORSMiddleware-from memgpt.constants import JSON_ENSURE_ASCII-from memgpt.server.rest_api.agents.index import setup_agents_index_router+from memgpt.server.rest_api.admin.users import setup_admin_routerfrom memgpt.server.rest_api.agents.command import setup_agents_command_routerfrom memgpt.server.rest_api.agents.config import setup_agents_config_router+from memgpt.server.rest_api.agents.index import setup_agents_index_routerfrom memgpt.server.rest_api.agents.memory import setup_agents_memory_routerfrom memgpt.server.rest_api.agents.message import setup_agents_message_routerfrom memgpt.server.rest_api.auth.index import setup_auth_routerfrom memgpt.server.rest_api.config.index import setup_config_index_routerfrom memgpt.server.rest_api.humans.index import setup_humans_index_router-from memgpt.server.rest_api.personas.index import setup_personas_index_router+from memgpt.server.rest_api.interface import QueuingInterfacefrom memgpt.server.rest_api.models.index import setup_models_index_routerfrom memgpt.server.rest_api.openai_assistants.assistants import setup_openai_assistant_router-from memgpt.server.rest_api.admin.users import setup_admin_router-from memgpt.server.server import SyncServer-from memgpt.server.rest_api.interface import QueuingInterface+from memgpt.server.rest_api.personas.index import setup_personas_index_routerfrom memgpt.server.rest_api.static_files import mount_static_files+from memgpt.server.server import SyncServer"""Basic REST API sitting on top of the internal MemGPT python server (SyncServer)@@ -82,21 +77,24 @@ app.add_middleware(allow_headers=["*"],)+# /api/auth endpoints+app.include_router(setup_auth_router(server, interface, password), prefix=API_PREFIX)+# /admin/users endpointsapp.include_router(setup_admin_router(server, interface), prefix=ADMIN_PREFIX, dependencies=[Depends(verify_password)])# /api/agents endpoints-app.include_router(setup_agents_command_router(server, interface), prefix=API_PREFIX)-app.include_router(setup_agents_config_router(server, interface), prefix=API_PREFIX)-app.include_router(setup_agents_index_router(server, interface), prefix=API_PREFIX)-app.include_router(setup_agents_memory_router(server, interface), prefix=API_PREFIX)-app.include_router(setup_agents_message_router(server, interface), prefix=API_PREFIX)-app.include_router(setup_humans_index_router(server, interface), prefix=API_PREFIX)-app.include_router(setup_personas_index_router(server, interface), prefix=API_PREFIX)-app.include_router(setup_models_index_router(server, interface), prefix=API_PREFIX)+app.include_router(setup_agents_command_router(server, interface, password), prefix=API_PREFIX)+app.include_router(setup_agents_config_router(server, interface, password), prefix=API_PREFIX)+app.include_router(setup_agents_index_router(server, interface, password), prefix=API_PREFIX)+app.include_router(setup_agents_memory_router(server, interface, password), prefix=API_PREFIX)+app.include_router(setup_agents_message_router(server, interface, password), prefix=API_PREFIX)+app.include_router(setup_humans_index_router(server, interface, password), prefix=API_PREFIX)+app.include_router(setup_personas_index_router(server, interface, password), prefix=API_PREFIX)+app.include_router(setup_models_index_router(server, interface, password), prefix=API_PREFIX)# /api/config endpoints-app.include_router(setup_config_index_router(server, interface), prefix=API_PREFIX)+app.include_router(setup_config_index_router(server, interface, password), prefix=API_PREFIX)# /v1/assistants endpointsapp.include_router(setup_openai_assistant_router(server, interface), prefix=OPENAI_API_PREFIX)@@ -120,7 +118,7 @@ def on_startup():# print(f"Writing out openapi.json file")# json.dump(app.openapi_schema, file, indent=2)- # Split the API docs into MemGPT API, and OpenAI Assisstants compatible API+ # Split the API docs into MemGPT API, and OpenAI Assistants compatible APImemgpt_api = app.openapi_schema.copy()memgpt_api["paths"] = {key: value for key, value in memgpt_api["paths"].items() if not key.startswith(OPENAI_API_PREFIX)}memgpt_api["info"]["title"] = "MemGPT API"