Commit c795f812 by lijiabin

chore: debugger

parent 0a161bc8
import cors from 'cors' import cors from 'cors'
import express from 'express' import express from 'express'
import { setAuthorization } from './utils/headers' import { setAuthorization } from './utils/headers'
import { createOpenAIClient } from './utils/openai' import { getOpenAIClient } from './utils/openai'
import { registerChatRoutes } from './route/chat' import { registerChatRoutes } from './route/chat'
import { registerBasicRoutes } from './route/basic' import { registerBasicRoutes } from './route/basic'
const client = createOpenAIClient() const client = getOpenAIClient()
const app = express() const app = express()
const PORT = 4000 const PORT = 4000
......
import fs from 'fs'
import { getOpenAIClient } from './openai'
const openai = getOpenAIClient()
export async function createFile(filePath: string) {
let result
if (filePath.startsWith('http://') || filePath.startsWith('https://')) {
// Download the file content from the URL
const res = await fetch(filePath)
console.log(res, 22)
const buffer = await res.arrayBuffer()
const urlParts = filePath.split('/')
const fileName = urlParts[urlParts.length - 1]
const file = new File([buffer], fileName)
result = await openai.files.create({
file: file,
purpose: 'assistants',
})
} else {
// Handle local file path
const fileContent = fs.createReadStream(filePath)
result = await openai.files.create({
file: fileContent,
purpose: 'assistants',
})
}
return result.id
}
// Replace with your own file path or URL
const fileId = await createFile('https://cdn.openai.com/API/docs/deep_research_blog.pdf')
console.log(fileId, 11)
import dotenv from 'dotenv' import dotenv from 'dotenv'
import OpenAI from 'openai' import OpenAI from 'openai'
export function createOpenAIClient() { let openaiClient: OpenAI | null = null
export function getOpenAIClient() {
if (openaiClient) return openaiClient
// 只在这里加载 .env.local,避免启动时重复/冲突 // 只在这里加载 .env.local,避免启动时重复/冲突
dotenv.config({ path: '.env.local', override: true }) dotenv.config({ path: '.env.local', override: true })
...@@ -18,6 +22,6 @@ export function createOpenAIClient() { ...@@ -18,6 +22,6 @@ export function createOpenAIClient() {
process.exit(1) process.exit(1)
} }
return new OpenAI({ apiKey, baseURL }) openaiClient = new OpenAI({ apiKey, baseURL })
return openaiClient
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment