Coder Social home page Coder Social logo

pydantic-chatcompletion's Introduction

Pydantic ChatCompletion

This repository provides a wrapper around the OpenAI ChatCompletion API that help enables the extraction of structured data (in the form of a Pydantic model) from unstructured text via language model.

This package provide a pydantic_chatcompletion.create() function that is similar to the openai.ChatCompletion.create() function.

The pydantic_chatcompletion.create() function takes an arbitrary pydantic class as an additional argument (in addition to the normal arguments supported by openai.ChatCompletion.create()) and tries to return an instance of the supplied pydantic class as return data.

The create function in the pydantic_chatcompletion package is designed to interact with the OpenAI ChatCompletion API to progressively guide the language model to produce structured data according to a provided Pydantic model. Here's how it accomplishes this task:

  1. It starts by appending a system message to the list of messages, instructing the language model to respond with valid JSON that conforms to the Pydantic model's JSON schema. The message also asks the language model not to include any additional text besides the JSON object.

  2. If the language model output is not valid json, append the json.loads() exception output as an additional system message and try again.

  3. If the lanugage model output is valid json but does not pass pydantic validation, append the pydantic exception output as an additional system message and try again.

In most cases the error remediation of the 2nd and 3rd steps is not required as the language model is usually able to output correct data on the first try.

Installation

pip install pydantic_chatcompletion

Usage

import pydantic_chatcompletion
from pydantic import BaseModel


messages = [{"role": "user", "content": "All of your unstructured text to process via language model..."]


class MyPydanticModel(BaseModel)
      """
      The data we extract from the unstructured text via lanugage model.
      """
      some_data: int
      more_data: str

instance_of_my_data =  pydantic_chatcompletion.create(messages, MyPydanticModel, model='gpt-3.5-turbo')

print(instance_of_my_data.some_data, instance_of_my_data.more_data)

Summary of Examples

  1. example/book_info.py: Extracts structured book information (title, author, publication year, genre, characters, and summary) from a given unstructured text describing the book "Pride and Prejudice".

  2. example/doc_metadata.py: Extracts metadata (title, author, creation date, and primary language) from an unstructured text related to a document about investment opportunities in Generative AI.

  3. example/list_event_dates.py: Extracts a list of events, their names, and their relative start and end dates from an unstructured text containing a calendar of events.

  4. example/movie_info.py: Extracts structured movie information (title, director, release year, cast, genre, duration, plot, and awards) from an unstructured text about the movie "The Godfather".

  5. example/nested.py: Extracts a nested curriculum structure (curriculum title, description, courses, course titles, instructors, and course durations) from an unstructured text describing a data science bootcamp.

  6. example/user_details.py: Extracts user details (name, age, email, and country) from an unstructured text about a software engineer named John Doe.

pydantic-chatcompletion's People

Contributors

wskish avatar

Stargazers

moonuke avatar Dr. Christopher G. Snyder avatar LJ avatar Abdullah Cetin Cavdar avatar Nikita avatar Sidney Radcliffe avatar  avatar Jeff Carpenter avatar Pedro Batista avatar Charlene avatar Zaki Mughal [sivoais] avatar Abhimanyu avatar K. S. Ernest (iFire) Lee avatar Sergey Filimonov avatar Jack Langerman avatar Shaun Springer avatar Paritosh Piplewar avatar Tetsuya Takasawa avatar zK avatar Nikolaus Schlemm avatar Elwynn Chen avatar Rishi avatar  avatar Voro avatar Stephan Fitzpatrick avatar Austin avatar  avatar  avatar Kamil Legault avatar Sean Bowman avatar Mike Alche avatar Tao Hansen avatar Hugo Montenegro avatar Ben Graves avatar Aquib Master avatar Oliver Yerbury-Hodgson avatar LeLoew avatar Jakub Kukul avatar Justyn Shull avatar Raymond Yee avatar Vin Howe avatar Rob avatar omar avatar Avirut Mehta avatar Zach Williams avatar Roberto Salas avatar  avatar eunho avatar Amy Wang avatar  avatar  avatar  avatar

Watchers

Amy Wang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.