Skip to content

Commit

Permalink
add schema catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnl committed Sep 6, 2023
1 parent 942e62c commit 53384f1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
Empty file.
14 changes: 14 additions & 0 deletions examples/upload-schema/model.py
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
72 changes: 72 additions & 0 deletions examples/upload-schema/readme.md
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`.
39 changes: 39 additions & 0 deletions instructor/cli/catalog.py
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()
2 changes: 2 additions & 0 deletions instructor/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typer
import instructor.cli.jobs as jobs
import instructor.cli.files as files
import instructor.cli.catalog as catalog

app = typer.Typer(
name="instructor-ft",
Expand All @@ -9,3 +10,4 @@

app.add_typer(jobs.app, name="jobs", help="Monitor and create fine tuning jobs")
app.add_typer(files.app, name="files", help="Manage files on OpenAI's servers")
app.add_typer(catalog.app, name="schema-catalog", help="Manage Schema Catalogs")

0 comments on commit 53384f1

Please sign in to comment.