{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "bd3fc279", "metadata": {}, "outputs": [], "source": [ "#\n", "# For licensing see accompanying LICENSE.md file.\n", "# Copyright (C) 2026 Apple Inc. All Rights Reserved.\n", "#" ] }, { "cell_type": "code", "execution_count": null, "id": "bfc724c9", "metadata": {}, "outputs": [], "source": [ "from openai import OpenAI\n", "import json\n", "import tqdm\n", "\n", "model_name: str = \"gpt-4o-mini\"\n", "OPENAI_API_KEY = \"\"\n", "client = OpenAI(api_key=OPENAI_API_KEY)" ] }, { "cell_type": "code", "execution_count": 8, "id": "b726ad3d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading 1751 data samples.\n" ] } ], "source": [ "# Generating eval jsonl file first through `convert_to_eval_jsonl.py`.\n", "label_path = \"data/so_bench_eval.jsonl\"\n", "with open(label_path, \"r\", encoding=\"utf-8\") as f:\n", " labels = [json.loads(line) for line in f]\n", "print(f\"Loading {len(labels)} data samples.\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "7d9089bc", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 1751/1751 [5:06:54<00:00, 10.52s/it] \n" ] } ], "source": [ "# Running inferences. You can either directly adding prediction \n", "# results directly back to labels for evaluation.\n", "for sample in tqdm.tqdm(labels):\n", " response = client.chat.completions.create(\n", " model=model_name,\n", " messages=sample[\"messages\"],\n", " )\n", " content = response.choices[0].message.content\n", " sample[\"generation\"] = content" ] }, { "cell_type": "code", "execution_count": 10, "id": "092b0293", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'num_examples': 1751, 'num_invalid_generation': 15, 'exact_match': 0.010850942318675044, 'followed_schema': 0.8680753854940034, 'relaxed_field_match_accuracy': 0.49982894286691754, 'relaxed_all_fields_match_accuracy': 0.052995391705069124}\n" ] } ], "source": [ "# Compute evalaution metrics.\n", "from eval import metric_fn\n", "labels_with_predictions = labels\n", "print(metric_fn(labels))" ] } ], "metadata": { "kernelspec": { "display_name": "ajax", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }