Skip to content

Commit

Permalink
add json_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ziaukhan committed Nov 14, 2023
1 parent 47fa03b commit 4c86124
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions 04_json_mode/.env_bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OPENAI_API_KEY = ""
PINECONE_ENV = ""
PINECONE_API_KEY =""
2 changes: 2 additions & 0 deletions 04_json_mode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
__pycache__
104 changes: 101 additions & 3 deletions 04_json_mode/json_mode.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,113 @@
"source": [
"# JSON Mode\n",
"\n",
"Read Guide:\n",
"Read and watch:\n",
"\n",
"https://platform.openai.com/docs/guides/text-generation/json-mode"
"https://platform.openai.com/docs/guides/text-generation/json-mode\n",
"\n",
"https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/47 \n",
"\n",
"https://www.youtube.com/watch?v=o4q2qsGKVkE\n",
"\n",
"https://github.com/rokbenko/openai-api-tutorials/tree/main/Tutorials/%231%20How%20do%20I%20get%20an%20OpenAI%20API%20response%20in%20JSON%20format"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"from dotenv import load_dotenv, find_dotenv\n",
"\n",
"_ : bool = load_dotenv(find_dotenv()) # read local .env file\n",
"\n",
"client : OpenAI = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"months\": [\n",
" \"April\",\n",
" \"June\",\n",
" \"September\",\n",
" \"November\"\n",
" ]\n",
"}\n"
]
}
],
"source": [
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-1106\",\n",
" response_format={ \"type\": \"json_object\" },\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant designed to output JSON.\"},\n",
" {\"role\": \"user\", \"content\": \"List of months that have 30 days\"}\n",
" ]\n",
")\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Parse JSON - Convert from JSON to Python\n",
"\n",
"https://www.w3schools.com/python/python_json.asp"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"April\n"
]
}
],
"source": [
"import json\n",
"\n",
"# parse response:\n",
"obj: dict[str, list[str]] = json.loads(response.choices[0].message.content)\n",
"\n",
"# the result is a Python dictionary:\n",
"print(obj[\"months\"][0])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions 04_json_mode/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python-dotenv
openai

0 comments on commit 4c86124

Please sign in to comment.