Last active
February 24, 2024 12:34
-
-
Save a-v-ershov/720f5ce9b23633d70e0629e61bcf9d87 to your computer and use it in GitHub Desktop.
OpenAI JSON response + pydantic example
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Imports" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pydantic import BaseModel\n", | |
"from openai import OpenAI\n", | |
"import json\n", | |
"from typing import Literal" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## OpenAI client set up" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"openai_key = \"YOUR_OPENAI_KEY\"\n", | |
"openai_client = OpenAI(api_key=openai_key)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Create prompt to analyze product review sentiment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def analyze_review_prompt(review: str) -> str:\n", | |
" return (\n", | |
" f\"Analyse review sentiment (positive, negative or neutral) \"\n", | |
" \"and name of the product (if not available return empty string) \"\n", | |
" f\"for the following review:\\n```\\n{review}\\n```\\n\"\n", | |
" \"Response format:\\n\"\n", | |
" \"json object with the following fields:\\n\"\n", | |
" \"- sentiment: string, sentiment of the review (positive, negative or neutral)\\n\"\n", | |
" \"- product_name: string, name of the product (if not available return empty string)\"\n", | |
" )" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Pydantic class to validate JSON response" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class ReviewAnalysis(BaseModel):\n", | |
" sentiment: Literal[\"positive\", \"negative\", \"neutral\"]\n", | |
" product_name: str" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Invoke OpenAI API" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"resp = openai_client.chat.completions.create(\n", | |
" response_format={\"type\": \"json_object\"},\n", | |
" messages=[{\"role\": \"user\", \"content\": analyze_review_prompt(\"Samsung Galaxy S21 is the best phone ever!\")}],\n", | |
" model=\"gpt-3.5-turbo\",\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Parse and validate the response" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"sentiment='positive' product_name='Samsung Galaxy S21'\n" | |
] | |
} | |
], | |
"source": [ | |
"parsed_json = json.loads(resp.choices[0].message.content)\n", | |
"validated_response = ReviewAnalysis.model_validate(parsed_json)\n", | |
"print(validated_response)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.12.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment