Skip to content

armgit / letta

Public Imported from GitHub
0

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.py
index 01a6383aa..49068d32f 100644
--- a/memgpt/server/rest_api/admin/users.py
+++ b/memgpt/server/rest_api/admin/users.py
@@ -1,13 +1,12 @@
import uuid
from typing import List, Optional
-from fastapi import APIRouter, Body, HTTPException, Depends, Query
+from fastapi import APIRouter, Body, HTTPException, Query
from pydantic import BaseModel, Field
from memgpt.data_types import User
from memgpt.server.rest_api.interface import QueuingInterface
from memgpt.server.server import SyncServer
-from memgpt.server.rest_api.auth_token import get_current_user
router = APIRouter()
@@ -145,7 +144,7 @@ def setup_admin_router(server: SyncServer, interface: QueuingInterface):
raise
except 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.py
index 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 partial
from fastapi import APIRouter, Body, HTTPException, Depends
from pydantic import BaseModel, Field
+from memgpt.server.rest_api.auth_token import get_current_user
from memgpt.server.rest_api.interface import QueuingInterface
from memgpt.server.server import SyncServer
-from memgpt.server.rest_api.auth_token import get_current_user
router = 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.py
index 1134f50f4..bb46e363a 100644
--- a/memgpt/server/rest_api/agents/config.py
+++ b/memgpt/server/rest_api/agents/config.py
@@ -1,15 +1,15 @@
-import uuid
import re
+import uuid
from functools import partial
from fastapi import APIRouter, Body, Depends, Query, HTTPException, status
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
+from memgpt.models.pydantic_models import AgentStateModel
+from memgpt.server.rest_api.auth_token import get_current_user
from memgpt.server.rest_api.interface import QueuingInterface
from memgpt.server.server import SyncServer
-from memgpt.server.rest_api.auth_token import get_current_user
-from memgpt.models.pydantic_models import AgentStateModel
router = 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.py
index 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 List
from functools import partial
+from typing import List
-from fastapi import APIRouter, Depends, Body, Query, HTTPException
+from fastapi import APIRouter, Depends, Body, HTTPException
from pydantic import BaseModel, Field
from 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.py
index 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 Optional
from functools import partial
from fastapi import APIRouter, Depends, Body, Query
from pydantic import BaseModel, Field
+from memgpt.server.rest_api.auth_token import get_current_user
from memgpt.server.rest_api.interface import QueuingInterface
from memgpt.server.server import SyncServer
-from memgpt.server.rest_api.auth_token import get_current_user
router = 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.py
index 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 Enum
import json
import uuid
-from typing import List
+from asyncio import AbstractEventLoop
+from enum import Enum
from functools import partial
+from typing import List
from fastapi import APIRouter, Body, HTTPException, Query, Depends
from pydantic import BaseModel, Field
from starlette.responses import StreamingResponse
from memgpt.constants import JSON_ENSURE_ASCII
+from memgpt.server.rest_api.auth_token import get_current_user
from memgpt.server.rest_api.interface import QueuingInterface
from memgpt.server.server import SyncServer
-from memgpt.server.rest_api.auth_token import get_current_user
router = 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.py
index 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:
raise
diff --git a/memgpt/server/rest_api/auth_token.py b/memgpt/server/rest_api/auth_token.py
index 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 SyncServer
security = 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_id
except HTTPException:
raise
except 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.py
index d65836806..041953282 100644
--- a/memgpt/server/rest_api/config/index.py
+++ b/memgpt/server/rest_api/config/index.py
@@ -1,12 +1,12 @@
import uuid
from functools import partial
-from fastapi import APIRouter, Depends, Query, HTTPException
+from fastapi import APIRouter, Depends
from pydantic import BaseModel, Field
+from memgpt.server.rest_api.auth_token import get_current_user
from memgpt.server.rest_api.interface import QueuingInterface
from memgpt.server.server import SyncServer
-from memgpt.server.rest_api.auth_token import get_current_user
router = 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.py
index 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 partial
from typing import List
-from fastapi import APIRouter, Query, Depends
+
+from fastapi import APIRouter, Depends
from pydantic import BaseModel, Field
-from functools import partial
-from memgpt.server.server import SyncServer
-from memgpt.server.rest_api.interface import QueuingInterface
from memgpt.server.rest_api.auth_token import get_current_user
+from memgpt.server.rest_api.interface import QueuingInterface
+from memgpt.server.server import SyncServer
router = 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.py
index 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 partial
from typing import List
-from fastapi import APIRouter, Query, Depends
+
+from fastapi import APIRouter, Depends
from pydantic import BaseModel, Field
-from functools import partial
-from memgpt.server.server import SyncServer
-from memgpt.server.rest_api.interface import QueuingInterface
from memgpt.server.rest_api.auth_token import get_current_user
+from memgpt.server.rest_api.interface import QueuingInterface
+from memgpt.server.server import SyncServer
router = 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.py
index 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 partial
from typing import List
-from fastapi import APIRouter, Query, Depends
+
+from fastapi import APIRouter, Depends
from pydantic import BaseModel, Field
-from functools import partial
-from memgpt.server.server import SyncServer
-from memgpt.server.rest_api.interface import QueuingInterface
from memgpt.server.rest_api.auth_token import get_current_user
+from memgpt.server.rest_api.interface import QueuingInterface
+from memgpt.server.server import SyncServer
router = 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.py
index 776723c53..4fdfab8c1 100644
--- a/memgpt/server/rest_api/server.py
+++ b/memgpt/server/rest_api/server.py
@@ -1,31 +1,26 @@
-import os
import json
+import os
import secrets
-import uuid
-from contextlib import asynccontextmanager
-from fastapi import FastAPI, HTTPException, Depends, Header
+from fastapi import FastAPI, HTTPException, Depends
from 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_router
from memgpt.server.rest_api.agents.command import setup_agents_command_router
from memgpt.server.rest_api.agents.config import setup_agents_config_router
+from memgpt.server.rest_api.agents.index import setup_agents_index_router
from memgpt.server.rest_api.agents.memory import setup_agents_memory_router
from memgpt.server.rest_api.agents.message import setup_agents_message_router
from memgpt.server.rest_api.auth.index import setup_auth_router
from memgpt.server.rest_api.config.index import setup_config_index_router
from 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 QueuingInterface
from memgpt.server.rest_api.models.index import setup_models_index_router
from 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_router
from 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 endpoints
app.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 endpoints
app.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 API
memgpt_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"