From fa365e2768c09e4d8b85be92a14732b9d27e5094 Mon Sep 17 00:00:00 2001 From: Yixin Zhang Date: Sun, 9 Jul 2023 23:59:49 +0800 Subject: [PATCH] =?UTF-8?q?Update=206.=20=E6=96=87=E6=9C=AC=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=20Transforming.ipynb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../6. 文本转换 Transforming.ipynb | 213 +++++++++++++++--- 1 file changed, 183 insertions(+), 30 deletions(-) diff --git a/content/Prompt Engineering/6. 文本转换 Transforming.ipynb b/content/Prompt Engineering/6. 文本转换 Transforming.ipynb index 343afb3..6cbb33d 100644 --- a/content/Prompt Engineering/6. 文本转换 Transforming.ipynb +++ b/content/Prompt Engineering/6. 文本转换 Transforming.ipynb @@ -1,6 +1,7 @@ { "cells": [ { + "attachments": {}, "cell_type": "markdown", "id": "78624add", "metadata": {}, @@ -9,6 +10,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "2fac57c2", "metadata": {}, @@ -19,6 +21,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "f7816496", "metadata": {}, @@ -26,6 +29,20 @@ "首先,我们需要OpenAI包,加载API密钥,定义getCompletion函数。" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "acf125be", + "metadata": {}, + "outputs": [], + "source": [ + "import openai\n", + "# 导入第三方库\n", + "\n", + "openai.api_key = \"sk-...\"\n", + "# 设置 API_KEY, 请替换成您自己的 API_KEY\n" + ] + }, { "cell_type": "code", "execution_count": 7, @@ -33,12 +50,6 @@ "metadata": {}, "outputs": [], "source": [ - "import openai\n", - "import os\n", - "import time\n", - "OPENAI_API_KEY = os.environ.get(\"OPENAI_API_KEY2\")\n", - "openai.api_key = OPENAI_API_KEY\n", - "\n", "def get_completion(prompt, model=\"gpt-3.5-turbo\", temperature=0): \n", " messages = [{\"role\": \"user\", \"content\": prompt}]\n", " response = openai.ChatCompletion.create(\n", @@ -50,6 +61,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "bf3733d4", "metadata": {}, @@ -58,6 +70,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "1b418e32", "metadata": {}, @@ -65,6 +78,21 @@ "**中文转西班牙语**" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "5b521646", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = f\"\"\"\n", + "Translate the following English text to Spanish: \\ \n", + "```Hi, I would like to order a blender```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, { "cell_type": "code", "execution_count": 2, @@ -91,6 +119,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "e3e922b4", "metadata": {}, @@ -98,6 +127,21 @@ "**识别语种**" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "769b6e2e", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = f\"\"\"\n", + "Tell me which language this is: \n", + "```Combien coûte le lampadaire?```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, { "cell_type": "code", "execution_count": 3, @@ -122,6 +166,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "c1841354", "metadata": {}, @@ -129,6 +174,22 @@ "**多语种翻译**" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "a53bc53b", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = f\"\"\"\n", + "Translate the following text to French and Spanish\n", + "and English pirate: \\\n", + "```I want to order a basketball```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, { "cell_type": "code", "execution_count": 4, @@ -156,6 +217,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "68723ba5", "metadata": {}, @@ -163,6 +225,22 @@ "**翻译+正式语气**" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "a4770dcc", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = f\"\"\"\n", + "Translate the following text to Spanish in both the \\\n", + "formal and informal forms: \n", + "'Would you like to order a pillow?'\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, { "cell_type": "code", "execution_count": 6, @@ -188,6 +266,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "b2dc4c56", "metadata": {}, @@ -196,6 +275,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "54b00aa4", "metadata": {}, @@ -219,6 +299,26 @@ "]" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "5cb69e31", + "metadata": {}, + "outputs": [], + "source": [ + "for issue in user_messages:\n", + " prompt = f\"Tell me what language this is: ```{issue}```\"\n", + " lang = get_completion(prompt)\n", + " print(f\"Original message ({lang}): {issue}\")\n", + "\n", + " prompt = f\"\"\"\n", + " Translate the following text to English \\\n", + " and Korean: ```{issue}```\n", + " \"\"\"\n", + " response = get_completion(prompt)\n", + " print(response, \"\\n\")\n" + ] + }, { "cell_type": "code", "execution_count": 8, @@ -276,14 +376,16 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "6ab558a2", "metadata": {}, "source": [ - "## 3 语气/风格调整" + "## 3 语气/ 写作风格调整" ] }, { + "attachments": {}, "cell_type": "markdown", "id": "b85ae847", "metadata": {}, @@ -291,6 +393,21 @@ "写作的语气往往会根据受众对象而有所调整。例如,对于工作邮件,我们常常需要使用正式语气与书面用词,而对同龄朋友的微信聊天,可能更多地会使用轻松、口语化的语气。" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d62ac977", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = f\"\"\"\n", + "Translate the following from slang to a business letter: \n", + "'Dude, This is Joe, check out this spec on this standing lamp.'\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, { "cell_type": "code", "execution_count": 9, @@ -325,6 +442,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "98df9009", "metadata": {}, @@ -333,6 +451,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "0bf9c074", "metadata": {}, @@ -354,6 +473,21 @@ "]}" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "7e904f70", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = f\"\"\"\n", + "Translate the following python dictionary from JSON to an HTML \\\n", + "table with column headers and title: {data_json}\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, { "cell_type": "code", "execution_count": 10, @@ -445,6 +579,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "29b7167b", "metadata": {}, @@ -453,13 +588,14 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "22776140", "metadata": {}, "source": [ - "拼写及语法的检查与纠正是一个十分常见的需求,特别是使用非母语语言,例如发表英文论文时,这是一件十分重要的事情。\n", + "拼写及语法的检查与纠正是一个十分常见的需求,特别是使用非母语语言,例如,在论坛发帖时,或发表英文论文时,校对是一件十分重要的事情。\n", "\n", - "以下给了一个例子,有一个句子列表,其中有些句子存在拼写或语法问题,有些则没有,我们循环遍历每个句子,要求模型校对文本,如果正确则输出“未发现错误”,如果错误则输出纠正后的文本。" + "下述例子给定了一个句子列表,其中有些句子存在拼写或语法问题,有些则没有,我们循环遍历每个句子,要求模型校对文本,如果正确则输出“未发现错误”,如果错误则输出纠正后的文本。" ] }, { @@ -480,6 +616,23 @@ "]" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d48f8d3f", + "metadata": {}, + "outputs": [], + "source": [ + "for t in text:\n", + " prompt = f\"\"\"Proofread and correct the following text\n", + " and rewrite the corrected version. If you don't find\n", + " and errors, just say \"No errors found\". Don't use \n", + " any punctuation around the text:\n", + " ```{t}```\"\"\"\n", + " response = get_completion(prompt)\n", + " print(response)\n" + ] + }, { "cell_type": "code", "execution_count": 16, @@ -515,11 +668,12 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "538181e0", "metadata": {}, "source": [ - "以下是一个简单的类Grammarly纠错示例,输入原始文本,输出纠正后的文本,并基于Redlines输出纠错过程。" + "以下是一个简单的语法纠错示例(译注:与Grammarly功能类似),输入文本为一段关于熊猫玩偶的评价,输出为纠正后的文本。本例使用的prompt较为简单,你也可以进一步要求进行语调的更改。" ] }, { @@ -541,6 +695,18 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "8f3b2341", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = f\"proofread and correct this review: ```{text}```\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, { "cell_type": "code", "execution_count": 14, @@ -562,15 +728,12 @@ ] }, { - "cell_type": "code", - "execution_count": 19, - "id": "51de86c3", + "attachments": {}, + "cell_type": "markdown", + "id": "2e2d1f6a", "metadata": {}, - "outputs": [], "source": [ - "response = \"\"\"\n", - "I got this for my daughter's birthday because she keeps taking mine from my room. Yes, adults also like pandas too. She takes it everywhere with her, and it's super soft and cute. However, one of the ears is a bit lower than the other, and I don't think that was designed to be asymmetrical. It's also a bit smaller than I expected for the price. I think there might be other options that are bigger for the same price. On the bright side, it arrived a day earlier than expected, so I got to play with it myself before giving it to my daughter.\n", - "\"\"\"" + "引入Redlines包,详细显示并对比纠错过程:" ] }, { @@ -612,11 +775,13 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "3ee5d487", "metadata": {}, "source": [ - "## 6 一个综合样例:文本翻译+拼写纠正+风格调整+格式转换" + "## 6 综合样例\n", + "下述例子展示了同一段评论,用一段prompt同时进行文本翻译+拼写纠正+风格调整+格式转换。" ] }, { @@ -640,8 +805,6 @@ }, { "cell_type": "code", -<<<<<<< Updated upstream -======= "execution_count": null, "id": "83235c7b", "metadata": {}, @@ -667,7 +830,6 @@ "id": "4bd30c51", "metadata": {}, "source": [ - "注:以下结果暂时由new bing生成(2023年7月5日,more creative)经校对与原视频输出结构与内容大致相同。\n", "\n", "**Review of Panda Plush Toy**\n", "\n", @@ -690,7 +852,6 @@ }, { "cell_type": "code", ->>>>>>> Stashed changes "execution_count": 24, "id": "5061d6a3", "metadata": { @@ -738,14 +899,6 @@ "response = get_completion(prompt)\n", "display(Markdown(response))" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3d54f151", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {