换 ai apikey

This commit is contained in:
geyun
2026-08-21 08:47:57 +08:00
parent 81f9433691
commit 4108ea882e

View File

@@ -121,9 +121,13 @@ app.use(express.urlencoded({ extended: true })); // 支持 form-data 字段
// 初始化客户端
const client = new OpenAI({
//apiKey: "4pJOKPLhkW7qJQOeQBe9ACXyBrcu7A8bAjTgo2auV5lSUZbmcn1rJQQJ99CEACHYHv6XJ3w3AAAAACOGk1hV",
//baseURL: "https://tech-mp6cvddb-eastus2.cognitiveservices.azure.com/",
//apiKey: "DxvRiQWROzLTapiSHcK0FXKAAXDBlhci1XaqTTpr50sgUsAkmxfUJQQJ99BIACYeBjFXJ3w3AAABACOGZdHp",
apiKey: "DxvRiQWROzLTapiSHcK0FXKAAXDBlhci1XaqTTpr50sgUsAkmxfUJQQJ99BIACYeBjFXJ3w3AAABACOGZdHp",
baseURL: "https://satabot.openai.azure.com/openai/deployments/gpt-4.1",
defaultQuery: { "api-version": "2024-04-01-preview" },
baseURL: "https://satabot.services.ai.azure.com/openai/v1/",
//baseURL: "https://satabot.openai.azure.com/openai/deployments/gpt-4.1",
//defaultQuery: { "api-version": "2024-04-01-preview" },
timeout: 120000,
fetch: fetchIPv4, // 关键点:把自定义 fetch 注入 SDK
// 确保底层也传 agent(可选,但保险)
@@ -132,9 +136,14 @@ const client = new OpenAI({
}
});
const client1 = new OpenAI({
//apiKey: "4pJOKPLhkW7qJQOeQBe9ACXyBrcu7A8bAjTgo2auV5lSUZbmcn1rJQQJ99CEACHYHv6XJ3w3AAAAACOGk1hV",
//baseURL: "https://tech-mp6cvddb-eastus2.cognitiveservices.azure.com/",
//apiKey: "DxvRiQWROzLTapiSHcK0FXKAAXDBlhci1XaqTTpr50sgUsAkmxfUJQQJ99BIACYeBjFXJ3w3AAABACOGZdHp",
apiKey: "DxvRiQWROzLTapiSHcK0FXKAAXDBlhci1XaqTTpr50sgUsAkmxfUJQQJ99BIACYeBjFXJ3w3AAABACOGZdHp",
baseURL: "https://satabot.openai.azure.com/openai/deployments/gpt-5-mini/",
defaultQuery: { "api-version": "2024-04-01-preview" },
baseURL: "https://satabot.services.ai.azure.com/openai/v1/",
//baseURL: "https://satabot.openai.azure.com/openai/deployments/gpt-5-mini/",
//defaultQuery: { "api-version": "2024-04-01-preview" },
timeout: 120000,
fetch: fetchIPv4, // 关键点:把自定义 fetch 注入 SDK
// 确保底层也传 agent(可选,但保险)
@@ -160,8 +169,8 @@ async function directCall() {
console.timeEnd("directCall");
}
test1();
directCall();
//test1();
//directCall();
// 获取 access_token
// getAccessToken();
@@ -2317,8 +2326,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
console.log("====== 开始图像分析55... ======");
if (imageBase64)
{
if (imageBase64) {
const imageAnalysisMessages = [
{
role: "system",
@@ -2350,7 +2358,8 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
{
role: "user",
content: [
{ type: "text", text:
{
type: "text", text:
`
输出格式要求(固定话术,仅替换【】内容,必须1句完整文本):
用户上传了一张【图片类型】的图片,从图片描述来看,这明显是一个【场景属性】的图片,有【标注特征】的特点。
@@ -2375,7 +2384,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
// 去掉max_tokens限制,保证描述完整
const completion = await client.chat.completions.create({
model: "gpt-4.1",
model: "gpt-5.6-luna",
stream: true,
messages: imageAnalysisMessages
});
@@ -2430,7 +2439,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
{ role: "assistant", content: sceneText } // 传入精准场景描述
];
const thinkingStream = await client.chat.completions.create({
model: "gpt-4",
model: "gpt-5.6-luna",
stream: true,
messages: thinkingMessages,
max_completion_tokens: 1500, // 根据需要调整
@@ -2486,7 +2495,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
];
const answerStream = await client.chat.completions.create({
model: "gpt-4",
model: "gpt-5.6-luna",
stream: true,
messages: answerMessages
});
@@ -2528,13 +2537,13 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
if (existingTitle) {
await pool.query(
"INSERT INTO ai_chat_messages (session_id, user_id, role, content,thinking) VALUES (?, ?, 'assistant', ?,?)",
[sessId, userId || null, llmText,sceneText]
[sessId, userId || null, llmText, sceneText]
);
} else {
try {
const titlePrompt = `请根据以下对话内容生成一个简短的标题(不超过15字):\n用户问题: ${userPrompt}\nAI回答: ${llmText}`;
const titleResp = await client1.chat.completions.create({
model: "gpt-5-mini",
model: "gpt-5.6-luna",
messages: [
{ role: "system", content: "你是标题生成助手,只返回标题,不要解释。" },
{ role: "user", content: titlePrompt }
@@ -2545,7 +2554,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
console.log("生成的标题:", title);
await pool.query(
"INSERT INTO ai_chat_messages (session_id, user_id, role, content, title,thinking) VALUES (?, ?, 'assistant', ?, ?,?)",
[sessId, userId || null, llmText, title,sceneText]
[sessId, userId || null, llmText, title, sceneText]
);
} catch (err) {
console.error("生成标题失败:", err.message);
@@ -2566,7 +2575,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
if (!res.headersSent) {
res.status(500).json({ error: err.message });
} else {
res.write(`data: ${JSON.stringify({type: "error",message: err.message})}\n\n`);
res.write(`data: ${JSON.stringify({ type: "error", message: err.message })}\n\n`);
res.end();
}
} finally {
@@ -2574,7 +2583,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
if (userChatNum !== -1 && userChatNum !== undefined) {
userChatNum = Math.max(0, userChatNum - 1);
await setQuota(userId, userChatNum);
await pool.query("update sys_user set ai_chat_num = ? where user_id= ?",[userChatNum, userId]);
await pool.query("update sys_user set ai_chat_num = ? where user_id= ?", [userChatNum, userId]);
console.log("更新用户对话次数:", userId, userChatNum);
}
}