Skip to content

Commit

Permalink
feat: add bash script to allow people to generate openapi schema with… (
Browse files Browse the repository at this point in the history
#2015)

Co-authored-by: Shubham Naik <[email protected]>
  • Loading branch information
4shub and Shubham Naik authored Nov 7, 2024
1 parent 09e8d59 commit 9c1b0d3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
12 changes: 12 additions & 0 deletions letta/server/generate_openapi_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
echo "Generating OpenAPI schema..."

# check if poetry is installed
if ! command -v poetry &> /dev/null
then
echo "Poetry could not be found. Please install poetry to generate the OpenAPI schema."
exit
fi

# generate OpenAPI schema
poetry run python -c 'from letta.server.rest_api.app import app, generate_openapi_schema; generate_openapi_schema(app);'
60 changes: 32 additions & 28 deletions letta/server/rest_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@
log = logging.getLogger("uvicorn")


def generate_openapi_schema(app: FastAPI):
# Update the OpenAPI schema
if not app.openapi_schema:
app.openapi_schema = app.openapi()

openai_docs, letta_docs = [app.openapi_schema.copy() for _ in range(2)]

openai_docs["paths"] = {k: v for k, v in openai_docs["paths"].items() if k.startswith("/openai")}
openai_docs["info"]["title"] = "OpenAI Assistants API"
letta_docs["paths"] = {k: v for k, v in letta_docs["paths"].items() if not k.startswith("/openai")}
letta_docs["info"]["title"] = "Letta API"
letta_docs["components"]["schemas"]["LettaResponse"] = {
"properties": LettaResponse.model_json_schema(ref_template="#/components/schemas/LettaResponse/properties/{model}")["$defs"]
}

# Split the API docs into Letta API, and OpenAI Assistants compatible API
for name, docs in [
(
"openai",
openai_docs,
),
(
"letta",
letta_docs,
),
]:
if settings.cors_origins:
docs["servers"] = [{"url": host} for host in settings.cors_origins]
Path(f"openapi_{name}.json").write_text(json.dumps(docs, indent=2))


def create_application() -> "FastAPI":
"""the application start routine"""
# global server
Expand Down Expand Up @@ -122,34 +153,7 @@ def on_startup():

# Tool.load_default_tools(get_db_session())

# Update the OpenAPI schema
if not app.openapi_schema:
app.openapi_schema = app.openapi()

openai_docs, letta_docs = [app.openapi_schema.copy() for _ in range(2)]

openai_docs["paths"] = {k: v for k, v in openai_docs["paths"].items() if k.startswith("/openai")}
openai_docs["info"]["title"] = "OpenAI Assistants API"
letta_docs["paths"] = {k: v for k, v in letta_docs["paths"].items() if not k.startswith("/openai")}
letta_docs["info"]["title"] = "Letta API"
letta_docs["components"]["schemas"]["LettaResponse"] = {
"properties": LettaResponse.model_json_schema(ref_template="#/components/schemas/LettaResponse/properties/{model}")["$defs"]
}

# Split the API docs into Letta API, and OpenAI Assistants compatible API
for name, docs in [
(
"openai",
openai_docs,
),
(
"letta",
letta_docs,
),
]:
if settings.cors_origins:
docs["servers"] = [{"url": host} for host in settings.cors_origins]
Path(f"openapi_{name}.json").write_text(json.dumps(docs, indent=2))
generate_openapi_schema(app)

@app.on_event("shutdown")
def on_shutdown():
Expand Down

0 comments on commit 9c1b0d3

Please sign in to comment.