-
-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pydantic import BaseModel | ||
from enum import Enum | ||
|
||
|
||
class Topic(Enum): | ||
SCIENCE = "science" | ||
TECHNOLOGY = "technology" | ||
ART = "art" | ||
|
||
|
||
class Article(BaseModel): | ||
title: str | ||
content: str | ||
topic: Topic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Instructor CLI Tool: Schema Catalog Management | ||
|
||
## Overview | ||
|
||
The Instructor CLI tool is designed for managing and uploading schema catalogs to OpenAI's servers. Leveraging the power of Pydantic models, this CLI tool allows for dynamic schema generation and upload. | ||
|
||
## Features | ||
|
||
- **Dynamic Model Import**: Specify Pydantic models at runtime for schema creation. | ||
- **Directory-Agnostic**: Run the CLI tool from any directory containing your Pydantic model file. | ||
- **User-Friendly**: Intuitive command-line interface for quick and easy schema management. | ||
|
||
## Installation | ||
|
||
If you haven't installed the CLI tool yet, you can install it using pip: | ||
|
||
```bash | ||
pip install instructor | ||
``` | ||
|
||
## Usage | ||
|
||
### Uploading a Schema Catalog | ||
|
||
To upload a schema catalog, navigate to the directory that contains your Pydantic model (e.g., `model.py`). Then execute the following command: | ||
|
||
```bash | ||
instructor schema-catalog upload model:Article | ||
``` | ||
|
||
On successful execution, you should see an output similar to: | ||
|
||
```plaintext | ||
Creating Schema Catalog for model:Article using Instructor API Key None | ||
{ | ||
"$defs": { | ||
"Topic": { | ||
"enum": [ | ||
"science", | ||
"technology", | ||
"art" | ||
], | ||
"title": "Topic", | ||
"type": "string" | ||
} | ||
}, | ||
... (rest of the schema) | ||
} | ||
``` | ||
|
||
### Example `model.py` | ||
|
||
Below is an example `model.py` that defines an `Article` model with an enumerated `Topic`: | ||
|
||
```python | ||
from pydantic import BaseModel | ||
from enum import Enum | ||
|
||
class Topic(Enum): | ||
SCIENCE = "science" | ||
TECHNOLOGY = "technology" | ||
ART = "art" | ||
|
||
class Article(BaseModel): | ||
title: str | ||
content: str | ||
topic: Topic | ||
``` | ||
|
||
## Troubleshooting | ||
|
||
If you encounter the error "No module named 'model'", ensure you are running the command in the directory containing your `model.py`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sys | ||
from pydantic import BaseModel | ||
import typer | ||
import importlib | ||
import os | ||
|
||
app = typer.Typer( | ||
name="schema-catalog", | ||
help="Manage Schema Catalogs", | ||
) | ||
|
||
|
||
@app.command( | ||
help="Upload a Schema Catalog to OpenAI's servers", | ||
) | ||
def upload( | ||
model: str, | ||
): | ||
# Add the current working directory to sys.path | ||
sys.path.insert(0, os.getcwd()) | ||
|
||
module_name, class_name = model.split(":") | ||
|
||
# Dynamically import the Pydantic model class | ||
module = importlib.import_module(module_name) | ||
ModelClass: BaseModel = getattr(module, class_name) | ||
|
||
# Create an instance of the dynamically loaded Pydantic model | ||
import json | ||
|
||
INSTRUCTOR_API_KEY = os.environ.get("INSTRUCTOR_API_KEY", None) | ||
typer.echo( | ||
f"Creating Schema Catalog for {model} using Instructor API Key {INSTRUCTOR_API_KEY}" | ||
) | ||
typer.echo(json.dumps(ModelClass.model_json_schema(), indent=2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters