Sentiment analysis in Excel using ChatGPT (in Hindi)

Описание к видео Sentiment analysis in Excel using ChatGPT (in Hindi)

How to classify sentences in Excel worksheet as Postive/Negative or Pro-BJP/Pro-Congress using ChatGPT


//SAMPLE CODE
//NOTE :- Replace "GREATER-THAN" with actual symbol before running the script
async function main(workbook: ExcelScript.Workbook) {

// Set the OpenAI API key - You'll need to add this in the Excel file or replace this part with your key
const apiKey = workbook.getWorksheet("Key").getRange("A1").getValue();
const endpoint: string = "https://api.openai.com/v1/completions";

// get worksheet info
const sheet = workbook.getWorksheet("Sentiment");
let usedRange = sheet.getUsedRange();
let usedRangeValues = usedRange.getValues();
// the ask
for(let i=1;i GREATER-THAN usedRangeValues.length;i++) {
const mytext = usedRangeValues[i][0];
//console.log(mytext);
const ans = sheet.getCell(i, 1);

ans.setValue(" ");

// Set the model engine and prompt
const model: string = "text-davinci-003";
const prompt: (string | boolean | number) = "Classify the sentiment in this sentence as pro-BJP or pro-Congress \"" + mytext + "\"";

// Set the HTTP heade rs
const headers: Headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", `Bearer ${apiKey}`);

// Set the HTTP body
const body: (string | boolean | number) = JSON.stringify({
model: model,
prompt: prompt,
max_tokens: 1024,
n: 1,
temperature: 0.5,
});

// Send the HTTP request
const response: Response = await fetch(endpoint, {
method: "POST",
headers: headers,
body: body,
});

// Parse the response as JSON
const json: { choices: { text: (string | boolean | number) }[] } = await response.json();

// Get the answer - i.e. output
const text: (string | boolean | number) = json.choices[0].text;
ans.setValue(text);
}
}

Комментарии

Информация по комментариям в разработке