1.2.0 • Published 4 months ago

@coze/api v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Introduction

The Coze API SDK for Node or Browser is a versatile tool for integrating Coze's open APIs into your projects.

Requirements

  • Node.js: >=14

Install

npm install @coze/api

Usage

Examples

ExampleFile
Personal Access Token authenticationexamples/node/src/auth/auth-pat.mjs
OAuth Web application authenticationexamples/node/src/auth/auth-oauth-web.mjs
OAuth PKCE authenticationexamples/node/src/auth/auth-oauth-pkce.mjs
OAuth JWT authenticationexamples/node/src/auth/auth_oauth_jwt.mjs
OAuth JWT authentication for channel accessexamples/node/src/auth/auth-oauth-jwt-channel.mjs
OAuth Device Code flow authenticationexamples/node/src/auth/auth-oauth-device.mjs
Chat (streaming and non-streaming)examples/node/src/chat.mjs
Bot creation and managementexamples/node/src/bot.mjs
File operationsexamples/node/src/file.mjs
Knowledge base document managementexamples/node/src/knowledge.mjs
Workspace listingexamples/node/src/workspace.mjs
Conversation managementexamples/node/src/conversation.mjs
Workflow execution (streaming and non-streaming)examples/node/src/workflow.mjs
Error handling examplesexamples/node/src/error-handle.mjs
Request options (Support all axios options)examples/node/src/request-options.mjs

Initialize the coze client

Fixed Auth Token

create Personal Auth Token at 扣子 or Coze Platform

import { CozeAPI,COZE_COM_BASE_URL ,COZE_CN_BASE_URL } from '@coze/api';

const client = new CozeAPI({
  token: 'your_token',
  baseURL: COZE_COM_BASE_URL, // you can change to COZE_CN_BASE_URL if you use https://use coze.cn
});

OAuth、JWT 和 Device Code

支持 OAuth 2.0 、 JWT 和 Device Code 等多种认证方式,请参考:

chat (stream mode)

Call the coze.chat.stream method to create a chat. The create method is a streaming chat and will return a Chat Iterator. Developers should iterate the iterator to get chat event and handle them.

import { CozeAPI, ChatEventType,COZE_COM_BASE_URL } from '@coze/api';
import { clearLine, cursorTo } from 'node:readline';

const client = new CozeAPI({
  token: 'your_token',
  baseURL: COZE_COM_BASE_URL,
});
const query = 'Hello';

async function streamingChat() {
  const v = await client.chat.stream({
    bot_id: botId,
    auto_save_history: false,
    additional_messages: [
      {
        role: 'user',
        content: query,
        content_type: 'text',
      },
    ],
  });

  for await (const part of v) {
    if (part.event === ChatEventType.CONVERSATION_CHAT_CREATED) {
      console.log('[START]');
      callback && callback(part.data);
    } else if (part.event === ChatEventType.CONVERSATION_MESSAGE_DELTA) {
      process.stdout.write(part.data.content);
    } else if (part.event === ChatEventType.CONVERSATION_MESSAGE_COMPLETED) {
      const { role, type, content } = part.data;
      if (role === 'assistant' && type === 'answer') {
        process.stdout.write('\n');
      } else {
        console.log('[%s]:[%s]:%s', role, type, content);
      }
    } else if (part.event === ChatEventType.CONVERSATION_CHAT_COMPLETED) {
      console.log(part.data.usage);
    } else if (part.event === ChatEventType.DONE) {
      console.log(part.data);
    }
  }

  console.log('=== End of Streaming Chat ===');
}

streamingChat();

chat (non-stream mode)

To simplify the call, the SDK provides a wrapped function to complete non-streaming chat, polling, and obtaining the messages of the chat. Developers can use create_and_poll to simplify the process.

import { CozeAPI, ChatStatus,COZE_COM_BASE_URL } from '@coze/api';

const client = new CozeAPI({
  token: 'your_token',
  baseURL: COZE_COM_BASE_URL,
});
const query = 'Hello';

async function nonStreamingChat() {
  console.log('=== Non-Streaming Chat ===');

  const v = await client.chat.create({
    bot_id: botId,
    user_id: '123***',
    additional_messages: [
      {
        role: 'user',
        content: query,
        content_type: 'text',
      },
    ],
  });

  const v = await client.chat.createAndPoll({
    bot_id: botId,
    user_id: '123',
    additional_messages: [
      {
        role: 'user',
        content: query,
        content_type: 'text',
      },
    ],
  });
  if (v.chat.status === ChatStatus.COMPLETED) {
    for (const item of v.messages) {
      console.log('[%s]:[%s]:%s', item.role, item.type, item.content);
    }
    console.log('usage', v.chat.usage);
  }
}
nonStreamingChat();

other examples

How to contribute

develop

pnpm install
pnpm run start

demo(node)

cd examples/node
npm install
cp config.default.js config.js
# and then edit config.js to use your PAT and other params

node chat.mjs
// or
COZE_ENV=zh node chat.mjs

demo(browser)

cd examples/web
npm install
npm run start

test

pnpm run test
1.2.1-beta.22

4 months ago

1.2.0

6 months ago

1.0.19

9 months ago

1.0.2

12 months ago

1.2.1-beta.21

5 months ago

1.0.18

9 months ago

1.0.1

12 months ago

1.1.1-alpha.fc4190

7 months ago

1.0.17

10 months ago

1.2.1-beta.23

4 months ago

1.0.16

10 months ago

1.2.1-alpha.c77c39

6 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.2.1-alpha.0ec8e1

5 months ago

1.0.6

11 months ago

1.0.5

12 months ago

1.2.1-beta.20

5 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.21

8 months ago

1.0.20

8 months ago

1.2.1-beta.11

5 months ago

1.2.1-beta.10

5 months ago

1.2.1-beta.13

5 months ago

1.2.1-beta.12

5 months ago

1.2.1-beta.15

5 months ago

1.2.1-beta.14

5 months ago

1.2.1-alpha.0f9932

5 months ago

1.2.1-beta.17

5 months ago

1.1.0-beta.7

7 months ago

1.1.1-beta.2

7 months ago

1.1.0-beta.2

8 months ago

1.1.0-beta.1

8 months ago

1.1.1-beta.1

7 months ago

1.0.15-beta.8

10 months ago

1.1.0-beta.6

7 months ago

1.0.10-alpha.576f79

10 months ago

1.1.1-beta.7

6 months ago

1.1.0-beta.5

7 months ago

1.1.0-beta.4

7 months ago

1.1.0-beta.3

7 months ago

1.2.1-beta.19

5 months ago

1.0.15-beta.2

10 months ago

1.0.15-beta.1

10 months ago

1.0.16-beta.1

10 months ago

1.0.15-beta.5

10 months ago

1.2.1-beta.9

5 months ago

1.1.1

7 months ago

1.1.0

8 months ago

1.0.10-alpha.48e991

10 months ago

1.0.19-beta.1

8 months ago

1.0.10-alpha.adddc0

10 months ago

1.2.1-alpha.a88fed

5 months ago

1.2.1-alpha.c0557c

6 months ago

1.2.1-beta.1

6 months ago

1.2.1-beta.2

6 months ago

1.2.1-beta.3

6 months ago

1.2.1-beta.4

6 months ago

1.2.1-beta.5

6 months ago

1.2.1-beta.7

5 months ago

1.0.10-alpha.f3cf80

10 months ago

1.1.0-alpha.272a30

7 months ago

1.2.1-alpha.6b5ec3

5 months ago

1.2.1-alpha.8e8db2

6 months ago

1.1.1-alpha.547c24

6 months ago

1.0.20-beta.2

8 months ago

1.2.1-alpha.93cd9f

6 months ago

1.0.10-alpha.899d79

10 months ago

1.0.11

11 months ago

1.0.10

11 months ago

1.1.1-alpha.c3de48

6 months ago

1.0.21-beta.1

8 months ago

1.0.20-beta.1

8 months ago

1.0.15-alpha.4d6fb7

10 months ago

1.0.21-beta.2

8 months ago

1.2.1-alpha.45e318

6 months ago

1.0.15

10 months ago

1.0.14

11 months ago

1.0.13

11 months ago

1.0.10-alpha.595d13

10 months ago

1.0.12

11 months ago

1.0.0

12 months ago