Skip to content

Commit

Permalink
feat: 支持OpenRouter接口;修复当Atom的published时间为空导致的解析问题,为空时使用修改时间;
Browse files Browse the repository at this point in the history
  • Loading branch information
shuiRong committed Dec 22, 2024
1 parent 5091890 commit b8b82e4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
2 changes: 2 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,5 @@ config :honyaku, groq_api_key: System.get_env("GROQ_API_KEY")
config :honyaku, deepl_api_key: System.get_env("DEEPL_API_KEY")
# Rapid API key 生产环境
config :honyaku, rapid_api_key: System.get_env("RAPID_API_KEY")
# Open Router API key 生产环境
config :honyaku, open_router_api_key: System.get_env("OPEN_ROUTER_API_KEY")
6 changes: 4 additions & 2 deletions lib/honyaku/contexts/feeds/feed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ defmodule Honyaku.Feeds do
def save_articles(feed, entries) do
entries
|> Enum.map(fn entry ->
with {:ok, original_published_at} <- DateTimeUtils.parse_datetime(entry["published"]),
{:ok, original_updated_at} <- DateTimeUtils.parse_datetime(entry["updated"]) do
with {:ok, original_published_at} <-
DateTimeUtils.parse_datetime(entry["published"] || entry["updated"]),
{:ok, original_updated_at} <-
DateTimeUtils.parse_datetime(entry["updated"] || entry["published"]) do
article = %{
feed_id: feed.id,
link: entry["links"] |> List.first() |> Map.get("href"),
Expand Down
12 changes: 5 additions & 7 deletions lib/honyaku/contexts/feeds/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ defmodule Honyaku.Feeds.ParseFeed do
"title" => nil
}
],
"published" => item["pub_date"],
"rights" => nil,
"source" => nil,
"summary" => %{
Expand All @@ -133,6 +132,7 @@ defmodule Honyaku.Feeds.ParseFeed do
"type" => "Text",
"value" => item["title"]
},
"published" => item["pub_date"],
"updated" => item["pub_date"]
}
end)
Expand Down Expand Up @@ -244,12 +244,10 @@ defmodule Honyaku.Feeds.ParseFeed do
}}
end

@doc """
获取 Feed 字段的翻译结果
1. 如果数据库中已存在翻译结果,则直接返回
2. 如果数据库中不存在翻译结果,则创建翻译任务
2.1 如果翻译任务成功,则将翻译结果保存到数据库中
"""
# 获取 Feed 字段的翻译结果
# 1. 如果数据库中已存在翻译结果,则直接返回
# 2. 如果数据库中不存在翻译结果,则创建翻译任务
# 2.1 如果翻译任务成功,则将翻译结果保存到数据库中
defp get_feed_field_translation(feed_with_preload, saved_feed, field, target_lang, source_lang) do
Task.async(fn ->
case Enum.find(feed_with_preload.translations, fn t ->
Expand Down
13 changes: 11 additions & 2 deletions lib/honyaku/external/load_balancer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ defmodule Honyaku.External.TranslationBalancer do
Rapid.FreeGoogleTranslator,
Rapid.AiBitTranslator,
Rapid.DeepLTranslator,
Groq.Gemma2_2b
Groq.Gemma2_2b,
OpenRouter.Gemini2_Flash
}

@apis [Flash1_5, Flash2, FreeGoogleTranslator, AiBitTranslator, DeepLTranslator, Gemma2_2b]
@apis [
Flash1_5,
Flash2,
FreeGoogleTranslator,
AiBitTranslator,
DeepLTranslator,
Gemma2_2b,
Gemini2_Flash
]

@doc """
翻译文本,使用指定的负载均衡算法。
Expand Down
53 changes: 53 additions & 0 deletions lib/honyaku/external/open_router/gemini_2_flash.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule Honyaku.External.OpenRouter.Gemini2_Flash do
require Logger

@base_url "https://openrouter.ai/api/v1"

def translate(text, target_lang, source_lang) do
key = Application.fetch_env!(:honyaku, :open_router_api_key)

headers = [
{"Authorization", "Bearer #{key}"}
]

body = %{
"messages" => [
%{
"role" => "user",
"content" => """
Translate the following text from #{source_lang} to #{target_lang}, and return the translated text only:
#{text}
"""
}
],
"model" => "google/gemini-2.0-flash-thinking-exp:free",
"temperature" => 1,
"stream" => false
}

case Req.post(
"#{@base_url}/chat/completions",
headers: headers,
json: body
) do
{:ok,
%Req.Response{
status: 200,
body: %{"choices" => [%{"message" => %{"content" => content}}]}
}} ->
{:ok, content}

{:ok, %Req.Response{status: 429}} ->
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("翻译失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("Gemini API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
end

0 comments on commit b8b82e4

Please sign in to comment.