所有文章 > API设计 > 使用AI进行API设计
使用AI进行API设计

使用AI进行API设计

自2022年11月推出ChatGPT以来,人工智能AI)工具在科技界引发了广泛关注。这些工具形式多样,功能丰富,但它们的共同目标是提升用户的工作效率和优化工作流程。然而,如果不了解这些工具的工作原理以及如何与之高效交互,使用它们可能会变得具有挑战性。本文将通过一个实际案例,展示如何利用AI工具(如smol-developer)来设计和生成RESTful CRUD API,并探讨其优势与局限性。


使用AI进行API设计的准备工作

在开始本教程之前,请确保满足以下前提条件:

  • 拥有一个OpenAI账户和API密钥。
  • 配备代码编辑器(推荐使用VS Code)。
  • 确保OpenAI账户中有足够的积分或已配置付款方式。

每次调用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](https://www.explinks.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:

  • getAllUsers
  • getUserById
  • createUser
  • updateUser
  • deleteUser

userService.js

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

  • getAllUsers
  • getUserById
  • createUser
  • updateUser
  • deleteUser

通过增加模块化结构的详细说明,生成的代码将更符合预期。


调查结果与改进建议

非确定性问题

AI生成代码的非确定性可能导致每次运行结果不同,尤其在反复修改规范时,这会增加调试难度。

上下文和一致性问题

AI工具在生成代码时可能忽略上下文,导致模块之间不一致或调用错误。例如,生成的函数签名可能与调用处不匹配。

标准化需求

目前缺乏标准化的描述语言来明确传达需求,这使得生成的代码质量在很大程度上依赖于提示的清晰度和AI的理解能力。


总结

本文展示了如何利用AI工具(如smol-developer)快速生成API代码,并通过迭代优化规范以提高代码质量。尽管AI工具在代码生成方面展现了强大的潜力,但其非确定性、上下文局限性和一致性问题表明,它们尚未完全适合生产环境。然而,随着技术的不断进步,这些问题有望逐步得到解决。

在此过程中,开发者应结合传统工具(如Tyk API网关)以确保API开发的稳定性和可靠性。Tyk提供了全面的功能支持,包括身份验证、速率限制和日志记录,是构建高质量API的理想选择。

原文链接: https://tyk.io/blog/using-ai-for-api-design/
#你可能也喜欢这些API文章!

我们有何不同?

API服务商零注册

多API并行试用

数据驱动选型,提升决策效率

查看全部API→
🔥

热门场景实测,选对API

#AI文本生成大模型API

对比大模型API的内容创意新颖性、情感共鸣力、商业转化潜力

25个渠道
一键对比试用API 限时免费

#AI深度推理大模型API

对比大模型API的逻辑推理准确性、分析深度、可视化建议合理性

10个渠道
一键对比试用API 限时免费