Module: GeekDict::OpenRouter
- Defined in:
- lib/geekdict/openrouter/api.rb
Class Method Summary collapse
- .system_prompt(word) ⇒ Object
-
.translate(word, model: nil) ⇒ Object
Update translate method to accept model.
Class Method Details
.system_prompt(word) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/geekdict/openrouter/api.rb', line 48 def system_prompt(word) <<-EOS You are a precise language translator specializing in English-Chinese translation. Follow these guidelines: 1. If the input is Chinese, translate to English. If English, translate to Chinese. 2. For words with multiple meanings, prioritize the most common usage first. 3. For misspelled or incorrect words, suggest the closest correct word. 4. For idiomatic expressions, provide both literal and figurative translations. 5. For technical terms, include the field/domain where appropriate. Format your response as follows: - Translation: [primary translation] - Explanation: [concise explanation of meaning and usage] - Examples: 1. [example sentence in target language] [translation in source language] 2. [second example if helpful] [translation in source language] Keep explanations clear and concise. Do NOT include pinyin in Chinese text. EOS end |
.translate(word, model: nil) ⇒ Object
Update translate method to accept model
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/geekdict/openrouter/api.rb', line 9 def translate(word, model: nil) # Add model keyword argument @debugger = GeekDict.debugger # Use the provided model, or fallback to a default if nil (though CLI should provide one) effective_model = model || 'google/gemini-2.5-flash-preview' # Fallback, though CLI provides default client = HTTPClient.new # OpenRouter is stateless; avoid parsing unsupported SameSite cookie attributes. client. = nil headers = { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{ENV.fetch('OPENROUTER_API_KEY')}", 'HTTP-Referer' => 'https://github.com/wbingli/geekdict/', # Identify app 'X-Title' => 'GeekDict' # Set app title } body = { model: effective_model, # Use the determined model messages: [ { role: "system", content: system_prompt(word) }, { role: "user", content: word } ], temperature: 0.2 } response = client.post( 'https://openrouter.ai/api/v1/chat/completions', body.to_json, headers ) if response.status == 200 result = JSON.parse(response.body) result.dig("choices", 0, "message", "content") else "Error: Failed to get translation (#{response.status})" end end |