commented out the try/catch block in the gpt analysis function

pull/77/head
Maria-Aidarus 2023-11-29 17:57:22 +03:00
rodzic 801cabdce9
commit 326f4bd897
1 zmienionych plików z 37 dodań i 43 usunięć

Wyświetl plik

@ -1,16 +1,6 @@
// import OpenAIApi from 'openai'; const fetch = require('node-fetch'); // Import for webscraping in fetchContentFromURL()
// import Configuration from 'openai/lib/configuration'; import { OpenAIApi, Configuration } from 'openai';
const { Configuration, OpenAIApi } = require('openai'); // const { Configuration, OpenAIApi } = require('openai');
// const fetch = require('node-fetch');
// let fetch;
// async function loadFetch() {
// if (!fetch) {
// fetch = (await import('node-fetch')).default;
// }
// }
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
// Function to fetch content from URL using a web scraping service // Function to fetch content from URL using a web scraping service
async function fetchContentFromURL(url) { async function fetchContentFromURL(url) {
@ -56,34 +46,38 @@ function simplifyContent(content) {
async function performGPTAnalysis(simplifiedContent, apiKey) { async function performGPTAnalysis(simplifiedContent, apiKey) {
// Implement logic to send content to Mistral-7b via OpenRouter for GPT analysis // Implement logic to send content to Mistral-7b via OpenRouter for GPT analysis
// Send content and receive GPT analysis response // Send content and receive GPT analysis response
// Placeholder code
const inferredMediaType = "article";
// const extractedTopics = ["topic1", "topic2"];
try { // this is the code that we tried to use for the GPT Analysis
const configuration = new Configuration({ // try {
apiKey: apiKey, // Use the provided API key // const configuration = new Configuration({
baseURL: "https://openrouter.ai/api/v1" // Your custom API endpoint // apiKey: apiKey, // Use the provided API key
}); // baseURL: "https://openrouter.ai/api/v1" // Your custom API endpoint
// });
const openai = new OpenAIApi(configuration); // const openai = new OpenAIApi(configuration);
// Using the specified prompt // // Using the specified prompt
const prompt = `Analyze the following text and provide the media type and key topics: ${simplifiedContent}`; // const prompt = `Analyze the following text and provide the media type and key topics: ${simplifiedContent}`;
const completion = await openai.createCompletion({ // const completion = await openai.createCompletion({
model: "mistralai/mistral-7b-instruct", // model: "mistralai/mistral-7b-instruct",
prompt: prompt, // prompt: prompt,
max_tokens: 150 // Adjust as needed // max_tokens: 150 // Adjust as needed
}); // });
//return completion.data.choices[0].text.trim(); // //return completion.data.choices[0].text.trim();
return inferredMediaType; // return inferredMediaType;
} catch (error) { // } catch (error) {
console.error('Error with OpenAI completion:', error); // console.error('Error with OpenAI completion:', error);
throw error; // throw error;
} // }
// return inferredMediaType; // however, it gives the error below:
// { "error": "Something went wrong", "details": "Configuration is not a constructor" }
// Placeholder code
const inferredMediaType = ["article"];
const extractedTopics = ["topic1", "topic2"];
return { inferredMediaType, extractedTopics };
} }
// Placeholder function to map inferred values to predefined formats and topics // Placeholder function to map inferred values to predefined formats and topics
@ -108,7 +102,7 @@ function formatResponse(predefinedMediaType, predefinedTopics) {
return response; return response;
} }
async function handler(event) { export async function handler(event) {
try { try {
// Extract URL and API Key from the request body // Extract URL and API Key from the request body
const { url, apiKey } = JSON.parse(event.body); const { url, apiKey } = JSON.parse(event.body);
@ -128,18 +122,19 @@ async function handler(event) {
const simplifiedContent = simplifyContent(fetchedContent); const simplifiedContent = simplifyContent(fetchedContent);
// Step 3: Perform GPT analysis for media type and topics // Step 3: Perform GPT analysis for media type and topics
const responseText = await performGPTAnalysis(simplifiedContent, apiKey); const { inferredMediaType, extractedTopics } = await performGPTAnalysis(simplifiedContent, apiKey);
// Step 4: Map inferred values to predefined formats and topics // Step 4: Map inferred values to predefined formats and topics
// const { predefinedMediaType, predefinedTopics } = mapInferredValues(inferredMediaType, extractedTopics); const { predefinedMediaType, predefinedTopics } = mapInferredValues(inferredMediaType, extractedTopics);
// Step 5: Format the response // Step 5: Format the response
// const formattedResponse = formatResponse(predefinedMediaType, predefinedTopics); const formattedResponse = formatResponse(predefinedMediaType, predefinedTopics);
// Return the formatted response // Return the formatted response
return { return {
statusCode: 200, statusCode: 200,
body: JSON.stringify(responseText), // returning the output of the simplifyContent function, to test the function
body: JSON.stringify(simplifiedContent),
}; };
} catch (error) { } catch (error) {
console.error('Error occurred:', error.message); console.error('Error occurred:', error.message);
@ -148,5 +143,4 @@ async function handler(event) {
body: JSON.stringify({ error: 'Something went wrong', details: error.message }), body: JSON.stringify({ error: 'Something went wrong', details: error.message }),
}; };
} }
} }
module.exports = { handler };