Skip to content

Commit

Permalink
Merge pull request #5 from rwx-labs/mkroman/tldr-google-flash-2
Browse files Browse the repository at this point in the history
tldr: Use google flash 2.0 (experimental) for summaries
  • Loading branch information
mkroman authored Dec 27, 2024
2 parents 74d4cd3 + aa2c892 commit 78985ce
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions scripts/tldr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def initialize
@token = @config['token'] || ENV.fetch('KAGI_SESSION_TOKEN', nil)
raise "`token' (or KAGI_SESSION_TOKEN) is not set" unless @token

@anthropic_api_key = @config['anthropic_api_key'] || ENV.fetch('ANTHROPIC_API_KEY', nil)
raise "`anthropic_api_key' (or ANTHROPIC_API_KEY) is not set" unless @anthropic_api_key
@google_ai_key = @config['google_ai_key'] || ENV.fetch('GOOGLE_AI_KEY', nil)
raise "`google_ai_key' (or GOOGLE_AI_KEY) is not set" unless @google_ai_key

@http = HTTPX.with_timeout(total_timeout: 60)
end
Expand Down Expand Up @@ -147,7 +147,6 @@ def summarize(url, summary_type: 'summary', target_language: nil)
logger.info("requesting #{summary_type} for url #{url} using language #{target_language}")

response = @http.post(SUMMARY_API_URL, params:, headers:)
logger.debug("response: #{response.inspect}")

response.raise_for_status
process_summary_response(response)
Expand All @@ -159,11 +158,12 @@ def resummarize(summary)
response = request_resummary(summary).wait

json = response&.json
return unless json&.key?('content')
candidates = json['candidates']
raise UnexpectedResponseError, "no candidates" unless candidates

content = json['content']
text_content = content.filter { |c| c['type'] == 'text' }
text = text_content.map { |c| c['text'] }.join($INPUT_RECORD_SEPARATOR)
content = candidates.find { |candidate| candidate.key?('content') }['content']
text_parts = content['parts'].select { |part| part.key?('text') }
text = text_parts.map { |part| part['text'] }.join($INPUT_RECORD_SEPARATOR)

if text =~ %r{<short_summary>(.*?)</short_summary>}mi
Regexp.last_match(1)
Expand All @@ -178,18 +178,17 @@ def request_resummary(summary)
Async do
prompt = RESUMMARIZE_PROMPT.gsub('{{LONG_SUMMARY}}', summary)
request = {
'model' => 'claude-3-5-haiku-latest',
'max_tokens' => 1024,
'messages' => [
{ 'role' => 'user', 'content' => [{ type: 'text', text: prompt }] }
]
'contents' => [
{ 'role' => 'user', 'parts' => [{ text: prompt }] }
],
'generationConfig' => {
'maxOutputTokens' => 1024
}
}
headers = {
'anthropic-version' => '2023-06-01',
'x-api-key' => @anthropic_api_key,
'content-type' => 'application/json'
params = {
'key' => @google_ai_key
}
response = @http.post('https://api.anthropic.com/v1/messages', json: request, headers:)
response = @http.post('https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent', json: request, params:)
response.raise_for_status
response
end
Expand Down

0 comments on commit 78985ce

Please sign in to comment.