Add initial implementation for create-chat-conversation function and related configuration files
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "a9a99d9d",
|
||||
"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": 9,
|
||||
"id": "89b3fffb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import { createClient } from \"@supabase/supabase-js\";\n",
|
||||
"const supabaseUrl = Deno.env.get(\"SUPABASE_URL\") || env.SUPABASE_URL;\n",
|
||||
"const supabaseKey = Deno.env.get(\"SUPABASE_ANON_KEY\") || env.SUPABASE_ANON_KEY;\n",
|
||||
"const supabase = createClient(supabaseUrl, supabaseKey);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "33290880",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Create a conversation for a plan de estudio\n",
|
||||
"\n",
|
||||
"const { data: input } = await supabase.from(\"planes_estudio\").limit(1).select(\"id\").single();\n",
|
||||
"const openaiConversation = await openai.conversations.create({\n",
|
||||
" metadata: {\n",
|
||||
" \"tabla\": \"planes_estudio\",\n",
|
||||
" \"id\": input.id,\n",
|
||||
"\n",
|
||||
" instanciador: \"alex\",\n",
|
||||
" },\n",
|
||||
"\n",
|
||||
" items: [\n",
|
||||
" { type: \"message\", role: \"system\", content: \"En caso de que te pidan algo que no tiene nada que ver con planes de estudio o asignatura responde con un refusal.\" },\n",
|
||||
" ],\n",
|
||||
"});\n",
|
||||
"\n",
|
||||
"const conversationPlane = await supabase.from(\"conversation_planes_estudio\").insert({\n",
|
||||
" conversation_id: openaiConversation.id,\n",
|
||||
" plan_estudio_id: input.id,\n",
|
||||
"}).select(\"id\").single();"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f726b80e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Archivar una conversación\n",
|
||||
"const { data: conversation } = await supabase.from(\"conversation_planes_estudio\").limit(1).select(\"conversation_id\").single();\n",
|
||||
"\n",
|
||||
"const items = await openai.conversations.items(conversation.conversation_id);\n",
|
||||
"await supabase.from(\"conversation_planes_estudio\").update({\n",
|
||||
" estado: \"ARCHIVANDO\",\n",
|
||||
" conversation_json: items,\n",
|
||||
"}).eq(\"id\", conversationPlane.id);\n",
|
||||
"\n",
|
||||
"await openai.conversations.delete(conversation.conversation_id);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "cfae6e48",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Listar conversaciones dadas de un plan de estudio\n",
|
||||
"const { data: conversations } = await supabase.from(\"conversation_planes_estudio\").select(\"*\").eq(\"plan_estudio_id\", input.id);\n",
|
||||
"conversations"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "83e060ae",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Add a message to the conversation\n",
|
||||
"const { data: conversationToUpdate } = await supabase.from(\n",
|
||||
" \"conversation_planes_estudio\",\n",
|
||||
").eq(\"id\", conversationPlane.id)\n",
|
||||
" .select(\"conversation_id\").single();\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user