Skip to content

Commit

Permalink
Add AI summary feature to render.rb
Browse files Browse the repository at this point in the history
Add AI summary feature to the `render.rb` file.

* **render.rb**
  - Add `require 'dotenv/load'` to load environment variables from `.env` file.
  - Add `require 'json'` to parse JSON responses from AI API.
  - Add method `fetch_ai_summary` to query AI API using provided curl command.
  - Modify `feed` method to call `fetch_ai_summary` and include AI-generated summary.

* **templates/index.html.erb**
  - Update to display AI-generated summary for each feed item.

* **README.md**
  - Add instructions on setting up AI summary feature.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/djdefi/rss-firehose?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
djdefi committed Dec 18, 2024
1 parent 44495e2 commit 72aa152
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
38 changes: 38 additions & 0 deletions render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'erb'
require 'rss'
require 'httparty'
require 'dotenv/load'
require 'json'

def title
ENV['RSS_TITLE'] || 'News Firehose'
Expand Down Expand Up @@ -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}"
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions templates/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<% feed(url).items.each do |item| -%>
<li>
<a href='<%= item.link %>' > <%= item.title %></a>
<p><%= item.summary %></p>
</li>
<% end -%>
</ul>
Expand Down

0 comments on commit 72aa152

Please sign in to comment.