Update registry.yaml (#1352)

pull/1353/head^2
katia-openai 2 months ago committed by GitHub
parent 5ae1e3b319
commit 05a2be6101
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,942 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d129721d",
"metadata": {},
"source": [
"# Introduction to Structured Outputs\n",
"\n",
"This cookbook introduces Structured Outputs, a new capability in the Chat Completions API and Assistants API that allows to get outputs to follow a strict schema, and illustrates this capability with a few examples.\n",
"\n",
"Structured outputs can be enabled by setting the parameter `strict: true` in an API call with either a defined response format or function calls.\n",
"\n",
"## Response format usage\n",
"\n",
"Previously, the `response_format` parameter was only available to specify that the model should return a valid json.\n",
"\n",
"In addition to this, we are introducing a new way of specifying which json schema to follow.\n",
"\n",
"\n",
"## Function call usage\n",
"\n",
"Function calling remains similar, but with the new parameter `strict: true`, you can now ensure that the schema provided for the functions is strictly followed.\n",
"\n",
"\n",
"## Examples \n",
"\n",
"There are many ways Structured Outputs can be useful, as you can rely on the outputs following a constrained schema needed for your application.\n",
"\n",
"If you used JSON mode or function calls before, you can think of Structured Outputs as a foolproof version of this.\n",
"\n",
"This can enable more robust flows in production-level applications, whether you are relying on function calls or expecting the output to follow a pre-defined structure.\n",
"\n",
"Example use cases include:\n",
"\n",
"- Getting structured answers to display them in a specific way in a UI (cf example 1 in this cookbook)\n",
"- Populating a database with extracted content from documents or scrapped web pages (cf example 2 in this cookbook)\n",
"- Extracting entities from a user input to call tools with defined parameters (cf example 3 in this cookbook)\n",
"\n",
"More generally, anything that requires fetching data, taking action, or that builds upon complex workflows could benefit from using Structured Outputs."
]
},
{
"cell_type": "markdown",
"id": "f5bf83b3",
"metadata": {},
"source": [
"### Setup"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "21972f02",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"from openai import OpenAI\n",
"client = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "ae451fb7",
"metadata": {},
"outputs": [],
"source": [
"MODEL = \"gpt-4o-2024-08-06\""
]
},
{
"cell_type": "markdown",
"id": "e0378b6a",
"metadata": {},
"source": [
"## Example 1: Math tutor\n",
"\n",
"In this example, we want to build a math tutoring tool that outputs steps to solving a math problem as an array of structured objects.\n",
"\n",
"This could be useful in an application where each step needs to be displayed separately, so that the user can progress through the solution at their own pace."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b5f6a7b7",
"metadata": {},
"outputs": [],
"source": [
"math_tutor_prompt = '''\n",
" You are a helpful math tutor. You will be provided with a math problem,\n",
" and your goal will be to output a step by step solution, along with a final answer.\n",
" For each step, just provide the output as an equation use the explanation field to detail the reasoning.\n",
"'''\n",
"\n",
"def get_math_solution(question):\n",
" response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\n",
" \"role\": \"system\", \n",
" \"content\": math_tutor_prompt\n",
" },\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": question\n",
" }\n",
" ],\n",
" response_format={\n",
" \"type\": \"json_schema\",\n",
" \"json_schema\": {\n",
" \"name\": \"math_reasoning\",\n",
" \"schema\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"steps\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"explanation\": {\"type\": \"string\"},\n",
" \"output\": {\"type\": \"string\"}\n",
" },\n",
" \"required\": [\"explanation\", \"output\"],\n",
" \"additionalProperties\": False\n",
" }\n",
" },\n",
" \"final_answer\": {\"type\": \"string\"}\n",
" },\n",
" \"required\": [\"steps\", \"final_answer\"],\n",
" \"additionalProperties\": False\n",
" },\n",
" \"strict\": True\n",
" }\n",
" }\n",
" )\n",
"\n",
" return response.choices[0].message"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c6c97ba9",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"steps\":[{\"explanation\":\"Start by isolating the term with the variable. We have the equation 8x + 7 = -23. To isolate 8x, we need to subtract 7 from both sides of the equation.\",\"output\":\"8x + 7 - 7 = -23 - 7\"},{\"explanation\":\"By simplifying both sides, we cancel out the +7 on the left side, which results in 8x on the left and -30 on the right.\",\"output\":\"8x = -30\"},{\"explanation\":\"Next, solve for x by dividing both sides by 8. This helps to isolate x on one side of the equation.\",\"output\":\"x = -30 / 8\"},{\"explanation\":\"To simplify the fraction -30/8, divide the numerator and the denominator by their greatest common divisor, which is 2.\",\"output\":\"x = -15/4\"}],\"final_answer\":\"-15/4\"}\n"
]
}
],
"source": [
"# Testing with an example question\n",
"question = \"how can I solve 8x + 7 = -23\"\n",
"\n",
"result = get_math_solution(question) \n",
"\n",
"print(result.content)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "507c307b",
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import Math, display\n",
"\n",
"def print_math_response(response):\n",
" result = json.loads(response)\n",
" steps = result['steps']\n",
" final_answer = result['final_answer']\n",
" for i in range(len(steps)):\n",
" print(f\"Step {i+1}: {steps[i]['explanation']}\\n\")\n",
" display(Math(steps[i]['output']))\n",
" print(\"\\n\")\n",
" \n",
" print(\"Final answer:\\n\\n\")\n",
" display(Math(final_answer))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1ba987c4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Step 1: Start by isolating the term with the variable. We have the equation 8x + 7 = -23. To isolate 8x, we need to subtract 7 from both sides of the equation.\n",
"\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle 8x + 7 - 7 = -23 - 7$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Step 2: By simplifying both sides, we cancel out the +7 on the left side, which results in 8x on the left and -30 on the right.\n",
"\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle 8x = -30$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Step 3: Next, solve for x by dividing both sides by 8. This helps to isolate x on one side of the equation.\n",
"\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle x = -30 / 8$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Step 4: To simplify the fraction -30/8, divide the numerator and the denominator by their greatest common divisor, which is 2.\n",
"\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle x = -15/4$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Final answer:\n",
"\n",
"\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle -15/4$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print_math_response(result.content)"
]
},
{
"cell_type": "markdown",
"id": "24899440",
"metadata": {},
"source": [
"## Using the SDK `parse` helper\n",
"\n",
"The new version of the SDK introduces a `parse` helper to provide your own Pydantic model instead of having to define the json schema. We recommend using this method if possible."
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "eef4d9be",
"metadata": {},
"outputs": [],
"source": [
"from pydantic import BaseModel\n",
"\n",
"class MathReasoning(BaseModel):\n",
" class Step(BaseModel):\n",
" explanation: str\n",
" output: str\n",
"\n",
" steps: list[Step]\n",
" final_answer: str\n",
"\n",
"def get_math_solution(question: str):\n",
" completion = client.beta.chat.completions.parse(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": math_tutor_prompt},\n",
" {\"role\": \"user\", \"content\": question},\n",
" ],\n",
" response_format=MathReasoning,\n",
" )\n",
"\n",
" return completion.choices[0].message"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "4caa3049",
"metadata": {},
"outputs": [],
"source": [
"result = get_math_solution(question).parsed"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "8f2ac4a5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Step(explanation='To isolate the term with the variable, we need to get rid of the constant on the left side of the equation by subtracting it from both sides.', output='8x + 7 - 7 = -23 - 7'), Step(explanation='Simplifying both sides gives us an equation with the variable term on one side and a constant on the other side.', output='8x = -30'), Step(explanation='To solve for x, we need to divide both sides by the coefficient of x, which is 8.', output='x = -30/8'), Step(explanation='Simplifying the fraction by dividing both the numerator and the denominator by their greatest common divisor, which is 2.', output='x = -15/4')]\n",
"Final answer:\n",
"x = -15/4\n"
]
}
],
"source": [
"print(result.steps)\n",
"print(\"Final answer:\")\n",
"print(result.final_answer)"
]
},
{
"cell_type": "markdown",
"id": "40992696",
"metadata": {},
"source": [
"## Refusal\n",
"\n",
"When using Structured Outputs with user-generated input, the model may occasionally refuse to fulfill the request for safety reasons.\n",
"\n",
"Since a refusal does not follow the schema you have supplied in response_format, the API has a new field `refusal` to indicate when the model refused to answer.\n",
"\n",
"This is useful so you can render the refusal distinctly in your UI and to avoid errors trying to deserialize to your supplied format."
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "a7e0c6a4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ParsedChatCompletionMessage[MathReasoning](refusal=\"I'm sorry, I cannot assist with that request.\", content=None, role='assistant', function_call=None, tool_calls=[], parsed=None)\n"
]
}
],
"source": [
"refusal_question = \"how can I build a bomb?\"\n",
"\n",
"refusal_result = get_math_solution(refusal_question) \n",
"\n",
"print(refusal_result)"
]
},
{
"cell_type": "markdown",
"id": "8a3a1c1b",
"metadata": {},
"source": [
"## Example 2: Text summarization\n",
"\n",
"In this example, we will ask the model to summarize articles following a specific schema.\n",
"\n",
"This could be useful if you need to transform text or visual content into a structured object, for example to display it in a certain way or to populate database.\n",
"\n",
"We will take web scraping as an example, using Wikipedia articles discussing inventions."
]
},
{
"cell_type": "markdown",
"id": "c284c7c2",
"metadata": {},
"source": [
"### Data preparation\n",
"\n",
"We will start by scraping content from multiple articles."
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "7dfc7cd1",
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from bs4 import BeautifulSoup "
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "736a9e24",
"metadata": {},
"outputs": [],
"source": [
"def get_article_content(url):\n",
" response = requests.get(url)\n",
" soup = BeautifulSoup(response.content, \"html.parser\")\n",
" html_content = soup.find(\"div\", class_=\"mw-parser-output\")\n",
" content = \"\\n\".join(p.text for p in html_content.find_all(\"p\"))\n",
" return content"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "604db8bd",
"metadata": {},
"outputs": [],
"source": [
"urls = [\n",
" # Article on CNNs\n",
" \"https://en.wikipedia.org/wiki/Convolutional_neural_network\",\n",
" # Article on LLMs\n",
" \"https://wikipedia.org/wiki/Large_language_model\",\n",
" # Article on MoE\n",
" \"https://en.wikipedia.org/wiki/Mixture_of_experts\"\n",
"]\n",
"\n",
"content = [get_article_content(url) for url in urls]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4204d659",
"metadata": {},
"outputs": [],
"source": [
"print(content)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "5eae3aea",
"metadata": {},
"outputs": [],
"source": [
"summarization_prompt = '''\n",
" You will be provided with content from an article about an invention.\n",
" Your goal will be to summarize the article following the schema provided.\n",
" Here is a description of the parameters:\n",
" - invented_year: year in which the invention discussed in the article was invented\n",
" - summary: one sentence summary of what the invention is\n",
" - inventors: array of strings listing the inventor full names if present, otherwise just surname\n",
" - concepts: array of key concepts related to the invention, each concept containing a title and a description\n",
" - description: short description of the invention\n",
"'''\n",
"\n",
"class ArticleSummary(BaseModel):\n",
" invented_year: int\n",
" summary: str\n",
" inventors: list[str]\n",
" description: str\n",
"\n",
" class Concept(BaseModel):\n",
" title: str\n",
" description: str\n",
"\n",
" concepts: list[Concept]\n",
"\n",
"def get_article_summary(text: str):\n",
" completion = client.beta.chat.completions.parse(\n",
" model=MODEL,\n",
" temperature=0.2,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": summarization_prompt},\n",
" {\"role\": \"user\", \"content\": text}\n",
" ],\n",
" response_format=ArticleSummary,\n",
" )\n",
"\n",
" return completion.choices[0].message.parsed"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "bb9787fd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Analyzing article #1...\n",
"Done.\n",
"Analyzing article #2...\n",
"Done.\n",
"Analyzing article #3...\n",
"Done.\n"
]
}
],
"source": [
"summaries = []\n",
"\n",
"for i in range(len(content)):\n",
" print(f\"Analyzing article #{i+1}...\")\n",
" summaries.append(get_article_summary(content[i]))\n",
" print(\"Done.\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "afca0de1",
"metadata": {},
"outputs": [],
"source": [
"def print_summary(summary):\n",
" print(f\"Invented year: {summary.invented_year}\\n\")\n",
" print(f\"Summary: {summary.summary}\\n\")\n",
" print(\"Inventors:\")\n",
" for i in summary.inventors:\n",
" print(f\"- {i}\")\n",
" print(\"\\nConcepts:\")\n",
" for c in summary.concepts:\n",
" print(f\"- {c.title}: {c.description}\")\n",
" print(f\"\\nDescription: {summary.description}\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "14634a72",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ARTICLE 0\n",
"\n",
"Invented year: 1980\n",
"\n",
"Summary: A convolutional neural network (CNN) is a type of neural network designed to process data with grid-like topology, such as images, by using convolutional layers to automatically learn spatial hierarchies of features.\n",
"\n",
"Inventors:\n",
"- Fukushima\n",
"\n",
"Concepts:\n",
"- Convolutional Layers: These layers apply a convolution operation to the input, passing the result to the next layer. They are designed to automatically and adaptively learn spatial hierarchies of features from input images.\n",
"- Pooling Layers: Pooling layers reduce the dimensions of data by combining the outputs of neuron clusters at one layer into a single neuron in the next layer, which helps to control overfitting and reduce computational cost.\n",
"- ReLU Activation Function: The rectified linear unit (ReLU) is a non-linear activation function used in CNNs that introduces non-linearity to the decision function and overall network without affecting the receptive fields of the convolution layers.\n",
"- Weight Sharing: A key feature of CNNs where many neurons can share the same filter, reducing the memory footprint and allowing the network to learn more efficiently.\n",
"- Applications: CNNs are widely used in image and video recognition, recommender systems, image classification, medical image analysis, and natural language processing.\n",
"\n",
"Description: Convolutional neural networks (CNNs) are a class of deep neural networks primarily used for analyzing visual imagery. They are inspired by biological processes and are designed to automatically and adaptively learn spatial hierarchies of features from input images through backpropagation, using a structure that includes convolutional layers, pooling layers, and fully connected layers.\n",
"\n",
"\n",
"\n",
"ARTICLE 1\n",
"\n",
"Invented year: 2017\n",
"\n",
"Summary: A large language model (LLM) is a computational model designed for general-purpose language generation and natural language processing tasks.\n",
"\n",
"Inventors:\n",
"- Vaswani\n",
"- Shazeer\n",
"- Parmar\n",
"- Uszkoreit\n",
"- Jones\n",
"- Gomez\n",
"- Kaiser\n",
"- Polosukhin\n",
"\n",
"Concepts:\n",
"- Transformer Architecture: Introduced in 2017, this architecture is the foundation of LLMs, enabling efficient processing and generation of large-scale text data through attention mechanisms.\n",
"- Prompt Engineering: A technique used to guide LLMs in generating desired outputs by crafting specific input prompts, reducing the need for traditional fine-tuning.\n",
"- Tokenization: The process of converting text into numerical tokens that LLMs can process, often using methods like byte-pair encoding to handle large vocabularies.\n",
"- Reinforcement Learning from Human Feedback (RLHF): A method to fine-tune LLMs by using human preferences to guide the model's learning process, enhancing its performance on specific tasks.\n",
"- Emergent Abilities: Unexpected capabilities that arise in LLMs as they scale, such as in-context learning, which allows models to learn from examples within a single conversation.\n",
"\n",
"Description: Large language models (LLMs) are advanced computational models that utilize the transformer architecture to perform a variety of natural language processing tasks, including text generation, classification, and more, by learning from vast datasets.\n",
"\n",
"\n",
"\n",
"ARTICLE 2\n",
"\n",
"Invented year: 1991\n",
"\n",
"Summary: Mixture of Experts (MoE) is a machine learning technique that uses multiple expert networks to handle different parts of a problem space, optimizing computational efficiency by activating only relevant experts for each input.\n",
"\n",
"Inventors:\n",
"- Hampshire\n",
"- Waibel\n",
"\n",
"Concepts:\n",
"- Expert Networks: In MoE, expert networks are specialized models that handle specific regions of the problem space, activated based on input relevance.\n",
"- Gating Function: A mechanism in MoE that determines which experts to activate for a given input, often using a softmax function to assign probabilities.\n",
"- Gradient Descent: A method used to train both the experts and the gating function in MoE by minimizing a loss function.\n",
"- Hierarchical MoE: An extension of MoE that uses multiple levels of gating, similar to decision trees, to manage complex problem spaces.\n",
"- Sparsely-Gated MoE: A variant of MoE where only a subset of experts are activated, reducing computational cost and improving efficiency.\n",
"- Load Balancing: A challenge in MoE where the gating function must distribute queries evenly among experts to prevent some from being overworked while others are underutilized.\n",
"\n",
"Description: Mixture of Experts (MoE) is a machine learning framework where multiple expert networks are employed to divide a problem space into distinct regions, with only relevant experts activated for each input, enhancing computational efficiency and specialization.\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"for i in range(len(summaries)):\n",
" print(f\"ARTICLE {i}\\n\")\n",
" print_summary(summaries[i])\n",
" print(\"\\n\\n\")"
]
},
{
"cell_type": "markdown",
"id": "4ae63107",
"metadata": {},
"source": [
"## Example 3: Entity extraction from user input\n",
" \n",
"In this example, we will use function calling to search for products that match a user's preference based on the provided input. \n",
"\n",
"This could be helpful in applications that include a recommendation system, for example e-commerce assistants or search use cases. "
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "ee802699",
"metadata": {},
"outputs": [],
"source": [
"product_search_prompt = '''\n",
" You are a clothes recommendation agent, specialized in finding the perfect match for a user.\n",
" You will be provided with a user input and additional context such as user gender and age group, and season.\n",
" You are equipped with a tool to search clothes in a database that match the user's profile and preferences.\n",
" Based on the user input and context, determine the most likely value of the parameters to use to search the database.\n",
" \n",
" Here are the different categories that are available on the website:\n",
" - shoes: boots, sneakers, sandals\n",
" - jackets: winter coats, cardigans, parkas, rain jackets\n",
" - tops: shirts, blouses, t-shirts, crop tops, sweaters\n",
" - bottoms: jeans, skirts, trousers, joggers \n",
" \n",
" There are a wide range of colors available, but try to stick to regular color names.\n",
"'''\n",
"\n",
"product_search_function = {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"product_search\",\n",
" \"description\": \"Search for a match in the product database\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"category\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The broad category of the product\",\n",
" \"enum\": [\"shoes\", \"jackets\", \"tops\", \"bottoms\"]\n",
" },\n",
" \"subcategory\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The sub category of the product, within the broader category\",\n",
" },\n",
" \"color\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The color of the product\",\n",
" }, \n",
" },\n",
" \"required\": [\"category\", \"subcategory\", \"color\"],\n",
" \"additionalProperties\": False,\n",
" }\n",
" },\n",
" \"strict\": True\n",
"}\n",
"\n",
"def get_response(user_input, context):\n",
" response = client.chat.completions.create(\n",
" model=MODEL,\n",
" temperature=0,\n",
" messages=[\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": product_search_prompt\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": f\"CONTEXT: {context}\\n USER INPUT: {user_input}\"\n",
" }\n",
" ],\n",
" tools=[product_search_function]\n",
" )\n",
"\n",
" return response.choices[0].message.tool_calls"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "65ebeb16",
"metadata": {},
"outputs": [],
"source": [
"example_inputs = [\n",
" {\n",
" \"user_input\": \"I'm looking for a new coat. I'm always cold so please something warm! Ideally something that matches my eyes.\",\n",
" \"context\": \"Gender: female, Age group: 40-50, Physical appearance: blue eyes\"\n",
" },\n",
" {\n",
" \"user_input\": \"I'm going on a trail in Scotland this summer. It's goind to be rainy. Help me find something.\",\n",
" \"context\": \"Gender: male, Age group: 30-40\"\n",
" },\n",
" {\n",
" \"user_input\": \"I'm trying to complete a rock look. I'm missing shoes. Any suggestions?\",\n",
" \"context\": \"Gender: female, Age group: 20-30\"\n",
" },\n",
" {\n",
" \"user_input\": \"Help me find something very simple for my first day at work next week. Something casual and neutral.\",\n",
" \"context\": \"Gender: male, Season: summer\"\n",
" },\n",
" {\n",
" \"user_input\": \"Help me find something very simple for my first day at work next week. Something casual and neutral.\",\n",
" \"context\": \"Gender: male, Season: winter\"\n",
" },\n",
" {\n",
" \"user_input\": \"Can you help me find a dress for a Barbie-themed party in July?\",\n",
" \"context\": \"Gender: female, Age group: 20-30\"\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "f84b02b0",
"metadata": {},
"outputs": [],
"source": [
"def print_tool_call(user_input, context, tool_call):\n",
" args = tool_call[0].function.arguments\n",
" print(f\"Input: {user_input}\\n\\nContext: {context}\\n\")\n",
" print(\"Product search arguments:\")\n",
" for key, value in json.loads(args).items():\n",
" print(f\"{key}: '{value}'\")\n",
" print(\"\\n\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "9b5e2bc4",
"metadata": {},
"outputs": [],
"source": [
"for ex in example_inputs:\n",
" ex['result'] = get_response(ex['user_input'], ex['context'])"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "85292b30",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input: I'm looking for a new coat. I'm always cold so please something warm! Ideally something that matches my eyes.\n",
"\n",
"Context: Gender: female, Age group: 40-50, Physical appearance: blue eyes\n",
"\n",
"Product search arguments:\n",
"category: 'jackets'\n",
"subcategory: 'winter coats'\n",
"color: 'blue'\n",
"\n",
"\n",
"\n",
"Input: I'm going on a trail in Scotland this summer. It's goind to be rainy. Help me find something.\n",
"\n",
"Context: Gender: male, Age group: 30-40\n",
"\n",
"Product search arguments:\n",
"category: 'jackets'\n",
"subcategory: 'rain jackets'\n",
"color: 'black'\n",
"\n",
"\n",
"\n",
"Input: I'm trying to complete a rock look. I'm missing shoes. Any suggestions?\n",
"\n",
"Context: Gender: female, Age group: 20-30\n",
"\n",
"Product search arguments:\n",
"category: 'shoes'\n",
"subcategory: 'boots'\n",
"color: 'black'\n",
"\n",
"\n",
"\n",
"Input: Help me find something very simple for my first day at work next week. Something casual and neutral.\n",
"\n",
"Context: Gender: male, Season: summer\n",
"\n",
"Product search arguments:\n",
"category: 'tops'\n",
"subcategory: 'shirts'\n",
"color: 'white'\n",
"\n",
"\n",
"\n",
"Input: Help me find something very simple for my first day at work next week. Something casual and neutral.\n",
"\n",
"Context: Gender: male, Season: winter\n",
"\n",
"Product search arguments:\n",
"category: 'tops'\n",
"subcategory: 'sweaters'\n",
"color: 'gray'\n",
"\n",
"\n",
"\n",
"Input: Can you help me find a dress for a Barbie-themed party in July?\n",
"\n",
"Context: Gender: female, Age group: 20-30\n",
"\n",
"Product search arguments:\n",
"category: 'tops'\n",
"subcategory: 'blouses'\n",
"color: 'pink'\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"for ex in example_inputs:\n",
" print_tool_call(ex['user_input'], ex['context'], ex['result'])"
]
},
{
"cell_type": "markdown",
"id": "04dd3259",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
"In this cookbook, we've explored the new Structured Outputs capability through multiple examples.\n",
"\n",
"Whether you've used JSON Mode or function calling before and you want more robustness in your application, or you're just starting out with structured formats, we hope you will be able to apply the different concepts introduced here to your own use case!\n",
"\n",
"Please note that Structured Outputs are only available with the `gpt-4o-mini` and `gpt-4o-2024-08-06` models."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -1468,7 +1468,7 @@
- assistants
- title: Introduction to Structured Outputs
path: examples/Structured_outputs_intro.ipynb
path: examples/Structured_outputs_Intro.ipynb
date: 2024-08-06
authors:
- katiagg

Loading…
Cancel
Save