Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
piercefreeman committed Apr 14, 2024
1 parent 9ecb934 commit 21dcec6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

`gpt-json` is a wrapper around GPT that allows for declarative definition of expected output format. Set up a schema, write a prompt, and get results back as beautiful typehinted objects.

Specifically this library:

- Utilizes Pydantic schema definitions for type casting and validations
- Adds typehinting for both the API and the output schema
- Allows GPT to respond with both single-objects and lists of objects
- Includes some lightweight transformations of the output to remove superfluous context and fix broken json
- Includes retry logic for the most common API failures
- Formats the JSON schema as a flexible prompt that can be added into any message
- Supports templating of prompts to allow for dynamic content
- Validate typehinted function calls in the new GPT models, to better support agent creation
- Lightweight dependencies: only OpenAI, pydantic, and backoff
This library introduces the following features:

- 🏗️ Pydantic schema definitions for type casting and validations
- 🧵 Templating of prompts to allow for dynamic content
- 🔎 Supports Vision API, Function Calling, and standard chat prompts
- 🚕 Lightweight transformations of the output to fix broken json
- ♻️ Retry logic for the most common API failures
- 📋 Predict single-objects and lists of objects
- ✈️ Lightweight dependencies: only OpenAI, pydantic, and backoff

## Getting Started

Expand Down
11 changes: 7 additions & 4 deletions gpt_json/gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
handler.setFormatter(formatter)
logger.addHandler(handler)

# https://github.com/openai/openai-python/issues/1306
ChatCompletionChunk.model_rebuild()


def handle_backoff(details):
logger.warning(
Expand Down Expand Up @@ -261,11 +264,11 @@ async def run(
except (ValueError, ValidationError):
raise InvalidFunctionParameters(function_name, function_args_string)

raw_response = GPTMessage.model_validate_json(response_message.model_dump_json())
raw_response = GPTMessage.model_validate(response_message.model_dump())
raw_response.allow_templating = False

extracted_json, fixed_payload = self.extract_json(
response_message, self.extract_type
raw_response, self.extract_type
)

# Cast to schema model
Expand Down Expand Up @@ -372,12 +375,12 @@ async def stream(
yield partial_response
previous_partial = partial_response

def extract_json(self, response_message, extract_type: ResponseType):
def extract_json(self, response_message: GPTMessage, extract_type: ResponseType):
"""
Assumes one main block of results, either list of dictionary
"""

full_response = response_message.content
full_response = self.get_content_text(response_message.get_content_payloads())
if not full_response:
return None, None

Expand Down

0 comments on commit 21dcec6

Please sign in to comment.