with streaming nb

This commit is contained in:
ishaan-jaff 2023-08-12 15:11:33 -07:00
parent 406290b9e4
commit fe159b576d

View file

@ -2,53 +2,16 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "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"
]
}
],
"source": [ "source": [
"!pip install litellm==0.1.384" "!pip install litellm==0.1.385"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 15,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -56,42 +19,42 @@
"import asyncio" "import asyncio"
] ]
}, },
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup Messages"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 16,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"\n",
"user_message = \"respond in 20 words. who are you?\"\n", "user_message = \"respond in 20 words. who are you?\"\n",
"messages = [{ \"content\": user_message,\"role\": \"user\"}]" "messages = [{ \"content\": user_message,\"role\": \"user\"}]"
] ]
}, },
{ {
"cell_type": "code", "attachments": {},
"execution_count": 7, "cell_type": "markdown",
"metadata": {}, "metadata": {},
"outputs": [],
"source": [ "source": [
"\n", "### Call Ollama - llama2 with chatGPT Input/Output using litellm.completion() "
"async def get_response(generator):\n",
" response = \"\"\n",
" async for elem in generator:\n",
" print(elem)\n",
" response += elem[\"content\"]\n",
" return response"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 17,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"<async_generator object get_ollama_response_stream at 0x103d51940>\n" "<async_generator object get_ollama_response_stream at 0x1069198b0>\n"
] ]
} }
], ],
@ -101,12 +64,93 @@
] ]
}, },
{ {
"cell_type": "code", "attachments": {},
"execution_count": null, "cell_type": "markdown",
"metadata": {}, "metadata": {},
"outputs": [],
"source": [ "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)"
] ]
} }
], ],