Created
November 8, 2023 16:04
-
-
Save kkdai/2bd42d7df6b12bdb824b2e77ec741107 to your computer and use it in GitHub Desktop.
devday2023.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import os\n", | |
| "import pickle\n", | |
| "import requests\n", | |
| "import json\n", | |
| "from dotenv import load_dotenv\n", | |
| "\n", | |
| "# Laden Sie die Umgebungsvariablen aus der .env-Datei\n", | |
| "load_dotenv()\n", | |
| "\n", | |
| "openai_api_key = os.environ.get(\"OPENAI_API_KEY\")\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def get_completion(messages, model=\"gpt-3.5-turbo\", temperature=0, max_tokens=300):\n", | |
| " payload = { \"model\": model, \"temperature\": temperature, \"messages\": messages, \"max_tokens\": max_tokens }\n", | |
| " headers = { \"Authorization\": f'Bearer {openai_api_key}', \"Content-Type\": \"application/json\" }\n", | |
| " response = requests.post('https://api.openai.com/v1/chat/completions', headers = headers, data = json.dumps(payload) )\n", | |
| " obj = json.loads(response.text)\n", | |
| " if response.status_code == 200 :\n", | |
| " return obj[\"choices\"][0][\"message\"][\"content\"]\n", | |
| " else :\n", | |
| " return obj[\"error\"]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import requests\n", | |
| "import json\n", | |
| "from pprint import pp" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# GPT-4V API\n", | |
| "\n", | |
| "content 參數變成 hash 惹,超簡單" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "messages = [\n", | |
| " { \"role\": \"user\", \"content\": [\n", | |
| " {\n", | |
| " \"type\": \"image_url\",\n", | |
| " \"image_url\": \"https://ccmsassets.ithome.com.tw/2023/7/27/8bc15cdc-bb93-431c-9b26-542704d88df9.png\",\n", | |
| " },\n", | |
| " { \"type\": \"text\", \"text\": \"能根據照片資訊猜測哪裡拍的嗎?\" }\n", | |
| " ] }\n", | |
| "]\n", | |
| "\n", | |
| "get_completion(messages, model=\"gpt-4-vision-preview\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "!pip show openai" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Assistants API with retriever" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## 0. 上傳檔案" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# 找個範例檔案 https://report.nat.gov.tw/ReportFront/ReportDetail/detail?sysId=C11201557\n", | |
| "!wget -O 'C11201717_1.pdf' https://report.nat.gov.tw/ReportFront/PageSystem/reportFileDownload/C11201717/001" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "data = { \"purpose\": \"assistants\" }\n", | |
| "f = open('C11201717_1.pdf', 'rb')\n", | |
| "files = [(\"file\", (\"C11201717_1.pdf\", f, \"application/octet-stream\"))]\n", | |
| "\n", | |
| "headers = { \"Authorization\": f'Bearer {openai_api_key}', \"OpenAI-Beta\": \"assistants=v1\" }\n", | |
| "response = requests.post(f\"https://api.openai.com/v1/files\", headers = headers, data = data, files = files )\n", | |
| "f_obj = json.loads(response.text)\n", | |
| "\n", | |
| "print(f_obj)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "f_obj['id']" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## 1. 建立 assistant with retriever (這時 OpenAI 才跑檔案索引)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "payload = { \"model\": \"gpt-4-1106-preview\", \"name\": \"evan-rag\", \"tools\": [{\"type\": \"retrieval\"}],\n", | |
| " \"instructions\": \"You are a customer support chatbot. Use your knowledge base to best respond to customer queries.\",\n", | |
| " \"file_ids\": [f_obj['id']] }\n", | |
| "headers = { \"Authorization\": f'Bearer {openai_api_key}', \"Content-Type\": \"application/json\", \"OpenAI-Beta\": \"assistants=v1\" }\n", | |
| "response = requests.post('https://api.openai.com/v1/assistants', headers = headers, data = json.dumps(payload) )\n", | |
| "ass_obj = json.loads(response.text)\n", | |
| "\n", | |
| "print(ass_obj)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## 2. 初次建立 thread 對話" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "payload = { \"messages\": [ { \"role\": \"user\", \"content\": \"請問和 Pentera 的會晤討論了什麼?\"} ] }\n", | |
| "headers = { \"Authorization\": f'Bearer {openai_api_key}', \"Content-Type\": \"application/json\", \"OpenAI-Beta\": \"assistants=v1\" }\n", | |
| "response = requests.post('https://api.openai.com/v1/threads', headers = headers, data = json.dumps(payload) )\n", | |
| "th_obj = json.loads(response.text)\n", | |
| "\n", | |
| "print(th_obj)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### 3. 執行 run,產生 AI 回覆" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "assistant_id = ass_obj['id']\n", | |
| "thread_id = th_obj['id']\n", | |
| "\n", | |
| "payload = { \"assistant_id\": assistant_id }\n", | |
| "headers = { \"Authorization\": f'Bearer {openai_api_key}', \"Content-Type\": \"application/json\", \"OpenAI-Beta\": \"assistants=v1\" }\n", | |
| "response = requests.post(f\"https://api.openai.com/v1/threads/{thread_id}/runs\", headers = headers, data = json.dumps(payload) )\n", | |
| "run_obj = json.loads(response.text)\n", | |
| "\n", | |
| "print(run_obj)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### 5. 查看目前 messages" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "headers = { \"Authorization\": f'Bearer {openai_api_key}', \"Content-Type\": \"application/json\", \"OpenAI-Beta\": \"assistants=v1\" }\n", | |
| "response = requests.get(f\"https://api.openai.com/v1/threads/{thread_id}/messages\", headers = headers )\n", | |
| "ret_obj = json.loads(response.text)\n", | |
| "\n", | |
| "pp(ret_obj)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## 可繼續加 message 到 thread 中,繼續 run 對話" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "payload = { \"role\": \"user\", \"content\": \"和 Chain Reaction 談了什麼?\" }\n", | |
| "headers = { \"Authorization\": f'Bearer {openai_api_key}', \"Content-Type\": \"application/json\", \"OpenAI-Beta\": \"assistants=v1\" }\n", | |
| "response = requests.post(f\"https://api.openai.com/v1/threads/{thread_id}/messages\", headers = headers, data = json.dumps(payload) )\n", | |
| "obj = json.loads(response.text)\n", | |
| "\n", | |
| "print(obj)" | |
| ] | |
| } | |
| ], | |
| "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.10.11" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment