1 line
22 KiB
Plaintext
1 line
22 KiB
Plaintext
{"cells": [{"cell_type": "markdown", "id": "ae5bcee9-6588-4d29-bbb9-6fb351ef6630", "metadata": {}, "source": ["# \u7b2c\u4e8c\u7ae0 \u8bed\u8a00\u6a21\u578b\uff0c\u63d0\u95ee\u8303\u5f0f\u4e0e Token\n", "\n", " - [\u4e00\u3001\u73af\u5883\u914d\u7f6e](#\u4e00\u3001\u73af\u5883\u914d\u7f6e)\n", " - [1.1 \u52a0\u8f7d API key \u548c\u4e00\u4e9b Python \u7684\u5e93\u3002](#1.1-\u52a0\u8f7d-API-key-\u548c\u4e00\u4e9b-Python-\u7684\u5e93\u3002)\n", " - [1.2 Helper function \u8f85\u52a9\u51fd\u6570](#1.2-Helper-function-\u8f85\u52a9\u51fd\u6570)\n", " - [\u4e8c\u3001\u5c1d\u8bd5\u5411\u6a21\u578b\u63d0\u95ee\u5e76\u5f97\u5230\u7ed3\u679c](#\u4e8c\u3001\u5c1d\u8bd5\u5411\u6a21\u578b\u63d0\u95ee\u5e76\u5f97\u5230\u7ed3\u679c)\n", " - [\u4e09\u3001Tokens](#\u4e09\u3001Tokens)\n", " - [\u56db\u3001Helper function \u8f85\u52a9\u51fd\u6570 (\u63d0\u95ee\u8303\u5f0f)](#\u56db\u3001Helper-function-\u8f85\u52a9\u51fd\u6570-(\u63d0\u95ee\u8303\u5f0f))\n"]}, {"cell_type": "markdown", "id": "baaf0c21", "metadata": {}, "source": ["\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u548c\u60a8\u5206\u4eab\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u7684\u5de5\u4f5c\u539f\u7406\u3001\u8bad\u7ec3\u65b9\u5f0f\u4ee5\u53ca\u5206\u8bcd\u5668\uff08tokenizer\uff09\u7b49\u7ec6\u8282\u5bf9 LLM \u8f93\u51fa\u7684\u5f71\u54cd\u3002\u6211\u4eec\u8fd8\u5c06\u4ecb\u7ecd LLM \u7684\u63d0\u95ee\u8303\u5f0f\uff08chat format\uff09\uff0c\u8fd9\u662f\u4e00\u79cd\u6307\u5b9a\u7cfb\u7edf\u6d88\u606f\uff08system message\uff09\u548c\u7528\u6237\u6d88\u606f\uff08user message\uff09\u7684\u65b9\u5f0f\uff0c\u8ba9\u60a8\u4e86\u89e3\u5982\u4f55\u5229\u7528\u8fd9\u79cd\u80fd\u529b\u3002"]}, {"cell_type": "markdown", "id": "0c797991-8486-4d79-8c1d-5dc0c1289c2f", "metadata": {}, "source": ["## \u4e00\u3001\u73af\u5883\u914d\u7f6e"]}, {"cell_type": "markdown", "id": "e33004b0", "metadata": {}, "source": ["### 1.1 \u52a0\u8f7d API key \u548c\u4e00\u4e9b Python \u7684\u5e93\u3002\n", "\u5728\u672c\u8bfe\u7a0b\u4e2d\uff0c\u4e3a\u60a8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u52a0\u8f7d OpenAI API key \u7684\u4ee3\u7801\u3002"]}, {"cell_type": "code", "execution_count": null, "id": "fddf1a10", "metadata": {}, "outputs": [], "source": ["!pip install openai\n", "!pip install langchain\n", "!pip install --upgrade tiktoken"]}, {"cell_type": "code", "execution_count": 1, "id": "19cd4e96", "metadata": {"height": 132}, "outputs": [], "source": ["import os\n", "import openai\n", "# import tiktoken \u8fd9\u4e2a\u540e\u9762\u6ca1\u7528\u5230\uff0c\u82e5\u60a8\u5bf9\u5176\u7528\u5904\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u53c2\u8003\u672c\u6587\u4ee5\u4e86\u89e3\u76f8\u5173\u5185\u5bb9\uff1ahttps://zhuanlan.zhihu.com/p/629776230\n", "\n", "# from dotenv import load_dotenv, find_dotenv\n", "# _ = load_dotenv(find_dotenv()) # \u8bfb\u53d6\u672c\u5730\u7684.env\u73af\u5883\u6587\u4ef6\u3002\uff08\u63a8\u8350\u540e\u7eed\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5c06 key \u653e\u5728 .env \u6587\u4ef6\u91cc\u3002\u4fdd\u62a4\u81ea\u5df1\u7684 key\uff09\n", "\n", "openai.api_key = 'sk-***' # \u66f4\u6362\u6210\u60a8\u81ea\u5df1\u7684key"]}, {"cell_type": "markdown", "id": "47ba0938-7ca5-46c4-a9d1-b55708d4dc7c", "metadata": {}, "source": ["### 1.2 Helper function \u8f85\u52a9\u51fd\u6570\n", "\u5982\u679c\u4e4b\u524d\u66fe\u53c2\u52a0\u8fc7\u300aChatGPT Prompt Engineering for Developers\u300b\u8bfe\u7a0b\uff0c\u90a3\u4e48\u5bf9\u6b64\u5c31\u76f8\u5bf9\u8f83\u4e3a\u719f\u6089\u3002\n", "\u8c03\u7528\u8be5\u51fd\u6570\u8f93\u5165 Prompt \u5176\u5c06\u4f1a\u7ed9\u51fa\u5bf9\u5e94\u7684 Completion \u3002"]}, {"cell_type": "code", "execution_count": 2, "id": "1ed96988", "metadata": {"height": 149}, "outputs": [], "source": ["# \u5b98\u65b9\u6587\u6863\u5199\u6cd5 https://platform.openai.com/overview\n", "\n", "def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n", " \"\"\"\n", " \u4f7f\u7528 OpenAI \u7684\u6a21\u578b\u751f\u6210\u804a\u5929\u56de\u590d\u3002\n", "\n", " \u53c2\u6570:\n", " prompt: \u7528\u6237\u7684\u8f93\u5165\uff0c\u5373\u804a\u5929\u7684\u63d0\u793a\u3002\n", " model: \u4f7f\u7528\u7684\u6a21\u578b\uff0c\u9ed8\u8ba4\u4e3a\"gpt-3.5-turbo\"\u3002\n", " \"\"\"\n", " messages = [{\"role\": \"user\", \"content\": prompt}]\n", " response = openai.ChatCompletion.create(\n", " model=model,\n", " messages=messages,\n", " temperature=0,\n", " )\n", " return response.choices[0].message[\"content\"] # \u6a21\u578b\u751f\u6210\u7684\u56de\u590d"]}, {"cell_type": "markdown", "id": "fe10a390-2461-447d-bf8b-8498db404c44", "metadata": {}, "source": ["## \u4e8c\u3001\u5c1d\u8bd5\u5411\u6a21\u578b\u63d0\u95ee\u5e76\u5f97\u5230\u7ed3\u679c"]}, {"cell_type": "code", "execution_count": 13, "id": "e1cc57b2", "metadata": {"height": 72}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["The capital of China is Beijing.\n"]}], "source": ["response = get_completion(\"What is the capital of China?\")\n", "print(response)"]}, {"cell_type": "code", "execution_count": 14, "id": "10f34f3b", "metadata": {"height": 64}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u4e2d\u56fd\u7684\u9996\u90fd\u662f\u5317\u4eac\u3002\n"]}], "source": ["response = get_completion(\"\u4e2d\u56fd\u7684\u9996\u90fd\u662f\u54ea\u91cc\uff1f\")\n", "print(response)"]}, {"cell_type": "markdown", "id": "b83d4e38-3e3c-4c5a-a949-040a27f29d63", "metadata": {}, "source": ["## \u4e09\u3001Tokens"]}, {"cell_type": "code", "execution_count": 15, "id": "cc2d9e40", "metadata": {"height": 64}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["The reversed letters of \"lollipop\" are \"pillipol\".\n"]}], "source": ["# \u4e3a\u4e86\u66f4\u597d\u5c55\u793a\u6548\u679c\uff0c\u8fd9\u91cc\u5c31\u6ca1\u6709\u7ffb\u8bd1\u6210\u4e2d\u6587\u7684 Prompt\n", "# \u6ce8\u610f\u8fd9\u91cc\u7684\u5b57\u6bcd\u7ffb\u8f6c\u51fa\u73b0\u4e86\u9519\u8bef\uff0c\u5434\u6069\u8fbe\u8001\u5e08\u6b63\u662f\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\u6765\u89e3\u91ca token \u7684\u8ba1\u7b97\u65b9\u5f0f\n", "response = get_completion(\"Take the letters in lollipop \\\n", "and reverse them\")\n", "print(response)"]}, {"cell_type": "markdown", "id": "9d2b14d0-749d-4a79-9812-7b00ace9ae6f", "metadata": {}, "source": ["\"lollipop\" in reverse should be \"popillol\""]}, {"cell_type": "code", "execution_count": 17, "id": "37cab84f", "metadata": {"height": 88}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["p-o-p-i-l-l-o-l\n"]}], "source": ["response = get_completion(\"\"\"Take the letters in \\\n", "l-o-l-l-i-p-o-p and reverse them\"\"\")\n", "\n", "print(response)"]}, {"cell_type": "markdown", "id": "f5a6cb95", "metadata": {}, "source": [""]}, {"cell_type": "markdown", "id": "8b46bc72", "metadata": {}, "source": ["\u5bf9\u4e8e\u82f1\u6587\u8f93\u5165\uff0c\u4e00\u4e2a token \u4e00\u822c\u5bf9\u5e94 4 \u4e2a\u5b57\u7b26\u6216\u8005\u56db\u5206\u4e4b\u4e09\u4e2a\u5355\u8bcd\uff1b\u5bf9\u4e8e\u4e2d\u6587\u8f93\u5165\uff0c\u4e00\u4e2a token \u4e00\u822c\u5bf9\u5e94\u4e00\u4e2a\u6216\u534a\u4e2a\u8bcd\u3002\n", "\n", "\u4e0d\u540c\u6a21\u578b\u6709\u4e0d\u540c\u7684 token \u9650\u5236\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u7684 token \u9650\u5236\u662f\u8f93\u5165\u7684 Prompt \u548c\u8f93\u51fa\u7684 completion \u7684 token \u6570\u4e4b\u548c\uff0c\u56e0\u6b64\u8f93\u5165\u7684 Prompt \u8d8a\u957f\uff0c\u80fd\u8f93\u51fa\u7684 completion \u7684\u4e0a\u9650\u5c31\u8d8a\u4f4e\u3002\n", "\n", "ChatGPT3.5-turbo \u7684 token \u4e0a\u9650\u662f 4096\u3002"]}, {"cell_type": "markdown", "id": "c8b88940-d3ab-4c00-b5c0-31531deaacbd", "metadata": {}, "source": ["## \u56db\u3001Helper function \u8f85\u52a9\u51fd\u6570 (\u63d0\u95ee\u8303\u5f0f)\n", "\u4e0b\u9762\u662f\u8bfe\u7a0b\u4e2d\u7528\u5230\u7684\u8f85\u52a9\u51fd\u6570\u3002\n", "\u4e0b\u56fe\u662f OpenAI \u63d0\u4f9b\u7684\u4e00\u79cd\u63d0\u95ee\u8303\u5f0f\uff0c\u63a5\u4e0b\u6765\u5434\u6069\u8fbe\u8001\u5e08\u5c31\u662f\u5728\u6f14\u793a\u5982\u4f55\u5229\u7528\u8fd9\u79cd\u8303\u5f0f\u8fdb\u884c\u66f4\u597d\u7684\u63d0\u95ee\n", ""]}, {"cell_type": "markdown", "id": "9e6b6b3d", "metadata": {}, "source": ["System \u4fe1\u606f\u7528\u4e8e\u6307\u5b9a\u6a21\u578b\u7684\u89c4\u5219\uff0c\u4f8b\u5982\u8bbe\u5b9a\u3001\u56de\u7b54\u51c6\u5219\u7b49\uff0c\u800c assistant \u4fe1\u606f\u5c31\u662f\u8ba9\u6a21\u578b\u5b8c\u6210\u7684\u5177\u4f53\u6307\u4ee4"]}, {"cell_type": "code", "execution_count": 5, "id": "8f89efad", "metadata": {"height": 200}, "outputs": [], "source": ["def get_completion_from_messages(messages, \n", " model=\"gpt-3.5-turbo\", \n", " temperature=0, \n", " max_tokens=500):\n", " '''\n", " \u5c01\u88c5\u4e00\u4e2a\u652f\u6301\u66f4\u591a\u53c2\u6570\u7684\u81ea\u5b9a\u4e49\u8bbf\u95ee OpenAI GPT3.5 \u7684\u51fd\u6570\n", "\n", " \u53c2\u6570: \n", " messages: \u8fd9\u662f\u4e00\u4e2a\u6d88\u606f\u5217\u8868\uff0c\u6bcf\u4e2a\u6d88\u606f\u90fd\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b role(\u89d2\u8272\uff09\u548c content(\u5185\u5bb9)\u3002\u89d2\u8272\u53ef\u4ee5\u662f'system'\u3001'user' \u6216 'assistant\u2019\uff0c\u5185\u5bb9\u662f\u89d2\u8272\u7684\u6d88\u606f\u3002\n", " model: \u8c03\u7528\u7684\u6a21\u578b\uff0c\u9ed8\u8ba4\u4e3a gpt-3.5-turbo(ChatGPT)\uff0c\u6709\u5185\u6d4b\u8d44\u683c\u7684\u7528\u6237\u53ef\u4ee5\u9009\u62e9 gpt-4\n", " temperature: \u8fd9\u51b3\u5b9a\u6a21\u578b\u8f93\u51fa\u7684\u968f\u673a\u7a0b\u5ea6\uff0c\u9ed8\u8ba4\u4e3a0\uff0c\u8868\u793a\u8f93\u51fa\u5c06\u975e\u5e38\u786e\u5b9a\u3002\u589e\u52a0\u6e29\u5ea6\u4f1a\u4f7f\u8f93\u51fa\u66f4\u968f\u673a\u3002\n", " max_tokens: \u8fd9\u51b3\u5b9a\u6a21\u578b\u8f93\u51fa\u7684\u6700\u5927\u7684 token \u6570\u3002\n", " '''\n", " response = openai.ChatCompletion.create(\n", " model=model,\n", " messages=messages,\n", " temperature=temperature, # \u8fd9\u51b3\u5b9a\u6a21\u578b\u8f93\u51fa\u7684\u968f\u673a\u7a0b\u5ea6\n", " max_tokens=max_tokens, # \u8fd9\u51b3\u5b9a\u6a21\u578b\u8f93\u51fa\u7684\u6700\u5927\u7684 token \u6570\n", " )\n", " return response.choices[0].message[\"content\"]"]}, {"cell_type": "code", "execution_count": 6, "id": "b28c3424", "metadata": {"height": 183}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["In a garden so bright, a carrot would sprout,\n", "With a cheery orange hue, without a doubt.\n", "With a leafy green top, it danced in the breeze,\n", "A happy carrot, so eager to please.\n", "\n", "It grew in the soil, oh so deep and grand,\n", "Stretching its roots, reaching far and expand.\n", "With a joyful smile, it soaked up the sun,\n", "Growing tall and strong, its journey begun.\n", "\n", "Days turned to weeks, as it grew day and night,\n", "Round and plump, it was quite a delight.\n", "With every raindrop that fell from above,\n", "The carrot grew sweeter, spreading more love.\n", "\n", "At last, the day came when it was time to eat,\n", "With a grin on my face, I took a seat.\n", "I chopped and I sliced, so grateful, you see,\n", "For this happy carrot, bringing joy to me.\n", "\n", "So let us remember, when times may get tough,\n", "A happy carrot's journey, it's enough.\n", "For even in darkness, there's always delight,\n", "Just like a carrot, shining so bright.\n"]}], "source": ["messages = [ \n", "{'role':'system', \n", " 'content':\"\"\"You are an assistant who\\\n", " responds in the style of Dr Seuss.\"\"\"}, \n", "{'role':'user', \n", " 'content':\"\"\"write me a very short poem\\\n", " about a happy carrot\"\"\"}, \n", "] \n", "response = get_completion_from_messages(messages, temperature=1)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 18, "id": "3d0ef08f", "metadata": {"height": 149}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u5728\u6d77\u6d0b\u7684\u6df1\u5904\uff0c\u6709\u4e00\u53ea\u5c0f\u9cb8\u9c7c\uff0c\n", "\u5979\u5feb\u4e50\u53c8\u806a\u660e\uff0c\u7075\u611f\u4ece\u4e0d\u532e\u4e4f\u3002\n", "\u5979\u6e38\u904d\u4e94\u5927\u6d32\uff0c\u63a2\u7d22\u672a\u77e5\u7684\u79d8\u5bc6\uff0c\n", "\u7528\u6b4c\u58f0\u4f20\u9012\u559c\u60a6\uff0c\u4ee4\u4eba\u5fc3\u65f7\u795e\u6021\u3002\n", "\n", "\u5979\u8dc3\u51fa\u6d77\u9762\uff0c\u9ad8\u9ad8\u7684\u98de\u7fd4\uff0c\n", "\u5c3e\u5df4\u62bd\u7a7a\u7740\u6c34\u82b1\uff0c\u50cf\u68a6\u5e7b\u822c\u7684\u753b\u3002\n", "\u5979\u548c\u6d77\u8c5a\u4e00\u8d77\uff0c\u8df3\u8dc3\u5728\u592a\u9633\u4e0b\uff0c\n", "\u4e0e\u6d77\u6d0b\u4e2d\u7684\u751f\u547d\uff0c\u5728\u6b22\u4e50\u4e2d\u5171\u821e\u3002\n", "\n", "\u5979\u559c\u6b22\u548c\u6d77\u9f9f\u4e00\u8d77\uff0c\u7f13\u7f13\u6f2b\u6e38\uff0c\n", "\u770b\u7f8e\u4e3d\u7684\u73ca\u745a\uff0c\u548c\u8272\u5f69\u9c9c\u8273\u7684\u9c7c\u7fa4\u3002\n", "\u5979\u6b22\u8fce\u6bcf\u4e2a\u65b0\u670b\u53cb\uff0c\u65e0\u8bba\u5927\u6216\u5c0f\uff0c\n", "\u56e0\u4e3a\u5728\u5979\u773c\u4e2d\uff0c\u6bcf\u4e2a\u4eba\u90fd\u72ec\u7279\u800c\u73cd\u8d35\u3002\n", "\n", "\u5979\u77e5\u9053\u5feb\u4e50\u662f\u5982\u6b64\u7b80\u5355\uff0c\u5982\u6b64\u5b9d\u8d35\uff0c\n", "\u5728\u6bcf\u4e2a\u65f6\u523b\u4e2d\uff0c\u5979\u90fd\u52aa\u529b\u4f20\u8fbe\u5e78\u798f\u7684\u8868\u60c5\u3002\n", "\u6240\u4ee5\u5f53\u4f60\u611f\u5230\u75b2\u60eb\uff0c\u6cae\u4e27\u6216\u8005\u4f4e\u843d\uff0c\n", "\u60f3\u8d77\u5c0f\u9cb8\u9c7c\u7684\u5feb\u4e50\uff0c\u8ba9\u4f60\u5fc3\u4e2d\u518d\u6b21\u5145\u6ee1\u9c9c\u6d3b\u3002\n"]}], "source": ["messages = [ \n", "{'role':'system', \n", " 'content':'\u4f60\u662f\u4e00\u4e2a\u52a9\u7406\uff0c \u5e76\u4ee5 Seuss \u82cf\u65af\u535a\u58eb\u7684\u98ce\u683c\u4f5c\u51fa\u56de\u7b54\u3002'}, \n", "{'role':'user', \n", " 'content':'\u5c31\u5feb\u4e50\u7684\u5c0f\u9cb8\u9c7c\u4e3a\u4e3b\u9898\u7ed9\u6211\u5199\u4e00\u9996\u77ed\u8bd7'}, \n", "] \n", "response = get_completion_from_messages(messages, temperature=1)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 7, "id": "56c6978d", "metadata": {"height": 183}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Once upon a time, there was a cheerful carrot named Charlie who always brightened everyone's day with his vibrant orange color and contagious laughter.\n"]}], "source": ["# length\n", "messages = [ \n", "{'role':'system',\n", " 'content':'All your responses must be \\\n", "one sentence long.'}, \n", "{'role':'user',\n", " 'content':'write me a story about a happy carrot'}, \n", "] \n", "response = get_completion_from_messages(messages, temperature =1)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 19, "id": "e34c399e", "metadata": {"height": 166}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u5728\u8ffd\u968f\u6ce2\u6d6a\u7684\u8d77\u4f0f\u4e2d\uff0c\u5c0f\u9cb8\u9c7c\u5feb\u4e50\u5730\u8df3\u8dc3\u7740\uff0c\u56e0\u4e3a\u5b83\u77e5\u9053\u6e38\u6cf3\u7684\u771f\u6b63\u4e50\u8da3\u4e0d\u4ec5\u4ec5\u5728\u76ee\u7684\u5730\uff0c\u800c\u662f\u5728\u4e8e\u4eab\u53d7\u6574\u4e2a\u65c5\u7a0b\u3002\n"]}], "source": ["# \u957f\u5ea6\u63a7\u5236\n", "messages = [ \n", "{'role':'system',\n", " 'content':'\u4f60\u7684\u6240\u6709\u7b54\u590d\u53ea\u80fd\u662f\u4e00\u53e5\u8bdd'}, \n", "{'role':'user',\n", " 'content':'\u5199\u4e00\u4e2a\u5173\u4e8e\u5feb\u4e50\u7684\u5c0f\u9cb8\u9c7c\u7684\u6545\u4e8b'}, \n", "] \n", "response = get_completion_from_messages(messages, temperature =1)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 8, "id": "14fd6331", "metadata": {"height": 217}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Once upon a time, there was a carrot so happy and bright, it danced and sang from morning till night.\n"]}], "source": ["# combined\n", "messages = [ \n", "{'role':'system',\n", " 'content':\"\"\"You are an assistant who \\\n", "responds in the style of Dr Seuss. \\\n", "All your responses must be one sentence long.\"\"\"}, \n", "{'role':'user',\n", " 'content':\"\"\"write me a story about a happy carrot\"\"\"},\n", "] \n", "response = get_completion_from_messages(messages, \n", " temperature =1)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 20, "id": "0ca678de", "metadata": {"height": 181}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u5728\u84dd\u8272\u7684\u5927\u6d77\u91cc\uff0c\u6709\u4e00\u53ea\u5c0f\u9cb8\u9c7c\uff0c\u65e0\u5fe7\u65e0\u8651\uff0c\u5feb\u4e50\u6e38\u6cf3\uff0c\u4e00\u5207\u56e0\u5feb\u4e50\u800c\u53d8\u5f97\u5149\u8f89\u3002\n"]}], "source": ["# \u4ee5\u4e0a\u7ed3\u5408\n", "messages = [ \n", "{'role':'system',\n", " 'content':'\u4f60\u662f\u4e00\u4e2a\u52a9\u7406\uff0c \u5e76\u4ee5 Seuss \u82cf\u65af\u535a\u58eb\u7684\u98ce\u683c\u4f5c\u51fa\u56de\u7b54\uff0c\u53ea\u56de\u7b54\u4e00\u53e5\u8bdd'}, \n", "{'role':'user',\n", " 'content':'\u5199\u4e00\u4e2a\u5173\u4e8e\u5feb\u4e50\u7684\u5c0f\u9cb8\u9c7c\u7684\u6545\u4e8b'},\n", "] \n", "response = get_completion_from_messages(messages, temperature =1)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 9, "id": "89a70c79", "metadata": {"height": 370}, "outputs": [], "source": ["def get_completion_and_token_count(messages, \n", " model=\"gpt-3.5-turbo\", \n", " temperature=0, \n", " max_tokens=500):\n", " \"\"\"\n", " \u4f7f\u7528 OpenAI \u7684 GPT-3 \u6a21\u578b\u751f\u6210\u804a\u5929\u56de\u590d\uff0c\u5e76\u8fd4\u56de\u751f\u6210\u7684\u56de\u590d\u5185\u5bb9\u4ee5\u53ca\u4f7f\u7528\u7684 token \u6570\u91cf\u3002\n", "\n", " \u53c2\u6570:\n", " messages: \u804a\u5929\u6d88\u606f\u5217\u8868\u3002\n", " model: \u4f7f\u7528\u7684\u6a21\u578b\u540d\u79f0\u3002\u9ed8\u8ba4\u4e3a\"gpt-3.5-turbo\"\u3002\n", " temperature: \u63a7\u5236\u751f\u6210\u56de\u590d\u7684\u968f\u673a\u6027\u3002\u503c\u8d8a\u5927\uff0c\u751f\u6210\u7684\u56de\u590d\u8d8a\u968f\u673a\u3002\u9ed8\u8ba4\u4e3a 0\u3002\n", " max_tokens: \u751f\u6210\u56de\u590d\u7684\u6700\u5927 token \u6570\u91cf\u3002\u9ed8\u8ba4\u4e3a 500\u3002\n", "\n", " \u8fd4\u56de:\n", " content: \u751f\u6210\u7684\u56de\u590d\u5185\u5bb9\u3002\n", " token_dict: \u5305\u542b'prompt_tokens'\u3001'completion_tokens'\u548c'total_tokens'\u7684\u5b57\u5178\uff0c\u5206\u522b\u8868\u793a\u63d0\u793a\u7684 token \u6570\u91cf\u3001\u751f\u6210\u7684\u56de\u590d\u7684 token \u6570\u91cf\u548c\u603b\u7684 token \u6570\u91cf\u3002\n", " \"\"\"\n", " response = openai.ChatCompletion.create(\n", " model=model,\n", " messages=messages,\n", " temperature=temperature, \n", " max_tokens=max_tokens,\n", " )\n", "\n", " content = response.choices[0].message[\"content\"]\n", " \n", " token_dict = {\n", "'prompt_tokens':response['usage']['prompt_tokens'],\n", "'completion_tokens':response['usage']['completion_tokens'],\n", "'total_tokens':response['usage']['total_tokens'],\n", " }\n", "\n", " return content, token_dict"]}, {"cell_type": "code", "execution_count": 24, "id": "a64cf3c6", "metadata": {"height": 166}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["In a garden so bright, with colors so cheery,\n", "There lived a carrot, oh so merry!\n", "With a vibrant orange hue, and a leafy green top,\n", "This happy carrot just couldn't stop.\n", "\n", "It danced in the breeze, with a joyful sway,\n", "Spreading happiness throughout the day.\n", "With a smile so wide, and eyes full of glee,\n", "This carrot was as happy as can be.\n", "\n", "It loved the sunshine, and the rain's gentle touch,\n", "Growing tall and strong, oh so much!\n", "From the earth it sprouted, reaching for the sky,\n", "A happy carrot, oh my, oh my!\n", "\n", "So if you're feeling down, just remember this tale,\n", "Of a carrot so happy, it'll never fail.\n", "Find joy in the little things, and let your heart sing,\n", "Just like that carrot, oh what joy it will bring!\n"]}], "source": ["messages = [\n", "{'role':'system', \n", " 'content':\"\"\"You are an assistant who responds\\\n", " in the style of Dr Seuss.\"\"\"}, \n", "{'role':'user',\n", " 'content':\"\"\"write me a very short poem \\ \n", " about a happy carrot\"\"\"}, \n", "] \n", "response, token_dict = get_completion_and_token_count(messages)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 22, "id": "cfd8fbd4", "metadata": {"height": 146}, "outputs": [], "source": ["messages = [ \n", "{'role':'system', \n", " 'content':'\u4f60\u662f\u4e00\u4e2a\u52a9\u7406\uff0c \u5e76\u4ee5 Seuss \u82cf\u65af\u535a\u58eb\u7684\u98ce\u683c\u4f5c\u51fa\u56de\u7b54\u3002'}, \n", "{'role':'user', \n", " 'content':'\u5c31\u5feb\u4e50\u7684\u5c0f\u9cb8\u9c7c\u4e3a\u4e3b\u9898\u7ed9\u6211\u5199\u4e00\u9996\u77ed\u8bd7'}, \n", "] \n", "response, token_dict = get_completion_and_token_count(messages)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 25, "id": "352ad320", "metadata": {"height": 30}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["{'prompt_tokens': 37, 'completion_tokens': 173, 'total_tokens': 210}\n"]}], "source": ["print(token_dict)"]}, {"cell_type": "markdown", "id": "cfe248d6", "metadata": {}, "source": ["\u4e0b\u4e00\u4e2a\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u7ec4\u4ef6\u6765\u8bc4\u4f30\u5ba2\u6237\u670d\u52a1\u52a9\u624b\u7684\u8f93\u5165\u3002\n", "\u8fd9\u5c06\u662f\u672c\u8bfe\u7a0b\u4e2d\u6784\u5efa\u5728\u7ebf\u96f6\u552e\u5546\u5ba2\u6237\u670d\u52a1\u52a9\u624b\u7684\u66f4\u5b8c\u6574\u793a\u4f8b\u7684\u4e00\u90e8\u5206\u3002"]}], "metadata": {"kernelspec": {"display_name": "Python 3.9.6 64-bit", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6"}, "vscode": {"interpreter": {"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"}}}, "nbformat": 4, "nbformat_minor": 5} |