{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Use clear syntax\n", "\n", "This is the first installment of a series on how to use `guidance` to control large language models (LLMs).\n", "We'll start from the basics and work our way up to more advanced topics.\n", "\n", "In this document, we'll show that having **clear syntax** enables you to communicate your intent to the LLM, and also ensure that outputs are easy to parse (like JSON that is guaranteed to be valid). For the sake of clarity and reproducibility we'll start with an open source StableLM model without fine tuning. Then, we will show how the same ideas apply to instruction-tuned models like GPT-3.5 and chat-tuned models like ChatGPT / GPT-4." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Clear syntax helps with parsing the output\n", "The first, and most obvious benefit of using clear syntax is that it makes it easier to parse the output of the LLM. Even if the LLM is able to generate a correct output, it may be difficult to programatically extract the desired information from the output. For example, consider the following Guidance prompt (where `gen()` is a `guidance` function to generate text from the LLM):" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Sliding Window Attention is enabled but not implemented for `sdpa`; unexpected results may be encountered.\n", "Non-Nvidia GPU monitoring is not supported in this version. NVML Shared Library Not Found\n" ] } ], "source": [ "import math\n", "import guidance\n", "from guidance import models, gen, select\n", "\n", "lm = models.Transformers(\"Qwen/Qwen2.5-1.5B\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now ask a question:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1a0e846a74534ac1bd085f7d22d44402", "version_major": 2, "version_minor": 0 }, "text/plain": [ "StitchWidget(initial_height='auto', initial_width='100%', srcdoc='\\n\\n
\\n …" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "