You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
5.2 KiB
122 lines
5.2 KiB
import { readFileSync } from "fs";
|
|
|
|
export async function jsonToAgent(filePath) {
|
|
|
|
let system_prompt;
|
|
|
|
const res = readFileSync('./assets/agent.txt');
|
|
if (res) {
|
|
system_prompt = res.toString();
|
|
} else {
|
|
console.error("Failed to load system prompt");
|
|
return;
|
|
}
|
|
|
|
console.log('processing file:', filePath);
|
|
|
|
let content;
|
|
const fileRes = readFileSync(filePath);
|
|
if (fileRes) {
|
|
content = fileRes.toString();
|
|
} else {
|
|
console.error("Failed to load JSON file");
|
|
return;
|
|
}
|
|
|
|
// 使用 OpenAI Chat Completions API 搭配 Structured Outputs (json_schema)
|
|
const response = await fetch('https://api.openai.com/v1/chat/completions', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
|
|
},
|
|
body: JSON.stringify({
|
|
model: "gpt-4o",
|
|
messages: [
|
|
{ role: "system", content: system_prompt },
|
|
{ role: "user", content: content }
|
|
],
|
|
store: false,
|
|
response_format: {
|
|
type: "json_schema",
|
|
json_schema: {
|
|
name: "conversation_analysis",
|
|
strict: true,
|
|
schema: {
|
|
type: "object",
|
|
properties: {
|
|
output: {
|
|
type: "array",
|
|
description: "包含對話中所有留言的分析陣列",
|
|
items: {
|
|
type: "object",
|
|
properties: {
|
|
summry: {
|
|
type: "string",
|
|
description: "簡短精煉的全域對話主題(20字以內)。注意:此欄位在所有 array items 中必須完全一致。"
|
|
},
|
|
keywords: {
|
|
type: "array",
|
|
items: { type: "string" },
|
|
description: "從留言中提取的關鍵字(事件、概念、情緒等)"
|
|
},
|
|
number: {
|
|
type: "integer",
|
|
description: "目前留言的序號"
|
|
},
|
|
total: {
|
|
type: "integer",
|
|
description: "留言總數 M"
|
|
},
|
|
content: {
|
|
type: "string",
|
|
description: "原始留言內容"
|
|
},
|
|
metadata: {
|
|
type: "object",
|
|
description: "原始留言的後設資料",
|
|
properties:{
|
|
text: { type:'string'},
|
|
published_on: { type:'integer'},
|
|
like_count: { type:'integer'},
|
|
reply_count: { type:'integer'},
|
|
username: { type:'string'},
|
|
id: { type:'string'},
|
|
code: { type:'string'},
|
|
parent_post_id: { type:'string'}
|
|
},
|
|
required: ["text", "published_on", "like_count", "reply_count", "username", "id", "code", "parent_post_id"],
|
|
// 在 strict: true 模式下,這必須為 false
|
|
additionalProperties: false,
|
|
}
|
|
},
|
|
required: ["summry", "keywords", "number", "total", "content", "metadata"],
|
|
additionalProperties: false
|
|
}
|
|
}
|
|
},
|
|
required: ["output"],
|
|
additionalProperties: false
|
|
}
|
|
}
|
|
},
|
|
temperature: 0.1
|
|
})
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.error) {
|
|
console.error("API Error:", data.error);
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
// 從 choices[0].message.content 中解析 AI 回傳的 JSON 字串
|
|
const output = JSON.parse(data.choices[0].message.content);
|
|
return output;
|
|
} catch (err) {
|
|
console.error("Failed to parse agent response as JSON:", data);
|
|
return null;
|
|
}
|
|
} |