# FastAi **Repository Path**: phoenix1121/fast-ai ## Basic Information - **Project Name**: FastAi - **Description**: 一款开箱即用的chat ai框架,实现简单调用,直接填入token验证就可以调用各家接口的js框架 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-10-19 - **Last Updated**: 2025-10-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # fastai-chat 一个简单易用的 AI 聊天接口库,支持 OpenAI API,让开发者能够快速集成 AI 对话功能到项目中。 ## ✨ 特性 - 🤖 支持 OpenAI GPT-4o-mini 模型 - 📦 轻量级,无额外依赖 - 🔧 TypeScript 完整支持 - 🚀 简单易用的 API - ⚡ 基于原生 fetch,性能优秀 - 🛡️ 完善的错误处理 ## 📦 安装 ```bash npm install fastai-chat ``` 或者使用其他包管理器: ```bash # 使用 pnpm pnpm add fastai-chat # 使用 yarn yarn add fastai-chat ``` ## 🚀 快速开始 ### 1. 获取 OpenAI API Key 首先,你需要在 [OpenAI 官网](https://platform.openai.com/api-keys) 获取 API Key。 > ⚠️ **地区限制说明**: OpenAI API 在某些地区(如中国大陆)可能无法直接访问。如果遇到 403 错误,请: > - 使用 VPN 或代理服务 > - 在支持的地区使用服务 > - 考虑使用云服务器在支持的地区部署 ### 2. 基础使用 ```typescript import { ChatAI } from "fastai-chat"; // 设置你的 OpenAI API Key const apiKey = "your-openai-api-key-here"; // 发送消息并获取回复 const response = await ChatAI.openai(apiKey, "你好,请介绍一下自己"); console.log(response); ``` ### 3. 在 Node.js 项目中使用 ```javascript // 使用 ES 模块 import { ChatAI } from "fastai-chat"; async function chatWithAI() { const apiKey = process.env.OPENAI_API_KEY; const question = "什么是人工智能?"; try { const answer = await ChatAI.openai(apiKey, question); console.log("AI 回答:", answer); } catch (error) { console.error("错误:", error.message); } } chatWithAI(); ``` ### 4. 在 TypeScript 项目中使用 ```typescript import { ChatAI } from "fastai-chat"; interface ChatResponse { question: string; answer: string; } async function getChatResponse(question: string): Promise { const apiKey = process.env.OPENAI_API_KEY!; try { const answer = await ChatAI.openai(apiKey, question); return { question, answer }; } catch (error) { throw new Error(`AI 聊天失败: ${error.message}`); } } // 使用示例 getChatResponse("解释一下量子计算") .then(response => console.log(response)) .catch(error => console.error(error)); ``` ## 🔧 高级用法 ### 环境变量配置 推荐使用环境变量来管理 API Key: ```bash # .env 文件 OPENAI_API_KEY=your-api-key-here ``` ```typescript import { ChatAI } from "fastai-chat"; import dotenv from 'dotenv'; dotenv.config(); const apiKey = process.env.OPENAI_API_KEY; if (!apiKey) { throw new Error("请设置 OPENAI_API_KEY 环境变量"); } const response = await ChatAI.openai(apiKey, "你的问题"); ``` ### 错误处理 ```typescript import { ChatAI } from "fastai-chat"; async function safeChat(apiKey: string, message: string) { try { const response = await ChatAI.openai(apiKey, message); return { success: true, data: response }; } catch (error) { if (error.message.includes("401")) { return { success: false, error: "API Key 无效" }; } else if (error.message.includes("429")) { return { success: false, error: "请求过于频繁,请稍后重试" }; } else if (error.message.includes("403")) { return { success: false, error: "地区不支持或账户受限" }; } else { return { success: false, error: error.message }; } } } ``` ### 批量处理 ```typescript import { ChatAI } from "fastai-chat"; async function batchChat(apiKey: string, questions: string[]) { const results = await Promise.allSettled( questions.map(question => ChatAI.openai(apiKey, question) ) ); return results.map((result, index) => ({ question: questions[index], answer: result.status === 'fulfilled' ? result.value : null, error: result.status === 'rejected' ? result.reason.message : null })); } // 使用示例 const questions = [ "什么是机器学习?", "解释一下深度学习", "AI 的未来发展趋势" ]; batchChat(apiKey, questions) .then(results => console.log(results)); ``` ## 📚 API 参考 ### ChatAI.openai(apiKey, message) 发送消息到 OpenAI API 并获取回复。 **参数:** - `apiKey` (string): OpenAI API Key - `message` (string): 要发送的消息 **返回值:** - `Promise`: AI 的回复内容 **异常:** - 当 API Key 无效时抛出错误 - 当网络请求失败时抛出错误 - 当 OpenAI API 返回错误时抛出错误 ## 🛠️ 开发 如果你想参与开发或查看源码: ```bash # 克隆仓库 git clone https://gitee.com/phoenix1121/fast-ai.git cd fast-ai # 安装依赖 pnpm install # 开发模式运行示例 pnpm dev # 构建项目 pnpm build ``` ## 📄 许可证 MIT License ## 🤝 贡献 欢迎提交 Issue 和 Pull Request! - 项目地址: https://gitee.com/phoenix1121/fast-ai - 问题反馈: https://gitee.com/phoenix1121/fast-ai/issues ## 📞 支持 如果你在使用过程中遇到问题,可以: 1. 查看 [Issues](https://gitee.com/phoenix1121/fast-ai/issues) 寻找解决方案 2. 提交新的 Issue 描述你的问题 3. 联系作者: ZHANG Xinsen --- ⭐ 如果这个项目对你有帮助,请给它一个星标!