Class: SentimentAI::Core::OpenAIDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/sentiment_ai/core/openai_driver.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ OpenAIDriver

Returns a new instance of OpenAIDriver.



8
9
10
# File 'lib/sentiment_ai/core/openai_driver.rb', line 8

def initialize(api_key)
  @sentiment_ai = OpenAI::Client.new(access_token: api_key)
end

Instance Method Details

#analyze_sentence(sentence) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sentiment_ai/core/openai_driver.rb', line 12

def analyze_sentence(sentence)
  text_request = I18n.t('prompt.sentence', sentence: sentence)

  @sentiment_ai.chat(
    parameters: {
      model: 'gpt-4o',
      messages: [{ role: 'user', content: text_request }],
      temperature: 0.7,
      stream: proc do |chunk, _bytesize|
        print chunk.dig('choices', 0, 'delta', 'content')
      end
    }
  )
end

#positive_check(sentence) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sentiment_ai/core/openai_driver.rb', line 27

def positive_check(sentence)
  text_request = I18n.t('prompt.positive_check', sentence: sentence)

  @sentiment_ai.chat(
    parameters: {
      model: 'gpt-4o',
      messages: [{ role: 'user', content: text_request }],
      temperature: 0.7,
      stream: proc do |chunk, _bytesize|
        print chunk.dig('choices', 0, 'delta', 'content')
      end
    }
  )
end