diff --git a/docs/examples/db.png b/docs/examples/db.png new file mode 100644 index 0000000..e2bb75e Binary files /dev/null and b/docs/examples/db.png differ diff --git a/docs/examples/index.md b/docs/examples/index.md index a2f4ad6..d324c97 100644 --- a/docs/examples/index.md +++ b/docs/examples/index.md @@ -15,5 +15,7 @@ 11. [How to enable OpenAI's moderation](moderation.md) 12. [How to extract tables using GPT-Vision?](extracting_tables.md) 13. [How to generate advertising copy from image inputs](image_to_ad_copy.md) +14. [How to use local models from Ollama](ollama.md) +15. [How to store responses in a database with SQLModel](sqlmodel.md) Explore more! diff --git a/docs/examples/sqlmodel.md b/docs/examples/sqlmodel.md new file mode 100644 index 0000000..c45c6cb --- /dev/null +++ b/docs/examples/sqlmodel.md @@ -0,0 +1,65 @@ +# Integrating Instructor with SQLModel + +[SQLModel](https://sqlmodel.tiangolo.com/) is a library designed for interacting with SQL databases from Python code using Python objects. `SQLModel` is based on `Pydantic` and `SQLAlchemy` and was created by [tiangolo](https://twitter.com/tiangolo) who also developed `FastAPI`. So you can expect seamless integration across all these libraries, reducing code duplicating and improving your developer experience. + +# Example: Adding responses from Instructor directly to your DB + +## Defining the Models + +First we'll define a model that will serve as a table for our database and the structure of our outputs from `Instructor` + +!!! tips "Model Definition" + + You'll need to subclass your models with both `SQLModel` and `instructor.OpenAISchema` for them to work with SQLModel + +```python +import instructor +from openai import OpenAI +from typing import Optional +from sqlmodel import Field, SQLModel, create_engine + + +class Hero(SQLModel, instructor.OpenAISchema, table=True): + id: Optional[int] = Field(default=None, primary_key=True) + name: str + secret_name: str + age: Optional[int] = None +``` + +## Generating a record + +The `create_hero` function will query `OpenAI` for a `Hero` record + +```python +client = instructor.patch(OpenAI()) + +def create_hero() -> Hero: + return client.chat.completions.create( + model="gpt-3.5-turbo", + response_model=Hero, + messages=[ + {"role": "user", "content": "Make a new superhero"}, + ], + ) +``` + +## Inserting the response into the DB + +```python +engine = create_engine("sqlite:///database.db") +SQLModel.metadata.create_all(engine) + +hero = create_hero() +print(hero.model_dump()) + """ + {'name': 'SuperNova', 'secret_name': 'Mia Thompson', 'age': 28, 'id': None} + """ + +with Session(engine) as session: + session.add(hero) + session.commit() +``` + +![Image of hero record in the database](db.png) + +And there you have it! You can now use the same models for your database and `Instructor` enabling them work seamlessly! Also checkout the [FastAPI](../concepts/fastapi.md) guide to see how you can use these models in an API as well. \ No newline at end of file diff --git a/examples/sqlmodel/run.py b/examples/sqlmodel/run.py new file mode 100644 index 0000000..44cac58 --- /dev/null +++ b/examples/sqlmodel/run.py @@ -0,0 +1,39 @@ +import instructor +from openai import OpenAI +from typing import Optional +from sqlmodel import Field, SQLModel, create_engine, Session + + +# Define the model that will serve as a Table for the database +class Hero(SQLModel, instructor.OpenAISchema, table=True): + id: Optional[int] = Field(default=None, primary_key=True) + name: str + secret_name: str + age: Optional[int] = None + + +# Function to query OpenAI for a Hero record +client = instructor.patch(OpenAI()) + + +def create_hero() -> Hero: + return client.chat.completions.create( + model="gpt-3.5-turbo", + response_model=Hero, + messages=[ + {"role": "user", "content": "Make a new superhero"}, + ], + ) + + +# Insert the response into the database +engine = create_engine("sqlite:///database.db") +SQLModel.metadata.create_all(engine) + +hero = create_hero() +print(hero.model_dump()) + + +with Session(engine) as session: + session.add(hero) + session.commit() diff --git a/mkdocs.yml b/mkdocs.yml index 822c2a6..ebcc4f0 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -165,7 +165,8 @@ nav: - PII Data Sanitization: 'examples/pii.md' - Enabling Open Source Models: 'examples/open_source.md' - Image to Ad Copy: 'examples/image_to_ad_copy.md' - - Ollama + LiteLLM: 'examples/ollama.md' + - Ollama: 'examples/ollama.md' + - SQLModel Integration: 'examples/sqlmodel.md' - CLI Reference: - "CLI Reference": "cli/index.md" - "Finetuning GPT-3.5": "cli/finetune.md"