Skip to content

Latest commit

 

History

History

README.md

🧭 CozeLoop SDK

npm version License: MIT

English | 简体中文

Official Node.js SDK for CozeLoop API platform.

Quick Start

1. Installation

npm install @cozeloop/ai
# or
pnpm install @cozeloop/ai

2. Basic Usage

import { ApiClient, PromptHub, PromptAsAService } from '@cozeloop/ai';

// 1. Setup API client
const apiClient = new ApiClient({
  baseURL: 'https://api.coze.cn',
  token: 'your_access_token',
});

// 2. Using `PromptHub` or `PromptAsAService`
const hub = new PromptHub({
  // or set it as process.env.COZELOOP_WORKSPACE_ID,
  workspaceId: 'your_workspace_id',
  apiClient,
});
// hub.getPrompt(key, version);
// hub.formatPrompt(prompt);

const model = new PromptAsAService({
  // or set it as process.env.COZELOOP_WORKSPACE_ID,
  workspaceId: 'your_workspace_id',
  // prompt to invoke as a service
  prompt: {
    prompt_key: 'your_prompt_key',
  },
  apiClient,
});
// model.invoke({
//   messages: [{ role: 'user', content: 'hi' }],
// });
// model.stream({
//   messages: [{ role: 'user', content: 'hi' }],
// });

Key Features

  • 🗂️ Prompt Hub: Develop, submit and publish prompts on CozeLoop, and access them via PromptHub
  • 🛠️ Prompt as a Service: Develop, submit and publish prompts on CozeLoop, and invoke them as services
  • 🔐 Authentication Methods: PAT, SAT and JWT
  • ⚙️ Configurable: Timeout, headers, signal, debug options

Authentication Options

  1. Personal Access Token (PAT, the simplest way)
const apiClient = new ApiClient({
  baseURL: 'https://api.coze.cn',
  token: 'your_pat_token',
});
  1. JWT
const authFlow = new OAuthJWTFlow({
  baseURL: 'https://api.coze.cn',
  appId: '1177045121217', // Auth App Id
  aud: 'api.coze.cn', // just use api.coze.cn
  keyid: 'public_key_id of Auth App',
  privateKey: 'private_key_content',
});

const tokenResp = await authFlow.getToken();
const apiClient = new ApiClient({
  baseURL: 'https://api.coze.cn',
  token: tokenResp.access_token,
});