diff --git a/README.md b/README.md index 0121d2b60..ce353d8bf 100644 --- a/README.md +++ b/README.md @@ -63,3 +63,27 @@ Available environment variable options: "RSS_TITLE=My News" "RSS_DESCRIPTION=My really awesome news aggregation page" ``` + +### Setting up AI Summary Feature + +To enable the AI summary feature, follow these steps: + +1. Set up your environment variables in a `.env` file or export them in your shell: + +``` +export GITHUB_TOKEN="your_github_token" +``` + +2. Ensure you have the required gems by running: + +``` +bundle install +``` + +3. Run the render script: + +``` +ruby render.rb +``` + +The AI summary feature will now be enabled, and each feed item will include an AI-generated summary. diff --git a/render.rb b/render.rb index 45a7ce91b..ca6f3470d 100644 --- a/render.rb +++ b/render.rb @@ -4,6 +4,8 @@ require 'erb' require 'rss' require 'httparty' +require 'dotenv/load' +require 'json' def title ENV['RSS_TITLE'] || 'News Firehose' @@ -58,6 +60,10 @@ def feed(url) # If the feed is empty or nil, set the rss_content a single item stating the feed is offline rss_content = RSS::Rss.new('2.0') if rss_content.nil? || rss_content.items.empty? + rss_content.items.each do |item| + item.summary = fetch_ai_summary(item.title) + end + rss_content rescue HTTParty::Error, RSS::Error => e puts "Error fetching or parsing feed from '#{url}': #{e.class} - #{e.message}" @@ -68,6 +74,38 @@ def feed(url) end end +def fetch_ai_summary(content) + url = "https://models.inference.ai.azure.com/chat/completions" + headers = { + "Content-Type" => "application/json", + "Authorization" => "Bearer #{ENV['GITHUB_TOKEN']}" + } + body = { + "messages": [ + { + "role": "system", + "content": "Provide a concise summary of a news brief, focusing on the most important details and key context." + }, + { + "role": "user", + "content": content + } + ], + "model": "gpt-4o", + "temperature": 1, + "max_tokens": 4096, + "top_p": 1 + }.to_json + + response = HTTParty.post(url, headers: headers, body: body) + if response.code == 200 + json_response = JSON.parse(response.body) + json_response["choices"].first["message"]["content"] + else + "Summary not available" + end +end + begin render_manifest render_html diff --git a/templates/index.html.erb b/templates/index.html.erb index b66a029f4..c1ff751fe 100644 --- a/templates/index.html.erb +++ b/templates/index.html.erb @@ -30,6 +30,7 @@ <% feed(url).items.each do |item| -%>
<%= item.summary %>