Class: Langchain::LLM::OllamaResponse

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

Instance Attribute Summary

Attributes inherited from BaseResponse

#context, #model, #raw_response

Instance Method Summary collapse

Methods inherited from BaseResponse

#chat_completions

Constructor Details

#initialize(raw_response, model: nil, prompt_tokens: nil) ⇒ OllamaResponse

Returns a new instance of OllamaResponse.



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

def initialize(raw_response, model: nil, prompt_tokens: nil)
  @prompt_tokens = prompt_tokens
  super(raw_response, model: model)
end

Instance Method Details

#chat_completionObject



16
17
18
# File 'lib/langchain/llm/response/ollama_response.rb', line 16

def chat_completion
  raw_response.dig("message", "content")
end

#completionObject



20
21
22
# File 'lib/langchain/llm/response/ollama_response.rb', line 20

def completion
  completions.first
end

#completion_tokensObject



44
45
46
# File 'lib/langchain/llm/response/ollama_response.rb', line 44

def completion_tokens
  raw_response.dig("eval_count")
end

#completionsObject



24
25
26
# File 'lib/langchain/llm/response/ollama_response.rb', line 24

def completions
  raw_response.is_a?(String) ? [raw_response] : []
end

#created_atObject



10
11
12
13
14
# File 'lib/langchain/llm/response/ollama_response.rb', line 10

def created_at
  if raw_response.dig("created_at")
    Time.parse(raw_response.dig("created_at"))
  end
end

#embeddingObject



28
29
30
# File 'lib/langchain/llm/response/ollama_response.rb', line 28

def embedding
  embeddings.first
end

#embeddingsObject



32
33
34
# File 'lib/langchain/llm/response/ollama_response.rb', line 32

def embeddings
  [raw_response&.dig("embedding")]
end

#prompt_tokensObject



40
41
42
# File 'lib/langchain/llm/response/ollama_response.rb', line 40

def prompt_tokens
  raw_response.dig("prompt_eval_count")
end

#roleObject



36
37
38
# File 'lib/langchain/llm/response/ollama_response.rb', line 36

def role
  "assistant"
end

#total_tokensObject



48
49
50
# File 'lib/langchain/llm/response/ollama_response.rb', line 48

def total_tokens
  prompt_tokens + completion_tokens
end