Getting Started with AI Streaming Calls in Node.js: A Guide for Indie Developers
As an indie developer or a small team member, you may have keenly observed that the interaction experience of modern applications is being reshaped by AI. In the past, we were accustomed to the traditional mode of "send request -> wait -> receive result all at once." However, with the proliferation of Large Language Models (LLMs), this model faces a huge challenge: model content generation takes time. If users are left staring at a blank screen waiting for ten seconds or longer, the experience is disastrous.
This leads us to the topic of this article—Streaming Calls.
Streaming calls allow data to be pushed to users continuously like flowing water. Users can see text appearing character by character. This "typewriter effect" not only alleviates waiting anxiety but also makes the application look smarter and smoother. For Node.js developers, its asynchronous non-blocking nature is practically made for this.
This article will guide you from scratch on how to quickly connect to mainstream AI models through a unified API gateway service and run your first piece of streaming call code.
Why Do You Need a Unified API Gateway?
Before diving into the code, we need to address a realistic problem: the fragmentation of model providers.
If you want to develop an application that supports GPT-4, Claude 3.5, or other open-source models, the traditional approach is to visit the official websites of OpenAI, Anthropic, and others separately to register accounts, bind credit cards, and read different API documentation. For indie developers and small teams, this implies huge maintenance costs and switching barriers.
This is where ThisToken.AI comes in. It acts as an intelligent aggregation gateway, providing a standard OpenAI SDK compatible interface. What does this mean? You only need to maintain one set of code, and by simply changing the model parameter, you can seamlessly switch between different LLMs. Whether you are saving money using lightweight models or pursuing performance using flagship models, the API calling method remains exactly the same.
Step 1: Registration and Obtaining an API Key
Before writing code, we need to get the "key" to this ecosystem.
- Register Account: Visit the ThisToken.AI official website. As a developer, you don't need complex qualification audits; you can complete registration with simple email or phone verification.
- Get Key: After logging into the console, find "API Keys" or a similar menu item in the sidebar. Click "Create New Key," and the system will generate a string starting with
sk-. - Save Securely: Be sure to copy and save this key immediately. For security reasons, the key is usually only displayed once upon creation. If it is leaked, please revoke and reset it immediately.
Once you have the Key, we can proceed to the coding section.
Step 2: Environment Preparation
This article assumes that your local environment has Node.js installed (v18 or higher is recommended for better fetch and stream processing support).
First, create a new project folder and initialize it:
mkdir ai-streaming-demo
cd ai-streaming-demo
npm init -yTo simplify development, we will use the official standard openai Node.js SDK. Although we are calling the ThisToken.AI gateway, since the interface is fully compatible, we can directly reuse this mature library:
npm install openaiStep 3: Writing Your First Streaming Call Code
Next is the main event. We will write a piece of code to implement the most basic chat function. Please read the comments in the code carefully; this will help you understand the core logic of stream processing.
Create a file named app.js in your project root directory and copy the following code:
// 引入 OpenAI SDK
import OpenAI from 'openai';
// 1. 初始化客户端
// 注意:这里我们将 baseURL 指向 ThisToken.AI 的网关地址
const client = new OpenAI({
apiKey: process.env.THISTOKEN_API_KEY || '你的_API_KEY_写在这里_建议使用环境变量',
baseURL: 'https://api.thistoken.ai/v1', // 核心:指定网关地址
});
async function main() {
console.log('AI 正在思考,请稍候...\n');
try {
// 2. 创建流式对话请求
const stream = await client.chat.completions.create({
model: 'gpt-3.5-turbo', // 你可以在这里替换成 'claude-3-haiku-20240307' 或其他支持的模型
messages: [{ role: 'user', content: '请用50个字介绍一下Node.js对开发者最大的优势是什么?Bạn muốn thử Token.AI?
Tạo API Key cấp dự án, bật kênh trong bảng điều khiển và định cấu hình định tuyến, ngân sách và nhật ký kiểm tra.
注册 ThisToken.AI 并获取 API Key