Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e20df68

Browse files
committedSep 6, 2023
add LiteLLM_batch_completions cookbook
1 parent 0ace48d commit e20df68

File tree

2 files changed

+169
-6
lines changed

2 files changed

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

‎docs/my-website/docs/completion/batching.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Batching Completion Calls - batch_completion
1+
# Batching Completion() Calls
22

3-
Batch Completion allows you to pass a batch of completion() requests to process multiple `messages` in a single API call.
3+
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.
44

55
## Example Code
66
```python
@@ -28,7 +28,4 @@ responses = batch_completion(
2828
]
2929
]
3030
)
31-
```
32-
33-
34-
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.
31+
```

0 commit comments

Comments
 (0)
Please sign in to comment.