Ali-C137 commited on
Commit
409acf6
1 Parent(s): 82014e8

Upload baseline.ipynb

Browse files

Updated the dataset_to_purpose_target

Files changed (1) hide show
  1. baseline.ipynb +782 -693
baseline.ipynb CHANGED
@@ -1,697 +1,786 @@
1
  {
2
- "cells": [
3
- {
4
- "cell_type": "markdown",
5
- "id": "883e3354-1538-4f98-bf42-67552215bba3",
6
- "metadata": {},
7
- "source": [
8
- "# Setup"
9
- ]
10
- },
11
- {
12
- "cell_type": "code",
13
- "execution_count": 1,
14
- "id": "b45bd52f-03e9-419f-8110-1013ff45fb1b",
15
- "metadata": {
16
- "tags": []
17
- },
18
- "outputs": [],
19
- "source": [
20
- "from huggingface_hub import InferenceClient, login"
21
- ]
22
- },
23
- {
24
- "cell_type": "code",
25
- "execution_count": 2,
26
- "id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e",
27
- "metadata": {
28
- "tags": []
29
- },
30
- "outputs": [
31
- {
32
- "data": {
33
- "application/vnd.jupyter.widget-view+json": {
34
- "model_id": "515c96c357454fdc9a38ecc995ff1b3d",
35
- "version_major": 2,
36
- "version_minor": 0
37
- },
38
- "text/plain": [
39
- "VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
40
- ]
41
- },
42
- "metadata": {},
43
- "output_type": "display_data"
44
- }
45
- ],
46
- "source": [
47
- "login()"
48
- ]
49
- },
50
- {
51
- "cell_type": "markdown",
52
- "id": "4c254d6f-f3a1-49c1-815e-36e41e75ca25",
53
- "metadata": {},
54
- "source": [
55
- "<div class=\"alert alert-danger\" role=\"alert\" style=\"display: flex; align-items: center;\">\n",
56
- " <div style=\"text-align: center; padding-right: 10px;\">\n",
57
- " <i class=\"fa fa-exclamation-triangle fa-2x\"></i>\n",
58
- " </div>\n",
59
- " <div style=\"display: flex; align-items: center; margin-top: 4px;\"> <!-- Added margin-top to lower the text -->\n",
60
- " <strong>Warning: You will need to point to a model/deployment that is running.</strong>\n",
61
- " </div>\n",
62
- "</div>\n"
63
- ]
64
- },
65
- {
66
- "cell_type": "code",
67
- "execution_count": 3,
68
- "id": "84e6cb89-30d3-4ef5-8063-07783798e045",
69
- "metadata": {},
70
- "outputs": [],
71
- "source": [
72
- "MODEL = \"CohereForAI/c4ai-command-r-plus\"\n",
73
- "client = InferenceClient(MODEL)"
74
- ]
75
- },
76
- {
77
- "cell_type": "markdown",
78
- "id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb",
79
- "metadata": {},
80
- "source": [
81
- "# Translation\n",
82
- "Our goal is to explore translation between English and Arabic and how prompt engineering can impact it. There has been [some work](https://arxiv.org/pdf/2308.01391), but we didn't find as much as we were hoping, especially for open source models.\n",
83
- "\n",
84
- "We have created a dataset across 6 domains and want to compare each method by having human rankers. We also have human translations to ground these rankings."
85
- ]
86
- },
87
- {
88
- "cell_type": "markdown",
89
- "id": "c5bee77b-da1c-43b3-ab14-d15e871f7502",
90
- "metadata": {},
91
- "source": [
92
- "## Baseline\n",
93
- "\n",
94
- "For our baseline we will translate with a simple system prompt and instruction."
95
- ]
96
- },
97
- {
98
- "cell_type": "markdown",
99
- "id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f",
100
- "metadata": {},
101
- "source": [
102
- "### System Prompt\n",
103
- "This is a pretty basic system prompt. We give a role, and an assumed understanding. We also push for goals like \"highly motivated and detail-oriented\". \n",
104
- "\n",
105
- "> You are a skilled translator with extensive experience in English to Arabic translations. You possess a deep understanding of the linguistic, cultural, and contextual nuances essential for accurate and effective translation between these languages. Highly motivated and detail-oriented, you are committed to delivering translations that maintain the integrity and intent of the original text. Your role is crucial in ensuring clear and precise communication in our multilingual system."
106
- ]
107
- },
108
- {
109
- "cell_type": "code",
110
- "execution_count": 4,
111
- "id": "032c86d2-868e-4fa6-b03e-58f1c41434cc",
112
- "metadata": {
113
- "tags": []
114
- },
115
- "outputs": [],
116
- "source": [
117
- "system_prompt = \"\"\"You are a skilled translator with extensive experience in English and Arabic translations. You possess a deep understanding of the linguistic, cultural, and contextual nuances essential for accurate and effective translation between these languages. Highly motivated and detail-oriented, you are committed to delivering translations that maintain the integrity and intent of the original text. Your role is crucial in ensuring clear and precise communication in our multilingual system.\"\"\""
118
- ]
119
- },
120
- {
121
- "cell_type": "markdown",
122
- "id": "803ddeba-03de-4f13-95d1-5fb097058cf2",
123
- "metadata": {},
124
- "source": [
125
- "### Instruction\n",
126
- "> Translate this from arabic to english: {translation_input}.\n",
127
- ">\n",
128
- "> Translation: \n",
129
- "\n",
130
- "We will use a simple instruction to get a translation."
131
- ]
132
- },
133
- {
134
- "cell_type": "code",
135
- "execution_count": 5,
136
- "id": "b7f1722c-c484-4e22-a025-53f95943fc76",
137
- "metadata": {},
138
- "outputs": [],
139
- "source": [
140
- "def baseline_chat_completion(system_prompt, translation_input):\n",
141
- " \"\"\"\n",
142
- " Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
143
- " \"\"\"\n",
144
- " messages = [\n",
145
- " {\"role\": \"system\", \"content\": system_prompt},\n",
146
- " {\n",
147
- " \"role\": \"user\",\n",
148
- " \"content\": f\"Translate this from english to arabic: {translation_input}.\\nTranslation: \",\n",
149
- " },\n",
150
- " ]\n",
151
- " return client.chat_completion(messages, max_tokens=10_000)"
152
- ]
153
- },
154
- {
155
- "cell_type": "code",
156
- "execution_count": 6,
157
- "id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e",
158
- "metadata": {
159
- "tags": []
160
- },
161
- "outputs": [],
162
- "source": [
163
- "translation_input = \"Float like a butterfly sting like a bee – his hands can’t hit what his eyes can’t see.\"\n",
164
- "response = baseline_chat_completion(\n",
165
- " system_prompt,\n",
166
- " translation_input,\n",
167
- ")"
168
- ]
169
- },
170
- {
171
- "cell_type": "markdown",
172
- "id": "2bca574c-461d-4822-b0dd-b12a3b9846b3",
173
- "metadata": {},
174
- "source": [
175
- "### Token Cost\n",
176
- "Here we can see that the cost is quite cheap, only 92 tokens!"
177
- ]
178
- },
179
- {
180
- "cell_type": "code",
181
- "execution_count": 7,
182
- "id": "4e305b1e-56e0-44da-8c17-496cbcc35fad",
183
- "metadata": {
184
- "tags": []
185
- },
186
- "outputs": [
187
- {
188
- "data": {
189
- "text/plain": [
190
- "120"
191
- ]
192
- },
193
- "execution_count": 7,
194
- "metadata": {},
195
- "output_type": "execute_result"
196
- }
197
- ],
198
- "source": [
199
- "response.usage.prompt_tokens"
200
- ]
201
- },
202
- {
203
- "cell_type": "code",
204
- "execution_count": 8,
205
- "id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3",
206
- "metadata": {
207
- "tags": []
208
- },
209
- "outputs": [
210
- {
211
- "name": "stdout",
212
- "output_type": "stream",
213
- "text": [
214
- "يطير كالفراشة ويلسع كالنحلة - لا يمكن ليديه أن تصيبا ما لا تستطيع عيناه رؤيته.\n"
215
- ]
216
- }
217
- ],
218
- "source": [
219
- "print(response.choices[0].message.content)"
220
- ]
221
- },
222
- {
223
- "cell_type": "markdown",
224
- "id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715",
225
- "metadata": {},
226
- "source": [
227
- "## Manual Purpose Driven Translation\n",
228
- "\n",
229
- "[Optimizing Machine Translation through Prompt Engineering](https://arxiv.org/pdf/2308.01391) has done some good exploratory work in examining how prompt engineering can impact translation. They were working between Japanese and English and showed that translations influenced by prompts tailored to **specific purposes** and **target audiences** generally adhered more closely to the translation specifications, suggesting that such prompted translations could be more culturally and contextually appropriate than standard machine translations.\n",
230
- "\n",
231
- "### Prompt\n",
232
- "One of the approaches in the paper was to include the purpose and target audience specification. This was motivated by the author’s experience as a professional translator, leading to the conclusion that these two parameters are essential even in everyday translation work. You can find the prompt below adapted for Arabic to English:\n",
233
- "\n",
234
- "> Translate the following English [source text] into Arabic. Please fulfill the following conditions when translating. \n",
235
- "> Purpose of the translation: `<Manual description>` \n",
236
- "> Target audience: `<Manual description>` \n",
237
- "> [source text] `{translation_input}` \n",
238
- "> [translated text] \n",
239
- "\n",
240
- "You can see that we need to provide the Purpose and the Target Audience for each translation. This makes sense as we will be able to steer our model appropriately, but the drawback is that we need to do this for each subject. In the real world this likely won't scale and is rather tedious.\n",
241
- "\n",
242
- "Lets go ahead and create these for each of our datasets."
243
- ]
244
- },
245
- {
246
- "cell_type": "code",
247
- "execution_count": 9,
248
- "id": "4714f6a2-fd0b-48ee-80bc-860f40ee2baa",
249
- "metadata": {
250
- "tags": []
251
- },
252
- "outputs": [],
253
- "source": [
254
- "dataset_to_purpose_target = {\n",
255
- " \"ELRC-24ss\": {\n",
256
- " \"purpose\": \"Enhancing understanding and knowledge about COVID-19 and health-related topics.\",\n",
257
- " \"audience\": \"Individuals seeking reliable and comprehensible information about COVID-19 and related health topics.\",\n",
258
- " },\n",
259
- " \"GNOME-25ss\": {\"purpose\": \"\", \"audience\": \"\"},\n",
260
- " \"HPLT-25ss\": {\"purpose\": \"\", \"audience\": \"\"},\n",
261
- " \"OpenSubtitles-25ss\": {\"purpose\": \"\", \"audience\": \"\"},\n",
262
- " \"TED2020-25ss\": {\"purpose\": \"\", \"audience\": \"\"},\n",
263
- " \"UNPC-24ss\": {\"purpose\": \"\", \"audience\": \"\"},\n",
264
- "}"
265
- ]
266
- },
267
- {
268
- "cell_type": "code",
269
- "execution_count": 10,
270
- "id": "f865e9f8-7c63-4e72-b539-0d5916eda44f",
271
- "metadata": {},
272
- "outputs": [],
273
- "source": [
274
- "def purpose_driven_chat_completion(system_prompt, translation_input, dataset):\n",
275
- " \"\"\"\n",
276
- " Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
277
- " \"\"\"\n",
278
- "\n",
279
- " prompt = f\"\"\"Translate the following English [source text] into Arabic. Please fulfill the following conditions when translating.\n",
280
- "Purpose of the translation: {dataset_to_purpose_target[dataset]['purpose']}\n",
281
- "Target audience: {dataset_to_purpose_target[dataset]['audience']}\n",
282
- "[source text] `{translation_input}`\n",
283
- "[translated text] \"\"\"\n",
284
- "\n",
285
- " messages = [\n",
286
- " {\"role\": \"system\", \"content\": system_prompt},\n",
287
- " {\n",
288
- " \"role\": \"user\",\n",
289
- " \"content\": prompt,\n",
290
- " },\n",
291
- " ]\n",
292
- " return client.chat_completion(messages, max_tokens=10_000)"
293
- ]
294
- },
295
- {
296
- "cell_type": "code",
297
- "execution_count": 11,
298
- "id": "4115515d-cbcd-405a-b2e0-a805880a40c4",
299
- "metadata": {
300
- "tags": []
301
- },
302
- "outputs": [],
303
- "source": [
304
- "translation_input = \"We have observed that when groups of stakeholders work to define visions, this leads to debate over whether to emphasize ecosystem health or human well-being … Whether the priority is ecosystems or people greatly influences stakeholders' assessment of desirable ecological and social states.\"\n",
305
- "response = purpose_driven_chat_completion(system_prompt, translation_input, \"ELRC-24ss\")"
306
- ]
307
- },
308
- {
309
- "cell_type": "code",
310
- "execution_count": 12,
311
- "id": "6296d255-d11d-4df7-aa0e-1226ef3d963a",
312
- "metadata": {
313
- "tags": []
314
- },
315
- "outputs": [
316
- {
317
- "data": {
318
- "text/plain": [
319
- "208"
320
- ]
321
- },
322
- "execution_count": 12,
323
- "metadata": {},
324
- "output_type": "execute_result"
325
- }
326
- ],
327
- "source": [
328
- "response.usage.prompt_tokens"
329
- ]
330
- },
331
- {
332
- "cell_type": "code",
333
- "execution_count": 13,
334
- "id": "1f1c6dd0-11bf-4b88-9029-8bce1e7bcb1c",
335
- "metadata": {
336
- "tags": []
337
- },
338
- "outputs": [
339
- {
340
- "name": "stdout",
341
- "output_type": "stream",
342
- "text": [
343
- "\"لقد لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد ... الرؤى، فإن هذا يؤدي إلى نقاش حول ما إذا كان ينبغي التركيز على صحة النظام البيئي أو رفاهية الإنسان ... إن معرفة ما إذا كانت الأولوية للنظم البيئية أو للبشر تؤثر بشكل كبير على تقييم أصحاب المصلحة للحالات البيئية والاجتماعية المرغوبة.\"\n"
344
- ]
345
- }
346
- ],
347
- "source": [
348
- "print(response.choices[0].message.content)"
349
- ]
350
- },
351
- {
352
- "cell_type": "markdown",
353
- "id": "898a909e-88a2-4efb-99ed-64ceee317037",
354
- "metadata": {},
355
- "source": [
356
- "## Automatic Purpose Driven Structured Generation Translation\n",
357
- "\n",
358
- "Manual Purpose Driven Translation is a great step in the right direction, but its challenging to scale. Instead of having the user submit these purposes and target audiences, what if we use a model to do that? The easiest way to get this input in a format that is convenient is going to be by using [structured generation](https://huggingface.co/blog/evaluation-structured-outputs) to get a json. We can easily do this in InferenceClient easily just by using [tools](https://huggingface.co/docs/huggingface_hub/en/package_reference/inference_client#huggingface_hub.InferenceClient.chat_completion.tools)"
359
- ]
360
- },
361
- {
362
- "cell_type": "markdown",
363
- "id": "8aca3847-7f3d-43d3-8e80-0c7e2282073b",
364
- "metadata": {},
365
- "source": [
366
- "## Instruction"
367
- ]
368
- },
369
- {
370
- "cell_type": "markdown",
371
- "id": "2f5deb21-18fb-4c5b-9045-c7fe5e751c05",
372
- "metadata": {},
373
- "source": [
374
- "Its usually helpful if we tell the LLM what we want to create when we prompt it. I have found using the tags like `<source text>` can be really useful to denote what you are specifying.\n",
375
- "\n",
376
- "> I want to translate the following \\<source text\\> from English into Arabic. But first I want to create a json that includes the following: \n",
377
- "{\"intent\": \"\", \"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}. \n",
378
- "Can you fill this out and be specific to how this can help you translate in the next step? No need to translate yet! \n",
379
- "\\<source text\\> \n",
380
- "\\</source text\\> "
381
- ]
382
- },
383
- {
384
- "cell_type": "markdown",
385
- "id": "55f02988-1c63-4f03-a5a6-ca86c83b3c17",
386
- "metadata": {},
387
- "source": [
388
- "## Tool Definition"
389
- ]
390
- },
391
- {
392
- "cell_type": "code",
393
- "execution_count": 14,
394
- "id": "affe3668-aa37-47b5-be37-a0bd5dabab56",
395
- "metadata": {
396
- "tags": []
397
- },
398
- "outputs": [],
399
- "source": [
400
- "translation_tools = [\n",
401
- " {\n",
402
- " \"type\": \"function\",\n",
403
- " \"function\": {\n",
404
- " \"name\": \"get_translation_audience_purpose\",\n",
405
- " \"description\": \"Get the background of a text to assist in translation\",\n",
406
- " \"parameters\": {\n",
407
- " \"type\": \"object\",\n",
408
- " \"properties\": {\n",
409
- " \"subject\": {\n",
410
- " \"type\": \"string\",\n",
411
- " \"description\": \"The topic or central theme that the text revolves around.\",\n",
412
- " },\n",
413
- " \"assumptions relating to the content\": {\n",
414
- " \"type\": \"string\",\n",
415
- " \"description\": \"Write out any assumptions relating to the text.\",\n",
416
- " },\n",
417
- " \"purpose\": {\n",
418
- " \"type\": \"string\",\n",
419
- " \"description\": \"Why the text was written\",\n",
420
- " },\n",
421
- " \"audience\": {\n",
422
- " \"type\": \"string\",\n",
423
- " \"description\": \"The inferred audience that the text is written for.\",\n",
424
- " },\n",
425
- " },\n",
426
- " \"required\": [\n",
427
- " \"subject\",\n",
428
- " \"assumptions relating to the content\",\n",
429
- " \"purpose\",\n",
430
- " \"audience\",\n",
431
- " ],\n",
432
- " },\n",
433
- " },\n",
434
- " }\n",
435
- "]"
436
- ]
437
- },
438
- {
439
- "cell_type": "code",
440
- "execution_count": 15,
441
- "id": "b6436ce6-03af-4206-a283-0c2ecd17bd88",
442
- "metadata": {},
443
- "outputs": [],
444
- "source": [
445
- "def tool_call_chat_completion(system_prompt, translation_input, tools):\n",
446
- " \"\"\"\n",
447
- " Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
448
- " \"\"\"\n",
449
- "\n",
450
- " prompt = f\"\"\"I want to translate the following <source text> from English into Arabic. But first I want to create a json that includes the following: \n",
451
- "{{\"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}}. \n",
452
- "Can you fill this out and be specific to how this can help you translate in the next step? No need to translate yet! \n",
453
- "<source text> \n",
454
- "{translation_input}\n",
455
- "</source text> \n",
456
- "\"\"\"\n",
457
- " messages = [\n",
458
- " {\"role\": \"system\", \"content\": system_prompt},\n",
459
- " {\n",
460
- " \"role\": \"user\",\n",
461
- " \"content\": prompt,\n",
462
- " },\n",
463
- " ]\n",
464
- " return client.chat_completion(messages, max_tokens=10_000, tools=tools, tool_choice='get_translation_audience_purpose')"
465
- ]
466
- },
467
- {
468
- "cell_type": "code",
469
- "execution_count": 16,
470
- "id": "a731b2c0-54a3-4b8e-83f6-1663c759cf79",
471
- "metadata": {
472
- "tags": []
473
- },
474
- "outputs": [],
475
- "source": [
476
- "translation_input = \"We have observed that when groups of stakeholders work to define … visions, this leads to debate over whether to emphasize ecosystem health or human well-being … Whether the priority is ecosystems or people greatly influences stakeholders' assessment of desirable ecological and social states.\"\n",
477
- "response = tool_call_chat_completion(system_prompt, translation_input, translation_tools)"
478
- ]
479
- },
480
- {
481
- "cell_type": "code",
482
- "execution_count": 17,
483
- "id": "07e0f133-f6e1-4bc6-969c-da9d36bfba2f",
484
- "metadata": {
485
- "tags": []
486
- },
487
- "outputs": [
488
- {
489
- "data": {
490
- "text/plain": [
491
- "454"
492
- ]
493
- },
494
- "execution_count": 17,
495
- "metadata": {},
496
- "output_type": "execute_result"
497
- }
498
- ],
499
- "source": [
500
- "function_call_tokens = response.usage.prompt_tokens\n",
501
- "function_call_tokens"
502
- ]
503
- },
504
- {
505
- "cell_type": "code",
506
- "execution_count": 18,
507
- "id": "02be1827-7137-463f-a026-0b26dec6f552",
508
- "metadata": {
509
- "tags": []
510
- },
511
- "outputs": [
512
- {
513
- "name": "stdout",
514
- "output_type": "stream",
515
- "text": [
516
- "{'assumptions relating to the content': 'The text assumes that the readers '\n",
517
- " 'understand the concept of stakeholder '\n",
518
- " 'groups and their potential differing '\n",
519
- " 'priorities regarding environmental '\n",
520
- " 'and social issues.',\n",
521
- " 'audience': 'The target audience for this text is likely individuals or '\n",
522
- " 'organizations involved in sustainability, ecology, or social '\n",
523
- " 'welfare fields, as well as stakeholders with an interest in '\n",
524
- " 'these areas.',\n",
525
- " 'purpose': 'The purpose of this text is to highlight the observations made '\n",
526
- " 'about the debates that arise when stakeholder groups define their '\n",
527
- " 'visions, specifically regarding the priority between ecosystem '\n",
528
- " 'health and human well-being, and how this priority influences '\n",
529
- " 'their assessment of desired ecological and social outcomes.',\n",
530
- " 'subject': 'Debates on Prioritizing Ecosystem vs. Human Well-being by '\n",
531
- " 'Stakeholder Groups'}\n"
532
- ]
533
- }
534
- ],
535
- "source": [
536
- "from pprint import pprint\n",
537
- "description_json = response.choices[0].message.tool_calls[0].function.arguments\n",
538
- "pprint(description_json)"
539
- ]
540
- },
541
- {
542
- "cell_type": "code",
543
- "execution_count": 19,
544
- "id": "7575bd09-2d20-49ae-bb10-162a0e469f16",
545
- "metadata": {},
546
- "outputs": [],
547
- "source": [
548
- "def automatic_purpose_driven_chat_completion(system_prompt, translation_input, description_json):\n",
549
- " \"\"\"\n",
550
- " Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
551
- " \"\"\"\n",
552
- "\n",
553
- " prompt = f\"\"\"Given the following descriptive json translate <source text> from English to Arabic\n",
554
- "{description_json}\n",
555
- "<source text> \n",
556
- "{translation_input}\n",
557
- "</source text> \n",
558
- "Translation: \n",
559
- "\"\"\"\n",
560
- " messages = [\n",
561
- " {\"role\": \"system\", \"content\": system_prompt},\n",
562
- " {\n",
563
- " \"role\": \"user\",\n",
564
- " \"content\": prompt,\n",
565
- " },\n",
566
- " ]\n",
567
- " return client.chat_completion(messages, max_tokens=10_000)"
568
- ]
569
- },
570
- {
571
- "cell_type": "code",
572
- "execution_count": 20,
573
- "id": "32bccca0-c866-4006-84d1-d5b783b73689",
574
- "metadata": {
575
- "tags": []
576
- },
577
- "outputs": [],
578
- "source": [
579
- "response = automatic_purpose_driven_chat_completion(system_prompt, translation_input, description_json)"
580
- ]
581
- },
582
- {
583
- "cell_type": "code",
584
- "execution_count": 21,
585
- "id": "b0867efb-39ea-4f9a-b073-2a84261f3821",
586
- "metadata": {
587
- "tags": []
588
- },
589
- "outputs": [
590
- {
591
- "data": {
592
- "text/plain": [
593
- "311"
594
- ]
595
- },
596
- "execution_count": 21,
597
- "metadata": {},
598
- "output_type": "execute_result"
599
- }
600
- ],
601
- "source": [
602
- "automatic_purpose_driven_tokens = response.usage.prompt_tokens\n",
603
- "automatic_purpose_driven_tokens"
604
- ]
605
- },
606
- {
607
- "cell_type": "code",
608
- "execution_count": 22,
609
- "id": "462ca84c-9ffd-4924-880d-e06b724caf02",
610
- "metadata": {
611
- "tags": []
612
- },
613
- "outputs": [
614
- {
615
- "name": "stdout",
616
- "output_type": "stream",
617
- "text": [
618
- "Here is the translated text from English to Arabic:\n",
619
- "\n",
620
- "{\"افتراضات متعلقة بالمحتوى\": \"يفترض النص أن القراء يفهمون مفهوم مجموعات أصحاب المصلحة وأولوياتهم المختلفة المحتملة فيما يتعلق بالقضايا البيئية والاجتماعية.\", \"الجمهور\": \"الجمهور المستهدف لهذا النص هو على الأرجح أفراد أو منظمات تعمل في مجالات الاستدامة أو الإيكولوجيا أو الرعاية الاجتماعية، بالإضافة إلى أصحاب المصلحة المهتمين بهذه المجالات.\", \"الغرض\": \"الغرض من هذا النص هو تسليط الضوء على الملاحظات التي أبديت حول النقاشات التي تنشأ عندما تحدد مجموعات أصحاب المصلحة رؤيتها، خاصة فيما يتعلق بالأولوية بين صحة النظام البيئي ورفاهية الإنسان، وكيف تؤثر هذه الأولوية على تقييمها للنتائج الإيكولوجية والاجتماعية المرجوة.\", \"الموضوع\": \"النقاشات حول أولويات النظام البيئي مقابل رفاهية الإنسان من قبل مجموعات أصحاب المصلحة\": \"لقد لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد الرؤى، فإن ذلك يؤدي إلى نقاش حول ما إذا كان ينبغي التركيز على صحة النظام البيئي أو رفاهية الإنسان... إن أولوية النظم البيئية أو البشر تؤثر بشكل كبير على تقييم أصحاب المصلحة للوضع الاجتماعي والإيكولوجي المرغوب.\"}\n"
621
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
622
  }
623
- ],
624
- "source": [
625
- "print(response.choices[0].message.content)"
626
- ]
627
- },
628
- {
629
- "cell_type": "markdown",
630
- "id": "1ec3b20b-8393-4fda-a51d-cf67984cc166",
631
- "metadata": {},
632
- "source": [
633
- "# Push to the hub"
634
- ]
635
- },
636
- {
637
- "cell_type": "code",
638
- "execution_count": 23,
639
- "id": "6fc55725-216f-45dd-9c6d-dae77e16d606",
640
- "metadata": {
641
- "tags": []
642
- },
643
- "outputs": [
644
- {
645
- "data": {
646
- "text/plain": [
647
- "CommitInfo(commit_url='https://huggingface.co/arabic-translation-prompt-engineering/atpe-notebooks/commit/d112cc49a99a72106b75c3eae9be453264fb488d', commit_message='Upload baseline.ipynb with huggingface_hub', commit_description='', oid='d112cc49a99a72106b75c3eae9be453264fb488d', pr_url=None, pr_revision=None, pr_num=None)"
648
- ]
649
- },
650
- "execution_count": 23,
651
- "metadata": {},
652
- "output_type": "execute_result"
653
  }
654
- ],
655
- "source": [
656
- "from huggingface_hub import HfApi\n",
657
- "\n",
658
- "api = HfApi()\n",
659
- "api.upload_file(\n",
660
- " path_or_fileobj=\"baseline.ipynb\",\n",
661
- " path_in_repo=\"baseline.ipynb\",\n",
662
- " repo_id=\"arabic-translation-prompt-engineering/atpe-notebooks\",\n",
663
- " repo_type=\"model\",\n",
664
- ")"
665
- ]
666
- },
667
- {
668
- "cell_type": "code",
669
- "execution_count": null,
670
- "id": "6f21f4cf-1872-4934-bb91-8aef311ed729",
671
- "metadata": {},
672
- "outputs": [],
673
- "source": []
674
- }
675
- ],
676
- "metadata": {
677
- "kernelspec": {
678
- "display_name": "Python 3 (ipykernel)",
679
- "language": "python",
680
- "name": "python3"
681
  },
682
- "language_info": {
683
- "codemirror_mode": {
684
- "name": "ipython",
685
- "version": 3
686
- },
687
- "file_extension": ".py",
688
- "mimetype": "text/x-python",
689
- "name": "python",
690
- "nbconvert_exporter": "python",
691
- "pygments_lexer": "ipython3",
692
- "version": "3.10.9"
693
- }
694
- },
695
- "nbformat": 4,
696
- "nbformat_minor": 5
697
- }
 
1
  {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "883e3354-1538-4f98-bf42-67552215bba3",
6
+ "metadata": {
7
+ "id": "883e3354-1538-4f98-bf42-67552215bba3"
8
+ },
9
+ "source": [
10
+ "# Setup"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "id": "b45bd52f-03e9-419f-8110-1013ff45fb1b",
17
+ "metadata": {
18
+ "tags": [],
19
+ "id": "b45bd52f-03e9-419f-8110-1013ff45fb1b"
20
+ },
21
+ "outputs": [],
22
+ "source": [
23
+ "from huggingface_hub import InferenceClient, login"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": null,
29
+ "id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e",
30
+ "metadata": {
31
+ "tags": [],
32
+ "id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e",
33
+ "outputId": "cb57d6e8-7d61-49ca-ec60-05594a7d5842",
34
+ "colab": {
35
+ "referenced_widgets": [
36
+ "515c96c357454fdc9a38ecc995ff1b3d"
37
+ ]
38
+ }
39
+ },
40
+ "outputs": [
41
+ {
42
+ "data": {
43
+ "application/vnd.jupyter.widget-view+json": {
44
+ "model_id": "515c96c357454fdc9a38ecc995ff1b3d",
45
+ "version_major": 2,
46
+ "version_minor": 0
47
+ },
48
+ "text/plain": [
49
+ "VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
50
+ ]
51
+ },
52
+ "metadata": {},
53
+ "output_type": "display_data"
54
+ }
55
+ ],
56
+ "source": [
57
+ "login()"
58
+ ]
59
+ },
60
+ {
61
+ "cell_type": "markdown",
62
+ "id": "4c254d6f-f3a1-49c1-815e-36e41e75ca25",
63
+ "metadata": {
64
+ "id": "4c254d6f-f3a1-49c1-815e-36e41e75ca25"
65
+ },
66
+ "source": [
67
+ "<div class=\"alert alert-danger\" role=\"alert\" style=\"display: flex; align-items: center;\">\n",
68
+ " <div style=\"text-align: center; padding-right: 10px;\">\n",
69
+ " <i class=\"fa fa-exclamation-triangle fa-2x\"></i>\n",
70
+ " </div>\n",
71
+ " <div style=\"display: flex; align-items: center; margin-top: 4px;\"> <!-- Added margin-top to lower the text -->\n",
72
+ " <strong>Warning: You will need to point to a model/deployment that is running.</strong>\n",
73
+ " </div>\n",
74
+ "</div>\n"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": null,
80
+ "id": "84e6cb89-30d3-4ef5-8063-07783798e045",
81
+ "metadata": {
82
+ "id": "84e6cb89-30d3-4ef5-8063-07783798e045"
83
+ },
84
+ "outputs": [],
85
+ "source": [
86
+ "MODEL = \"CohereForAI/c4ai-command-r-plus\"\n",
87
+ "client = InferenceClient(MODEL)"
88
+ ]
89
+ },
90
+ {
91
+ "cell_type": "markdown",
92
+ "id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb",
93
+ "metadata": {
94
+ "id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb"
95
+ },
96
+ "source": [
97
+ "# Translation\n",
98
+ "Our goal is to explore translation between English and Arabic and how prompt engineering can impact it. There has been [some work](https://arxiv.org/pdf/2308.01391), but we didn't find as much as we were hoping, especially for open source models.\n",
99
+ "\n",
100
+ "We have created a dataset across 6 domains and want to compare each method by having human rankers. We also have human translations to ground these rankings."
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "markdown",
105
+ "id": "c5bee77b-da1c-43b3-ab14-d15e871f7502",
106
+ "metadata": {
107
+ "id": "c5bee77b-da1c-43b3-ab14-d15e871f7502"
108
+ },
109
+ "source": [
110
+ "## Baseline\n",
111
+ "\n",
112
+ "For our baseline we will translate with a simple system prompt and instruction."
113
+ ]
114
+ },
115
+ {
116
+ "cell_type": "markdown",
117
+ "id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f",
118
+ "metadata": {
119
+ "id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f"
120
+ },
121
+ "source": [
122
+ "### System Prompt\n",
123
+ "This is a pretty basic system prompt. We give a role, and an assumed understanding. We also push for goals like \"highly motivated and detail-oriented\".\n",
124
+ "\n",
125
+ "> You are a skilled translator with extensive experience in English to Arabic translations. You possess a deep understanding of the linguistic, cultural, and contextual nuances essential for accurate and effective translation between these languages. Highly motivated and detail-oriented, you are committed to delivering translations that maintain the integrity and intent of the original text. Your role is crucial in ensuring clear and precise communication in our multilingual system."
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "code",
130
+ "execution_count": null,
131
+ "id": "032c86d2-868e-4fa6-b03e-58f1c41434cc",
132
+ "metadata": {
133
+ "tags": [],
134
+ "id": "032c86d2-868e-4fa6-b03e-58f1c41434cc"
135
+ },
136
+ "outputs": [],
137
+ "source": [
138
+ "system_prompt = \"\"\"You are a skilled translator with extensive experience in English and Arabic translations. You possess a deep understanding of the linguistic, cultural, and contextual nuances essential for accurate and effective translation between these languages. Highly motivated and detail-oriented, you are committed to delivering translations that maintain the integrity and intent of the original text. Your role is crucial in ensuring clear and precise communication in our multilingual system.\"\"\""
139
+ ]
140
+ },
141
+ {
142
+ "cell_type": "markdown",
143
+ "id": "803ddeba-03de-4f13-95d1-5fb097058cf2",
144
+ "metadata": {
145
+ "id": "803ddeba-03de-4f13-95d1-5fb097058cf2"
146
+ },
147
+ "source": [
148
+ "### Instruction\n",
149
+ "> Translate this from arabic to english: {translation_input}.\n",
150
+ ">\n",
151
+ "> Translation:\n",
152
+ "\n",
153
+ "We will use a simple instruction to get a translation."
154
+ ]
155
+ },
156
+ {
157
+ "cell_type": "code",
158
+ "execution_count": null,
159
+ "id": "b7f1722c-c484-4e22-a025-53f95943fc76",
160
+ "metadata": {
161
+ "id": "b7f1722c-c484-4e22-a025-53f95943fc76"
162
+ },
163
+ "outputs": [],
164
+ "source": [
165
+ "def baseline_chat_completion(system_prompt, translation_input):\n",
166
+ " \"\"\"\n",
167
+ " Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
168
+ " \"\"\"\n",
169
+ " messages = [\n",
170
+ " {\"role\": \"system\", \"content\": system_prompt},\n",
171
+ " {\n",
172
+ " \"role\": \"user\",\n",
173
+ " \"content\": f\"Translate this from english to arabic: {translation_input}.\\nTranslation: \",\n",
174
+ " },\n",
175
+ " ]\n",
176
+ " return client.chat_completion(messages, max_tokens=10_000)"
177
+ ]
178
+ },
179
+ {
180
+ "cell_type": "code",
181
+ "execution_count": null,
182
+ "id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e",
183
+ "metadata": {
184
+ "tags": [],
185
+ "id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e"
186
+ },
187
+ "outputs": [],
188
+ "source": [
189
+ "translation_input = \"Float like a butterfly sting like a bee – his hands can’t hit what his eyes can’t see.\"\n",
190
+ "response = baseline_chat_completion(\n",
191
+ " system_prompt,\n",
192
+ " translation_input,\n",
193
+ ")"
194
+ ]
195
+ },
196
+ {
197
+ "cell_type": "markdown",
198
+ "id": "2bca574c-461d-4822-b0dd-b12a3b9846b3",
199
+ "metadata": {
200
+ "id": "2bca574c-461d-4822-b0dd-b12a3b9846b3"
201
+ },
202
+ "source": [
203
+ "### Token Cost\n",
204
+ "Here we can see that the cost is quite cheap, only 92 tokens!"
205
+ ]
206
+ },
207
+ {
208
+ "cell_type": "code",
209
+ "execution_count": null,
210
+ "id": "4e305b1e-56e0-44da-8c17-496cbcc35fad",
211
+ "metadata": {
212
+ "tags": [],
213
+ "id": "4e305b1e-56e0-44da-8c17-496cbcc35fad",
214
+ "outputId": "a97a8a75-e2e6-4ac3-e7ac-db674a1f46c9"
215
+ },
216
+ "outputs": [
217
+ {
218
+ "data": {
219
+ "text/plain": [
220
+ "120"
221
+ ]
222
+ },
223
+ "execution_count": 7,
224
+ "metadata": {},
225
+ "output_type": "execute_result"
226
+ }
227
+ ],
228
+ "source": [
229
+ "response.usage.prompt_tokens"
230
+ ]
231
+ },
232
+ {
233
+ "cell_type": "code",
234
+ "execution_count": null,
235
+ "id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3",
236
+ "metadata": {
237
+ "tags": [],
238
+ "id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3",
239
+ "outputId": "0d7417e1-b648-40aa-eba7-5f1b0dc962b3"
240
+ },
241
+ "outputs": [
242
+ {
243
+ "name": "stdout",
244
+ "output_type": "stream",
245
+ "text": [
246
+ "يطير كالفراشة ويلسع كالنحلة - لا يمكن ليديه أن تصيبا ما لا تستطيع عيناه رؤيته.\n"
247
+ ]
248
+ }
249
+ ],
250
+ "source": [
251
+ "print(response.choices[0].message.content)"
252
+ ]
253
+ },
254
+ {
255
+ "cell_type": "markdown",
256
+ "id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715",
257
+ "metadata": {
258
+ "id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715"
259
+ },
260
+ "source": [
261
+ "## Manual Purpose Driven Translation\n",
262
+ "\n",
263
+ "[Optimizing Machine Translation through Prompt Engineering](https://arxiv.org/pdf/2308.01391) has done some good exploratory work in examining how prompt engineering can impact translation. They were working between Japanese and English and showed that translations influenced by prompts tailored to **specific purposes** and **target audiences** generally adhered more closely to the translation specifications, suggesting that such prompted translations could be more culturally and contextually appropriate than standard machine translations.\n",
264
+ "\n",
265
+ "### Prompt\n",
266
+ "One of the approaches in the paper was to include the purpose and target audience specification. This was motivated by the author’s experience as a professional translator, leading to the conclusion that these two parameters are essential even in everyday translation work. You can find the prompt below adapted for Arabic to English:\n",
267
+ "\n",
268
+ "> Translate the following English [source text] into Arabic. Please fulfill the following conditions when translating. \n",
269
+ "> Purpose of the translation: `<Manual description>` \n",
270
+ "> Target audience: `<Manual description>` \n",
271
+ "> [source text] `{translation_input}` \n",
272
+ "> [translated text]\n",
273
+ "\n",
274
+ "You can see that we need to provide the Purpose and the Target Audience for each translation. This makes sense as we will be able to steer our model appropriately, but the drawback is that we need to do this for each subject. In the real world this likely won't scale and is rather tedious.\n",
275
+ "\n",
276
+ "Lets go ahead and create these for each of our datasets."
277
+ ]
278
+ },
279
+ {
280
+ "cell_type": "code",
281
+ "execution_count": null,
282
+ "id": "4714f6a2-fd0b-48ee-80bc-860f40ee2baa",
283
+ "metadata": {
284
+ "tags": [],
285
+ "id": "4714f6a2-fd0b-48ee-80bc-860f40ee2baa"
286
+ },
287
+ "outputs": [],
288
+ "source": [
289
+ "dataset_to_purpose_target = {\n",
290
+ " \"ELRC-24ss\": {\n",
291
+ " \"purpose\": \"Enhancing understanding and knowledge about COVID-19 and health-related topics.\",\n",
292
+ " \"audience\": \"Individuals seeking reliable and comprehensible information about COVID-19 and related health topics.\",\n",
293
+ " },\n",
294
+ " \"GNOME-25ss\": {\n",
295
+ " \"purpose\": \"Facilitating localization and translation of GNOME software.\",\n",
296
+ " \"audience\": \"Translators and developers working on GNOME projects.\"\n",
297
+ " },\n",
298
+ " \"HPLT-25ss\": {\n",
299
+ " \"purpose\": \"Providing multilingual data for high-performance language technologies.\",\n",
300
+ " \"audience\": \"Researchers and developers working on multilingual NLP applications.\"\n",
301
+ " },\n",
302
+ " \"OpenSubtitles-25ss\": {\n",
303
+ " \"purpose\": \"Creating parallel corpora from movie and TV subtitles.\",\n",
304
+ " \"audience\": \"Researchers and developers in NLP and machine translation. And Movies and TV Shows translators\"\n",
305
+ " },\n",
306
+ " \"TED2020-25ss\": {\n",
307
+ " \"purpose\": \"Generating multilingual sentence embeddings using TED transcripts.\",\n",
308
+ " \"audience\": \"Researchers and developers working on multilingual sentence embeddings.\"\n",
309
+ " },\n",
310
+ " \"UNPC-24ss\": {\n",
311
+ " \"purpose\": \"Offering a parallel corpus of United Nations documents for linguistic research.\",\n",
312
+ " \"audience\": \"Researchers and linguists studying multilingual and legal texts.\"\n",
313
+ " }\n",
314
+ "}\n"
315
+ ]
316
+ },
317
+ {
318
+ "cell_type": "code",
319
+ "execution_count": null,
320
+ "id": "f865e9f8-7c63-4e72-b539-0d5916eda44f",
321
+ "metadata": {
322
+ "id": "f865e9f8-7c63-4e72-b539-0d5916eda44f"
323
+ },
324
+ "outputs": [],
325
+ "source": [
326
+ "def purpose_driven_chat_completion(system_prompt, translation_input, dataset):\n",
327
+ " \"\"\"\n",
328
+ " Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
329
+ " \"\"\"\n",
330
+ "\n",
331
+ " prompt = f\"\"\"Translate the following English [source text] into Arabic. Please fulfill the following conditions when translating.\n",
332
+ "Purpose of the translation: {dataset_to_purpose_target[dataset]['purpose']}\n",
333
+ "Target audience: {dataset_to_purpose_target[dataset]['audience']}\n",
334
+ "[source text] `{translation_input}`\n",
335
+ "[translated text] \"\"\"\n",
336
+ "\n",
337
+ " messages = [\n",
338
+ " {\"role\": \"system\", \"content\": system_prompt},\n",
339
+ " {\n",
340
+ " \"role\": \"user\",\n",
341
+ " \"content\": prompt,\n",
342
+ " },\n",
343
+ " ]\n",
344
+ " return client.chat_completion(messages, max_tokens=10_000)"
345
+ ]
346
+ },
347
+ {
348
+ "cell_type": "code",
349
+ "execution_count": null,
350
+ "id": "4115515d-cbcd-405a-b2e0-a805880a40c4",
351
+ "metadata": {
352
+ "tags": [],
353
+ "id": "4115515d-cbcd-405a-b2e0-a805880a40c4"
354
+ },
355
+ "outputs": [],
356
+ "source": [
357
+ "translation_input = \"We have observed that when groups of stakeholders work to define … visions, this leads to debate over whether to emphasize ecosystem health or human well-being … Whether the priority is ecosystems or people greatly influences stakeholders' assessment of desirable ecological and social states.\"\n",
358
+ "response = purpose_driven_chat_completion(system_prompt, translation_input, \"ELRC-24ss\")"
359
+ ]
360
+ },
361
+ {
362
+ "cell_type": "code",
363
+ "execution_count": null,
364
+ "id": "6296d255-d11d-4df7-aa0e-1226ef3d963a",
365
+ "metadata": {
366
+ "tags": [],
367
+ "id": "6296d255-d11d-4df7-aa0e-1226ef3d963a",
368
+ "outputId": "45282f39-18ef-4dc0-f708-1113ca1769ce"
369
+ },
370
+ "outputs": [
371
+ {
372
+ "data": {
373
+ "text/plain": [
374
+ "208"
375
+ ]
376
+ },
377
+ "execution_count": 12,
378
+ "metadata": {},
379
+ "output_type": "execute_result"
380
+ }
381
+ ],
382
+ "source": [
383
+ "response.usage.prompt_tokens"
384
+ ]
385
+ },
386
+ {
387
+ "cell_type": "code",
388
+ "execution_count": null,
389
+ "id": "1f1c6dd0-11bf-4b88-9029-8bce1e7bcb1c",
390
+ "metadata": {
391
+ "tags": [],
392
+ "id": "1f1c6dd0-11bf-4b88-9029-8bce1e7bcb1c",
393
+ "outputId": "a12fc058-249f-4493-eb3e-f021c12651dc"
394
+ },
395
+ "outputs": [
396
+ {
397
+ "name": "stdout",
398
+ "output_type": "stream",
399
+ "text": [
400
+ "\"لقد لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد ... الرؤى، فإن هذا يؤدي إلى نقاش حول ما إذا كان ينبغي التركيز على صحة النظام البيئي أو رفاهية الإنسان ... إن معرفة ما إذا كانت الأولوية للنظم البيئية أو للبشر تؤثر بشكل كبير على تقييم أصحاب المصلحة للحالات البيئية والاجتماعية المرغوبة.\"\n"
401
+ ]
402
+ }
403
+ ],
404
+ "source": [
405
+ "print(response.choices[0].message.content)"
406
+ ]
407
+ },
408
+ {
409
+ "cell_type": "markdown",
410
+ "id": "898a909e-88a2-4efb-99ed-64ceee317037",
411
+ "metadata": {
412
+ "id": "898a909e-88a2-4efb-99ed-64ceee317037"
413
+ },
414
+ "source": [
415
+ "## Automatic Purpose Driven Structured Generation Translation\n",
416
+ "\n",
417
+ "Manual Purpose Driven Translation is a great step in the right direction, but its challenging to scale. Instead of having the user submit these purposes and target audiences, what if we use a model to do that? The easiest way to get this input in a format that is convenient is going to be by using [structured generation](https://huggingface.co/blog/evaluation-structured-outputs) to get a json. We can easily do this in InferenceClient easily just by using [tools](https://huggingface.co/docs/huggingface_hub/en/package_reference/inference_client#huggingface_hub.InferenceClient.chat_completion.tools)"
418
+ ]
419
+ },
420
+ {
421
+ "cell_type": "markdown",
422
+ "id": "8aca3847-7f3d-43d3-8e80-0c7e2282073b",
423
+ "metadata": {
424
+ "id": "8aca3847-7f3d-43d3-8e80-0c7e2282073b"
425
+ },
426
+ "source": [
427
+ "## Instruction"
428
+ ]
429
+ },
430
+ {
431
+ "cell_type": "markdown",
432
+ "id": "2f5deb21-18fb-4c5b-9045-c7fe5e751c05",
433
+ "metadata": {
434
+ "id": "2f5deb21-18fb-4c5b-9045-c7fe5e751c05"
435
+ },
436
+ "source": [
437
+ "Its usually helpful if we tell the LLM what we want to create when we prompt it. I have found using the tags like `<source text>` can be really useful to denote what you are specifying.\n",
438
+ "\n",
439
+ "> I want to translate the following \\<source text\\> from English into Arabic. But first I want to create a json that includes the following: \n",
440
+ "{\"intent\": \"\", \"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}. \n",
441
+ "Can you fill this out and be specific to how this can help you translate in the next step? No need to translate yet! \n",
442
+ "\\<source text\\> \n",
443
+ "\\</source text\\> "
444
+ ]
445
+ },
446
+ {
447
+ "cell_type": "markdown",
448
+ "id": "55f02988-1c63-4f03-a5a6-ca86c83b3c17",
449
+ "metadata": {
450
+ "id": "55f02988-1c63-4f03-a5a6-ca86c83b3c17"
451
+ },
452
+ "source": [
453
+ "## Tool Definition"
454
+ ]
455
+ },
456
+ {
457
+ "cell_type": "code",
458
+ "execution_count": null,
459
+ "id": "affe3668-aa37-47b5-be37-a0bd5dabab56",
460
+ "metadata": {
461
+ "tags": [],
462
+ "id": "affe3668-aa37-47b5-be37-a0bd5dabab56"
463
+ },
464
+ "outputs": [],
465
+ "source": [
466
+ "translation_tools = [\n",
467
+ " {\n",
468
+ " \"type\": \"function\",\n",
469
+ " \"function\": {\n",
470
+ " \"name\": \"get_translation_audience_purpose\",\n",
471
+ " \"description\": \"Get the background of a text to assist in translation\",\n",
472
+ " \"parameters\": {\n",
473
+ " \"type\": \"object\",\n",
474
+ " \"properties\": {\n",
475
+ " \"subject\": {\n",
476
+ " \"type\": \"string\",\n",
477
+ " \"description\": \"The topic or central theme that the text revolves around.\",\n",
478
+ " },\n",
479
+ " \"assumptions relating to the content\": {\n",
480
+ " \"type\": \"string\",\n",
481
+ " \"description\": \"Write out any assumptions relating to the text.\",\n",
482
+ " },\n",
483
+ " \"purpose\": {\n",
484
+ " \"type\": \"string\",\n",
485
+ " \"description\": \"Why the text was written\",\n",
486
+ " },\n",
487
+ " \"audience\": {\n",
488
+ " \"type\": \"string\",\n",
489
+ " \"description\": \"The inferred audience that the text is written for.\",\n",
490
+ " },\n",
491
+ " },\n",
492
+ " \"required\": [\n",
493
+ " \"subject\",\n",
494
+ " \"assumptions relating to the content\",\n",
495
+ " \"purpose\",\n",
496
+ " \"audience\",\n",
497
+ " ],\n",
498
+ " },\n",
499
+ " },\n",
500
+ " }\n",
501
+ "]"
502
+ ]
503
+ },
504
+ {
505
+ "cell_type": "code",
506
+ "execution_count": null,
507
+ "id": "b6436ce6-03af-4206-a283-0c2ecd17bd88",
508
+ "metadata": {
509
+ "id": "b6436ce6-03af-4206-a283-0c2ecd17bd88"
510
+ },
511
+ "outputs": [],
512
+ "source": [
513
+ "def tool_call_chat_completion(system_prompt, translation_input, tools):\n",
514
+ " \"\"\"\n",
515
+ " Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
516
+ " \"\"\"\n",
517
+ "\n",
518
+ " prompt = f\"\"\"I want to translate the following <source text> from English into Arabic. But first I want to create a json that includes the following:\n",
519
+ "{{\"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}}.\n",
520
+ "Can you fill this out and be specific to how this can help you translate in the next step? No need to translate yet!\n",
521
+ "<source text>\n",
522
+ "{translation_input}\n",
523
+ "</source text>\n",
524
+ "\"\"\"\n",
525
+ " messages = [\n",
526
+ " {\"role\": \"system\", \"content\": system_prompt},\n",
527
+ " {\n",
528
+ " \"role\": \"user\",\n",
529
+ " \"content\": prompt,\n",
530
+ " },\n",
531
+ " ]\n",
532
+ " return client.chat_completion(messages, max_tokens=10_000, tools=tools, tool_choice='get_translation_audience_purpose')"
533
+ ]
534
+ },
535
+ {
536
+ "cell_type": "code",
537
+ "execution_count": null,
538
+ "id": "a731b2c0-54a3-4b8e-83f6-1663c759cf79",
539
+ "metadata": {
540
+ "tags": [],
541
+ "id": "a731b2c0-54a3-4b8e-83f6-1663c759cf79"
542
+ },
543
+ "outputs": [],
544
+ "source": [
545
+ "translation_input = \"We have observed that when groups of stakeholders work to define … visions, this leads to debate over whether to emphasize ecosystem health or human well-being … Whether the priority is ecosystems or people greatly influences stakeholders' assessment of desirable ecological and social states.\"\n",
546
+ "response = tool_call_chat_completion(system_prompt, translation_input, translation_tools)"
547
+ ]
548
+ },
549
+ {
550
+ "cell_type": "code",
551
+ "execution_count": null,
552
+ "id": "07e0f133-f6e1-4bc6-969c-da9d36bfba2f",
553
+ "metadata": {
554
+ "tags": [],
555
+ "id": "07e0f133-f6e1-4bc6-969c-da9d36bfba2f",
556
+ "outputId": "82e2506a-7336-4909-b8e4-fc52f671c511"
557
+ },
558
+ "outputs": [
559
+ {
560
+ "data": {
561
+ "text/plain": [
562
+ "454"
563
+ ]
564
+ },
565
+ "execution_count": 17,
566
+ "metadata": {},
567
+ "output_type": "execute_result"
568
+ }
569
+ ],
570
+ "source": [
571
+ "function_call_tokens = response.usage.prompt_tokens\n",
572
+ "function_call_tokens"
573
+ ]
574
+ },
575
+ {
576
+ "cell_type": "code",
577
+ "execution_count": null,
578
+ "id": "02be1827-7137-463f-a026-0b26dec6f552",
579
+ "metadata": {
580
+ "tags": [],
581
+ "id": "02be1827-7137-463f-a026-0b26dec6f552",
582
+ "outputId": "60cd7be7-b6ac-412b-e0bc-3b8d3088f2b2"
583
+ },
584
+ "outputs": [
585
+ {
586
+ "name": "stdout",
587
+ "output_type": "stream",
588
+ "text": [
589
+ "{'assumptions relating to the content': 'The text assumes that the readers '\n",
590
+ " 'understand the concept of stakeholder '\n",
591
+ " 'groups and their potential differing '\n",
592
+ " 'priorities regarding environmental '\n",
593
+ " 'and social issues.',\n",
594
+ " 'audience': 'The target audience for this text is likely individuals or '\n",
595
+ " 'organizations involved in sustainability, ecology, or social '\n",
596
+ " 'welfare fields, as well as stakeholders with an interest in '\n",
597
+ " 'these areas.',\n",
598
+ " 'purpose': 'The purpose of this text is to highlight the observations made '\n",
599
+ " 'about the debates that arise when stakeholder groups define their '\n",
600
+ " 'visions, specifically regarding the priority between ecosystem '\n",
601
+ " 'health and human well-being, and how this priority influences '\n",
602
+ " 'their assessment of desired ecological and social outcomes.',\n",
603
+ " 'subject': 'Debates on Prioritizing Ecosystem vs. Human Well-being by '\n",
604
+ " 'Stakeholder Groups'}\n"
605
+ ]
606
+ }
607
+ ],
608
+ "source": [
609
+ "from pprint import pprint\n",
610
+ "description_json = response.choices[0].message.tool_calls[0].function.arguments\n",
611
+ "pprint(description_json)"
612
+ ]
613
+ },
614
+ {
615
+ "cell_type": "code",
616
+ "execution_count": null,
617
+ "id": "7575bd09-2d20-49ae-bb10-162a0e469f16",
618
+ "metadata": {
619
+ "id": "7575bd09-2d20-49ae-bb10-162a0e469f16"
620
+ },
621
+ "outputs": [],
622
+ "source": [
623
+ "def automatic_purpose_driven_chat_completion(system_prompt, translation_input, description_json):\n",
624
+ " \"\"\"\n",
625
+ " Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
626
+ " \"\"\"\n",
627
+ "\n",
628
+ " prompt = f\"\"\"Given the following descriptive json translate <source text> from English to Arabic\n",
629
+ "{description_json}\n",
630
+ "<source text>\n",
631
+ "{translation_input}\n",
632
+ "</source text>\n",
633
+ "Translation:\n",
634
+ "\"\"\"\n",
635
+ " messages = [\n",
636
+ " {\"role\": \"system\", \"content\": system_prompt},\n",
637
+ " {\n",
638
+ " \"role\": \"user\",\n",
639
+ " \"content\": prompt,\n",
640
+ " },\n",
641
+ " ]\n",
642
+ " return client.chat_completion(messages, max_tokens=10_000)"
643
+ ]
644
+ },
645
+ {
646
+ "cell_type": "code",
647
+ "execution_count": null,
648
+ "id": "32bccca0-c866-4006-84d1-d5b783b73689",
649
+ "metadata": {
650
+ "tags": [],
651
+ "id": "32bccca0-c866-4006-84d1-d5b783b73689"
652
+ },
653
+ "outputs": [],
654
+ "source": [
655
+ "response = automatic_purpose_driven_chat_completion(system_prompt, translation_input, description_json)"
656
+ ]
657
+ },
658
+ {
659
+ "cell_type": "code",
660
+ "execution_count": null,
661
+ "id": "b0867efb-39ea-4f9a-b073-2a84261f3821",
662
+ "metadata": {
663
+ "tags": [],
664
+ "id": "b0867efb-39ea-4f9a-b073-2a84261f3821",
665
+ "outputId": "485ece36-c47e-4d1d-cf50-bbe1a9960776"
666
+ },
667
+ "outputs": [
668
+ {
669
+ "data": {
670
+ "text/plain": [
671
+ "311"
672
+ ]
673
+ },
674
+ "execution_count": 21,
675
+ "metadata": {},
676
+ "output_type": "execute_result"
677
+ }
678
+ ],
679
+ "source": [
680
+ "automatic_purpose_driven_tokens = response.usage.prompt_tokens\n",
681
+ "automatic_purpose_driven_tokens"
682
+ ]
683
+ },
684
+ {
685
+ "cell_type": "code",
686
+ "execution_count": null,
687
+ "id": "462ca84c-9ffd-4924-880d-e06b724caf02",
688
+ "metadata": {
689
+ "tags": [],
690
+ "id": "462ca84c-9ffd-4924-880d-e06b724caf02",
691
+ "outputId": "bf026405-eb15-4a28-8357-d99e174967e7"
692
+ },
693
+ "outputs": [
694
+ {
695
+ "name": "stdout",
696
+ "output_type": "stream",
697
+ "text": [
698
+ "Here is the translated text from English to Arabic:\n",
699
+ "\n",
700
+ "{\"افتراضات متعلقة بالمحتوى\": \"يفترض النص أن القراء يفهمون مفهوم مجموعات أصحاب المصلحة وأولوياتهم المختلفة المحتملة فيما يتعلق بالقضايا البيئية والاجتماعية.\", \"الجمهور\": \"الجمهور المستهدف لهذا النص هو على الأرجح أفراد أو منظمات تعمل في مجالات الاستدامة أو الإيكولوجيا أو الرعاية الاجتماعية، بالإضافة إلى أصحاب المصلحة المهتمين بهذه المجالات.\", \"الغرض\": \"الغرض من هذا النص هو تسليط الضوء على الملاحظات التي أبديت حول النقاشات التي تنشأ عندما تحدد مجموعات أصحاب المصلحة رؤيتها، خاصة فيما يتعلق بالأولوية بين صحة النظام البيئي ورفاهية الإنسان، وكيف تؤثر هذه الأولوية على تقييمها للنتائج الإيكولوجية والاجتماعية المرجوة.\", \"الموضوع\": \"النقاشات حول أولويات النظام البيئي مقابل رفاهية الإنسان من قبل مجموعات أصحاب المصلحة\": \"لقد لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد الرؤى، فإن ذلك يؤدي إلى نقاش حول ما إذا كان ينبغي التركيز على صحة النظام البيئي أو رفاهية الإنسان... إن أولوية النظم البيئية أو البشر تؤثر بشكل كبير على تقييم أصحاب المصلحة للوضع الاجتماعي والإيكولوجي المرغوب.\"}\n"
701
+ ]
702
+ }
703
+ ],
704
+ "source": [
705
+ "print(response.choices[0].message.content)"
706
+ ]
707
+ },
708
+ {
709
+ "cell_type": "markdown",
710
+ "id": "1ec3b20b-8393-4fda-a51d-cf67984cc166",
711
+ "metadata": {
712
+ "id": "1ec3b20b-8393-4fda-a51d-cf67984cc166"
713
+ },
714
+ "source": [
715
+ "# Push to the hub"
716
+ ]
717
+ },
718
+ {
719
+ "cell_type": "code",
720
+ "execution_count": null,
721
+ "id": "6fc55725-216f-45dd-9c6d-dae77e16d606",
722
+ "metadata": {
723
+ "tags": [],
724
+ "id": "6fc55725-216f-45dd-9c6d-dae77e16d606",
725
+ "outputId": "d3b9b994-e3af-4f05-88b4-3b29778d1dc7"
726
+ },
727
+ "outputs": [
728
+ {
729
+ "data": {
730
+ "text/plain": [
731
+ "CommitInfo(commit_url='https://huggingface.co/arabic-translation-prompt-engineering/atpe-notebooks/commit/d112cc49a99a72106b75c3eae9be453264fb488d', commit_message='Upload baseline.ipynb with huggingface_hub', commit_description='', oid='d112cc49a99a72106b75c3eae9be453264fb488d', pr_url=None, pr_revision=None, pr_num=None)"
732
+ ]
733
+ },
734
+ "execution_count": 23,
735
+ "metadata": {},
736
+ "output_type": "execute_result"
737
+ }
738
+ ],
739
+ "source": [
740
+ "from huggingface_hub import HfApi\n",
741
+ "\n",
742
+ "api = HfApi()\n",
743
+ "api.upload_file(\n",
744
+ " path_or_fileobj=\"baseline.ipynb\",\n",
745
+ " path_in_repo=\"baseline.ipynb\",\n",
746
+ " repo_id=\"arabic-translation-prompt-engineering/atpe-notebooks\",\n",
747
+ " repo_type=\"model\",\n",
748
+ ")"
749
+ ]
750
+ },
751
+ {
752
+ "cell_type": "code",
753
+ "execution_count": null,
754
+ "id": "6f21f4cf-1872-4934-bb91-8aef311ed729",
755
+ "metadata": {
756
+ "id": "6f21f4cf-1872-4934-bb91-8aef311ed729"
757
+ },
758
+ "outputs": [],
759
+ "source": []
760
  }
761
+ ],
762
+ "metadata": {
763
+ "kernelspec": {
764
+ "display_name": "Python 3 (ipykernel)",
765
+ "language": "python",
766
+ "name": "python3"
767
+ },
768
+ "language_info": {
769
+ "codemirror_mode": {
770
+ "name": "ipython",
771
+ "version": 3
772
+ },
773
+ "file_extension": ".py",
774
+ "mimetype": "text/x-python",
775
+ "name": "python",
776
+ "nbconvert_exporter": "python",
777
+ "pygments_lexer": "ipython3",
778
+ "version": "3.10.9"
779
+ },
780
+ "colab": {
781
+ "provenance": []
 
 
 
 
 
 
 
 
 
782
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
783
  },
784
+ "nbformat": 4,
785
+ "nbformat_minor": 5
786
+ }