Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions 09.Apps/OpenAI_Demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
{
"cell_type": "markdown",
"source": [
"- OPENAI_API_KEY 加入 Clobe 環境變數後可用(上課時老師會提供一組練習用)"
"- OPENAI_API_KEY 加入 Clobe 環境變數後可用(請使用自己的測試金鑰,勿將金鑰寫入 Notebook)"
],
"metadata": {
"id": "a_U2KT7GDnlY"
Expand All @@ -81,7 +81,7 @@
"from openai import OpenAI\n",
"client = OpenAI(api_key=OPENAI_API_KEY)\n",
"\n",
"system_content = \"你是一個AI助教,會用蘇格拉底教學法代替老師初步回應,如果有需要會提醒學生跟老師確認\" #@param\n",
"system_content = \"你是一個 AI 學習助理,會用蘇格拉底式提問引導學生思考;遇到課程規格或評量問題時,提醒學生查看課程公告\" #@param\n",
"user_content = \"如何用python抓取api資料?\" #@param\n",
"\n",
"chat_completion = client.chat.completions.create(\n",
Expand Down Expand Up @@ -116,7 +116,7 @@
"output_type": "execute_result",
"data": {
"text/plain": [
"ChatCompletion(id='chatcmpl-8b6oO2LTApIXOPHjoK03pHY3hjR8S', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='嗨!很高興看到你對使用Python抓取API資料有興趣。我可以提供一些基本的指導,但請確認你的問題具體指的是哪方面的API資料,因為不同的API服務可能有不同的使用方法。\\n\\n首先,你需要使用Python來發送HTTP請求以取得API的回應。你可以使用Python的內建模組 - `requests` 來執行這些操作。你可以透過以下步驟來開始:\\n\\n1. 安裝requests模組:你可以使用`pip`工具在終端機中執行以下指令:`pip install requests` (如果你已經安裝了Anaconda,則這個模組可能已經預先安裝了)。\\n\\n2. 導入requests模組:在你的Python腳本中,請確認你匯入了requests模組,以便使用它的功能。可以使用以下語句:`import requests`。\\n\\n3. 發送HTTP請求:使用requests模組的 `get()` 函數或 `post()` 函數來發送GET或POST請求。例如,如果你想發送一個GET請求獲取API的回應,請使用以下語句:`response = requests.get(url)`,其中 `url` 是API的網址。\\n\\n4. 處理回應:一旦你收到API的回應,你可以使用response的方法,如 `status_code` 和 `json()` 來檢查回應狀態並處理回應數據。例如,你可以使用以下語句檢查回應的狀態碼:`print(response.status_code)`。\\n\\n這是一個簡單的範例來幫助你開始:\\n\\n```python\\nimport requests\\n\\ndef fetch_data_from_api(url):\\n response = requests.get(url)\\n if response.status_code == 200:\\n data = response.json()\\n # 在此處處理API回應的資料\\n else:\\n print(\"無法取得API資料,請確認URL是否正確\")\\n\\nfetch_data_from_api(\\'https://api.example.com/data\\')\\n```\\n\\n記得在使用`fetch_data_from_api`函數之前,將URL修改為你要抓取的API的URL。\\n\\n上述只是一個簡單的示例,根據你具體使用的API和所需的資料處理邏輯,你可能需要進行更多的操作。\\n\\n請記得向你的老師查證以確保你按照他們的要求進行操作。希望這對你有所幫助!如果你需要更多幫助,請隨時問我。', role='assistant', function_call=None, tool_calls=None))], created=1703855012, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=784, prompt_tokens=83, total_tokens=867))"
"ChatCompletion(id='chatcmpl-8b6oO2LTApIXOPHjoK03pHY3hjR8S', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='嗨!很高興看到你對使用Python抓取API資料有興趣。我可以提供一些基本的指導,但請確認你的問題具體指的是哪方面的API資料,因為不同的API服務可能有不同的使用方法。\\n\\n首先,你需要使用Python來發送HTTP請求以取得API的回應。你可以使用Python的內建模組 - `requests` 來執行這些操作。你可以透過以下步驟來開始:\\n\\n1. 安裝requests模組:你可以使用`pip`工具在終端機中執行以下指令:`pip install requests` (如果你已經安裝了Anaconda,則這個模組可能已經預先安裝了)。\\n\\n2. 導入requests模組:在你的Python腳本中,請確認你匯入了requests模組,以便使用它的功能。可以使用以下語句:`import requests`。\\n\\n3. 發送HTTP請求:使用requests模組的 `get()` 函數或 `post()` 函數來發送GET或POST請求。例如,如果你想發送一個GET請求獲取API的回應,請使用以下語句:`response = requests.get(url)`,其中 `url` 是API的網址。\\n\\n4. 處理回應:一旦你收到API的回應,你可以使用response的方法,如 `status_code` 和 `json()` 來檢查回應狀態並處理回應數據。例如,你可以使用以下語句檢查回應的狀態碼:`print(response.status_code)`。\\n\\n這是一個簡單的範例來幫助你開始:\\n\\n```python\\nimport requests\\n\\ndef fetch_data_from_api(url):\\n response = requests.get(url)\\n if response.status_code == 200:\\n data = response.json()\\n # 在此處處理API回應的資料\\n else:\\n print(\"無法取得API資料,請確認URL是否正確\")\\n\\nfetch_data_from_api(\\'https://api.example.com/data\\')\\n```\\n\\n記得在使用`fetch_data_from_api`函數之前,將URL修改為你要抓取的API的URL。\\n\\n上述只是一個簡單的示例,根據你具體使用的API和所需的資料處理邏輯,你可能需要進行更多的操作。\\n\\n請對照教材與課程公告確認操作要求。希望這對你有所幫助!如果你需要更多幫助,請隨時問我。', role='assistant', function_call=None, tool_calls=None))], created=1703855012, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=784, prompt_tokens=83, total_tokens=867))"
]
},
"metadata": {},
Expand Down Expand Up @@ -174,7 +174,7 @@
"\n",
"上述只是一個簡單的示例,根據你具體使用的API和所需的資料處理邏輯,你可能需要進行更多的操作。\n",
"\n",
"請記得向你的老師查證以確保你按照他們的要求進行操作。希望這對你有所幫助!如果你需要更多幫助,請隨時問我。\n"
"請對照教材與課程公告確認操作要求。希望這對你有所幫助!如果你需要更多幫助,請隨時問我。\n"
]
}
]
Expand Down Expand Up @@ -309,7 +309,7 @@
"\n",
"def text2text(system_content, user_content):\n",
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
" system_content = system_content if system_content is not None else \"你是一個AI助教,會用蘇格拉底教學法代替老師初步回應,如果有需要會提醒學生跟老師確認\"\n",
" system_content = system_content if system_content is not None else \"你是一個 AI 學習助理,會用蘇格拉底式提問引導學生思考;遇到課程規格或評量問題時,提醒學生查看課程公告\"\n",
" user_content = user_content if user_content is not None else \"如何用python抓取api資料?\"\n",
" chat_completion = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n",
Expand Down
Loading
Loading