使用AI進行API設計的準備工作

在開始本教程之前,請確保滿足以下前提條件:

每次調用AI都會消耗一定的費用,例如,本文所需的所有運行成本約為1英鎊(GBP)。

環境設置

完成準備工作后,按照以下步驟設置項目環境:

  1. 克隆smol-developer項目到本地并進入項目目錄:

    git clone https://github.com/smol-ai/developer
    cddeveloper
  2. 使用pip安裝Python依賴項:

    pipinstall-rrequirements.txt
  3. OpenAI API密鑰導出為環境變量:

    export OPENAI_API_KEY=
  4. 測試環境是否配置正確:

    python3 main_no_modal.py "a simple hello world application written in nodejs"

運行上述命令后,smol-developer工具將根據輸入提示生成初始代碼文件,例如一個簡單的index.js文件,內容為“Hello, World!”的控制臺日志輸出。


定義API規范

在要求AI生成代碼之前,明確的需求定義至關重要。AI工具可以根據規范填補空白,但不完整或模糊的規范可能導致意外輸出。因此,建議采用迭代的方式逐步完善規范,每次修改后通過AI工具驗證效果。

構建初始API規范

在項目根目錄下創建一個名為my-prompt.md的Markdown文件,內容如下:

"""
Create a RESTful CRUD API using express.js.

## Resources

The API has the following resources and fields:

### User
- id
- name

### Ticket
- id
- summary
- description
- author_id

### Comment
- id
- content
- author_id
- ticket_id
- parent_id
"""

運行以下命令讓AI生成代碼:

python3 main_no_modal.py my-prompt.md

生成的代碼可能會因工具的非確定性特性而有所不同,但整體流程應保持一致。生成的文件通常包括index.js及其他相關文件。


迭代優化API規范

迭代1:簡化代碼生成

初始生成的代碼可能包含不必要的驗證邏輯和數據庫配置。為此,更新規范以移除這些內容:

"""
Create a RESTful CRUD API using express.js.

## Resources

The API has the following resources and fields:

### User
- id
- name

### Ticket
- id
- summary
- description
- author_id

### Comment
- id
- content
- author_id
- ticket_id
- parent_id

## Adjustments

- Do not validate requests or responses
- Do not use a database, just use an array as a datastore for now
"""

運行AI工具后,生成的代碼將更符合簡化需求,例如使用數組作為數據存儲。盡管如此,生成的代碼可能仍包含未使用的導入或不匹配的函數調用。

迭代2:定義模塊結構

為進一步提高代碼一致性和可維護性,可以在規范中明確模塊結構和文件內容。例如:

## Module structure

Each module (users, tickets, and comments) should use the following structure. "User" is used as an example, and the name should be changed appropriately for each given module.

- modules
 - users
 - userRoutes.js
 - userController.js
 - userService.js

### userRoutes.js

This file should be used in the index.js like so:

```javascript
app.use('/users',userRoutes);

The file itself should contain mappings between a given endpoint and the controller method that serves it, like this:

router.get('/',userController.getAllUsers);
router.get('/:id',userController.getUserById);
router.[post](http://www.dlbhg.com/provider/uid2024120814472139bfa9)('/',userController.createUser);
router.put('/:id',userController.updateUser);
router.delete('/:id',userController.deleteUser);

userController.js

This file should serve as an HTTP adapter, and should invoke business logic from the userService. This file should declare the following functions:

userService.js

This file should house the business logic for the module. This file will declare the following functions:

通過增加模塊化結構的詳細說明,生成的代碼將更符合預期。


調查結果與改進建議

非確定性問題

AI生成代碼的非確定性可能導致每次運行結果不同,尤其在反復修改規范時,這會增加調試難度。

上下文和一致性問題

AI工具在生成代碼時可能忽略上下文,導致模塊之間不一致或調用錯誤。例如,生成的函數簽名可能與調用處不匹配。

標準化需求

目前缺乏標準化的描述語言來明確傳達需求,這使得生成的代碼質量在很大程度上依賴于提示的清晰度和AI的理解能力。


總結

本文展示了如何利用AI工具(如smol-developer)快速生成API代碼,并通過迭代優化規范以提高代碼質量。盡管AI工具在代碼生成方面展現了強大的潛力,但其非確定性、上下文局限性和一致性問題表明,它們尚未完全適合生產環境。然而,隨著技術的不斷進步,這些問題有望逐步得到解決。

在此過程中,開發者應結合傳統工具(如Tyk API網關)以確保API開發的穩定性和可靠性。Tyk提供了全面的功能支持,包括身份驗證、速率限制和日志記錄,是構建高質量API的理想選擇。

原文鏈接: https://tyk.io/blog/using-ai-for-api-design/

上一篇:

API優先設計三原則

下一篇:

15 個值得嘗試的 API 設計工具
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費