2.0.0 • Published 10 days ago

zhipuai v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 days ago

ZhipuAI Library

The ZhipuAI library provides convenient access to the ZhipuAI API from js applications.

Installation

npm install zhipuai

Usage

import { ChatMessageRole, ModelType, ZhipuAI } from 'zhipuai';

const zhipuai = new ZhipuAI();

const prompt = [
  {
    role: ChatMessageRole.User,
    content: '1 + 1 = ?',
  },
  {
    role: ChatMessageRole.Assistant,
    content: '2',
  },
  {
    role: ChatMessageRole.User,
    content: '3 + 3 = ?',
  },
];

// Invoke API
const invokeData = await zhipuai.invoke({
  model: ModelType.ChatGLMTurbo,
  messages: prompt,
});

// Async Invoke API
const asyncInvokeData = await zhipuai.asyncInvoke({
  model: ModelType.ChatGLMTurbo,
  messages: prompt,
});

// Query Aync Invoke Result API
const queryAsyncInvokeResultData = await zhipuai.queryAsyncInvokeResult(
  asyncInvokeData.task_id,
);

// SSE Invoke API
const events = await zhipuai.sseInvoke({
  model: ModelType.ChatGLMTurbo,
  messages: prompt,
});

for await (const event of events) {
  // handle event
}

// CharacterGLM
const events = await zhipuai.sseInvoke({
  model: ModelType.CharacterGLM,
  messages: [
    {
      role: ChatMessageRole.User,
      content: 'What is your name?',
    },
  ],
  meta: {
    userInfo: 'I am pigteetee',
    userName: 'pigteetee',
    botInfo: 'I am Lego Master',
    botName: 'blastz',
  },
});

let result = '';

for await (const event of events) {
  if (event.event === 'add' || event.event === 'finish') {
    result += event.data;
  }
}

// handle result

License

MIT

2.0.0

10 days ago

1.1.0

6 months ago

1.0.0

6 months ago

0.3.0

7 months ago

0.2.0

8 months ago

0.1.0

9 months ago