{ "cells": [ { "cell_type": "markdown", "id": "883e3354-1538-4f98-bf42-67552215bba3", "metadata": { "id": "883e3354-1538-4f98-bf42-67552215bba3" }, "source": [ "# Setup" ] }, { "cell_type": "code", "execution_count": null, "id": "ddb3dfde-39cc-4ad9-917e-48413add2d9b", "metadata": { "tags": [] }, "outputs": [], "source": [ "%pip install -U -q transformers huggingface-hub" ] }, { "cell_type": "code", "execution_count": 1, "id": "b45bd52f-03e9-419f-8110-1013ff45fb1b", "metadata": { "id": "b45bd52f-03e9-419f-8110-1013ff45fb1b", "tags": [] }, "outputs": [], "source": [ "from huggingface_hub import InferenceClient, login\n", "from transformers import AutoTokenizer" ] }, { "cell_type": "code", "execution_count": 2, "id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e", "metadata": { "colab": { "referenced_widgets": [ "515c96c357454fdc9a38ecc995ff1b3d" ] }, "id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e", "outputId": "cb57d6e8-7d61-49ca-ec60-05594a7d5842", "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "da0ae7fafffb4005a5325a53896feb82", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value='
\n", "
\n", " \n", "
\n", "
\n", " Warning: You will need to point to a model/deployment that is running.\n", "
\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "84e6cb89-30d3-4ef5-8063-07783798e045", "metadata": { "id": "84e6cb89-30d3-4ef5-8063-07783798e045", "tags": [] }, "outputs": [], "source": [ "MODEL = \"CohereForAI/c4ai-command-r-plus\"\n", "tokenizer = AutoTokenizer.from_pretrained(MODEL, use_fast=True)\n", "client = InferenceClient(MODEL)" ] }, { "cell_type": "markdown", "id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb", "metadata": { "id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb" }, "source": [ "# Translation\n", "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", "\n", "We have created a dataset [arabic-translation-prompt-engineering/TpDwD](https://huggingface.co/datasets/arabic-translation-prompt-engineering/TpDwD) across 6 domains and want to compare each method by having human rankers. We also have human translations to ground these rankings.\n", "\n", "We will evaluate the following methods:\n", "- Baseline\n", "- Manual Purpose Driven\n", "- Automatic Purpose Driven\n", "- Automatic Motivation Driven" ] }, { "cell_type": "markdown", "id": "c5bee77b-da1c-43b3-ab14-d15e871f7502", "metadata": { "id": "c5bee77b-da1c-43b3-ab14-d15e871f7502" }, "source": [ "## Baseline\n", "\n", "For our baseline we will translate with a simple system prompt and instruction." ] }, { "cell_type": "markdown", "id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f", "metadata": { "id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f" }, "source": [ "### System Prompt\n", "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", "\n", "> 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." ] }, { "cell_type": "code", "execution_count": 4, "id": "032c86d2-868e-4fa6-b03e-58f1c41434cc", "metadata": { "id": "032c86d2-868e-4fa6-b03e-58f1c41434cc", "tags": [] }, "outputs": [], "source": [ "baseline_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.\"\"\"" ] }, { "cell_type": "markdown", "id": "803ddeba-03de-4f13-95d1-5fb097058cf2", "metadata": { "id": "803ddeba-03de-4f13-95d1-5fb097058cf2" }, "source": [ "### Instruction\n", "> Translate this from english to arabic: {translation_input}.\n", ">\n", "> Translation:\n", "\n", "We will use a simple instruction to get a translation." ] }, { "cell_type": "code", "execution_count": 5, "id": "b7f1722c-c484-4e22-a025-53f95943fc76", "metadata": { "id": "b7f1722c-c484-4e22-a025-53f95943fc76", "tags": [] }, "outputs": [], "source": [ "def baseline_chat_completion(translation_input):\n", " \"\"\"\n", " Generates a completion for a chat conversation using a specified system prompt and a user input.\n", " \"\"\"\n", " messages = [\n", " {\"role\": \"system\", \"content\": baseline_system_prompt},\n", " {\n", " \"role\": \"user\",\n", " \"content\": f\"Translate this from english to arabic: {translation_input}.\\nTranslation: \",\n", " },\n", " ]\n", " return client.chat_completion(messages, max_tokens=10_000)" ] }, { "cell_type": "code", "execution_count": 6, "id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e", "metadata": { "id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e", "tags": [] }, "outputs": [], "source": [ "translation_input = \"Float like a butterfly sting like a bee – his hands can’t hit what his eyes can’t see.\"\n", "response = baseline_chat_completion(\n", " translation_input,\n", ")" ] }, { "cell_type": "markdown", "id": "2bca574c-461d-4822-b0dd-b12a3b9846b3", "metadata": { "id": "2bca574c-461d-4822-b0dd-b12a3b9846b3" }, "source": [ "### Token Cost\n", "Here we can see that the cost is quite cheap, only 96 tokens!" ] }, { "cell_type": "code", "execution_count": 7, "id": "2afc890f-5d8d-4df8-b19a-25888211cf18", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Baseline Total Prompt tokens: 96'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f\"Baseline Total Prompt tokens: {response.usage.prompt_tokens - len(tokenizer(translation_input, return_tensors='pt')['input_ids'][0])}\"" ] }, { "cell_type": "code", "execution_count": 8, "id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3", "metadata": { "id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3", "outputId": "0d7417e1-b648-40aa-eba7-5f1b0dc962b3", "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "يسبح في الحلبة كالفراشة ويلسع كالنحلة - لا يمكن ليديه أن تصيبا ما لا تستطيع عيناه رؤيته\n" ] } ], "source": [ "print(response.choices[0].message.content)" ] }, { "cell_type": "markdown", "id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715", "metadata": { "id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715" }, "source": [ "## Manual Purpose Driven Translation\n", "\n", "[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", "\n", "### Prompt\n", "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", "\n", "> Translate the following English [source text] into Arabic. Please fulfill the following conditions when translating. \n", "> Purpose of the translation: `` \n", "> Target audience: `` \n", "> [source text] `{translation_input}` \n", "> [translated text]\n", "\n", "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", "\n", "Lets go ahead and create these for each of our datasets." ] }, { "cell_type": "code", "execution_count": 9, "id": "4714f6a2-fd0b-48ee-80bc-860f40ee2baa", "metadata": { "id": "4714f6a2-fd0b-48ee-80bc-860f40ee2baa", "tags": [] }, "outputs": [], "source": [ "dataset_to_purpose_target = {\n", " \"ELRC-24ss\": {\n", " \"purpose\": \"Enhancing understanding and knowledge about COVID-19 and health-related topics.\",\n", " \"audience\": \"Individuals seeking reliable and comprehensible information about COVID-19 and related health topics.\",\n", " },\n", " \"GNOME-25ss\": {\n", " \"purpose\": \"Facilitating localization and translation of GNOME software.\",\n", " \"audience\": \"Translators and developers working on GNOME projects.\"\n", " },\n", " \"HPLT-25ss\": {\n", " \"purpose\": \"Providing multilingual data for high-performance language technologies.\",\n", " \"audience\": \"Researchers and developers working on multilingual NLP applications.\"\n", " },\n", " \"OpenSubtitles-25ss\": {\n", " \"purpose\": \"Creating parallel corpora from movie and TV subtitles.\",\n", " \"audience\": \"Researchers and developers in NLP and machine translation. And Movies and TV Shows translators\"\n", " },\n", " \"TED2020-25ss\": {\n", " \"purpose\": \"Generating multilingual sentence embeddings using TED transcripts.\",\n", " \"audience\": \"Researchers and developers working on multilingual sentence embeddings.\"\n", " },\n", " \"UNPC-24ss\": {\n", " \"purpose\": \"Offering a parallel corpus of United Nations documents for linguistic research.\",\n", " \"audience\": \"Researchers and linguists studying multilingual and legal texts.\"\n", " }\n", "}\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "d0c3418e-0b87-458f-8517-1ca3e59ab57a", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Define the translation tool function\n", "purpose_driven_translation_tools = [\n", " {\n", " \"type\": \"function\",\n", " \"function\": {\n", " \"name\": \"purpose_driven_translation\",\n", " \"description\": \"Translate given the purpose and the target audience.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"translation\": {\n", " \"type\": \"string\",\n", " \"description\": \"The translated \\\"source_text\\\".\",\n", " },\n", " },\n", " \"required\": [\"translation\"],\n", " },\n", " },\n", " }\n", "]\n", "\n", "# Create the purpose-driven chat completion function using function calling\n", "def purpose_driven_chat_completion(translation_input, dataset):\n", " \"\"\"\n", " Generates a completion for a chat conversation using a specified system prompt and a user input,\n", " incorporating function calling to retrieve translation context.\n", " \"\"\"\n", " \n", " # Prepare the prompt\n", " prompt = f\"\"\"Translate the English \"source text\" into Arabic. Please fulfill the \"Purpose of the translation\" and tailor it to the \"target audience\". Respond in a json format with just the translation as the key.\n", "{{\n", " \"Purpose of the translation\": \"{dataset_to_purpose_target[dataset]['purpose']}\"\n", " \"Target audience\": \"{dataset_to_purpose_target[dataset]['audience']}\"\n", " \"source text\" `{translation_input}`\n", "}} \n", "Translation json: \"\"\"\n", "\n", " # Initial messages, including the function call to get context\n", " messages = [\n", " {\"role\": \"system\", \"content\": baseline_system_prompt},\n", " {\n", " \"role\": \"user\",\n", " \"content\": prompt,\n", " },\n", " ]\n", "\n", " \n", " # Call the chat completion API with the function tools and specific tool choice\n", " return client.chat_completion(messages, max_tokens=10_000, tools=purpose_driven_translation_tools, tool_choice='purpose_driven_translation')" ] }, { "cell_type": "code", "execution_count": 11, "id": "4115515d-cbcd-405a-b2e0-a805880a40c4", "metadata": { "id": "4115515d-cbcd-405a-b2e0-a805880a40c4", "tags": [] }, "outputs": [], "source": [ "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", "response = purpose_driven_chat_completion(translation_input, \"ELRC-24ss\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "51cc3241-ef6d-43e8-8740-defb6f542918", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Manual Purpose Driven Total Prompt tokens: 350'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f\"Manual Purpose Driven Total Prompt tokens: {response.usage.prompt_tokens - len(tokenizer(translation_input, return_tensors='pt')['input_ids'][0])}\"" ] }, { "cell_type": "code", "execution_count": 13, "id": "1f1c6dd0-11bf-4b88-9029-8bce1e7bcb1c", "metadata": { "id": "1f1c6dd0-11bf-4b88-9029-8bce1e7bcb1c", "outputId": "a12fc058-249f-4493-eb3e-f021c12651dc", "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'translation': 'لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد ... '\n", " 'الرؤى، فإن هذا يؤدي إلى نقاش حول ما إذا كان ينبغي التركيز على '\n", " 'صحة النظام البيئي أو رفاهية الإنسان ... إن مسألة ما إذا كان '\n", " 'الأولوية للنظم البيئية أو الناس تؤثر بشكل كبير على تقييم '\n", " 'أصحاب المصلحة للحالات الاجتماعية والبيئية المرغوبة.'}\n" ] } ], "source": [ "from pprint import pprint\n", "description_json = response.choices[0].message.tool_calls[0].function.arguments\n", "pprint(description_json)" ] }, { "cell_type": "markdown", "id": "898a909e-88a2-4efb-99ed-64ceee317037", "metadata": { "id": "898a909e-88a2-4efb-99ed-64ceee317037" }, "source": [ "## Automatic Purpose Driven Structured Generation Translation\n", "\n", "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)" ] }, { "cell_type": "markdown", "id": "8aca3847-7f3d-43d3-8e80-0c7e2282073b", "metadata": { "id": "8aca3847-7f3d-43d3-8e80-0c7e2282073b" }, "source": [ "## Instruction" ] }, { "cell_type": "markdown", "id": "2f5deb21-18fb-4c5b-9045-c7fe5e751c05", "metadata": { "id": "2f5deb21-18fb-4c5b-9045-c7fe5e751c05" }, "source": [ "Its usually helpful if we tell the LLM what we want to create when we prompt it. \n", "\n", "> ```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", "{\"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}.\n", "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", "{\n", " \"source_text\": {translation_input}\n", "}```\n", "\n" ] }, { "cell_type": "markdown", "id": "55f02988-1c63-4f03-a5a6-ca86c83b3c17", "metadata": { "id": "55f02988-1c63-4f03-a5a6-ca86c83b3c17" }, "source": [ "## Tool Definition" ] }, { "cell_type": "code", "execution_count": 14, "id": "affe3668-aa37-47b5-be37-a0bd5dabab56", "metadata": { "id": "affe3668-aa37-47b5-be37-a0bd5dabab56", "tags": [] }, "outputs": [], "source": [ "automatic_purpose_driven_translation_tools = [\n", " {\n", " \"type\": \"function\",\n", " \"function\": {\n", " \"name\": \"get_translation_audience_purpose\",\n", " \"description\": \"Get the background of a text to assist in translation\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"subject\": {\n", " \"type\": \"string\",\n", " \"description\": \"The topic or central theme that the text revolves around.\",\n", " },\n", " \"assumptions relating to the content\": {\n", " \"type\": \"string\",\n", " \"description\": \"Write out any assumptions relating to the text.\",\n", " },\n", " \"purpose\": {\n", " \"type\": \"string\",\n", " \"description\": \"Why the text was written\",\n", " },\n", " \"audience\": {\n", " \"type\": \"string\",\n", " \"description\": \"The inferred audience that the text is written for.\",\n", " },\n", " },\n", " \"required\": [\n", " \"subject\",\n", " \"assumptions relating to the content\",\n", " \"purpose\",\n", " \"audience\",\n", " ],\n", " },\n", " },\n", " }\n", "]" ] }, { "cell_type": "code", "execution_count": 15, "id": "b6436ce6-03af-4206-a283-0c2ecd17bd88", "metadata": { "id": "b6436ce6-03af-4206-a283-0c2ecd17bd88", "tags": [] }, "outputs": [], "source": [ "def tool_call_chat_completion(translation_input):\n", " \"\"\"\n", " Generates a completion for a chat conversation using a specified system prompt and a user input.\n", " \"\"\"\n", "\n", " 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", "{{\"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}}.\n", "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", "{{\n", " \"source_text\": {translation_input}\n", "}}\n", "\"\"\"\n", " messages = [\n", " {\"role\": \"system\", \"content\": baseline_system_prompt},\n", " {\n", " \"role\": \"user\",\n", " \"content\": prompt,\n", " },\n", " ]\n", " return client.chat_completion(messages, max_tokens=10_000, tools=automatic_purpose_driven_translation_tools, tool_choice='get_translation_audience_purpose')" ] }, { "cell_type": "code", "execution_count": 16, "id": "a731b2c0-54a3-4b8e-83f6-1663c759cf79", "metadata": { "id": "a731b2c0-54a3-4b8e-83f6-1663c759cf79", "tags": [] }, "outputs": [], "source": [ "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", "response = tool_call_chat_completion(translation_input)" ] }, { "cell_type": "code", "execution_count": 17, "id": "f87110a2-a40b-4d65-a12d-728dbdda8fbe", "metadata": { "id": "07e0f133-f6e1-4bc6-969c-da9d36bfba2f", "outputId": "82e2506a-7336-4909-b8e4-fc52f671c511", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Function Calling Prompt tokens: 406'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f\"Function Calling Prompt tokens: {response.usage.prompt_tokens - len(tokenizer(translation_input, return_tensors='pt')['input_ids'][0])}\"" ] }, { "cell_type": "code", "execution_count": 18, "id": "02be1827-7137-463f-a026-0b26dec6f552", "metadata": { "id": "02be1827-7137-463f-a026-0b26dec6f552", "outputId": "60cd7be7-b6ac-412b-e0bc-3b8d3088f2b2", "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'assumptions relating to the content': 'The source text assumes that there is '\n", " 'a debate between ecological health '\n", " 'and human well-being, and that '\n", " 'stakeholders have different '\n", " 'priorities that influence their '\n", " 'assessment of desirable ecological '\n", " 'and social outcomes.',\n", " 'audience': 'Individuals interested in environmental policy, ecology, '\n", " 'sustainability, and/or stakeholder engagement.',\n", " 'purpose': 'To communicate observations about the varying priorities of '\n", " 'different stakeholder groups and how these priorities impact '\n", " 'their definition of vision, particularly in the context of '\n", " 'ecosystem health versus human well-being.',\n", " 'subject': 'Stakeholder priorities and their impact on defining visions '\n", " 'related to ecological and social outcomes.'}\n" ] } ], "source": [ "from pprint import pprint\n", "description_json = response.choices[0].message.tool_calls[0].function.arguments\n", "pprint(description_json)" ] }, { "cell_type": "code", "execution_count": 19, "id": "7575bd09-2d20-49ae-bb10-162a0e469f16", "metadata": { "id": "7575bd09-2d20-49ae-bb10-162a0e469f16", "tags": [] }, "outputs": [], "source": [ "def automatic_purpose_driven_chat_completion(translation_input, description_json):\n", " \"\"\"\n", " Generates a completion for a chat conversation using a specified system prompt and a user input.\n", " \"\"\"\n", "\n", " prompt = f\"\"\"Given the following description translate source_text from English to Arabic\n", "{{\n", " \"description\": {description_json},\n", " \"translation\": {translation_input}\n", "}}\n", "Translation:\n", "\"\"\"\n", " messages = [\n", " {\"role\": \"system\", \"content\": baseline_system_prompt},\n", " {\"role\": \"user\", \"content\": prompt},\n", " ]\n", " return client.chat_completion(messages, max_tokens=10_000)" ] }, { "cell_type": "code", "execution_count": 20, "id": "32bccca0-c866-4006-84d1-d5b783b73689", "metadata": { "id": "32bccca0-c866-4006-84d1-d5b783b73689", "tags": [] }, "outputs": [], "source": [ "response = automatic_purpose_driven_chat_completion(translation_input, description_json)" ] }, { "cell_type": "code", "execution_count": 21, "id": "b0867efb-39ea-4f9a-b073-2a84261f3821", "metadata": { "id": "b0867efb-39ea-4f9a-b073-2a84261f3821", "outputId": "485ece36-c47e-4d1d-cf50-bbe1a9960776", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Automatic Purpose Driven Total Prompt tokens: 235'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f\"Automatic Purpose Driven Total Prompt tokens: {response.usage.prompt_tokens - len(tokenizer(translation_input, return_tensors='pt')['input_ids'][0])}\"" ] }, { "cell_type": "code", "execution_count": 22, "id": "462ca84c-9ffd-4924-880d-e06b724caf02", "metadata": { "id": "462ca84c-9ffd-4924-880d-e06b724caf02", "outputId": "bf026405-eb15-4a28-8357-d99e174967e7", "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"description\": {\n", " \"الافتراضات المتعلقة بالمحتوى\": \"يفترض النص المصدري وجود نقاش بين الصحة البيئية ورفاهية الإنسان، وأن أصحاب المصلحة لديهم أولويات مختلفة تؤثر على تقييمهم للنتائج البيئية والاجتماعية المرجوة.\",\n", " \"الجمهور\": \"الأفراد المهتمون بالسياسة البيئية، أو علم البيئة، أو الاستدامة، و/أو مشاركة أصحاب المصلحة.\",\n", " \"الغرض\": \"إيصال الملاحظات حول الأولويات المتنوعة لمجموعات أصحاب المصلحة المختلفة، وكيف تؤثر هذه الأولويات على تعريفهم للرؤى، خاصة في سياق صحة الأنظمة البيئية مقابل رفاهية الإنسان.\",\n", " \"الموضوع\": \"أولويات أصحاب المصلحة وتأثيرها على تحديد الرؤى المتعلقة بالنتائج البيئية والاجتماعية.\"\n", " },\n", " \"الترجمة\": \"لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد ... الرؤى، فإن هذا يؤدي إلى نقاش حول ما إذا كان ينبغي التأكيد على صحة النظام البيئي أو رفاهية الإنسان ... سواء كانت الأولوية للنظم البيئية أو للبشر يؤثر بشكل كبير على تقييم أصحاب المصلحة للحالات البيئية والاجتماعية المرغوبة.\"\n", "}\n" ] } ], "source": [ "print(response.choices[0].message.content)" ] }, { "cell_type": "markdown", "id": "28a0e518-358e-44f0-97a4-ea76a5563743", "metadata": {}, "source": [ "### Helper Function" ] }, { "cell_type": "code", "execution_count": 23, "id": "7fbf9e01-23d9-41b0-a1e3-fd7a99c55bd0", "metadata": { "tags": [] }, "outputs": [], "source": [ "def automatic_purpose_driven_chat(translation_input):\n", " response = tool_call_chat_completion(translation_input)\n", " description_json = response.choices[0].message.tool_calls[0].function.arguments\n", " return automatic_purpose_driven_chat_completion(translation_input, description_json)" ] }, { "cell_type": "code", "execution_count": 24, "id": "8c5dd9e6-44c1-4ac7-92ef-c2e594b7b91d", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'الافتراضات المتعلقة بالمحتوى: لا توجد افتراضات محددة.\\n\\nالجمهور المستهدف: جمهور عام لا يحتاج إلى معرفة تقنية محددة.\\n\\nالغرض: نقل رسالة بسيطة لاختبار الترجمة.\\n\\nالموضوع: اختبار الترجمة\\n\\nالترجمة: هذا اختبار'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "automatic_purpose_driven_chat(\"This is a test\").choices[0].message.content" ] }, { "cell_type": "markdown", "id": "4abf1aeb-ee4e-4b8f-97c3-e6b1664ac8b8", "metadata": { "id": "1ec3b20b-8393-4fda-a51d-cf67984cc166" }, "source": [ "## Dataset Creation" ] }, { "cell_type": "code", "execution_count": 25, "id": "4b2e3951-6704-43d5-a69b-7587f26e6491", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", "To disable this warning, you can either:\n", "\t- Avoid using `tokenizers` before the fork if possible\n", "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e983537088ae4305ada7aff87127eaa2", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/24 [00:00