Skip to content

MetaGLM/zhipuai-sdk-nodejs-v4

Repository files navigation

ZhipuAI Open Platform Node.js SDK

npm version License Node.js

中文文档

The official Node.js SDK for ZhipuAI Open Platform Big Model API, enabling developers to easily integrate ZhipuAI's powerful AI capabilities into their Node.js applications.

✨ Features

  • 🚀 Type-safe API: Complete TypeScript type definitions for excellent development experience
  • 🔧 Easy Integration: Simple and intuitive API design for quick integration
  • High Performance: Built with modern Node.js libraries for optimal performance
  • 🛡️ Secure: Built-in authentication and token management
  • 📦 Lightweight: Minimal dependencies for easy project integration
  • 🌊 Streaming Support: Full support for streaming response handling

📦 Installation

Requirements

  • Node.js 14 or higher
  • npm, yarn, or pnpm

npm

npm install zhipuai-sdk-nodejs-v4

yarn

yarn add zhipuai-sdk-nodejs-v4

pnpm

pnpm add zhipuai-sdk-nodejs-v4

📋 Dependencies

This SDK uses the following core dependencies:

Library Version
axios ^1.6.7
jsonwebtoken ^9.0.2

🚀 Quick Start

Basic Usage

  1. Create a Client with your API key
  2. Call the desired API methods

Client Configuration

import { ZhipuAI } from 'zhipuai-sdk-nodejs-v4';

// Use API key from environment variable
const ai = new ZhipuAI();

// Or pass API key directly
const ai = new ZhipuAI({
    apiKey: 'your_api_key_here',
    baseUrl: 'https://open.bigmodel.cn/api/paas/v4', // Optional, default value
    timeout: 30000, // Optional, request timeout
    cacheToken: true // Optional, whether to cache token
});

💡 Examples

Chat Model Invocation

Synchronous Invocation

  • Basic Chat
import { ZhipuAI } from 'zhipuai-sdk-nodejs-v4';

const dialogue = async () => {
    const ai = new ZhipuAI();
    const result = await ai.createCompletions({
        model: "glm-4",
        messages: [
            {"role": "user", "content": "What is the relationship between ZhipuAI and ChatGLM?"}
        ],
        stream: false
    });
    console.log(result);
};

dialogue();

Streaming Invocation (SSE)

  • Basic Chat
import { ZhipuAI } from 'zhipuai-sdk-nodejs-v4';

const streamDialogue = async () => {
    const ai = new ZhipuAI();
    const stream = await ai.createCompletions({
        model: "glm-4",
        messages: [
            {"role": "user", "content": "What is the relationship between ZhipuAI and ChatGLM?"}
        ],
        stream: true
    });
    
    // Process streaming results
    for await (const chunk of stream) {
        console.log(chunk.toString());
    }
};

streamDialogue();

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

📞 Support


Note: This SDK is currently in active development. If you encounter any bugs or need additional features, please submit an issue.

About

NodeJs SDK for ZhiPu AI

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •