
Yahoo Finance API – 完整指南
自2022年11月推出ChatGPT以来,人工智能(AI)工具在科技界引发了广泛关注。这些工具形式多样,功能丰富,但它们的共同目标是提升用户的工作效率和优化工作流程。然而,如果不了解这些工具的工作原理以及如何与之高效交互,使用它们可能会变得具有挑战性。本文将通过一个实际案例,展示如何利用AI工具(如smol-developer)来设计和生成RESTful CRUD API,并探讨其优势与局限性。
在开始本教程之前,请确保满足以下前提条件:
每次调用AI都会消耗一定的费用,例如,本文所需的所有运行成本约为1英镑(GBP)。
完成准备工作后,按照以下步骤设置项目环境:
克隆smol-developer项目到本地并进入项目目录:
git clone https://github.com/smol-ai/developer
cddeveloper
使用pip安装Python依赖项:
pipinstall-rrequirements.txt
将OpenAI API密钥导出为环境变量:
export OPENAI_API_KEY=
测试环境是否配置正确:
python3 main_no_modal.py "a simple hello world application written in nodejs"
运行上述命令后,smol-developer工具将根据输入提示生成初始代码文件,例如一个简单的index.js
文件,内容为“Hello, World!”的控制台日志输出。
在要求AI生成代码之前,明确的需求定义至关重要。AI工具可以根据规范填补空白,但不完整或模糊的规范可能导致意外输出。因此,建议采用迭代的方式逐步完善规范,每次修改后通过AI工具验证效果。
在项目根目录下创建一个名为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
及其他相关文件。
初始生成的代码可能包含不必要的验证逻辑和数据库配置。为此,更新规范以移除这些内容:
"""
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工具后,生成的代码将更符合简化需求,例如使用数组作为数据存储。尽管如此,生成的代码可能仍包含未使用的导入或不匹配的函数调用。
为进一步提高代码一致性和可维护性,可以在规范中明确模块结构和文件内容。例如:
## 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);
This file should serve as an HTTP adapter, and should invoke business logic from the userService
. This file should declare the following functions:
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/
Yahoo Finance API – 完整指南
WordPress REST API 内容注入漏洞分析
四款AI大模型API价格对比:DeepSeek R1、ChatGPT o3-mini、Grok3、通义千问 Max
四款AI大模型API基础参数、核心性能的区别:DeepSeek R1、ChatGPT o3-mini、Grok3、通义千问 Max
2025年多模态大模型API基础参数、核心性能:Deepseek、ChatGPT、文心一言
2025年最新推理大模型API价格对比:通义千问Max vs 豆包1.5 Pro vs 混元Lite
大模型新基座,基于FastAPI,利用Python开发MCP服务器
DeepSeek+ima:打造高效个人知识库,提升学习与工作效率
快速接入腾讯地图MCP Server