File size: 9,322 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "fNkMBurtxawJ"
      },
      "source": [
        "# LiteLLM Bedrock Usage\n",
        "Important Note: For Bedrock Requests you need to ensure you have `pip install boto3>=1.28.57`, boto3 supports bedrock from `boto3>=1.28.57` and higher "
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "htAufI28xeSy"
      },
      "source": [
        "## Pre-Requisites"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "jT5GbPjAuDTp"
      },
      "outputs": [],
      "source": [
        "!pip install litellm\n",
        "!pip install boto3>=1.28.57 # this version onwards has bedrock support"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "H4Vu4er2xnfI"
      },
      "source": [
        "## Set Bedrock/AWS Credentials"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "id": "CtTrBthWxp-t"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "os.environ[\"AWS_ACCESS_KEY_ID\"] = \"\" # Access key\n",
        "os.environ[\"AWS_SECRET_ACCESS_KEY\"] = \"\" # Secret access key\n",
        "os.environ[\"AWS_REGION_NAME\"] = \"\""
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "ycRK9NUdx1EI"
      },
      "source": [
        "## Anthropic Requests"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tgkuoHa5uLOy",
        "outputId": "27a78e86-c6a7-4bcc-8559-0813cb978426"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Claude instant 1, response\n",
            "{\n",
            "  \"object\": \"chat.completion\",\n",
            "  \"choices\": [\n",
            "    {\n",
            "      \"finish_reason\": \"stop\",\n",
            "      \"index\": 0,\n",
            "      \"message\": {\n",
            "        \"content\": \" I'm doing well, thanks for asking!\",\n",
            "        \"role\": \"assistant\",\n",
            "        \"logprobs\": null\n",
            "      }\n",
            "    }\n",
            "  ],\n",
            "  \"id\": \"chatcmpl-4f2e64a1-56d2-43f2-90d3-60ffd6f5086d\",\n",
            "  \"created\": 1696256761.3265705,\n",
            "  \"model\": \"anthropic.claude-instant-v1\",\n",
            "  \"usage\": {\n",
            "    \"prompt_tokens\": 11,\n",
            "    \"completion_tokens\": 9,\n",
            "    \"total_tokens\": 20\n",
            "  },\n",
            "  \"finish_reason\": \"stop_sequence\"\n",
            "}\n",
            "Claude v2, response\n",
            "{\n",
            "  \"object\": \"chat.completion\",\n",
            "  \"choices\": [\n",
            "    {\n",
            "      \"finish_reason\": \"stop\",\n",
            "      \"index\": 0,\n",
            "      \"message\": {\n",
            "        \"content\": \" I'm doing well, thanks for asking!\",\n",
            "        \"role\": \"assistant\",\n",
            "        \"logprobs\": null\n",
            "      }\n",
            "    }\n",
            "  ],\n",
            "  \"id\": \"chatcmpl-34f59b33-f94e-40c2-8bdb-f4af0813405e\",\n",
            "  \"created\": 1696256762.2137017,\n",
            "  \"model\": \"anthropic.claude-v2\",\n",
            "  \"usage\": {\n",
            "    \"prompt_tokens\": 11,\n",
            "    \"completion_tokens\": 9,\n",
            "    \"total_tokens\": 20\n",
            "  },\n",
            "  \"finish_reason\": \"stop_sequence\"\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "from litellm import completion\n",
        "\n",
        "response = completion(\n",
        "            model=\"bedrock/anthropic.claude-instant-v1\",\n",
        "            messages=[{ \"content\": \"Hello, how are you?\",\"role\": \"user\"}]\n",
        ")\n",
        "print(\"Claude instant 1, response\")\n",
        "print(response)\n",
        "\n",
        "\n",
        "response = completion(\n",
        "            model=\"bedrock/anthropic.claude-v2\",\n",
        "            messages=[{ \"content\": \"Hello, how are you?\",\"role\": \"user\"}]\n",
        ")\n",
        "print(\"Claude v2, response\")\n",
        "print(response)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "HnM-HtM3yFMT"
      },
      "source": [
        "## Anthropic Requests - With Streaming"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_JZvg2yovRsU"
      },
      "outputs": [],
      "source": [
        "from litellm import completion\n",
        "\n",
        "response = completion(\n",
        "            model=\"bedrock/anthropic.claude-instant-v1\",\n",
        "            messages=[{ \"content\": \"Hello, how are you?\",\"role\": \"user\"}],\n",
        "            stream=True,\n",
        ")\n",
        "print(\"Claude instant 1, response\")\n",
        "for chunk in response:\n",
        "  print(chunk)\n",
        "\n",
        "\n",
        "response = completion(\n",
        "            model=\"bedrock/anthropic.claude-v2\",\n",
        "            messages=[{ \"content\": \"Hello, how are you?\",\"role\": \"user\"}],\n",
        "            stream=True\n",
        ")\n",
        "print(\"Claude v2, response\")\n",
        "print(response)\n",
        "for chunk in response:\n",
        "  print(chunk)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "zj1U1mh9zEhP"
      },
      "source": [
        "## A121 Requests"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6wK6MZLovU7r",
        "outputId": "4cf80c04-f15d-4066-b4c7-113b551538de"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "J2 ultra response\n",
            "{\n",
            "  \"object\": \"chat.completion\",\n",
            "  \"choices\": [\n",
            "    {\n",
            "      \"finish_reason\": \"stop\",\n",
            "      \"index\": 0,\n",
            "      \"message\": {\n",
            "        \"content\": \"\\nHi, I'm doing well, thanks for asking! How about you?\",\n",
            "        \"role\": \"assistant\",\n",
            "        \"logprobs\": null\n",
            "      }\n",
            "    }\n",
            "  ],\n",
            "  \"id\": \"chatcmpl-f2de678f-0e70-4e36-a01f-8b184c2e4d50\",\n",
            "  \"created\": 1696257116.044311,\n",
            "  \"model\": \"ai21.j2-ultra\",\n",
            "  \"usage\": {\n",
            "    \"prompt_tokens\": 6,\n",
            "    \"completion_tokens\": 16,\n",
            "    \"total_tokens\": 22\n",
            "  }\n",
            "}\n",
            "J2 mid response\n",
            "{\n",
            "  \"object\": \"chat.completion\",\n",
            "  \"choices\": [\n",
            "    {\n",
            "      \"finish_reason\": \"stop\",\n",
            "      \"index\": 0,\n",
            "      \"message\": {\n",
            "        \"content\": \"\\nGood. And you?\",\n",
            "        \"role\": \"assistant\",\n",
            "        \"logprobs\": null\n",
            "      }\n",
            "    }\n",
            "  ],\n",
            "  \"id\": \"chatcmpl-420d6bf9-36d8-484b-93b4-4c9e00f7ce2e\",\n",
            "  \"created\": 1696257116.5756805,\n",
            "  \"model\": \"ai21.j2-mid\",\n",
            "  \"usage\": {\n",
            "    \"prompt_tokens\": 6,\n",
            "    \"completion_tokens\": 6,\n",
            "    \"total_tokens\": 12\n",
            "  }\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "response = completion(\n",
        "            model=\"bedrock/ai21.j2-ultra\",\n",
        "            messages=[{ \"content\": \"Hello, how are you?\",\"role\": \"user\"}],\n",
        ")\n",
        "print(\"J2 ultra response\")\n",
        "print(response)\n",
        "\n",
        "response = completion(\n",
        "            model=\"bedrock/ai21.j2-mid\",\n",
        "            messages=[{ \"content\": \"Hello, how are you?\",\"role\": \"user\"}],\n",
        ")\n",
        "print(\"J2 mid response\")\n",
        "print(response)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Y5gGZIwzzSON"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}