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: add basic https support to `memgpt server` for development where https is needed (#1052)

Charles Packer15b0aebf5239
diff --git a/memgpt/cli/cli.py b/memgpt/cli/cli.py
index fe48dd07c..1d9272fe4 100644
--- a/memgpt/cli/cli.py
+++ b/memgpt/cli/cli.py
@@ -279,10 +279,40 @@ def create_default_user_or_exit(config: MemGPTConfig, ms: MetadataStore):
return user
+def generate_self_signed_cert(cert_path="selfsigned.crt", key_path="selfsigned.key"):
+ """Generate a self-signed SSL certificate.
+
+ NOTE: intended to be used for development only.
+ """
+ subprocess.run(
+ [
+ "openssl",
+ "req",
+ "-x509",
+ "-newkey",
+ "rsa:4096",
+ "-keyout",
+ key_path,
+ "-out",
+ cert_path,
+ "-days",
+ "365",
+ "-nodes",
+ "-subj",
+ "/C=US/ST=Denial/L=Springfield/O=Dis/CN=localhost",
+ ],
+ check=True,
+ )
+ return cert_path, key_path
+
+
def server(
type: Annotated[ServerChoice, typer.Option(help="Server to run")] = "rest",
port: Annotated[Optional[int], typer.Option(help="Port to run the server on")] = None,
host: Annotated[Optional[str], typer.Option(help="Host to run the server on (default to localhost)")] = None,
+ use_ssl: Annotated[bool, typer.Option(help="Run the server using HTTPS?")] = False,
+ ssl_cert: Annotated[Optional[str], typer.Option(help="Path to SSL certificate (if use_ssl is True)")] = None,
+ ssl_key: Annotated[Optional[str], typer.Option(help="Path to SSL key file (if use_ssl is True)")] = None,
debug: Annotated[bool, typer.Option(help="Turn debugging output on")] = True,
):
"""Launch a MemGPT server process"""
@@ -313,8 +343,35 @@ def server(
sys.exit(1)
try:
- # Start the subprocess in a new session
- uvicorn.run(app, host=host or "localhost", port=port or REST_DEFAULT_PORT)
+ if use_ssl:
+ if ssl_cert is None: # No certificate path provided, generate a self-signed certificate
+ ssl_certfile, ssl_keyfile = generate_self_signed_cert()
+ print(f"Running server with self-signed SSL cert: {ssl_certfile}, {ssl_keyfile}")
+ else:
+ ssl_certfile, ssl_keyfile = ssl_cert, ssl_key # Assuming cert includes both
+ print(f"Running server with provided SSL cert: {ssl_certfile}, {ssl_keyfile}")
+
+ # This will start the server on HTTPS
+ assert isinstance(ssl_certfile, str) and os.path.exists(ssl_certfile), ssl_certfile
+ assert isinstance(ssl_keyfile, str) and os.path.exists(ssl_keyfile), ssl_keyfile
+ print(
+ f"Running: uvicorn {app}:app --host {host or 'localhost'} --port {port or REST_DEFAULT_PORT} --ssl-keyfile {ssl_keyfile} --ssl-certfile {ssl_certfile}"
+ )
+ uvicorn.run(
+ app,
+ host=host or "localhost",
+ port=port or REST_DEFAULT_PORT,
+ ssl_keyfile=ssl_keyfile,
+ ssl_certfile=ssl_certfile,
+ )
+ else:
+ # Start the subprocess in a new session
+ print(f"Running: uvicorn {app}:app --host {host or 'localhost'} --port {port or REST_DEFAULT_PORT}")
+ uvicorn.run(
+ app,
+ host=host or "localhost",
+ port=port or REST_DEFAULT_PORT,
+ )
except KeyboardInterrupt:
# Handle CTRL-C