Skip to content

请问支持openai nodejs sdk吗? #425

@youwei997

Description

@youwei997

我使用nodejs调用接口返回报错。但是我使用openrouter调用是正常的。apiKey和baseURL都是正确的

APIConnectionError: Connection error.
    at OpenAI.makeRequest (G:\project\electron\AI-RChat\node_modules\.pnpm\openai@5.23.1_zod@4.1.11\node_modules\openai\client.js:295:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async main (G:\project\electron\AI-RChat\out\main\index.js:54:24) {
  status: undefined,
  headers: undefined,
  requestID: undefined,
  error: undefined,
  code: undefined,
  param: undefined,
  type: undefined,
  cause: TypeError: fetch failed
      at node:internal/deps/undici/undici:13510:13
      at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
      at async OpenAI.fetchWithTimeout (G:\project\electron\AI-RChat\node_modules\.pnpm\openai@5.23.1_zod@4.1.11\node_modules\openai\client.js:371:20)
      at async OpenAI.makeRequest (G:\project\electron\AI-RChat\node_modules\.pnpm\openai@5.23.1_zod@4.1.11\node_modules\openai\client.js:262:26)
      at async main (G:\project\electron\AI-RChat\out\main\index.js:54:24) {
    [cause]: Error: read ECONNRESET
        at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) {
      errno: -4077,
      code: 'ECONNRESET',
      syscall: 'read'
    }
  }
}
import OpenAI from "openai"

// const openai = new OpenAI({
//   baseURL: "https://openrouter.ai/api/v1",
//   apiKey: "xxxxx"
// })

const openai = new OpenAI({
  baseURL: "https://api.chatanywhere.tech/v1",
  apiKey: "xxxxx"
})

async function main() {
  console.log("start")

  try {
    const completion = await openai.chat.completions.create({
      model: "gpt-4o-mini",
      messages: [{ role: "user", content: "Say this is a test" }]
    })

    console.log(completion.choices[0].message.content)
  } catch (error) {
    console.error(error)
  }
}
main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions