Class: SentimentInsights::Clients::Entities::OpenAIClient
- Inherits:
-
Object
- Object
- SentimentInsights::Clients::Entities::OpenAIClient
- Defined in:
- lib/sentiment_insights/clients/entities/open_ai_client.rb
Constant Summary collapse
- DEFAULT_MODEL =
"gpt-3.5-turbo"- DEFAULT_RETRIES =
3
Instance Method Summary collapse
- #extract_batch(entries, question: nil, prompt: nil) ⇒ Object
-
#initialize(api_key: ENV['OPENAI_API_KEY'], model: DEFAULT_MODEL, max_retries: DEFAULT_RETRIES) ⇒ OpenAIClient
constructor
A new instance of OpenAIClient.
Constructor Details
#initialize(api_key: ENV['OPENAI_API_KEY'], model: DEFAULT_MODEL, max_retries: DEFAULT_RETRIES) ⇒ OpenAIClient
Returns a new instance of OpenAIClient.
13 14 15 16 17 18 |
# File 'lib/sentiment_insights/clients/entities/open_ai_client.rb', line 13 def initialize(api_key: ENV['OPENAI_API_KEY'], model: DEFAULT_MODEL, max_retries: DEFAULT_RETRIES) @api_key = api_key or raise ArgumentError, "OpenAI API key is required" @model = model @max_retries = max_retries @logger = Logger.new($stdout) end |
Instance Method Details
#extract_batch(entries, question: nil, prompt: nil) ⇒ Object
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 47 48 49 50 51 52 53 54 |
# File 'lib/sentiment_insights/clients/entities/open_ai_client.rb', line 20 def extract_batch(entries, question: nil, prompt: nil) responses = [] entity_map = Hash.new { |h, k| h[k] = [] } entries.each_with_index do |entry, index| sentence = entry[:answer].to_s.strip next if sentence.empty? response_id = "r_#{index + 1}" entities = extract_entities_from_sentence(sentence, question: question, prompt: prompt) responses << { id: response_id, sentence: sentence, segment: entry[:segment] || {} } entities.each do |ent| next if ent[:text].empty? || ent[:type].empty? key = [ent[:text].downcase, ent[:type]] entity_map[key] << response_id end end entity_records = entity_map.map do |(text, type), ref_ids| { entity: text, type: type, mentions: ref_ids.uniq, summary: nil } end { entities: entity_records, responses: responses } end |