From fe159b576dee483be548dcd756fa39a0f3b6302e Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Sat, 12 Aug 2023 15:11:33 -0700 Subject: [PATCH] with streaming nb --- cookbook/liteLLM_Ollama.ipynb | 162 +++++++++++++++++++++------------- 1 file changed, 103 insertions(+), 59 deletions(-) diff --git a/cookbook/liteLLM_Ollama.ipynb b/cookbook/liteLLM_Ollama.ipynb index 3de54d8ef..c826490c4 100644 --- a/cookbook/liteLLM_Ollama.ipynb +++ b/cookbook/liteLLM_Ollama.ipynb @@ -2,53 +2,16 @@ "cells": [ { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Defaulting to user installation because normal site-packages is not writeable\n", - "Collecting litellm==0.1.384\n", - " Downloading litellm-0.1.384-py3-none-any.whl (43 kB)\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m43.1/43.1 kB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[?25hRequirement already satisfied: openai<0.28.0,>=0.27.8 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from litellm==0.1.384) (0.27.8)\n", - "Requirement already satisfied: python-dotenv<2.0.0,>=1.0.0 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from litellm==0.1.384) (1.0.0)\n", - "Requirement already satisfied: tiktoken<0.5.0,>=0.4.0 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from litellm==0.1.384) (0.4.0)\n", - "Requirement already satisfied: requests>=2.20 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from openai<0.28.0,>=0.27.8->litellm==0.1.384) (2.28.2)\n", - "Requirement already satisfied: tqdm in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from openai<0.28.0,>=0.27.8->litellm==0.1.384) (4.65.0)\n", - "Requirement already satisfied: aiohttp in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from openai<0.28.0,>=0.27.8->litellm==0.1.384) (3.8.4)\n", - "Requirement already satisfied: regex>=2022.1.18 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from tiktoken<0.5.0,>=0.4.0->litellm==0.1.384) (2023.6.3)\n", - "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from requests>=2.20->openai<0.28.0,>=0.27.8->litellm==0.1.384) (3.1.0)\n", - "Requirement already satisfied: idna<4,>=2.5 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from requests>=2.20->openai<0.28.0,>=0.27.8->litellm==0.1.384) (3.4)\n", - "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from requests>=2.20->openai<0.28.0,>=0.27.8->litellm==0.1.384) (1.26.6)\n", - "Requirement already satisfied: certifi>=2017.4.17 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from requests>=2.20->openai<0.28.0,>=0.27.8->litellm==0.1.384) (2023.5.7)\n", - "Requirement already satisfied: attrs>=17.3.0 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai<0.28.0,>=0.27.8->litellm==0.1.384) (23.1.0)\n", - "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai<0.28.0,>=0.27.8->litellm==0.1.384) (6.0.4)\n", - "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai<0.28.0,>=0.27.8->litellm==0.1.384) (4.0.2)\n", - "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai<0.28.0,>=0.27.8->litellm==0.1.384) (1.9.2)\n", - "Requirement already satisfied: frozenlist>=1.1.1 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai<0.28.0,>=0.27.8->litellm==0.1.384) (1.3.3)\n", - "Requirement already satisfied: aiosignal>=1.1.2 in /Users/ishaanjaffer/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai<0.28.0,>=0.27.8->litellm==0.1.384) (1.3.1)\n", - "Installing collected packages: litellm\n", - " Attempting uninstall: litellm\n", - " Found existing installation: litellm 0.1.379\n", - " Uninstalling litellm-0.1.379:\n", - " Successfully uninstalled litellm-0.1.379\n", - "Successfully installed litellm-0.1.384\n", - "\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.1.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49m/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip\u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ - "!pip install litellm==0.1.384" + "!pip install litellm==0.1.385" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -56,42 +19,42 @@ "import asyncio" ] }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Setup Messages" + ] + }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "\n", "user_message = \"respond in 20 words. who are you?\"\n", "messages = [{ \"content\": user_message,\"role\": \"user\"}]" ] }, { - "cell_type": "code", - "execution_count": 7, + "attachments": {}, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "\n", - "async def get_response(generator):\n", - " response = \"\"\n", - " async for elem in generator:\n", - " print(elem)\n", - " response += elem[\"content\"]\n", - " return response" + "### Call Ollama - llama2 with chatGPT Input/Output using litellm.completion() " ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\n" + "\n" ] } ], @@ -101,12 +64,93 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "attachments": {}, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "string_response = asyncio.run(get_response(response))" + "## Iterate through the generator - Streaming" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " I\n", + "{'role': 'assistant', 'content': ' I'}\n", + " am\n", + "{'role': 'assistant', 'content': ' am'}\n", + " L\n", + "{'role': 'assistant', 'content': ' L'}\n", + "La\n", + "{'role': 'assistant', 'content': 'La'}\n", + "MA\n", + "{'role': 'assistant', 'content': 'MA'}\n", + ",\n", + "{'role': 'assistant', 'content': ','}\n", + " an\n", + "{'role': 'assistant', 'content': ' an'}\n", + " A\n", + "{'role': 'assistant', 'content': ' A'}\n", + "I\n", + "{'role': 'assistant', 'content': 'I'}\n", + " assistant\n", + "{'role': 'assistant', 'content': ' assistant'}\n", + " developed\n", + "{'role': 'assistant', 'content': ' developed'}\n", + " by\n", + "{'role': 'assistant', 'content': ' by'}\n", + " Meta\n", + "{'role': 'assistant', 'content': ' Meta'}\n", + " A\n", + "{'role': 'assistant', 'content': ' A'}\n", + "I\n", + "{'role': 'assistant', 'content': 'I'}\n", + " that\n", + "{'role': 'assistant', 'content': ' that'}\n", + " can\n", + "{'role': 'assistant', 'content': ' can'}\n", + " understand\n", + "{'role': 'assistant', 'content': ' understand'}\n", + " and\n", + "{'role': 'assistant', 'content': ' and'}\n", + " respond\n", + "{'role': 'assistant', 'content': ' respond'}\n", + " to\n", + "{'role': 'assistant', 'content': ' to'}\n", + " human\n", + "{'role': 'assistant', 'content': ' human'}\n", + " input\n", + "{'role': 'assistant', 'content': ' input'}\n", + " in\n", + "{'role': 'assistant', 'content': ' in'}\n", + " a\n", + "{'role': 'assistant', 'content': ' a'}\n", + " convers\n", + "{'role': 'assistant', 'content': ' convers'}\n", + "ational\n", + "{'role': 'assistant', 'content': 'ational'}\n", + " manner\n", + "{'role': 'assistant', 'content': ' manner'}\n", + ".\n", + "{'role': 'assistant', 'content': '.'}\n" + ] + } + ], + "source": [ + "\n", + "async def get_response(generator):\n", + " response = \"\"\n", + " async for elem in generator:\n", + " print(elem)\n", + " response += elem[\"content\"]\n", + " return response\n", + "\n", + "string_response = await get_response(response)" ] } ],