From 429e4e405cded298f556d827083aecfee9a9034f Mon Sep 17 00:00:00 2001 From: nowadays0421 Date: Sat, 3 Jun 2023 22:57:49 +0800 Subject: [PATCH] =?UTF-8?q?system=20=E7=AC=AC=E4=BA=8C=E7=AB=A0=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E4=B8=80=E4=BA=9B=E6=96=87=E5=AD=97=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...e Models, the Chat Format and Tokens.ipynb | 73 ++++++++++++++----- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/content/Building Systems with the ChatGPT API/2.Language Models, the Chat Format and Tokens.ipynb b/content/Building Systems with the ChatGPT API/2.Language Models, the Chat Format and Tokens.ipynb index b4451be..eaf59f1 100644 --- a/content/Building Systems with the ChatGPT API/2.Language Models, the Chat Format and Tokens.ipynb +++ b/content/Building Systems with the ChatGPT API/2.Language Models, the Chat Format and Tokens.ipynb @@ -1,6 +1,7 @@ { "cells": [ { + "attachments": {}, "cell_type": "markdown", "id": "3e71eee8", "metadata": {}, @@ -9,6 +10,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "c7e800b0", "metadata": {}, @@ -92,7 +94,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 1, "id": "b8b8f1c7", "metadata": {}, "outputs": [], @@ -104,10 +106,11 @@ "# from dotenv import load_dotenv, find_dotenv\n", "# _ = load_dotenv(find_dotenv()) # 读取本地的.env环境文件\n", "\n", - "openai.api_key = 'sk-xxxxxxxxxxxx' #更换成你自己的key" + "openai.api_key = '***' #更换成你自己的key" ] }, { + "attachments": {}, "cell_type": "markdown", "id": "19cf84a8", "metadata": {}, @@ -118,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 2, "id": "3fda8d03", "metadata": {}, "outputs": [], @@ -136,6 +139,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "536ad0c2", "metadata": {}, @@ -236,6 +240,19 @@ "![image-2.png](attachment:image-2.png)" ] }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "c06944b4", + "metadata": {}, + "source": [ + "对于英文输入,一个 token 一般对应4个字符或者四分之三个单词;对于中文输入,一个 token 一般对应一个或半个词。\n", + "\n", + "不同模型有不同的 token 限制,需要注意的是,这里的 token 限制是输入的 prompt 和输出的 completion 的 token 数之和,因此输入的 prompt 越长,能输出的 completion 的上限就越低。\n", + "\n", + "ChatGPT3.5 的 token 上限是 4096。" + ] + }, { "attachments": { "image.png": { @@ -252,13 +269,23 @@ "![image.png](attachment:image.png)" ] }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "ffafb0f1", + "metadata": {}, + "source": [ + "System 信息用于制定模型的规则,例如设定、回答准则一类的,而 assistant 信息就是具体让模型完成的指令" + ] + }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "id": "bd009d67", "metadata": {}, "outputs": [], "source": [ + "# 支持更多参数自定义的封装函数\n", "def get_completion_from_messages(messages, \n", " model=\"gpt-3.5-turbo\", \n", " temperature=0, \n", @@ -274,7 +301,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "id": "e09def17", "metadata": {}, "outputs": [ @@ -282,20 +309,25 @@ "name": "stdout", "output_type": "stream", "text": [ - "在海洋深处有一个小鲸鱼,\n", - "它快乐的游啊游,\n", - "它的身体白晃晃,\n", - "好像穿着白雪的衣裳。\n", + "在蓝色的海洋深处,\n", + "有一只小鲸鱼嬉闹着。\n", + "它游啊游,欢笑着,\n", + "尽情享受自由的感受。\n", "\n", - "它游来游去,玩得很开心,\n", - "偶尔还秀一下游泳技能,\n", - "小鲸鱼是个聪明的孩子,\n", - "它让波涛跟着它摇晃。\n", + "它跳啊跃,翻滚着,\n", + "好像永远都停不下来。\n", + "它的身体,是那么轻盈,\n", + "好像没有任何重量。\n", "\n", - "快乐的小鲸鱼,\n", - "带给海洋欢乐的音符,\n", - "我们也跟着它笑起来,\n", - "真是一个美好的夏日午后。\n" + "它的笑声,犹如海浪,\n", + "响彻于大海深处。\n", + "它的快乐,也如海浪,\n", + "绵延不绝,没有尽头。\n", + "\n", + "啊,这可爱的小鲸鱼,\n", + "让人忍不住想嬉闹。\n", + "让我们跟它一起游,\n", + "在蓝色的海洋里畅游。\n" ] } ], @@ -364,7 +396,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "id": "b0907cac", "metadata": {}, "outputs": [], @@ -385,8 +417,11 @@ " content = response.choices[0].message[\"content\"]\n", " \n", " token_dict = {\n", + "# prompt 对应的 token 数\n", "'prompt_tokens':response['usage']['prompt_tokens'],\n", + "# completion 对应的 token 数\n", "'completion_tokens':response['usage']['completion_tokens'],\n", + "# 总共的 token 数\n", "'total_tokens':response['usage']['total_tokens'],\n", " }\n", "\n", @@ -475,7 +510,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.10.11" } }, "nbformat": 4,