Class: Ask::Agent::Chat
- Inherits:
-
Object
- Object
- Ask::Agent::Chat
- Defined in:
- lib/ask/agent/chat.rb
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#test_provider ⇒ Object
writeonly
Sets the attribute test_provider.
Instance Method Summary collapse
- #add_message(role:, content: nil, tool_call_id: nil, tool_calls: nil) ⇒ Object
- #ask(message = nil, &block) ⇒ Object
-
#initialize(model:, tools: [], temperature: nil, schema: nil, provider: nil, prompt_caching: nil) ⇒ Chat
constructor
A new instance of Chat.
- #model ⇒ Object
- #reset_messages! ⇒ Object
- #with_instructions(prompt) ⇒ Object
- #with_params(**params) ⇒ Object
- #with_schema(schema) ⇒ Object
Constructor Details
#initialize(model:, tools: [], temperature: nil, schema: nil, provider: nil, prompt_caching: nil) ⇒ Chat
Returns a new instance of Chat.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ask/agent/chat.rb', line 36 def initialize(model:, tools: [], temperature: nil, schema: nil, provider: nil, prompt_caching: 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 # Read configured middleware, transforms, and caching from global config config = Ask::Agent.configuration @middleware_pipeline = config.middleware.configured? ? config.middleware : nil @transform_pipeline = config.stream_transforms.configured? ? config.stream_transforms : nil @prompt_caching = prompt_caching.nil? ? config.prompt_caching : prompt_caching end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
32 33 34 |
# File 'lib/ask/agent/chat.rb', line 32 def @messages end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
26 27 28 |
# File 'lib/ask/agent/chat.rb', line 26 def model_id @model_id end |
#test_provider=(value) ⇒ Object (writeonly)
Sets the attribute test_provider
34 35 36 |
# File 'lib/ask/agent/chat.rb', line 34 def test_provider=(value) @test_provider = value end |
Instance Method Details
#add_message(role:, content: nil, tool_call_id: nil, tool_calls: nil) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/ask/agent/chat.rb', line 80 def (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
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 |
# File 'lib/ask/agent/chat.rb', line 53 def ask( = nil, &block) @messages << Ask::Message.new(role: :user, content: .to_s) if stream = block_given? tool_defs = @tools.map { |t| Ask::ToolDef.from_tool(t) } calls_acc = {} response_msg = chat_with_retry(stream, calls_acc, &block) @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 |
#model ⇒ Object
28 29 30 |
# File 'lib/ask/agent/chat.rb', line 28 def model @model_id end |
#reset_messages! ⇒ Object
105 106 107 |
# File 'lib/ask/agent/chat.rb', line 105 def @messages.clear end |
#with_instructions(prompt) ⇒ Object
89 90 91 92 93 |
# File 'lib/ask/agent/chat.rb', line 89 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
95 96 97 98 |
# File 'lib/ask/agent/chat.rb', line 95 def with_params(**params) @extra_params = (@extra_params || {}).merge(params) self end |
#with_schema(schema) ⇒ Object
100 101 102 103 |
# File 'lib/ask/agent/chat.rb', line 100 def with_schema(schema) @schema = schema.respond_to?(:to_json_schema) ? schema.to_json_schema : schema self end |