-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated summary endpoint and top links endpoint to include connection…
… to OpenAI
- Loading branch information
1 parent
1459258
commit 87d226b
Showing
14 changed files
with
968 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
class Api::V1::SummariesController < ApplicationController | ||
|
||
rescue_from NoMethodError, with: :not_found_error | ||
|
||
def show | ||
summary = SummaryService.new.summarize | ||
summary_json = JSON.parse(summary.body, symbolize_names: true) | ||
render json: SummarySerializer.new(summary_json).serialize_json | ||
link = Link.find_by(short: params[:link]) | ||
if link.summary == nil || link.summary_timestamp < (Time.current - 259200) #3 days in seconds | ||
new_summary = SummaryService.new.summarize(link.original) | ||
link.update(summary: new_summary) | ||
link.update(summary_timestamp: Time.current) | ||
render json: LinkSerializer.new(link) | ||
else | ||
render json: LinkSerializer.new(link) | ||
end | ||
end | ||
|
||
private | ||
|
||
def not_found_error | ||
render json: { errors: [{ message: 'Link not found in database' }] }, status: :not_found | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class LinkSerializer | ||
include JSONAPI::Serializer | ||
attributes :original, :short, :user_id, :tags, :click_count, :last_click, :private | ||
attributes :original, :short, :user_id, :tags, :click_count, :last_click, :private, :summary | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,40 @@ | ||
class SummaryService | ||
|
||
def get_html(link) | ||
faraday = Faraday.new(link) | ||
response = faraday.get | ||
response.body | ||
end | ||
|
||
def conn | ||
Faraday.new('https://nameless-garden-14218-de5663d17d61.herokuapp.com/') | ||
Faraday.new("https://api.openai.com/v1/chat/completions") do |faraday| | ||
faraday.headers[:Authorization] = "Bearer #{Rails.application.credentials.openai[:key]}" | ||
end | ||
end | ||
|
||
def summarize | ||
conn.get('/api/v1/ping/') | ||
def summarize(link) | ||
html_content = get_html(link) | ||
response = conn.post do |req| | ||
req.headers['Content-Type'] = 'application/json' | ||
req.body = { | ||
model: "gpt-4o-mini", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: "You are a helpful assistant." | ||
}, | ||
{ | ||
role: "user", | ||
content: "Summarize the following content: #{html_content}." | ||
}, | ||
{ | ||
role: "user", | ||
content: "Use 3 numbered bullet points." | ||
} | ||
] | ||
}.to_json | ||
end | ||
openai_response = response.body | ||
JSON.parse(response.body, symbolize_names: true)[:choices][0][:message][:content] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
9QGtxaTLtmlXMP9VSsGdHD+4jeGuHjcQx7kaz5ldHAXhLjA/WTpisoI5Be7FC9q7hDFhZ641LdJkV7rug0mkqMmvZ+9xw2ScX3qEo/PTfM8fQnUha+604Wg0XTXJwODheFA7AEjsWfbD8q+qQzNZPANRc+qeTz1TP+BwvBN+KVr/XbPiJ2joF01Q/18F1kyED/9sbdL3kI7rMA2e7Cqd1DYeTvC3pR6T4a3tVWEjtB4AqgHrek5yHKl2erG6sxmbmRMPwAH4ZWRYe3NPXUP3YbEdIWBbSKr90A9RUIRGhvlfC6upXTPTvKklZ6mPwB15J1NL/SJLx8mDl1UuYz6MMj8zSNiQK6JmbGTkS0dwI2kIGQ3jooBG9GVwg0BATwHhkVKF/Ij3pusSKS4r28mVkl/+A9/W--p2sxNS2GrO5A+YL0--FYNvp951oU7AiBtVupKviA== | ||
lVbutrr7eF1l7i2+e9pTfhURUcNzRFuFJTKbI9aMG0cJgXpZbR10iB2vcAHPyJMj59TuXHwk1TMMcRAPAyMS62aGgDiqm5aXdVOKQyQnfJSvVw3YBFYNWtgvggrm2tU0liuKkHbztJ4fo8LYMDfdOQvfWeELRBdFUJrsYyh/WKQdqYb7yNA7Do1PtpdFr4LGNlj/kHqU/IiUoTT4MIhmH39CjDZ3dbYJp3//az5sbHLfdTAkPdb/4RzFwbV2LcCfVZTwlC0jKsnBukD99yjhtqbcJToBkbUGo/T8qLprDXDj7QIKGP536aAPQCOcEk97vS6InnU9c8IQcB4RK8aPPqTzYkGWlTPgPTUzzB+Tqqkzx6IATipUyVXL85M5v+ek3Cy0s9Qy97oIuBxqE6thnJjlixNHBYYuDqjWYQa8xj9sa6Ib//U8pm32Q9TYi7MNVoO/fjqJHG0b+wvcIQoltVUloCeOw5c9J5EGFXtTqITi/UZXYqMcUdmqEmU1u+/k5X4XmycjyRuKJkWdrupFDYTCAvP/LE6BkJNfgR64t/cfPhdNI74U38+GueMAzoxE/06pIK5oRHjJxWoL1PudCuOJlafdLTLNhV4=--PofSmzfmWPDiJyky--8KUUUH2hN7pWC0QfNQYrRg== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddSummaryToLinks < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :links, :summary, :text, default: nil | ||
add_column :links, :summary_timestamp, :datetime, default: nil | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.