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.
 
 

207 lines
7.0 KiB

import { readFileSync } from "fs";
export async function jsonToAgent(filePath, agentPromptFile='agent.txt') {
console.log('processing file:', filePath);
let content;
let simple_content;
const fileRes = readFileSync(filePath);
if (fileRes) {
const filecontent = fileRes.toString();
// simplify content
let jsonData;
try {
jsonData = JSON.parse(filecontent);
} catch (err) {
console.error("Failed to parse JSON file:", err);
return;
}
content = jsonData;
let simplify = { input: [] };
simplify.input.push(jsonData.thread.text);
jsonData.replies.forEach(element => {
simplify.input.push(element.text);
});
simple_content = JSON.stringify(simplify);
// console.log(simple_content);
} else {
console.error("Failed to load JSON file");
return;
}
// return;
console.log('sending to agent...', content.replies.length + 1);
// 使用 OpenAI Chat Completions API 搭配 Structured Outputs (json_schema)
const response = await fetch('https://api.openai.com/v1/responses', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
},
body: JSON.stringify({
// model: "gpt-4.1-mini",
// input: [
// { role: "system", content: system_prompt.replaceAll("{{N}}", content.replies.length + 1) },
// { role: "user", content: simple_content }
// ],
prompt:{
id: "pmpt_69607acd9c10819696e7a25fd66cdd6b016fc7e2ef2c3ca5",
// version: "4",
variables:{
count: (content.replies.length + 1).toString(),
content: simple_content
}
},
store: false,
text: {
format:{
type: "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: "留言總數 count"
},
content: {
type: "string",
description: "原始留言內容"
},
teaser:{
type: "string",
description: "10字內具備共鳴的核心情緒"
}
},
required: ["summry", "keywords", "number", "total", "content", "teaser"],
additionalProperties: false
}
}
},
required: ["output"],
additionalProperties: false
}
}
}
})
});
const data = await response.json();
if (data.error) {
console.error("API Error:", data.error);
return null;
}
try {
// 從 choices[0].message.content 中解析 AI 回傳的 JSON 字串
let output_content = data.output.find(item => item.type === "message");
if (!output_content) {
console.error("No message content found in agent response:", data);
return data;
}
// console.log("Raw agent content:", output_content);
// parse JSON
let output = JSON.parse(output_content.content[0].text);
// console.log("Agent output:", output);
// add metadata info
output.output= output.output.map((item, index) => {
let metadata= index==0? content.thread : content.replies[index -1];
return {
...item,
metadata: metadata
};
});
return output;
} catch (err) {
console.error("Failed to parse agent response as JSON:", err);
return data;
}
}
export async function parseTest(filepath){
const fileRes = readFileSync(filepath);
if (!fileRes) {
console.error("Failed to load JSON file");
return;
}
const filecontent = fileRes.toString();
let data;
try {
data = JSON.parse(filecontent);
} catch (err) {
console.error("Failed to parse JSON file:", err);
return;
}
try {
// 從 choices[0].message.content 中解析 AI 回傳的 JSON 字串
let output_content = data.output.find(item => item.type === "message");
if (!output_content) {
console.error("No message content found in agent response:", data);
return data;
}
// console.log("Raw agent content:", output_content);
// parse JSON
let output = JSON.parse(output_content.content[0].text);
// console.log("Agent output:", output);
// add metadata info
output.output= output.output.map((item, index) => {
let metadata= index==0? content.thread : content.replies[index -1];
return {
...item,
metadata: metadata
};
});
return output;
} catch (err) {
console.error("Failed to parse agent response as JSON:", err);
return data;
}
}