-
Notifications
You must be signed in to change notification settings - Fork 10.8k
wip #1841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
wip #1841
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
301 changes: 301 additions & 0 deletions
301
examples/evaluation/use-cases/responses-evaluation.ipynb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,301 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Evaluating a new model on existing responses" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"In the following eval, we are going to compare how a new model (gpt-4.1-mini) compares to our old model (gpt-4o-mini) by evaluating it on some stored responses. The benefit of this is for most developers, they won't have to spend any time putting together a whole eval -- all of their data will already be stored in their [logs page](https://platform.openai.com/logs)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 30, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import openai\n", | ||
"import os\n", | ||
"\n", | ||
"\n", | ||
"client = openai.OpenAI()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We want to see how gpt-4.1 compares to gpt-4o on explaining a code base. Since can only use the responses datasource if you already have user traffic, we're going to generate some example traffic using 4o, and then compare how it does to gpt-4.1. \n", | ||
"\n", | ||
"We're going to get some example code files from the OpenAI SDK, and ask gpt-4o to explain them to us." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"openai_sdk_file_path = os.path.dirname(openai.__file__)\n", | ||
"\n", | ||
"# Get some example code files from the OpenAI SDK \n", | ||
"file_paths = [\n", | ||
" os.path.join(openai_sdk_file_path, \"resources\", \"evals\", \"evals.py\"),\n", | ||
" os.path.join(openai_sdk_file_path, \"resources\", \"responses\", \"responses.py\"),\n", | ||
" os.path.join(openai_sdk_file_path, \"resources\", \"images.py\"),\n", | ||
" os.path.join(openai_sdk_file_path, \"resources\", \"embeddings.py\"),\n", | ||
" os.path.join(openai_sdk_file_path, \"resources\", \"files.py\"),\n", | ||
"]\n", | ||
"\n", | ||
"print(file_paths[0])\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now, lets generate some responses. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"for file_path in file_paths:\n", | ||
" response = client.responses.create(\n", | ||
" input=[\n", | ||
" {\"role\": \"user\",\n", | ||
" \"content\": [\n", | ||
" {\n", | ||
" \"type\": \"input_text\",\n", | ||
" \"text\": \"What does this file do?\"\n", | ||
" },\n", | ||
" {\n", | ||
" \"type\": \"input_text\",\n", | ||
" \"text\": open(file_path, \"r\").read(),\n", | ||
" },\n", | ||
" ]},\n", | ||
" ],\n", | ||
" model=\"gpt-4o-mini\",\n", | ||
" )\n", | ||
" print(response.output_text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Note that in order for this to work, you'll have to be doing this on an org where data logging isn't disabled (through zdr, etc). If you aren't sure if this is the case for you, go to https://platform.openai.com/logs?api=responses and see if you can see the responses you just generated." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 31, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"grader_system_prompt = \"\"\"\n", | ||
"You are **Code-Explanation Grader**, an expert software engineer and technical writer. \n", | ||
"Your job is to score how well *Model A* explained the purpose and behaviour of a given source-code file.\n", | ||
"\n", | ||
"### What you receive\n", | ||
"1. **File contents** – the full text of the code file (or a representative excerpt). \n", | ||
"2. **Candidate explanation** – the answer produced by Model A that tries to describe what the file does.\n", | ||
"\n", | ||
"### What to produce\n", | ||
"Return a single JSON object that can be parsed by `json.loads`, containing:\n", | ||
"```json\n", | ||
"{\n", | ||
" \"steps\": [\n", | ||
" { \"description\": \"...\", \"result\": \"float\" },\n", | ||
" { \"description\": \"...\", \"result\": \"float\" },\n", | ||
" { \"description\": \"...\", \"result\": \"float\" }\n", | ||
" ],\n", | ||
" \"result\": \"float\"\n", | ||
"}\n", | ||
"```\n", | ||
"• Each object in `steps` documents your reasoning for one category listed under “Scoring dimensions”. \n", | ||
"• Place your final 1 – 7 quality score (inclusive) in the top-level `result` key as a **string** (e.g. `\"5.5\"`).\n", | ||
"\n", | ||
"### Scoring dimensions (evaluate in this order)\n", | ||
"\n", | ||
"1. **Correctness & Accuracy ≈ 45 %** \n", | ||
" • Does the explanation match the actual code behaviour, interfaces, edge cases, and side effects? \n", | ||
" • Fact-check every technical claim; penalise hallucinations or missed key functionality.\n", | ||
"\n", | ||
"2. **Completeness & Depth ≈ 25 %** \n", | ||
" • Are all major components, classes, functions, data flows, and external dependencies covered? \n", | ||
" • Depth should be appropriate to the file’s size/complexity; superficial glosses lose points.\n", | ||
"\n", | ||
"3. **Clarity & Organization ≈ 20 %** \n", | ||
" • Is the explanation well-structured, logically ordered, and easy for a competent developer to follow? \n", | ||
" • Good use of headings, bullet lists, and concise language is rewarded.\n", | ||
"\n", | ||
"4. **Insight & Usefulness ≈ 10 %** \n", | ||
" • Does the answer add valuable context (e.g., typical use cases, performance notes, risks) beyond line-by-line paraphrase? \n", | ||
" • Highlighting **why** design choices matter is a plus.\n", | ||
"\n", | ||
"### Error taxonomy\n", | ||
"• **Major error** – Any statement that materially misrepresents the file (e.g., wrong API purpose, inventing non-existent behaviour). \n", | ||
"• **Minor error** – Small omission or wording that slightly reduces clarity but doesn’t mislead. \n", | ||
"List all found errors in your `steps` reasoning.\n", | ||
"\n", | ||
"### Numeric rubric\n", | ||
"1 Catastrophically wrong; mostly hallucination or irrelevant. \n", | ||
"2 Many major errors, few correct points. \n", | ||
"3 Several major errors OR pervasive minor mistakes; unreliable. \n", | ||
"4 Mostly correct but with at least one major gap or multiple minors; usable only with caution. \n", | ||
"5 Solid, generally correct; minor issues possible but no major flaws. \n", | ||
"6 Comprehensive, accurate, and clear; only very small nit-picks. \n", | ||
"7 Exceptional: precise, thorough, insightful, and elegantly presented; hard to improve.\n", | ||
"\n", | ||
"Use the full scale. Reserve 6.5 – 7 only when you are almost certain the explanation is outstanding.\n", | ||
"\n", | ||
"Then set `\"result\": \"4.0\"` (example).\n", | ||
"\n", | ||
"Be rigorous and unbiased.\n", | ||
"\"\"\"\n", | ||
"user_input_message = \"\"\"**User input**\n", | ||
"\n", | ||
"{{item.input}}\n", | ||
"\n", | ||
"**Response to evaluate**\n", | ||
"\n", | ||
"{{sample.output_text}}\n", | ||
"\"\"\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 25, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"logs_eval = client.evals.create(\n", | ||
" name=\"Code QA Eval\",\n", | ||
" data_source_config={\n", | ||
" \"type\": \"logs\",\n", | ||
" },\n", | ||
" testing_criteria=[\n", | ||
" {\n", | ||
"\t\t\t\"type\": \"score_model\",\n", | ||
" \"name\": \"General Evaluator\",\n", | ||
" \"model\": \"o3\",\n", | ||
" \"input\": [{\n", | ||
" \"role\": \"system\",\n", | ||
" \"content\": grader_system_prompt,\n", | ||
" }, {\n", | ||
" \"role\": \"user\",\n", | ||
" \"content\": user_input_message,\n", | ||
" },\n", | ||
" ],\n", | ||
" \"range\": [1, 7],\n", | ||
" \"pass_threshold\": 5.5,\n", | ||
" }\n", | ||
" ]\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"First, lets kick off a run to evaluate how good the original responses were. To do this, we just set the filters for what responses we want to evaluate on" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 26, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpt_4o_mini_run = client.evals.runs.create(\n", | ||
" name=\"gpt-4o-mini\",\n", | ||
" eval_id=logs_eval.id,\n", | ||
" data_source={\n", | ||
" \"type\": \"responses\",\n", | ||
" \"source\": {\"type\": \"responses\", \"limit\": len(file_paths)}, # just grab the most recent responses\n", | ||
" },\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now, let's see how 4.1-mini does!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 27, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpt_41_mini_run = client.evals.runs.create(\n", | ||
" name=\"gpt-4.1-mini\",\n", | ||
" eval_id=logs_eval.id,\n", | ||
" data_source={\n", | ||
" \"type\": \"responses\",\n", | ||
" \"source\": {\"type\": \"responses\", \"limit\": len(file_paths)},\n", | ||
" \"input_messages\": {\n", | ||
" \"type\": \"item_reference\",\n", | ||
" \"item_reference\": \"item.input\",\n", | ||
" },\n", | ||
" \"model\": \"gpt-4.1-mini\",\n", | ||
" }\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now, lets go to the dashboard to see how we did!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpt_4o_mini_run.report_url" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [] | ||
} | ||
], | ||
"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.11.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully this eval is fairly repeatable, so ideally there can be a "takeaway" here, like:
"4.1-mini" does about as well as our original at less cost, etc.