function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Tools')
.addItem('Phân tích', 'analyzeContent')
.addToUi();
}
function analyzeContent() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3;
var lastRow = sheet.getLastRow();
for(var i = startRow; i <= lastRow; i++) {
var status = sheet.getRange(i, 12).getValue(); // Column L
if(status != "thành công") {
var content = sheet.getRange(i, 2).getValue(); // Column B
if(content) {
var response = callGPTAPI(content);
if(response) {
// Fill data
sheet.getRange(i, 5).setValue(response.address); // Column E - Địa chỉ
sheet.getRange(i, 6).setValue(response.price); // Column F - Giá bán
sheet.getRange(i, 7).setValue(response.area); // Column G - Diện tích
sheet.getRange(i, 8).setValue(response.floors); // Column H - Số tầng
sheet.getRange(i, 9).setValue(response.advantages); // Column I - Ưu điểm
sheet.getRange(i, 10).setValue(response.ownerName); // Column J - Tên chủ nhà
sheet.getRange(i, 11).setValue(response.phone); // Column K - Số điện thoại
sheet.getRange(i, 12).setValue("thành công"); // Column L - Trạng thái
}
}
}
}
}
function callGPTAPI(content) {
var apiKey = 'TOKEN LIEN HE BINH VUONG';
var apiEndpoint = 'https://yescale.one/v1/chat/completions';
var prompt = `Analyze the following real estate content and extract these information: address, price, area, number of floors, advantages, owner name, phone number. Only extract information that exists in the content. Return JSON format:
Content: ${content}`;
var payload = {
'model': 'gpt-3.5-turbo',
'messages': [
{'role': 'system', 'content': 'You are a helpful assistant that extracts real estate information.'},
{'role': 'user', 'content': prompt}
],
'temperature': 0.3
};
var options = {
'method': 'post',
'headers': {
'Authorization': 'Bearer ' + apiKey,
'Content-Type': 'application/json'
},
'payload': JSON.stringify(payload)
};
try {
var response = UrlFetchApp.fetch(apiEndpoint, options);
var json = JSON.parse(response.getContentText());
var result = JSON.parse(json.choices[0].message.content);
return {
address: result.address || '',
price: result.price || '',
area: result.area || '',
floors: result.floors || '',
advantages: result.advantages || '',
ownerName: result.owner_name || '',
phone: result.phone || ''
};
} catch(error) {
Logger.log('Error calling GPT API: ' + error);
return null;
}
}
Công khai Cập nhật lần cuối: 2024-10-28 03:21:41 PM