{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "jq2hIB2JJxuR" }, "outputs": [], "source": [ "import requests" ] }, { "cell_type": "code", "source": [ "import micropip\n", "await micropip.install(\"pyodide-http\")" ], "metadata": { "id": "3rSm8dJLJ0Os" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import pyodide_http\n", "pyodide_http.patch_all()\n" ], "metadata": { "id": "4pO0XQjlJ017" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import json\n", "from pyodide.http import pyfetch\n", "\n", "api_key = \"sk-...\" # Replace with your API key\n", "\n", "headers = {\n", " \"Authorization\": f\"Bearer {api_key}\",\n", " \"Content-Type\": \"application/json\"\n", "}\n", "\n", "data = {\n", " \"model\": \"gpt-3.5-turbo\",\n", " \"messages\": [{\"role\": \"user\", \"content\": \"What's the capital of France?\"}],\n", " \"temperature\": 0.2\n", "}\n", "\n", "response = await pyfetch(\n", " url=\"https://api.openai.com/v1/chat/completions\",\n", " method=\"POST\",\n", " headers=headers,\n", " body=json.dumps(data)\n", ")\n", "\n", "result = await response.json()\n", "print(result['choices'][0]['message']['content'])\n" ], "metadata": { "id": "ti3PRvr4J08y" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "sOmj7tknJ1Au" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "iqoo7htnJ1E7" }, "execution_count": null, "outputs": [] } ] }