iT邦幫忙

2024 iThome 鐵人賽

0
生成式 AI

運用生成式 AI 服務 所提供的API 實做應用開發(以Gemini及ChatGPT為例)系列 第 37

9-2 運用 chatGPT playground 實作基本 Assistant API 串接

  • 分享至 

  • xImage
  •  

9-2 運用 chatGPT playground 實作基本 Assistant API 串接

https://ithelp.ithome.com.tw/upload/images/20240811/20046160OYtt3loicH.png

以魏明谷海線鐵路站點資料 做 file Retrieval vs function calling 的 比較

原始內容:https://medium.com/ai-人機協作/chatgpt-assistants-api-串接-line-message-api-實踐-架空海線鐵路站點-查詢-c90f3d5f789a

使用 PlayGround Assistant 測試 Files 和 Retrieval

在這篇文章中,我們將詳細介紹如何使用 OpenAI PlayGround Assistant 測試文件(Files)和檢索(Retrieval)功能,並探討 function calling 的應用。

設定與細項

  1. 上傳檔案
    • 在 PlayGround 上選擇 gpt-3.5-turbo-1106,因為只有這個版本才支援 Retrieval 功能。
    • 上傳檔案後可以看到文件內容,確保上傳成功。
  2. 測試流程
    • 進行初步測試時,可能會遇到錯誤,這是因為模型還在訓練過程中。
    • 測試其他功能,但可能會出現錯誤的結果。
  3. 錯誤與修正
    • 即使經過訓練後的資料可以獲得,但仍可能存在錯誤。
    • 若需要完全正確的回答,不建議使用這種方式。Files 功能更適合用來理解後讓 ChatGPT 推理,而非提供精確答案。

使用 PlayGround Assistant 測試 function calling

詳細說明 Function Calling

Function calling 是一種讓模型呼叫預定義函數的功能,但它並非直接呼叫外部 API。以下是如何將自然語言轉換成 JSON schema 的過程:

  1. 轉換範例

    get_current_weather(location: string, unit: 'celsius' | 'fahrenheit')
    
    

    轉換成 JSON schema:

    {
      "name": "get_weather",
      "description": "Determine weather in my location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state e.g. San Francisco, CA"
          },
          "unit": {
            "type": "string",
            "enum": [
              "c",
              "f"
            ]
          }
        },
        "required": ["location"]
      }
    }
    
    
  2. 實例練習
    get_route(from: string, to: string) 轉換成 JSON schema:

    {
      "name": "get_route",
      "description": "Calculates the route from one location to another",
      "parameters": {
        "type": "object",
        "properties": {
          "from": {
            "type": "string",
            "description": "Starting location"
          },
          "to": {
            "type": "string",
            "description": "Destination location"
          }
        },
        "required": ["from", "to"]
      }
    }
    
    
  3. 添加選項
    fromto 的輸入限制為特定選項:

    {
      "name": "get_route",
      "description": "Calculates the route from one location to another",
      "parameters": {
        "type": "object",
        "properties": {
          "from": {
            "type": "string",
            "enum": ["台中龍井", "伸港", "線西", "鹿港", "福興", "北溪湖", "二林", "芳苑", "大城", "六輕", "北港", "太保", "布袋", "學甲", "佳里", "台南"],
            "description": "Starting location"
          },
          "to": {
            "type": "string",
            "enum": ["台中龍井", "伸港", "線西", "鹿港", "福興", "北溪湖", "二林", "芳苑", "大城", "六輕", "北港", "太保", "布袋", "學甲", "佳里", "台南"],
            "description": "Destination location"
          }
        },
        "required": ["from", "to"]
      }
    }
    
    
  4. 在 PlayGround 上進行測試

    • 將上述 JSON schema 加入 PlayGround。

    • 測試時,輸入 fromto 的值,例如:{"from": "鹿港", "to": "大城"}

    • 查看 runs 中的結果,應該顯示類似以下內容:

      {
        "required_action": {
          "type": "submit_tool_outputs",
          "submit_tool_outputs": {
            "tool_calls": [
              {
                "id": "call_5PvWtMxkLMimA03h8GqpbabF",
                "type": "function",
                "function": {
                  "name": "get_route",
                  "arguments": "{\\"from\\":\\"鹿港\\",\\"to\\":\\"大城\\"}"
                }
              }
            ]
          }
        }
      }
      
      
  5. 測試返回值

    • 根據輸入進行測試,觀察返回值,模擬實際應用場景。

總結

這篇文章詳細介紹了如何在 OpenAI PlayGround 上測試 Files 和 Retrieval 功能,以及 function calling 的詳細步驟和應用。這些功能可以幫助我們更好地利用 OpenAI 的模型進行多樣化的測試和應用,從而提升我們的開發效率和應用效果。

關於我

我是 Wolke。我是一名專業程式開發者,專長是開發 AI 和程式解決方案。

我投入了不少時間在專業發展上。我是多本書的作者,其中包括《LINE聊天機器人+AI+雲端+開源+程式:輕鬆入門到完整學習》《ChatGPT來襲,未來人人都需具備的運算思維!應用詠唱工程來釋放程式生產力—程式學習/開發篇》。也有出版線上課程,我熱衷於分享我的經驗和技術,幫助其他開發者更好地利用 AI 工具。

也在許多知名大學、論壇、社團擔任講者,如果貴方有需要也歡迎與我聯繫。
2023年 講座 紀錄

最後這篇文章若有切合你的需求,敬請訂閱按讚分享


上一篇
9-1 深入了解 Assistants API
下一篇
9-3 Assistant API RAG 向量資料庫訓練
系列文
運用生成式 AI 服務 所提供的API 實做應用開發(以Gemini及ChatGPT為例)42
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言