brians resume | file system access | ask another llm for help | Search

The code defines a function accessInternet that uses Selenium to extract articles from websites or search for knowledge on the internet based on user input, and summarizes the results using the summarizeArticle function.

The accessInternet function is an asynchronous function that takes a prompt model, session object, and prompt string as input and performs various tasks based on user input. These tasks include extracting articles from websites, searching for knowledge on the internet, and summarizing the results using the summarizeArticle function.

Run example

npm run import -- "access web information"

access web information

const extractArticle = importer.import("extract llm article")
const getClient = importer.import("selenium client")
const summerizeArticle = importer.import("summarize llm article")

const HTTPS_LINK = /https:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)/;

const SEARCH_ENGINES = [
  'https://www.google.com/search?q=',
  'https://www.bing.com/search?q=',
  'https://search.yahoo.com/search?p=',
  'https://www.ask.com/web?q=',
  'https://search.aol.com/aol/search?q=',
  'http://www.baidu.com/s?wd=',
  'https://www.wolframalpha.com/input/?i=',
  'https://duckduckgo.com/?q=',
  'https://www.yandex.com/search/?text=',
  'https://archive.org/search.php?query=',
];

async function accessInternet(promptModel, session, prompt) {
  let q1 = 'Given the following functions:\n'
    + 'extractArticle - extract article text from specific websites\n'
    + 'knowledgeSearch - search the web for related knowledge\n'
    + '\nWhich function would be related to solving this prompt:\n'
    + prompt
    + '\nAnswer with only the functions names, nothing else.'

  console.log('User: ' + q1)
  let a1 = await promptModel(q1)
  console.log('AI: ' + a1)

  let matching = [
    'extractArticle',
    'knowledgeSearch',
  ].filter(func => a1.match(func))

  let driver = await getClient()
  let link = HTTPS_LINK.exec(prompt)
  let summary1 = ''
  if (matching.includes('extractArticle') || link) {
    let article = await extractArticle(driver, link[0])
    let summary = await summerizeArticle(article)
    summary1 = 'TLDR: ' + summary[1] + '\n\n' + summary[0]
  }
  let summary2 = ''
  if(matching.includes('knowledgeSearch')) {
    let q2 = 'What is the search topic for this prompt:\n'
    + prompt
    + '\nOnly return the search text and nothing else.'
    console.log('User: ' + q2)
    let a2 = await promptModel(q2)
    console.log('AI: ' + a2)
    let article = await extractArticle(driver, 'https://www.google.com/search?q=' + a2)
    let summary = await summerizeArticle(article)
    summary2 = 'TLDR: ' + summary[1] + '\n\n' + summary[0]
  }
  driver.quit()
  return summary1 + summary2
}

module.exports = accessInternet

What the code could have been:

const importer = require('./importer'); // Import the importer module
const { extractArticle, summerizeArticle } = importer.import(['extract llm article','summarize llm article']); // Import functions
const Client = importer.import('selenium client'); // Import the selenium client class

// Regular expression for validating HTTPS links
const HTTPS_LINK_REGEX = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/i;

// Array of search engines
const SEARCH_ENGINES = [
  'https://www.google.com/search?q=',
  'https://www.bing.com/search?q=',
  'https://search.yahoo.com/search?p=',
  'https://www.ask.com/web?q=',
  'https://search.aol.com/aol/search?q=',
  'http://www.baidu.com/s?wd=',
  'https://www.wolframalpha.com/input/?i=',
  'https://duckduckgo.com/?q=',
  'https://www.yandex.com/search/?text=',
  'https://archive.org/search.php?query=',
];

/**
 * Accesses the internet by interacting with the user and searching for related knowledge.
 * 
 * @param {importer} promptModel - The model for generating prompts.
 * @param {Client} session - The selenium client session.
 * @param {string} prompt - The user prompt.
 * @returns {Promise} A summary of the search results in markdown format.
 */
async function accessInternet(promptModel, session, prompt) {
  // Generate the initial prompt for the user
  const q1 = `Given the following functions:\n\
  ${Object.keys(importer.importedFunctions).join('\n')}\n\
  \n\
  Which function would be related to solving this prompt:\n\
  ${prompt}\n\
  \n\
  Answer with only the functions names, nothing else.`;

  // Log the prompt and its response
  console.log('User:', q1);
  const a1 = await promptModel(q1);
  console.log('AI:', a1);

  // Identify the matching functions
  const matchingFunctions = Object.keys(importer.importedFunctions).filter(func => a1.match(func));

  // Create a new selenium client session
  const driver = new Client();

  // Get the link from the prompt
  let link = HTTPS_LINK_REGEX.exec(prompt);

  // Initialize the summaries
  let summaries = [];

  // Check if extractArticle is a matching function or if a link is present
  if (matchingFunctions.includes('extractArticle') || link) {
    // Extract the article text
    const article = await extractArticle(driver, link? link[0] : null);
    // Summarize the article
    const summary = await summerizeArticle(article);
    // Add the summary to the list
    summaries.push({ key: 'extractArticle', value: `TLDR: ${summary[1]}\n\n${summary[0]}` });
  }

  // Check if knowledgeSearch is a matching function
  if (matchingFunctions.includes('knowledgeSearch')) {
    // Generate the search query
    const q2 = `What is the search topic for this prompt:\n\
      ${prompt}\n\
      \n\
      Only return the search text and nothing else.`;
    console.log('User:', q2);
    const a2 = await promptModel(q2);
    console.log('AI:', a2);
    // Extract the article text
    const article = await extractArticle(driver, `https://www.google.com/search?q=${a2}`);
    // Summarize the article
    const summary = await summerizeArticle(article);
    // Add the summary to the list
    summaries.push({ key: 'knowledgeSearch', value: `TLDR: ${summary[1]}\n\n${summary[0]}` });
  }

  // Quit the selenium client session
  await driver.quit();

  // Join the summaries into a single string
  return summaries.map(summary => summary.value).join('\n\n');
}

module.exports = accessInternet;

Code Breakdown

Importing Modules

The code imports three modules:

Regular Expressions

The code defines a regular expression HTTPS_LINK to match HTTPS links.

Search Engines

The code defines an array SEARCH_ENGINES containing URLs of various search engines.

accessInternet Function

The accessInternet function is an asynchronous function that takes three parameters:

The function does the following:

  1. Ask User for Function Choice: It logs a question to the console asking the user to choose a function related to the prompt.
  2. Get User Response: It awaits the user's response and logs it to the console.
  3. Match Function Choice: It filters the user's response to match one of the two functions: extractArticle or knowledgeSearch.
  4. Get Driver and Link: It gets a Selenium client driver and extracts an HTTPS link from the prompt.
  5. Extract Article (if matched): If the user chose extractArticle or a link was found, it extracts the article text from the link using extractArticle function.
  6. Summarize Article (if extracted): If an article was extracted, it summarizes the article using summarizeArticle function.
  7. Search Knowledge (if matched): If the user chose knowledgeSearch, it asks the user for a search topic, extracts the article text from the search engine, and summarizes the article.

Variables and Strings

The code defines several variables and strings:

Notes