{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-03-02T14:37:58.792881Z", "start_time": "2023-03-02T14:37:57.989858Z" } }, "outputs": [], "source": [ "import openai" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-03-02T14:38:04.463263Z", "start_time": "2023-03-02T14:38:04.454376Z" } }, "outputs": [ { "data": { "text/plain": [ "'0.26.5'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "openai.version.VERSION" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-03-02T14:38:20.554869Z", "start_time": "2023-03-02T14:38:17.579099Z" }, "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Looking in indexes: http://mirrors.aliyun.com/pypi/simple/\n", "Requirement already satisfied: openai in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (0.26.5)\n", "Collecting openai\n", " Downloading http://mirrors.aliyun.com/pypi/packages/2f/15/b29ed5ad2eed1f49b8b3e2067fbbbb63cfbfc496ca2a27b772bcf0907ad8/openai-0.27.0-py3-none-any.whl (70 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.1/70.1 KB\u001b[0m \u001b[31m334.3 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: typing-extensions in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from openai) (3.7.4.3)\n", "Requirement already satisfied: aiohttp in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from openai) (3.7.4.post0)\n", "Requirement already satisfied: tqdm in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from openai) (4.42.0)\n", "Requirement already satisfied: requests>=2.20 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from openai) (2.22.0)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from requests>=2.20->openai) (1.24.3)\n", "Requirement already satisfied: idna<2.9,>=2.5 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from requests>=2.20->openai) (2.8)\n", "Requirement already satisfied: certifi>=2017.4.17 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from requests>=2.20->openai) (2021.10.8)\n", "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from requests>=2.20->openai) (3.0.4)\n", "Requirement already satisfied: attrs>=17.3.0 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from aiohttp->openai) (20.2.0)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from aiohttp->openai) (4.7.6)\n", "Requirement already satisfied: async-timeout<4.0,>=3.0 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from aiohttp->openai) (3.0.1)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from aiohttp->openai) (1.5.1)\n", "Installing collected packages: openai\n", " Attempting uninstall: openai\n", " Found existing installation: openai 0.26.5\n", " Uninstalling openai-0.26.5:\n", " Successfully uninstalled openai-0.26.5\n", "Successfully installed openai-0.27.0\n" ] } ], "source": [ "!pip install --upgrade openai" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2023-03-02T15:08:53.672876Z", "start_time": "2023-03-02T15:08:53.669163Z" } }, "outputs": [], "source": [ "import openai" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2023-03-02T15:08:55.740768Z", "start_time": "2023-03-02T15:08:55.735695Z" } }, "outputs": [ { "data": { "text/plain": [ "'0.27.0'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "openai.version.VERSION" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2023-03-02T15:09:02.580246Z", "start_time": "2023-03-02T15:09:02.577767Z" } }, "outputs": [], "source": [ "openai.api_key = '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'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## doc" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2023-03-02T15:11:31.593473Z", "start_time": "2023-03-02T15:11:30.167997Z" } }, "outputs": [ { "data": { "text/plain": [ " JSON: {\n", " \"choices\": [\n", " {\n", " \"finish_reason\": \"stop\",\n", " \"index\": 0,\n", " \"message\": {\n", " \"content\": \"The 2020 World Series between the Los Angeles Dodgers and the Tampa Bay Rays was played at Globe Life Field in Arlington, Texas.\",\n", " \"role\": \"assistant\"\n", " }\n", " }\n", " ],\n", " \"created\": 1677769891,\n", " \"id\": \"chatcmpl-6pesdOnQEmxpPZ6Q4uvHUeYGGifVr\",\n", " \"model\": \"gpt-3.5-turbo-0301\",\n", " \"object\": \"chat.completion\",\n", " \"usage\": {\n", " \"completion_tokens\": 29,\n", " \"prompt_tokens\": 56,\n", " \"total_tokens\": 85\n", " }\n", "}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "openai.ChatCompletion.create(\n", " model=\"gpt-3.5-turbo\",\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"},\n", " {\"role\": \"assistant\", \"content\": \"The Los Angeles Dodgers won the World Series in 2020.\"},\n", " {\"role\": \"user\", \"content\": \"Where was it played?\"}\n", " ]\n", ")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2023-03-02T15:12:57.297668Z", "start_time": "2023-03-02T15:12:56.579683Z" } }, "outputs": [], "source": [ "resp = openai.ChatCompletion.create(\n", " model=\"gpt-3.5-turbo\",\n", " messages=[\n", " {'role': 'system', 'content': 'You are a helpful assistant that translates English to Chinese.'},\n", " {'role': 'user', 'content': 'Translate the following English text to Chinese: \"{Who won the world series in 2020?}\"'}\n", " ]\n", ")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2023-03-02T15:13:25.098332Z", "start_time": "2023-03-02T15:13:25.093547Z" } }, "outputs": [ { "data": { "text/plain": [ "'\"2020年世界大赛冠军是谁?\"'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "resp['choices'][0]['message']['content']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## conversation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2023-03-02T15:18:06.609Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "👨‍💻: 你是谁\n", "🤖: 我是一个人工智能助手,可以帮助回答你的问题和提供有用的信息。你有什么需要帮助的吗?\n", "👨‍💻: 你从哪里来\n", "🤖: 我是由一些程序员和工程师团队开发的人工智能软件。我被设计为一种语言处理算法,可以理解和回答用户的问题。我的主要目的是提供有用的信息和帮助解决问题,以尽可能满足用户的需求。\n", "👨‍💻: 你要到哪里去\n", "🤖: 作为一个人工智能助手,我没有自己的旅行计划或目的地。我的任务是随时为您提供帮助和支持。无论您需要什么样的信息或帮助,在您需要我的地方,我将随时等待并准备好为您服务。\n", "👨‍💻: 我的第一个问题是什么\n", "🤖: 您的第一个问题是“你是谁”。\n" ] } ], "source": [ "messages = [\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", "]\n", "while True:\n", " message = input(\"👨‍💻: \")\n", " if message:\n", " messages.append(\n", " {\"role\": \"user\", \"content\": message},\n", " )\n", " chat_completion = openai.ChatCompletion.create(\n", " model=\"gpt-3.5-turbo\", messages=messages\n", " )\n", " # 获取回复\n", " reply = chat_completion.choices[0].message.content\n", " print(f\"🤖: {reply}\")\n", " messages.append({\"role\": \"assistant\", \"content\": reply})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }