Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web scraping function test #4

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions netlify/functions/handleMetadata.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
// Placeholder function to fetch content from URL using a web scraping service
import fetch from 'node-fetch'; // Import for webscraping (fetchContentFromURL(url) function
import OpenAI from "openai"; // Import for performGPTAnalysis(content) function

// Function to fetch content from URL using a web scraping service
async function fetchContentFromURL(url) {
// Implement logic to fetch content from the URL using a web scraping service
// Return the extracted content
// Placeholder code
const content = "<p>This is a sample content fetched from the URL</p>";
return content;
try {
// Make an HTTP GET request to the provided URL
const response = await fetch(url);
// Check if the response status is OK (status code 200)
if (!response.ok) {
throw new Error(`Failed to fetch URL: ${response.statusText}`);
}
// Read the response body as text (HTML content)
const content = await response.text();
// Return the extracted content
return content;
} catch (error) {
throw new Error(`Error fetching URL: ${error.message}`);
}
}

// Placeholder function to simplify the content for GPT analysis
Expand All @@ -20,6 +32,33 @@ function simplifyContent(content) {
async function performGPTAnalysis(content) {
// Implement logic to send content to Mistral-7b via OpenRouter for GPT analysis
// Send content and receive GPT analysis response


// Default code from OpenRouter documentation for the Mistral-7b model
// Retrieved from https://openrouter.ai/models/mistralai/mistral-7b-instruct?tab=api
// Using OpenAI's client API makes this easily replaceable with other models (ex. GPT-4)
```
const openai = new OpenAI({
baseURL: "https://openrouter.ai/api/v1",
apiKey: $OPENROUTER_API_KEY,
defaultHeaders: {
"HTTP-Referer": $YOUR_SITE_URL, // Optional, for including your app on openrouter.ai rankings.
"X-Title": $YOUR_SITE_NAME, // Optional. Shows in rankings on openrouter.ai.
},
// dangerouslyAllowBrowser: true,
})
async function main() {
const completion = await openai.chat.completions.create({
model: "mistralai/mistral-7b-instruct",
messages: [
{ role: "user", content: "Say this is a test" }
],
})
console.log(completion.choices[0].message)
}
main()
```

// Placeholder code
const inferredMediaType = "article";
const extractedTopics = ["topic1", "topic2"];
Expand Down Expand Up @@ -80,7 +119,8 @@ export async function handler(event) {
// Return the formatted response
return {
statusCode: 200,
body: JSON.stringify(formattedResponse),
body: JSON.stringify(fetchedContent),
// body: JSON.stringify(formattedResponse),
};
} catch (error) {
return {
Expand Down