Class: WordSmith::Services::OpenAI
- Inherits:
-
Object
- Object
- WordSmith::Services::OpenAI
- Extended by:
- T::Sig
- Defined in:
- lib/services/open_a_i.rb
Defined Under Namespace
Classes: OpenAIKeyNotSetError, OpenAIOrgIDNotSetError
Constant Summary collapse
- OPEN_AI_API_KEY_FILE =
Storage methods
File.join(File.dirname(__FILE__), '../../', '.openai_api_key')
- OPEN_AI_ORG_ID_FILE =
File.join(File.dirname(__FILE__), '../../', '.openai_org_id')
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ OpenAI
constructor
A new instance of OpenAI.
- #translate(text:, target_language:) ⇒ Object
- #translate_in_context_of_sentence(sentence:, word:, target_language:) ⇒ Object
Constructor Details
#initialize ⇒ OpenAI
Returns a new instance of OpenAI.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/services/open_a_i.rb', line 51 def initialize raise OpenAIKeyNotSetError if OpenAI.api_key.nil? raise OpenAIOrgIDNotSetError if OpenAI.org_id.nil? ::OpenAI.configure do |config| config.access_token = OpenAI.api_key config.organization_id = OpenAI.org_id config.log_errors = Config::DEBUG_MODE end @client = ::OpenAI::Client.new end |
Class Method Details
.api_key ⇒ Object
27 28 29 30 31 |
# File 'lib/services/open_a_i.rb', line 27 def api_key return nil unless File.exist?(OPEN_AI_API_KEY_FILE) File.read(OPEN_AI_API_KEY_FILE) end |
.org_id ⇒ Object
39 40 41 42 43 |
# File 'lib/services/open_a_i.rb', line 39 def org_id return nil unless File.exist?(OPEN_AI_ORG_ID_FILE) File.read(OPEN_AI_ORG_ID_FILE) end |
.store_api_key(key) ⇒ Object
22 23 24 |
# File 'lib/services/open_a_i.rb', line 22 def store_api_key(key) File.write(OPEN_AI_API_KEY_FILE, key) end |
.store_org_id(key) ⇒ Object
34 35 36 |
# File 'lib/services/open_a_i.rb', line 34 def store_org_id(key) File.write(OPEN_AI_ORG_ID_FILE, key) end |
Instance Method Details
#translate(text:, target_language:) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/services/open_a_i.rb', line 69 def translate(text:, target_language:) response = @client.chat( parameters: { model: 'gpt-4o-mini', response_format: { type: 'json_schema', json_schema: { name: 'Translation', strict: true, schema: { type: 'object', properties: { word: { type: 'string' }, pronunciation: { type: 'string' }, meaning: { type: 'string' }, example: { type: 'string' }, translation_to_target_language: { type: %w[string null] } }, additionalProperties: false, required: %w[word pronunciation meaning example translation_to_target_language] } } }, messages: [ { role: 'system', content: Helpers::Str.lstr_every_line(" You are a great translator. Give the user the meaning of the word in English. Also give the user an example of the sentence using the word. #{get_target_language_prompt(target_language)} Do not hallucinate. Be to the point and concise without an ") }, { role: 'user', content: text } ], temperature: 0.7 } ) JSON.parse(response.dig('choices', 0, 'message', 'content'), { symbolize_names: true }) end |
#translate_in_context_of_sentence(sentence:, word:, target_language:) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/services/open_a_i.rb', line 117 def translate_in_context_of_sentence(sentence:, word:, target_language:) response = @client.chat( parameters: { model: 'gpt-4o-mini', response_format: { type: 'json_schema', json_schema: { name: 'Translation', strict: true, schema: { type: 'object', properties: { word: { type: 'string' }, pronunciation: { type: 'string' }, meaning: { type: 'string' }, example: { type: 'string' }, translation_to_target_language: { type: %w[string null] } }, additionalProperties: false, required: %w[word pronunciation meaning example] } } }, messages: [ { role: 'system', content: Helpers::Str.lstr_every_line(" You are a great translator. Give the user the meaning of the word in context of the sentence in English. Also give the user an example of the sentence using the meaning of the word in that context. #{get_target_language_prompt(target_language)} Do not hallucinate. Be to the point and concise without an ") }, { role: 'user', content: Helpers::Str.lstr_every_line(" Sentence: #{sentence} Word: #{word} ") } ], temperature: 0.7 } ) JSON.parse(response.dig('choices', 0, 'message', 'content'), { symbolize_names: true }) end |