Skip to content

Commit bf0172b

Browse files
committed
LCORE-2755: Model dumper
1 parent 0fe1e95 commit bf0172b

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

src/utils/models_dumper.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Function to dump the schema of all data models into OpenAPI-compatible format."""
2+
3+
import json
4+
5+
from pydantic.json_schema import models_json_schema
6+
7+
import models.compaction as models_compaction
8+
from utils.json_schema_updater import recursive_update
9+
10+
11+
def dump_models(filename: str) -> None:
12+
"""Dump the schema of all models into OpenAPI-compatible JSON file.
13+
14+
Parameters:
15+
----------
16+
- filename: str - name of file to export the schema to
17+
18+
Returns:
19+
-------
20+
- None
21+
22+
Raises:
23+
------
24+
IOError: If the file cannot be written.
25+
"""
26+
with open(filename, "w", encoding="utf-8") as fout:
27+
# retrieve the schema
28+
_, schemas = models_json_schema(
29+
[(model, "validation") for model in [
30+
models_compaction.ConversationSummary
31+
]],
32+
ref_template="#/components/schemas/{model}",
33+
)
34+
35+
# fix the schema
36+
schemas = recursive_update(schemas)
37+
38+
# add all required metadata
39+
openapi_schema = {
40+
"openapi": "3.0.0",
41+
"info": {
42+
"title": "Lightspeed Core Stack",
43+
"version": "0.3.0",
44+
},
45+
"components": {
46+
"schemas": schemas.get("$defs", {}),
47+
},
48+
"paths": {},
49+
}
50+
json.dump(openapi_schema, fout, indent=4)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""Unit tests for utils/models_dumper module."""
2+
3+
from json import load
4+
from pathlib import Path
5+
6+
from utils.models_dumper import dump_models
7+
8+
9+
def test_dump_models(tmpdir: Path) -> None:
10+
"""Test that models can be dump into a JSON file.
11+
12+
An example of schema dump:
13+
{
14+
"openapi": "3.0.0",
15+
"info": {
16+
"title": "Lightspeed Core Stack",
17+
"version": "0.3.0"
18+
},
19+
"components": {
20+
"schemas": {
21+
"ConversationSummary": {
22+
"description": "A single compaction-produced summary chunk.\n\nAttributes:\n",
23+
"properties": {
24+
"summary_text": {
25+
"description": "Natural-language summary produced by the ...",
26+
"title": "Summary text",
27+
"type": "string"
28+
},
29+
"summarized_through_turn": {
30+
"description": "Running total of conversation items consumed by this ...",
31+
"minimum": 0,
32+
"title": "Summarized through turn",
33+
"type": "integer"
34+
},
35+
"token_count": {
36+
"description": "Number of tokens in summary_text.",
37+
"minimum": 0,
38+
"title": "Token count",
39+
"type": "integer"
40+
},
41+
"created_at": {
42+
"description": "ISO 8601 timestamp recording when this summary ...",
43+
"title": "Created at",
44+
"type": "string"
45+
},
46+
"model_used": {
47+
"description": "Fully-qualified model identifier used for the ...",
48+
"title": "Model used",
49+
"type": "string"
50+
}
51+
},
52+
"required": [
53+
"summary_text",
54+
"summarized_through_turn",
55+
"token_count",
56+
"created_at",
57+
"model_used"
58+
],
59+
"title": "ConversationSummary",
60+
"type": "object"
61+
}
62+
}
63+
},
64+
"paths": {}
65+
}
66+
"""
67+
filename = tmpdir / "foo.json"
68+
dump_models(str(filename))
69+
70+
with open(filename, "r", encoding="utf-8") as fin:
71+
# schema should be stored in JSON format
72+
content = load(fin)
73+
assert content is not None
74+
75+
# top-level keys test
76+
keys = ("openapi", "info", "components", "paths")
77+
for key in keys:
78+
assert key in content
79+
80+
# components should be top-level node
81+
components = content["components"]
82+
assert components is not None
83+
84+
# schemas should be a node stored inside components node
85+
assert "schemas" in components
86+
schemas = components["schemas"]
87+
assert schemas is not None
88+
89+
# list of schemas expected in a dump
90+
expected_schemas = ("ConversationSummary",)
91+
for expected_schema in expected_schemas:
92+
assert expected_schema in schemas

0 commit comments

Comments
 (0)