[isTMS]課程 - AI Agent 開發實戰
 

使用OpenAI API

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

Lab 1 : 使用 postman 呼叫 OpenAI completion API

步驟:

  1. 查詢文件, 找到completion API https://platform.openai.com/docs/api-reference/completions
  2. 使用 postman 呼叫API endpoint : https://api.openai.com/v1/completions Authorization: Bearer OPENAI_API_KEY
  3. 輸入API key, 組出request
{
  "model": "gpt-3.5-turbo-instruct",
  "prompt": "什麼是人工智慧? 這是",
  "max_tokens": 1000,
  "temperature": 0.8
}

image

image

  1. 取得response, 檢視結果 <== 截圖

ref: https://platform.openai.com/docs/models/gpt-3-5

Lab 2 : 使用 postman 呼叫 image generations API

步驟:

  1. 查詢文件, 找到image generations API https://platform.openai.com/docs/api-reference/images/create
  2. 使用postman, 組出request, 輸入API key
POST /v1/images/generations HTTP/1.1
Host: api.openai.com
Authorization: Bearer [replace key]
Content-Type: application/json
Content-Length: 65

{
  "prompt": "夕陽下孤獨的身影" , 
  "n": 1,
  "size": "1024x1024"
}
  1. 取得response, 檢視結果

Lab 3 : 使用 OpenAI API 呼叫ChatGPT進行對談

doc: https://platform.openai.com/docs/api-reference/chat

POST /v1/chat/completions HTTP/1.1
Host: api.openai.com
Authorization: Bearer [replace_key]
Content-Type: application/json
Content-Length: 138

{
  "model": "gpt-3.5-turbo",
  "max_tokens" : 300, 
  "temperature" : 0.1, 
  "messages": 
  [
      {"role": "user", "content": "今天外面天氣很好耶~你覺得我們該做些什麼?"}
  ]
}

👉試試看 詢問 "最南端的城市是哪一個呢?" 結果是? 👉改加入歷史訊息

{
  "model": "gpt-3.5-turbo",
  "max_tokens" : 300, 
  "temperature" : 0.1, 
  "messages": 
  [
      {"role": "user", "content": "台灣最大的城市是哪一個?"},
      {"role": "assistant", "content": "台灣最大的城市是台北市。"},
      {"role": "user", "content": "那最南端的城市是哪一個呢?"}
  ]
}

🙋‍♀️ 如果只問 "最南端的城市是哪一個呢?",不透過歷史訊息,也不改問題。還可以改什麼部分讓AI回答的答案是正確的? image

補充:關於JSON:

https://zh.wikipedia.org/wiki/JSON

檢查是否安裝好 dotnet core, VS Code image

建立Chat Bot對談機器人

  1. create a line bot (messaging api) https://developers.line.biz/en/ or https://entry.line.biz/form/entry/unverified

image 2. get Channel access token and Your UserID

👉Channel access token:
hrkYPpesi4yEJXYWxNChTe12e12e1eRyAdn6/ns5n5P33K/qCkppxLnj+K7Oj/+UQmSlSesAdKdU8riZasdasddLaB92KavsdvvrWzphLTFsvcvh0UtAe8UYbU42dVWHEG9qqjGwdB04t89/1O/w1cDnyilFU=
👉Your UserID:
U5291d50250812332c14297d3930