219 lines
6.4 KiB
Plaintext
219 lines
6.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"id": "902bff53",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import { load } from \"jsr:@std/dotenv\";\n",
|
|
"import OpenAI from \"jsr:@openai/openai\";\n",
|
|
"\n",
|
|
"const env = await load({\n",
|
|
" export: true,\n",
|
|
"});\n",
|
|
"\n",
|
|
"const openai = new OpenAI();"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "68f89168",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"const perishables = {\n",
|
|
" expiryDate: { type: \"string\", format: \"date\" },\n",
|
|
" batchNumber: {\n",
|
|
" type: \"string\",\n",
|
|
" maxLength: 20,\n",
|
|
" description: \"Puede ser inventado\",\n",
|
|
" },\n",
|
|
" storageTempC: {\n",
|
|
" type: \"number\",\n",
|
|
" minimum: -30,\n",
|
|
" maximum: 10,\n",
|
|
" description: \"Temperatura de almacenamiento en grados Celsius\",\n",
|
|
" },\n",
|
|
"};\n",
|
|
"\n",
|
|
"const origen = {\n",
|
|
" country: { type: \"string\", maxLength: 50 },\n",
|
|
" city: { type: \"string\", maxLength: 50 },\n",
|
|
"};\n",
|
|
"\n",
|
|
"// Transformar las definiciones: poner en properties, agregar required, y additionalProperties: false\n",
|
|
"\n",
|
|
"const perishablesWithMetadata = {\n",
|
|
" ...perishables,\n",
|
|
" type: \"object\",\n",
|
|
" required: Object.keys(perishables),\n",
|
|
" additionalProperties: false,\n",
|
|
"};\n",
|
|
"\n",
|
|
"const origenWithMetadata = {\n",
|
|
" ...origen,\n",
|
|
" type: \"object\",\n",
|
|
" required: Object.keys(origen),\n",
|
|
" additionalProperties: false,\n",
|
|
"};\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"const combinedSchema = {\n",
|
|
" type: \"object\",\n",
|
|
" $defs: {\n",
|
|
" perishables: perishablesWithMetadata,\n",
|
|
" origen: origenWithMetadata,\n",
|
|
" },\n",
|
|
" properties: {\n",
|
|
" nombre: { type: \"string\", maxLength: 100 },\n",
|
|
" fabricante: { type: \"string\", maxLength: 100 },\n",
|
|
"\n",
|
|
" // Agregar campo del preset\n",
|
|
" perishables: { $ref: \"#/$defs/perishables\" },\n",
|
|
" origen: { $ref: \"#/$defs/origen\" },\n",
|
|
" },\n",
|
|
" required: [\"nombre\", \"fabricante\", \"perishables\", \"origen\"],\n",
|
|
" additionalProperties: false,\n",
|
|
"};\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 26,
|
|
"id": "1403c789",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"const combinedSchema = {\n",
|
|
" type: \"object\",\n",
|
|
" $defs: {\n",
|
|
" perishables: {\n",
|
|
" type: \"object\",\n",
|
|
" properties: {\n",
|
|
" expiryDate: { type: \"string\", format: \"date\" },\n",
|
|
" batchNumber: {\n",
|
|
" type: \"string\",\n",
|
|
" maxLength: 20,\n",
|
|
" description: \"Puede ser inventado\",\n",
|
|
" },\n",
|
|
" storageTempC: {\n",
|
|
" type: \"number\",\n",
|
|
" minimum: -30,\n",
|
|
" maximum: 10,\n",
|
|
" description:\n",
|
|
" \"Temperatura de almacenamiento en grados Celsius\",\n",
|
|
" },\n",
|
|
" },\n",
|
|
" required: [\"expiryDate\", \"batchNumber\", \"storageTempC\"],\n",
|
|
" additionalProperties: false,\n",
|
|
" },\n",
|
|
" origen: {\n",
|
|
" type: \"object\",\n",
|
|
" properties: {\n",
|
|
" country: { type: \"string\", maxLength: 50 },\n",
|
|
" city: { type: \"string\", maxLength: 50 },\n",
|
|
" },\n",
|
|
" required: [\"country\", \"city\"],\n",
|
|
" additionalProperties: false,\n",
|
|
" },\n",
|
|
" },\n",
|
|
" properties: {\n",
|
|
" nombre: { type: \"string\", maxLength: 100 },\n",
|
|
" fabricante: { type: \"string\", maxLength: 100 },\n",
|
|
"\n",
|
|
" // Agregar campo del preset\n",
|
|
" perishables: { $ref: \"#/$defs/perishables\" },\n",
|
|
" origen: { $ref: \"#/$defs/origen\" },\n",
|
|
"\n",
|
|
" perecedero: { type: \"null\", \"x-column\": \"perishable\", const: null },\n",
|
|
" },\n",
|
|
" required: [\"nombre\", \"fabricante\", \"perishables\", \"origen\", \"perecedero\"],\n",
|
|
" additionalProperties: false,\n",
|
|
"};\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 27,
|
|
"id": "1fbda124",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"const result = await openai.responses.create({\n",
|
|
" model: \"gpt-5-nano\",\n",
|
|
" input:\n",
|
|
" \"Genera un producto con el siguiente formato JSON, sin texto adicional:\",\n",
|
|
" text: {\n",
|
|
" format: {\n",
|
|
" type: \"json_schema\",\n",
|
|
" name: \"producto\",\n",
|
|
" schema: combinedSchema,\n",
|
|
" },\n",
|
|
" },\n",
|
|
"});\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 28,
|
|
"id": "85e7967a",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{\n",
|
|
" nombre: \u001b[32m\"Galletas de avena premium\"\u001b[39m,\n",
|
|
" fabricante: \u001b[32m\"Industrias Gourmet S.L.\"\u001b[39m,\n",
|
|
" perishables: {\n",
|
|
" expiryDate: \u001b[32m\"2026-12-31\"\u001b[39m,\n",
|
|
" batchNumber: \u001b[32m\"BATCH-00123\"\u001b[39m,\n",
|
|
" storageTempC: \u001b[33m4\u001b[39m\n",
|
|
" },\n",
|
|
" origen: { country: \u001b[32m\"España\"\u001b[39m, city: \u001b[32m\"Valencia\"\u001b[39m },\n",
|
|
" perecedero: \u001b[1mnull\u001b[22m\n",
|
|
"}"
|
|
]
|
|
},
|
|
"execution_count": 28,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"JSON.parse(result.output_text)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dae842ad",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Deno",
|
|
"language": "typescript",
|
|
"name": "deno"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": "typescript",
|
|
"file_extension": ".ts",
|
|
"mimetype": "text/x.typescript",
|
|
"name": "typescript",
|
|
"nbconvert_exporter": "script",
|
|
"pygments_lexer": "typescript",
|
|
"version": "5.9.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|