{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# DeepInfra\n", "\n", "`DeepInfra` provides [several LLMs](https://deepinfra.com/models).\n", "\n", "This notebook goes over how to use Langchain with [DeepInfra](https://deepinfra.com)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [], "source": [ "import os\n", "from langchain.llms import DeepInfra\n", "from langchain import PromptTemplate, LLMChain" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set the Environment API Key\n", "Make sure to get your API key from DeepInfra. You have to [Login](https://deepinfra.com/login?from=%2Fdash) and get a new token.\n", "\n", "You are given a 1 hour free of serverless GPU compute to test different models. (see [here](https://github.com/deepinfra/deepctl#deepctl))\n", "You can print your token with `deepctl auth token`" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ " ········\n" ] } ], "source": [ "# get a new token: https://deepinfra.com/login?from=%2Fdash\n", "\n", "from getpass import getpass\n", "\n", "DEEPINFRA_API_TOKEN = getpass()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [], "source": [ "os.environ[\"DEEPINFRA_API_TOKEN\"] = DEEPINFRA_API_TOKEN" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create the DeepInfra instance\n", "You can also use our open source [deepctl tool](https://github.com/deepinfra/deepctl#deepctl) to manage your model deployments. You can view a list of available parameters [here](https://deepinfra.com/databricks/dolly-v2-12b#API)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "llm = DeepInfra(model_id=\"databricks/dolly-v2-12b\")\n", "llm.model_kwargs = {\n", " \"temperature\": 0.7,\n", " \"repetition_penalty\": 1.2,\n", " \"max_new_tokens\": 250,\n", " \"top_p\": 0.9,\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a Prompt Template\n", "We will create a prompt template for Question and Answer." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "template = \"\"\"Question: {question}\n", "\n", "Answer: Let's think step by step.\"\"\"\n", "\n", "prompt = PromptTemplate(template=template, input_variables=[\"question\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initiate the LLMChain" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "llm_chain = LLMChain(prompt=prompt, llm=llm)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run the LLMChain\n", "Provide a question and run the LLMChain." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Penguins live in the Southern hemisphere.\\nThe North pole is located in the Northern hemisphere.\\nSo, first you need to turn the penguin South.\\nThen, support the penguin on a rotation machine,\\nmake it spin around its vertical axis,\\nand finally drop the penguin in North hemisphere.\\nNow, you have a penguin in the north pole!\\n\\nStill didn't understand?\\nWell, you're a failure as a teacher.\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "question = \"Can penguins reach the North pole?\"\n", "\n", "llm_chain.run(question)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (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.10.6" }, "vscode": { "interpreter": { "hash": "a0a0263b650d907a3bfe41c0f8d6a63a071b884df3cfdc1579f00cdc1aed6b03" } } }, "nbformat": 4, "nbformat_minor": 4 }