Class: Ask::Agent::Chat

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/agent/chat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, tools: [], temperature: nil, schema: nil, provider: nil) ⇒ Chat

Returns a new instance of Chat.



27
28
29
30
31
32
33
34
35
36
# File 'lib/ask/agent/chat.rb', line 27

def initialize(model:, tools: [], temperature: nil, schema: nil, provider: nil, **)
  @model_id = model.respond_to?(:id) ? model.id : model.to_s
  @model_info = Ask::ModelCatalog.find(@model_id)
  @tools = tools
  @temperature = temperature
  @schema = schema
  @messages = []
  @provider_override = provider
  @provider = nil
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



25
26
27
# File 'lib/ask/agent/chat.rb', line 25

def messages
  @messages
end

#model_idObject (readonly)

Returns the value of attribute model_id.



19
20
21
# File 'lib/ask/agent/chat.rb', line 19

def model_id
  @model_id
end

Instance Method Details

#add_message(role:, content: nil, tool_call_id: nil, tool_calls: nil) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/ask/agent/chat.rb', line 98

def add_message(role:, content: nil, tool_call_id: nil, tool_calls: nil)
  @messages << Ask::Message.new(
    role: role,
    content: content,
    tool_call_id: tool_call_id,
    tool_calls: tool_calls
  )
end

#ask(message = nil, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ask/agent/chat.rb', line 38

def ask(message = nil, &block)
  @messages << Ask::Message.new(role: :user, content: message.to_s) if message

  stream = block_given?
  tool_defs = @tools.map { |t| Ask::ToolDef.from_tool(t) }

  calls_acc = {}

  provider_model = @model_id
  provider_tools = tool_defs
  provider_temp = @temperature
  provider_schema = @schema&.respond_to?(:to_json_schema) ? @schema.to_json_schema : @schema
  provider_params = @extra_params || {}

  result = provider.chat(
    @messages.map(&:to_h),
    model: provider_model,
    tools: provider_tools,
    temperature: provider_temp,
    stream: stream,
    schema: provider_schema,
    **provider_params
  ) do |raw_chunk|
    next unless block_given?

    accumulate_tool_calls(raw_chunk, calls_acc)

    yield ChatChunk.new(
      content: raw_chunk.content,
      tool_calls: build_current_tool_calls(calls_acc),
      thinking: raw_chunk.respond_to?(:thinking) ? raw_chunk.thinking : nil,
      input_tokens: nil,
      output_tokens: nil
    )
  end

  response_msg = if stream
    build_stream_response(result, calls_acc)
  else
    build_response(result)
  end

  @messages << Ask::Message.new(
    role: :assistant,
    content: response_msg.content,
    tool_calls: response_msg.tool_calls&.values&.map { |tc|
      { id: tc.id, type: "function", name: tc.name, arguments: tc.arguments }
    },
    metadata: {
      input_tokens: response_msg.input_tokens,
      output_tokens: response_msg.output_tokens,
      cost: response_msg.cost
    }.compact
  )

  emit_instrumentation(stream, response_msg)

  response_msg
end

#modelObject



21
22
23
# File 'lib/ask/agent/chat.rb', line 21

def model
  @model_id
end

#reset_messages!Object



123
124
125
# File 'lib/ask/agent/chat.rb', line 123

def reset_messages!
  @messages.clear
end

#with_instructions(prompt) ⇒ Object



107
108
109
110
111
# File 'lib/ask/agent/chat.rb', line 107

def with_instructions(prompt)
  @messages.reject! { |m| m.role == :system }
  @messages.unshift(Ask::Message.new(role: :system, content: prompt))
  self
end

#with_params(**params) ⇒ Object



113
114
115
116
# File 'lib/ask/agent/chat.rb', line 113

def with_params(**params)
  @extra_params = (@extra_params || {}).merge(params)
  self
end

#with_schema(schema) ⇒ Object



118
119
120
121
# File 'lib/ask/agent/chat.rb', line 118

def with_schema(schema)
  @schema = schema.respond_to?(:to_json_schema) ? schema.to_json_schema : schema
  self
end