├── README.md ├── images ├── chatGPT_withAPI_small2.jpg └── memo └── myChatGPT.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # ChatGPT API Example (Model: gpt-3.5-turbo) 2 | 3 | ## 实战案例:复现OpenAI ChatGPT功能代码: myChatGPT.ipynb(Google colab平台) 4 | 5 | 代码制作介绍(Youtube视频):Colab平台ChatGPT API实战教程,从入门到进阶 6 | 7 | [![30行代码完美复现ChatGPT,Colab平台ChatGPT API实战教程 | 从入门到进阶,全面掌握GPT3.5-turbo的必学案例 | 保姆级带你一步一步学习制作ChatGPT应用](images/chatGPT_withAPI_small2.jpg)](https://www.youtube.com/watch?v=JSuRn1jqXAY) 8 | 9 | 相关链接: 10 | 11 | OpenAI ChatGPT API Rrelease Announcements (2023.3.1): 12 | https://openai.com/blog/introducing-chatgpt-and-whisper-apis 13 | 14 | Google colab: https://colab.research.google.com/ 15 | 16 | OpenAI API Login: https://openai.com/api/login/ 17 | 18 | 头像图标: https://lets-emoji.com/emojilist/emojilist-22/ 19 | -------------------------------------------------------------------------------- /images/chatGPT_withAPI_small2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TechDIYLife/ChatGPT_API_example/8be77c6c8a27bd9db08e66e50f77dccb194bbe27/images/chatGPT_withAPI_small2.jpg -------------------------------------------------------------------------------- /images/memo: -------------------------------------------------------------------------------- 1 | memo 2 | -------------------------------------------------------------------------------- /myChatGPT.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyM1JNqn6a8oX0s3lKnt+JJi", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": { 33 | "id": "giyFMJ3CvnG4" 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "!pip install openai" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "source": [ 43 | "import openai" 44 | ], 45 | "metadata": { 46 | "id": "29-PCjw8xwO5" 47 | }, 48 | "execution_count": 2, 49 | "outputs": [] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "source": [ 54 | "openai.api_key=\"xxxxxxxxxxxxxxxxxxxxxxxxxxx\"" 55 | ], 56 | "metadata": { 57 | "id": "NzjmkduKx0KI" 58 | }, 59 | "execution_count": 3, 60 | "outputs": [] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "source": [ 65 | "response = openai.ChatCompletion.create(\n", 66 | " model=\"gpt-3.5-turbo\",\n", 67 | " messages=[\n", 68 | " {\"role\":\"user\",\"content\": \"How many workers in OpenAI?\"},\n", 69 | " ]\n", 70 | ")\n", 71 | "print(response)" 72 | ], 73 | "metadata": { 74 | "id": "WBowbryhyf78" 75 | }, 76 | "execution_count": null, 77 | "outputs": [] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "source": [ 82 | "class bcolors: # 文本颜色\n", 83 | " GREEN = '\\033[92m'\n", 84 | " BLUE = '\\033[94m'\n", 85 | "\n", 86 | "def print_w(s): #打印文本,内容宽度设置\n", 87 | " width = 150\n", 88 | " while len(s) > width:\n", 89 | " print(f'{s[:width]:<{width}}')\n", 90 | " s = s[width:]\n", 91 | " print(s)\n", 92 | " \n", 93 | "def chatGPT_api(list_msg,list_ans,message): # API调用函数\n", 94 | " # 设置system role\n", 95 | " system_role = \"\\u5C06\\u6587\\u672C\\u7FFB\\u8BD1\\u4E3A\\u82F1\\u8BED\" #@param {type:\"string\",title:\"\"} \n", 96 | " send_msg=[{\"role\":\"system\",\"content\": system_role}]\n", 97 | "\n", 98 | " # 读取历史对话记录\n", 99 | " for i in range(len(list_msg)):\n", 100 | " _msg={\"role\":\"user\",\"content\": list_msg[i]}\n", 101 | " send_msg.append(_msg)\n", 102 | " _ans={\"role\":\"assistant\",\"content\": list_ans[i]}\n", 103 | " send_msg.append(_ans)\n", 104 | "\n", 105 | " # 准备用户的新消息\n", 106 | " _msg={\"role\":\"user\",\"content\": message}\n", 107 | " send_msg.append(_msg)\n", 108 | "\n", 109 | " # 调用API,返回结果\n", 110 | " response = openai.ChatCompletion.create(\n", 111 | " model=\"gpt-3.5-turbo\",\n", 112 | " messages=send_msg\n", 113 | " )\n", 114 | " #返回结果\n", 115 | " return response[\"choices\"][0][\"message\"][\"content\"]\n", 116 | "\n", 117 | "def main():\n", 118 | " # 历史对话记录列表\n", 119 | " history_list_msg=[]\n", 120 | " history_list_ans=[]\n", 121 | " \n", 122 | " while(True):\n", 123 | " # 等待用户输入\n", 124 | " print(f\"👧 {bcolors.GREEN}\", end=\"\")\n", 125 | " message = input()\n", 126 | "\n", 127 | " # 调用API,返回结果\n", 128 | " answer = chatGPT_api(history_list_msg, history_list_ans, message)\n", 129 | " print(f\"⚛️ {bcolors.BLUE}\")\n", 130 | " print_w(answer)\n", 131 | "\n", 132 | " history_list_msg.append(message)\n", 133 | " history_list_ans.append(answer)\n", 134 | "\n", 135 | "if __name__ == \"__main__\":\n", 136 | " main()\n", 137 | "\n" 138 | ], 139 | "metadata": { 140 | "id": "RDrr9pUkzDxG" 141 | }, 142 | "execution_count": null, 143 | "outputs": [] 144 | } 145 | ] 146 | } --------------------------------------------------------------------------------