# 智能工具的 JSON 格式

## 總覽 <a href="#lgmg27qgfdrg" id="lgmg27qgfdrg"></a>

若要在SkyDeck.AI上創建一個智能工具，你需要根據文件結構部分提到的規範上傳一組文件。一旦上傳，我們的平台將進行初始設置，這可能需要幾分鐘的時間。之後，該工具將在GenStudio工作區中可用。

## 檔案結構 <a href="#id-7dcm0ac78x60" id="id-7dcm0ac78x60"></a>

### <工具\_名稱>.json <a href="#xvdq6eic1eed" id="xvdq6eic1eed"></a>

工具的行為也是透過 JSON 檔案配置的。以下是配置中關鍵欄位的簡短概述：

* **version**：工具的當前版本。
* **tool\_name**：工具的名稱。此名稱在您的工作區中應該是唯一的。
* **tool\_code**：包含要執行的 Python 代碼。關於此欄位的更多詳細信息將在下一節中介紹。
* **description**：闡述工具的功能的簡短描述。
* **usage\_notes**：如何使用工具的說明。
* **model\_version**：指定可用於後續問題的模型。要指定所有模型，使用 *\["gpt-4", "gpt-3.5", "claude", "chat-bison"]*。
* **creator**：關於工具創建者的信息，包括名稱、電子郵件和組織。
* **variables**：工具使用的變量數組。每個變量都有名稱、描述和默認值。UI 中變量的順序按照此數組中的順序。
* **expected\_output**：工具生成的輸出類型。在開發階段，值應始終為文本。
* **avatar\_type**：在工具使用者介面(UI)中使用的頭像的格式。
* **timestamp**：工具最後更新的日期和時間。
* **requirements**：指定運行 tool\_code 腳本所需的套件。
* **avatar**：代表這個工具標誌的字符串

### 工具\_代碼慣例: <a href="#id-177qobv2sfe9" id="id-177qobv2sfe9"></a>

此腳本概述了您的工具的功能。此腳本的主要組成部分是\_execute\_函數，該函數有以下要求：

* 函數應該有一個名為\_variables\_的單一輸入參數，該參數是一個字典。此字典中的每個鍵都對應於使用者會輸入到您的工具中的一個欄位。
* 該函數應返回一個字符串，該字符串將在GenStudio UI上顯示為響應。

## 工具示例 <a href="#h9o86a5lgfut" id="h9o86a5lgfut"></a>

### 使用 DALL-E 2 生成圖像 <a href="#quzi31gwxka6" id="quzi31gwxka6"></a>

**描述：**

此工具接受圖像描述作為輸入，並生成相對應的圖像 URL。輸出包括 URL 以及過期說明。該工具通過向 OpenAI DALL-E API 發送查詢並獲取響應來運作。

**輸入：**

* 描述：圖像描述，例如，“一隻白色的毛茸茸的貓”

**輸出：**

* 包含生成的圖像 URL 以及過期說明的訊息。

#### Python 腳本 (這將是 image\_generation.json 中的一個欄位)： <a href="#qw9m9923kptk" id="qw9m9923kptk"></a>

```python
import openai
def execute(variables):
    openai.api_key = '<用戶 API 金鑰>'
    description = variables['Description']
    response = openai.Image.create(
        prompt=description,
        n=1,
        size="1024x1024"
    )
    image_url = response['data'][0]['url']
    return f"這是你的圖片連結：{image_url}。此連結將在 1 小時後過期。"
```

#### image\_generation.json <a href="#id-4q3mtb2zf8gt" id="id-4q3mtb2zf8gt"></a>

```json
{
    "version": "0.1",
    "metadata": {
        "tool_name": "圖像生成",
        "tool_code": "import openai\n\ndef execute(variables):\n openai.api_key = ''\n description = variables['Description']\n response = openai.Image.create(\n prompt=description,\n n=1,\n size=\"1024x1024\"\n )\n\n image_url = response['data'][0]['url']\n return f\"這是您的圖像的鏈接:{image_url}。該鏈接將在1小時後過期。\"\n",
        "description": "利用OpenAI的DALL-E模型，根據說明生成圖像。",
        "usage_notes": "詳細描述圖像並將其放在描述欄位中。將返回圖像的URL。URL的生命期約為1小時，所以請確保在過期之前下載它",
        "model_version": ["gpt-3.5","gpt-3.5-turbo", "gpt-4", "claude"],
        "creator": {
            "name": "SkyDeck AI",
            "email": "skydeck@eastagile.com",
            "organization": "East Agile"
        },
        "variables": [
            {
                "name": "Description",
                "description": "圖像描述",
                "default": "一隻白色暹羅貓"
            }
        ],
        "expected_output": {
            "type": "text"
        },
        "avatar_type": "base64",
        "timestamp": "2023-05-23T10:00:00Z",
        "requirements": "openai>=0.27.4",
        "avatar": ""
    }
}
```

### 利用 Open-Meteo API 的實時天氣報告 <a href="#yhjv7lbl4mu0" id="yhjv7lbl4mu0"></a>

**描述**：

此工具使用 Open-Meteo API 來根據用戶的問題提供實時天氣信息。通過問關於天氣的問題，例如溫度，降水或風的情況，該工具可以獲取最相關和最新的數據。

此工具的運作依賴於 APIChain 這個 LangChain 庫的一個特性，用以訪問 Open-Meteo API 的文檔。這使工具能夠學習如何正確調用 API 並無縫取得所需信息。

**輸入**：

* 問題: 問一個關於天氣的具體問題，例如，"紐約市目前的溫度是多少?"

**輸出**：

* 提供所請求的天氣信息的回答。

#### Python 腳本（將位於 weather\_reporter.json 內的欄位）： <a href="#qw9m9923kptk" id="qw9m9923kptk"></a>

```python
from langchain.chains.api import open_meteo_docs
from langchain.chat_models import ChatOpenAI
from langchain.chains import APIChain
def execute(variables):
    question = variables['Question']
    llm = ChatOpenAI(
        model_name='gpt-3.5-turbo',
        openai_api_key='<使用者 API 金鑰>'
    )
    api_chain = APIChain.from_llm_and_api_docs(
        llm, open_meteo_docs.OPEN_METEO_DOCS, verbose=False
    )
    result = api_chain.run(question)
    return result
```

#### weather\_reporter.json <a href="#bgnhw14oi99h" id="bgnhw14oi99h"></a>

```json
{
  "version": "0.1",
  "metadata": {
    "tool_name": "天氣報告器",
    "tool_code": "from langchain.chains.api import open_meteo_docs\nfrom langchain.chat_models import ChatOpenAI\nfrom langchain.chains import APIChain\n\n\ndef execute(variables):\n question = variables['Question']\n llm = ChatOpenAI(model_name='gpt-3.5-turbo',\n openai_api_key='')\n api_chain = APIChain.from_llm_and_api_docs(\n llm, open_meteo_docs.OPEN_METEO_DOCS, verbose=False)\n result = api_chain.run(question)\n return result\n",
    "description": "利用開放天氣API取得即時天氣詳情",
    "usage_notes": "在提供的欄位中輸入您有關天氣的問題",
    "model_version": ["gpt-3.5", "gpt-3.5-turbo", "gpt-4", "claude"],
    "creator": {
      "name": "SkyDeck AI",
      "email": "skydeck@eastagile.com",
      "organization": "East Agile"
    },
    "variables": [
      {
        "name": "Question",
        "description": "詢問天氣情況",
        "default": "德國慕尼黑當前的溫度是多少，以攝氏度表示？"
      }
    ],
    "expected_output": {
      "type": "text"
    },
    "avatar_type": "base64",
    "timestamp": "2023-07-13T10:00:00Z",
    "requirements": "openai>=0.27.4\nlangchain>=0.0.229",
    "avatar": ""
  }
}
```

## 限制 <a href="#g32lfwd6xlxx" id="g32lfwd6xlxx"></a>

AWS Lambda 僅允許一個功能運行最多15分鐘和10GB的RAM。因此，工具應在此限制內完成執行。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.skydeck.ai/zh-hk/developers/develop-your-own-tools/json-format-for-smart-tools.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
