Class: Langchain::LLM::GoogleGeminiResponse

Inherits:
BaseResponse show all
Defined in:
lib/langchain/llm/response/google_gemini_response.rb

Instance Attribute Summary

Attributes inherited from BaseResponse

#context, #model, #raw_response

Instance Method Summary collapse

Methods inherited from BaseResponse

#chat_completions, #completion, #completions, #created_at

Constructor Details

#initialize(raw_response, model: nil) ⇒ GoogleGeminiResponse

Returns a new instance of GoogleGeminiResponse.



5
6
7
# File 'lib/langchain/llm/response/google_gemini_response.rb', line 5

def initialize(raw_response, model: nil)
  super
end

Instance Method Details

#chat_completionObject



9
10
11
# File 'lib/langchain/llm/response/google_gemini_response.rb', line 9

def chat_completion
  raw_response.dig("candidates", 0, "content", "parts", 0, "text")
end

#completion_tokensObject



41
42
43
# File 'lib/langchain/llm/response/google_gemini_response.rb', line 41

def completion_tokens
  raw_response.dig("usageMetadata", "candidatesTokenCount")
end

#embeddingObject



25
26
27
# File 'lib/langchain/llm/response/google_gemini_response.rb', line 25

def embedding
  embeddings.first
end

#embeddingsObject



29
30
31
32
33
34
35
# File 'lib/langchain/llm/response/google_gemini_response.rb', line 29

def embeddings
  if raw_response.key?("embedding")
    [raw_response.dig("embedding", "values")]
  else
    [raw_response.dig("predictions", 0, "embeddings", "values")]
  end
end

#prompt_tokensObject



37
38
39
# File 'lib/langchain/llm/response/google_gemini_response.rb', line 37

def prompt_tokens
  raw_response.dig("usageMetadata", "promptTokenCount")
end

#roleObject



13
14
15
# File 'lib/langchain/llm/response/google_gemini_response.rb', line 13

def role
  raw_response.dig("candidates", 0, "content", "role")
end

#tool_callsObject



17
18
19
20
21
22
23
# File 'lib/langchain/llm/response/google_gemini_response.rb', line 17

def tool_calls
  if raw_response.dig("candidates", 0, "content") && raw_response.dig("candidates", 0, "content", "parts", 0).has_key?("functionCall")
    raw_response.dig("candidates", 0, "content", "parts")
  else
    []
  end
end

#total_tokensObject



45
46
47
# File 'lib/langchain/llm/response/google_gemini_response.rb', line 45

def total_tokens
  raw_response.dig("usageMetadata", "totalTokenCount")
end