File size: 3,982 Bytes
447ebeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "MbLbs1tbISk-"
   },
   "source": [
    "# LiteLLM Batch Completions Example\n",
    "\n",
    "* This tutorial walks through using `batch_completion`\n",
    "* Docs: https://docs.litellm.ai/docs/completion/batching"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "Ty6-ko_aDlPF"
   },
   "outputs": [],
   "source": [
    "!pip install litellm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "KGhNJRUCIh1j"
   },
   "source": [
    "## Import Batch Completion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "id": "LOtI43snDrSK"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from litellm import batch_completion\n",
    "\n",
    "# set your API_KEY\n",
    "os.environ['ANTHROPIC_API_KEY'] = \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Xhv92NBaIpaw"
   },
   "source": [
    "## Calling `litellm.batch_completion`\n",
    "\n",
    "In the batch_completion method, you provide a list of messages where each sub-list of messages is passed to litellm.completion(), allowing you to process multiple prompts efficiently in a single API call."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "yY7GIRLsDywu",
    "outputId": "009ea67f-95d5-462b-947f-b0d21e60c5bb"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<ModelResponse at 0x7a164eed4450> JSON: {\n",
       "   \"choices\": [\n",
       "     {\n",
       "       \"finish_reason\": \"stop\",\n",
       "       \"index\": 0,\n",
       "       \"message\": {\n",
       "         \"content\": \" Good morning!\",\n",
       "         \"role\": \"assistant\",\n",
       "         \"logprobs\": null\n",
       "       }\n",
       "     }\n",
       "   ],\n",
       "   \"created\": 1694030351.309254,\n",
       "   \"model\": \"claude-2\",\n",
       "   \"usage\": {\n",
       "     \"prompt_tokens\": 11,\n",
       "     \"completion_tokens\": 3,\n",
       "     \"total_tokens\": 14\n",
       "   }\n",
       " },\n",
       " <ModelResponse at 0x7a164eed5800> JSON: {\n",
       "   \"choices\": [\n",
       "     {\n",
       "       \"finish_reason\": \"stop\",\n",
       "       \"index\": 0,\n",
       "       \"message\": {\n",
       "         \"content\": \" I'm an AI assistant created by Anthropic. I don't actually have a concept of the current time.\",\n",
       "         \"role\": \"assistant\",\n",
       "         \"logprobs\": null\n",
       "       }\n",
       "     }\n",
       "   ],\n",
       "   \"created\": 1694030352.1215081,\n",
       "   \"model\": \"claude-2\",\n",
       "   \"usage\": {\n",
       "     \"prompt_tokens\": 13,\n",
       "     \"completion_tokens\": 22,\n",
       "     \"total_tokens\": 35\n",
       "   }\n",
       " }]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "os.environ['ANTHROPIC_API_KEY'] = \"\"\n",
    "\n",
    "\n",
    "responses = batch_completion(\n",
    "    model=\"claude-2\",\n",
    "    messages = [\n",
    "        [\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": \"good morning? \"\n",
    "            }\n",
    "        ],\n",
    "        [\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": \"what's the time? \"\n",
    "            }\n",
    "        ]\n",
    "    ]\n",
    ")\n",
    "responses"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}