~$ curl https://api.openai.com/v1/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer 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" -d '{ "model": "gpt-3.5-turbo-instruct", "prompt": "寫一首關於AI的詩,50字以內" }'
{
"id": "cmpl-80LQCalwcouTwifiSgG2xXDV7MwkA",
"object": "text_completion",
"created": 1695093036,
"model": "gpt-3.5-turbo-instruct",
"choices": [
{
"text": "\n\n機器之腦\n人類所想",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 18,
"completion_tokens": 16,
"total_tokens": 34
}
}
今天時間有限,只會涵蓋部分內容並加上 Ruby Code 的示範,會後會分享完整的入門投影片
晶晶體是一種流行於臺灣以中文為基底,夾雜英語不成句的單字或片語的表達方式。特指所使用的英文字多為過於簡單、沒有替換必要者,進而產生有意炫耀雙語能力卻弄巧成拙的效果。
例如:
原文: 每位員工都要參加每週電話會議,沒有例外
晶晶體:
在一些較難描述明確指示的任務中,蠻適合用 few-shot 的方式讓模型自己學,例如文字風格、特定的輸出結構(某種schema)
晶晶體是一種流行於臺灣以中文為基底,夾雜英語不成句的單字或片語的表達方式。特指所使用的英文字多為過於簡單、沒有替換必要者,進而產生有意炫耀雙語能力卻弄巧成拙的效果。
例如:
原文: 我很忙,因為我很有事要做
晶晶體: 我是很busy,因為我很多things要do
原文: 天氣總算放晴,沒有下雨、太陽很大、有點熱、讓我想到以前還是學生時,喜歡在這樣的天氣,吃一球冰淇淋,真的會讓人很高興
晶晶體: 天氣總算放晴,沒有rain、太陽很big、有點hot、讓我想到以前還是student時,喜歡在這樣的天氣,吃一球ice cream,真的會讓人很happy
原文: 每位員工都要參加每週電話會議,沒有例外
晶晶體:
請按照以下步驟來回應使用者的輸入:
Step 1: 用戶會用三個引號 ``` 包裹文案,請以"Summary: "為開頭,摘要這個文案成一個句子。
Step 2: 請將步驟1的摘要結果,翻譯成英文,並在前面加上 "Translation:"
請處理這段文字: #{user input}
the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
#{這裡放搜尋出來最相關的 context 資料}
Question: #{這裡放用戶問題}
Helpful Answer:
ReAct 解說
ReAct: Synergizing Reasoning and Acting in Language Models 提出一種 Prompt Chaining 方式,可以進行連續推理和行動的代理人任務 圖片出處: edX 課程 Action: 根據用戶輸入,選擇應該用哪一個 Tool 就根據 tool 的文字描述來判斷 Action Input: 根據需要使用的 tool,從用戶輸入中提取參數 Observation: 觀察 Tool 得到的結果 Thought: 再看一次用戶輸入,判斷接下來怎麼辦 是回到 Action 用另一個工具? 還是這就是 Final Answer? Final Answer: Thought 看到 Observation 後,若認為這是解答,就給出最終輸出 實際 Prompt 範例 Answer the following questions as best you can. You have access to the following tools: 工具X: 工具X描述 工具Y: 工具Y描述 工具Z: 工具Z描述 Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [查詢最新消息, 計算機] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Question: #{用戶問題} Thought: 接著 AI 回傳 我需要先做什麼事情.... Action: 工具X Action Input: 工具X的參數 你在本機執行工具X,得到工具結果 下一個 prompt Answer the following questions as best you can. You have access to the following tools: 工具X: 工具X描述 工具Y: 工具Y描述 工具Z: 工具Z描述 Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [查詢最新消息, 計算機] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Question: #{用戶問題} Thought: #{我需要先做什麼事情} Action Input: #{工具X的參數} Observation: #{工具X執行後的結果} 接著 AI 可能回傳需要另一個工具 我需要先做什麼事情.... Action: 工具Y Action Input: 工具Y的參數 或是完成 我現在知道答案了。 Final Answer: 這是最後答案OOXX
{
"model": "gpt-3.5-turbo-0613",
"messages": [
{"role": "user", "content": "今天台北天氣如何?"}
],
"functions": [
{
"name": "get_current_weather",
"description": "傳入地點,回傳目前天氣資訊",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或地區"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
]
}
{
"id": "chatcmpl-123",
...
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{ \"location\": \"台北\"}"
}
},
"finish_reason": "function_call"
}]
}
{ "temperature": 22, "unit": "celsius", "description": "Sunny" }
{
"model": "gpt-3.5-turbo-0613",
"messages": [
{"role": "user", "content": "今天台北天氣如何?"},
{"role": "assistant", "content": null, "function_call": {"name": "get_current_weather", "arguments": "{ \"location\": \"Boston, MA\"}"}},
{"role": "function", "name": "get_current_weather", "content": "{\"temperature\": "22", \"unit\": \"celsius\", \"description\": \"Sunny\"}"}
],
"functions": [
{
"name": "get_current_weather",
"description": "傳入地點,回傳目前天氣資訊",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或地區"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
]
}
{
"id": "chatcmpl-123",
...
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "台北目前天氣晴朗,氣溫為攝氏22度。",
},
"finish_reason": "stop"
}]
}
範例只有用到一個查詢股價的工具,實際上可以給多個工具,例如 CRUD 四個操作。這個 Function Calling Agent 可以根據用戶需求自動判斷要用哪一個工具。另外,OpenAI 新模型 1106 版,也有支援 Parallel Function Calling
GitHub
GitHub - alexrudall/ruby-openai: OpenAI API + Ruby! 🤖🩵 Now with Assistants, Threads, Messages, Runs and Text to Speech 🍾
OpenAI API + Ruby! 🤖🩵 Now with Assistants, Threads, Messages, Runs and Text to Speech 🍾 - GitHub - alexrudall/ruby-openai: OpenAI API + Ruby! 🤖🩵 Now with Assistants, Threads, Messages, Runs and Tex...