|
1 | 1 | {
|
2 | 2 | "cells": [
|
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<a target=\"_blank\" href=\"https://colab.research.google.com/github/cohere-ai/notebooks/blob/main/notebooks/llmu/Classify_Endpoint.ipynb\">\n", |
| 8 | + " <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n", |
| 9 | + "</a>" |
| 10 | + ] |
| 11 | + }, |
3 | 12 | {
|
4 | 13 | "attachments": {},
|
5 | 14 | "cell_type": "markdown",
|
|
9 | 18 | "source": [
|
10 | 19 | "# The Classify Endpoint\n",
|
11 | 20 | "\n",
|
12 |
| - "In this lab, we'll learn how to use Cohere's Classify cohere endpoint. This codelab accompanies the [Classify endpoint lesson](https://docs.cohere.com/docs/classify-endpoint/) of LLM University.\n", |
13 |
| - "\n", |
14 |
| - "This is part of a bigger [colab](https://colab.research.google.com/github/cohere-ai/notebooks/blob/main/notebooks/Hello_World_Meet_Language_AI.ipynb#) containing more endpoints, please feel free to check it out!" |
| 21 | + "In this lab, we'll learn how to use Cohere's Classify cohere endpoint. This codelab accompanies the [Classify endpoint lesson](https://docs.cohere.com/docs/classify-endpoint/) of LLM University." |
15 | 22 | ]
|
16 | 23 | },
|
17 | 24 | {
|
|
43 | 50 | "outputs": [],
|
44 | 51 | "source": [
|
45 | 52 | "# Install the libraries\n",
|
46 |
| - "# TODO: upgrade to \"cohere>5\"", |
47 |
| -"! pip install \"cohere<5\" altair umap-learn > /dev/null" |
| 53 | + "# TODO: upgrade to \"cohere>5\"\n", |
| 54 | + "! pip install cohere altair umap-learn > /dev/null" |
48 | 55 | ]
|
49 | 56 | },
|
50 | 57 | {
|
51 | 58 | "cell_type": "code",
|
52 |
| - "execution_count": 14, |
53 |
| - "metadata": { |
54 |
| - "id": "YYi9JxxFRWX7" |
55 |
| - }, |
| 59 | + "execution_count": 6, |
| 60 | + "metadata": {}, |
56 | 61 | "outputs": [],
|
57 | 62 | "source": [
|
58 | 63 | "# Import the libraries\n",
|
|
63 | 68 | "import textwrap as tr\n",
|
64 | 69 | "\n",
|
65 | 70 | "# Setup the Cohere client\n",
|
66 |
| - "api_key = 'api_key' # Paste your API key here. Remember to not share it publicly\n", |
67 |
| - "co = cohere.Client(api_key)" |
| 71 | + "co = cohere.Client(\"COHERE_API_KEY\") # Get your API key here: https://dashboard.cohere.com/api-keys" |
68 | 72 | ]
|
69 | 73 | },
|
70 | 74 | {
|
|
99 | 103 | },
|
100 | 104 | {
|
101 | 105 | "cell_type": "code",
|
102 |
| - "execution_count": 9, |
| 106 | + "execution_count": 8, |
103 | 107 | "metadata": {
|
104 | 108 | "id": "-lSi_UmQEfy_"
|
105 | 109 | },
|
106 | 110 | "outputs": [],
|
107 | 111 | "source": [
|
108 | 112 | "# Create the training examples for the classifier\n",
|
109 |
| - "from cohere.responses.classify import Example\n", |
110 | 113 | "\n",
|
111 |
| - "examples = [Example(\"I’m so proud of you\", \"positive\"), \n", |
112 |
| - " Example(\"What a great time to be alive\", \"positive\"), \n", |
113 |
| - " Example(\"That’s awesome work\", \"positive\"), \n", |
114 |
| - " Example(\"The service was amazing\", \"positive\"), \n", |
115 |
| - " Example(\"I love my family\", \"positive\"), \n", |
116 |
| - " Example(\"They don't care about me\", \"negative\"), \n", |
117 |
| - " Example(\"I hate this place\", \"negative\"), \n", |
118 |
| - " Example(\"The most ridiculous thing I've ever heard\", \"negative\"), \n", |
119 |
| - " Example(\"I am really frustrated\", \"negative\"), \n", |
120 |
| - " Example(\"This is so unfair\", \"negative\"),\n", |
121 |
| - " Example(\"This made me think\", \"neutral\"), \n", |
122 |
| - " Example(\"The good old days\", \"neutral\"), \n", |
123 |
| - " Example(\"What's the difference\", \"neutral\"), \n", |
124 |
| - " Example(\"You can't ignore this\", \"neutral\"), \n", |
125 |
| - " Example(\"That's how I see it\", \"neutral\") \n", |
126 |
| - " ]" |
| 114 | + "from cohere import ClassifyExample\n", |
| 115 | + "\n", |
| 116 | + "examples = [ClassifyExample(text=\"I’m so proud of you\", label=\"positive\"), \n", |
| 117 | + " ClassifyExample(text=\"What a great time to be alive\", label=\"positive\"), \n", |
| 118 | + " ClassifyExample(text=\"That’s awesome work\", label=\"positive\"), \n", |
| 119 | + " ClassifyExample(text=\"The service was amazing\", label=\"positive\"), \n", |
| 120 | + " ClassifyExample(text=\"I love my family\", label=\"positive\"), \n", |
| 121 | + " ClassifyExample(text=\"They don't care about me\", label=\"negative\"), \n", |
| 122 | + " ClassifyExample(text=\"I hate this place\", label=\"negative\"), \n", |
| 123 | + " ClassifyExample(text=\"The most ridiculous thing I've ever heard\", label=\"negative\"), \n", |
| 124 | + " ClassifyExample(text=\"I am really frustrated\", label=\"negative\"), \n", |
| 125 | + " ClassifyExample(text=\"This is so unfair\", label=\"negative\"),\n", |
| 126 | + " ClassifyExample(text=\"This made me think\", label=\"neutral\"), \n", |
| 127 | + " ClassifyExample(text=\"The good old days\", label=\"neutral\"), \n", |
| 128 | + " ClassifyExample(text=\"What's the difference\", label=\"neutral\"), \n", |
| 129 | + " ClassifyExample(text=\"You can't ignore this\", label=\"neutral\"), \n", |
| 130 | + " ClassifyExample(text=\"That's how I see it\", label=\"neutral\")]" |
127 | 131 | ]
|
128 | 132 | },
|
129 | 133 | {
|
130 | 134 | "cell_type": "code",
|
131 |
| - "execution_count": 10, |
| 135 | + "execution_count": 9, |
132 | 136 | "metadata": {
|
133 | 137 | "id": "QpC1Z4xEEWs0"
|
134 | 138 | },
|
|
146 | 150 | " \"This is the worst thing\",\n",
|
147 | 151 | " \"I cannot stand this any longer\",\n",
|
148 | 152 | " \"This is really annoying\",\n",
|
149 |
| - " \"I am just plain fed up\"\n", |
150 |
| - " ]" |
| 153 | + " \"I am just plain fed up\"]" |
151 | 154 | ]
|
152 | 155 | },
|
153 | 156 | {
|
154 | 157 | "cell_type": "code",
|
155 |
| - "execution_count": 11, |
| 158 | + "execution_count": 10, |
156 | 159 | "metadata": {
|
157 | 160 | "id": "etTr200IRszm"
|
158 | 161 | },
|
|
181 | 184 | },
|
182 | 185 | {
|
183 | 186 | "cell_type": "code",
|
184 |
| - "execution_count": 12, |
| 187 | + "execution_count": 11, |
185 | 188 | "metadata": {
|
186 | 189 | "colab": {
|
187 | 190 | "base_uri": "https://localhost:8080/"
|
|
196 | 199 | "text": [
|
197 | 200 | "Input: Hello, world! What a beautiful day\n",
|
198 | 201 | "Prediction: positive\n",
|
199 |
| - "Confidence: 0.83\n", |
| 202 | + "Confidence: 0.40\n", |
200 | 203 | "----------\n",
|
201 | 204 | "Input: It was a great time with great people\n",
|
202 | 205 | "Prediction: positive\n",
|
203 |
| - "Confidence: 0.99\n", |
| 206 | + "Confidence: 0.49\n", |
204 | 207 | "----------\n",
|
205 | 208 | "Input: Great place to work\n",
|
206 | 209 | "Prediction: positive\n",
|
207 |
| - "Confidence: 0.91\n", |
| 210 | + "Confidence: 0.50\n", |
208 | 211 | "----------\n",
|
209 | 212 | "Input: That was a wonderful evening\n",
|
210 | 213 | "Prediction: positive\n",
|
211 |
| - "Confidence: 0.96\n", |
| 214 | + "Confidence: 0.48\n", |
212 | 215 | "----------\n",
|
213 | 216 | "Input: Maybe this is why\n",
|
214 | 217 | "Prediction: neutral\n",
|
215 |
| - "Confidence: 0.70\n", |
| 218 | + "Confidence: 0.45\n", |
216 | 219 | "----------\n",
|
217 | 220 | "Input: Let's start again\n",
|
218 | 221 | "Prediction: neutral\n",
|
219 |
| - "Confidence: 0.83\n", |
| 222 | + "Confidence: 0.42\n", |
220 | 223 | "----------\n",
|
221 | 224 | "Input: That's how I see it\n",
|
222 | 225 | "Prediction: neutral\n",
|
223 |
| - "Confidence: 1.00\n", |
| 226 | + "Confidence: 0.53\n", |
224 | 227 | "----------\n",
|
225 | 228 | "Input: These are all facts\n",
|
226 | 229 | "Prediction: neutral\n",
|
227 |
| - "Confidence: 0.78\n", |
| 230 | + "Confidence: 0.41\n", |
228 | 231 | "----------\n",
|
229 | 232 | "Input: This is the worst thing\n",
|
230 | 233 | "Prediction: negative\n",
|
231 |
| - "Confidence: 0.93\n", |
| 234 | + "Confidence: 0.52\n", |
232 | 235 | "----------\n",
|
233 | 236 | "Input: I cannot stand this any longer\n",
|
234 | 237 | "Prediction: negative\n",
|
235 |
| - "Confidence: 0.93\n", |
| 238 | + "Confidence: 0.52\n", |
236 | 239 | "----------\n",
|
237 | 240 | "Input: This is really annoying\n",
|
238 | 241 | "Prediction: negative\n",
|
239 |
| - "Confidence: 0.99\n", |
| 242 | + "Confidence: 0.56\n", |
240 | 243 | "----------\n",
|
241 | 244 | "Input: I am just plain fed up\n",
|
242 | 245 | "Prediction: negative\n",
|
243 |
| - "Confidence: 1.00\n", |
| 246 | + "Confidence: 0.57\n", |
244 | 247 | "----------\n"
|
245 | 248 | ]
|
246 | 249 | }
|
|
282 | 285 | "name": "python3"
|
283 | 286 | },
|
284 | 287 | "language_info": {
|
| 288 | + "codemirror_mode": { |
| 289 | + "name": "ipython", |
| 290 | + "version": 3 |
| 291 | + }, |
| 292 | + "file_extension": ".py", |
| 293 | + "mimetype": "text/x-python", |
285 | 294 | "name": "python",
|
286 |
| - "version": "3.10.0" |
| 295 | + "nbconvert_exporter": "python", |
| 296 | + "pygments_lexer": "ipython3", |
| 297 | + "version": "3.11.4" |
287 | 298 | },
|
288 | 299 | "vscode": {
|
289 | 300 | "interpreter": {
|
|
0 commit comments