Commit ea87652c by Walter Huang

init: project

parents
# Bun Starter
Quickly get started with [Bun](https://bun.sh/) using this starter! Bun is a fast all-in-one JavaScript runtime with a focus on speed.
- This starter starts a Bun HTTP server on [localhost:3000](http://localhost:3000).
- You can check [http.ts](./http.ts) to see how the server is started.
- If you want to upgrade Bun, you can change `BUN_VERSION` in the [Dockerfile](./.devcontainer/Dockerfile).
\ No newline at end of file
import { generateChatCompletions } from "./requestOpenAI";
Bun.serve({
port: 3000,
async fetch(req) {
const url = new URL(req.url);
// 处理 OPTIONS 预检请求
if (req.method === "OPTIONS") {
return new Response(null, {
status: 204, // No Content
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "86400",
},
});
}
// 处理实际请求
if (url.pathname === "/api/data" && req.method === "POST") {
try {
const body = await req.json();
const { prompt, executeTimes, params } = body;
const data = await generateChatCompletions(
prompt,
executeTimes,
params
);
return new Response(JSON.stringify({ data }), {
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
} catch (error) {
console.error("Error generating chat completions:", error);
return new Response("Internal Server Error", { status: 500 });
}
}
return new Response("Hello from Bun server!", {
headers: {
"Access-Control-Allow-Origin": "*",
},
});
},
});
export async function generateChatCompletions(prompt, executeTimes, params) {
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.API_KEY}`, // Replace with your OpenAI API key
};
const endpoint =
"https://api.openai.com/v1/chat/completions?api-version=2024-02-01";
const defaultConfig = {
model: "gpt-3.5-turbo-0613",
max_tokens: 1200,
temperature: 0.9,
top_p: 1.0,
n: 1,
};
function fetchWithTimeout(url, options = {}, timeout = 60) {
// 创建一个定时 Promise,模拟超时
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error("Request timed out")), timeout * 1000)
);
// 使用 Promise.race 来并行处理 fetch 和 timeout
return Promise.race([fetch(url, options), timeoutPromise]);
}
const sendRequest = async () => {
const payload = {
messages: [{ role: "user", content: prompt }],
...defaultConfig,
...params,
};
try {
const response = await fetchWithTimeout(endpoint, {
method: "POST",
headers,
body: JSON.stringify(payload),
});
const data = await response.json();
return data.choices[0]?.message?.content || ""; // 处理可能的错误
} catch (error) {
console.error("Error fetching completion:", error);
return ""; // 处理网络错误或其他异常
}
};
const requests = Array.from({ length: executeTimes }, sendRequest);
const newValues = await Promise.all(requests);
return newValues;
}
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