Last modified: 2025-02-11 16:13:13.251724
Compiled: Tue Feb 11 16:13:15 2025

1 chatAI4R and Genspark AI Search Documentation

This document demonstrates how to extract content from a webpage, generate AI-driven drafts using the chatAI4R package in combination with Genspark AI Search, and finally automate posting to Twitter.


1.1 1. Load Required Packages

Before you begin, load the necessary packages. The chatAI4R package is used to interact with OpenAI’s API, and other packages (e.g., magrittr and clipr) facilitate the workflow.

library(chatAI4R)

1.2 2. Set API Keys

Set the API keys for each web API. For example, to use OpenAI’s API, register at OpenAI API Keys and set your key as follows:

# Set your key for the OpenAI API
Sys.setenv(OPENAI_API_KEY = "Your API key")

1.3 3. Using Genspark AI Search with chatAI4R

This section shows how to extract webpage content, generate a draft using AI, and fine-tune it.

1.3.1 Step 3.1: Extract Main Content

First, copy the URL of the webpage you wish to analyze. Then, extract its main text content using an extraction function provided via an external source.

# Load the extraction script from the web
source("https://gist.githubusercontent.com/kumeS/f252b967f8060dcc56c7feb1e33c8b85/raw/617b25e2eaa0fdf581dbc1b77009b7bf068c8847/extract_and_decode_main_content.R")

# Example usage:
url <- "https://..."
# Alternatively, read the URL from the clipboard:
url <- clipr::read_clip()

# Uncomment the following line to open the URL in your browser:
# browseURL(url)

body_content <- extract_and_decode_main_content(url)
print(body_content)

# Pass URLs copied to the clipboard, extract the content, and write the result back to the clipboard:
library(magrittr)
clipr::read_clip() %>%
  extract_and_decode_main_content()  %>%
  clipr::write_clip()

1.3.2 Step 3.2: Generate and Fine-Tune the Draft

Use the chat4R_streaming function to generate a draft based on the extracted content. A system message (sourced from an external file) provides the context to guide the AI’s response.

# Define the system settings by sourcing the external file
source("https://gist.githubusercontent.com/kumeS/5452be22101600bee91d4153f1d82680/raw/cfd4da6fcd012e3544f3bc4d1a9ddb3854df6dfd/system_set.R")

res <- chat4R_streaming(
  content = body_content,
  Model = "gpt-4o-mini",
  temperature = 1,
  system_set = system_set
)

1.3.3 Step 3.3: Chain Functions Using Pipes

For a streamlined workflow, you can pipe the execution of multiple functions together using the magrittr package. In this example, a URL is read from the clipboard, its content extracted, and then passed directly to chat4R_streaming:

library(magrittr)

res <- clipr::read_clip() %>%
  extract_and_decode_main_content() %>%
  chat4R_streaming(
    content = .,
    Model = "gpt-4o-mini",
    temperature = 1,
    system_set = system_set
  )

1.4 4. Translation (Optional)

If you need to translate the content into another language, you can add your translation code here. (This section is currently left blank for your customization.)

# Insert your translation code here.

1.5 5. Posting to Twitter

To post your generated content on Twitter, start by signing up on the Twitter Developer Portal.

After setting up your account, you can use the rtweet package to authenticate and post tweets.

# Load the rtweet package
# install.packages("rtweet")
library(rtweet)

# Display your Twitter client information
client_list()
rtweet::auth_as()

# Post a tweet
tweet_post(text = "This is a sample tweet generated via the chatAI4R package!")

# Search for recent tweets containing a specific keyword
tweet_search_recent(q = "sample tweet")

# Open Twitter in your browser to verify your tweet
browseURL("https://twitter.com/home?lang=en")

1.6 6. Conclusion

This document demonstrated how to extract webpage content using Genspark AI Search, process it using the chatAI4R package, and automate the posting of AI-generated content to Twitter. You can modify the code and parameters as needed for your specific application.

Happy automating!