Node.js SSE Client Example

This example shows how to call the Bitseek Chat SSE service using the official openai Node.js library.

Installation

npm install openai

Code Example

import OpenAI from "openai";

async function main() {
  const client = new OpenAI({
    baseURL: "https://chat-proxy.bitseek.ai/v1",
    apiKey: "<API-KEY>",
  });

  const conversationId = `conv-${Date.now()}`;

  const stream = await client.chat.completions.create(
    {
      model: "bitseek-auto",
      messages: [
        {
          role: "user",
          content:
            "Hello, please introduce yourself, then call get_weather for Singapore.",
        },
      ],
      temperature: 0.2,
      tools: [
        {
          type: "function",
          function: {
            name: "get_weather",
            description: "Get weather by city",
            parameters: {
              type: "object",
              properties: { city: { type: "string" } },
              required: ["city"],
            },
          },
        },
      ],
      stream: true,
    },
    {
      headers: {
        Accept: "text/event-stream",
        "X-Conversation-Id": conversationId,
      },
    }
  );

  process.stdout.write("AI: ");
  for await (const chunk of stream) {
    const delta = chunk.choices?.[0]?.delta;
    if (delta?.content) {
      process.stdout.write(delta.content);
    }
  }
  process.stdout.write("\n");
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Key Parameters

  • baseURL: Set to https://chat-proxy.bitseek.ai/v1
  • apiKey: Your valid API key for authentication
  • model: Use bitseek-auto
  • temperature: Optional, normalized to 0 ~ 2
  • tools: Optional, OpenAI tool definitions for tool-call loop
  • stream: true: Enables SSE streaming response

results matching ""

    No results matching ""