Class: GenAI::Language::GooglePalm

Inherits:
Base
  • Object
show all
Defined in:
lib/gen_ai/language/google_palm.rb

Constant Summary collapse

DEFAULT_ROLE =
'0'
EMBEDDING_MODEL =
'textembedding-gecko-001'
COMPLETION_MODEL =
'text-bison-001'
CHAT_COMPLETION_MODEL =
'chat-bison-001'

Instance Method Summary collapse

Methods included from Dependency

#depends_on

Constructor Details

#initialize(token:, options: {}) ⇒ GooglePalm

Returns a new instance of GooglePalm.



11
12
13
14
15
# File 'lib/gen_ai/language/google_palm.rb', line 11

def initialize(token:, options: {})
  depends_on 'google_palm_api'

  @client = ::GooglePalmApi::Client.new(api_key: token)
end

Instance Method Details

#chat(message, context: nil, history: [], examples: [], **options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gen_ai/language/google_palm.rb', line 41

def chat(message, context: nil, history: [], examples: [], **options)
  parameters = build_chat_options(message, context, history, examples, options)

  response = handle_errors { client.generate_chat_message(**parameters) }

  build_result(
    model: parameters[:model],
    raw: response.merge('usage' => {}),
    parsed: extract_chat_messages(response)
  )
end

#complete(prompt, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gen_ai/language/google_palm.rb', line 29

def complete(prompt, options = {})
  parameters = build_completion_options(prompt, options)

  response = handle_errors { client.generate_text(**parameters) }

  build_result(
    model: parameters[:model],
    raw: response.merge('usage' => {}),
    parsed: extract_completions(response)
  )
end

#embed(input, model: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gen_ai/language/google_palm.rb', line 17

def embed(input, model: nil)
  responses = array_wrap(input).map do |text|
    handle_errors { client.embed(text: text, model: model) }
  end

  build_result(
    model: model || EMBEDDING_MODEL,
    raw: { 'data' => responses, 'usage' => {} },
    parsed: extract_embeddings(responses)
  )
end