Class: Raif::Agents::NativeToolCallingAgent

Inherits:
Raif::Agent show all
Includes:
Concerns::ToolCallValidation
Defined in:
app/models/raif/agents/native_tool_calling_agent.rb

Constant Summary collapse

RAW_ARGUMENTS_EXCERPT_LENGTH =
500

Instance Attribute Summary

Attributes inherited from Raif::Agent

#on_conversation_history_entry

Instance Method Summary collapse

Methods included from Concerns::ToolCallValidation

#validate_tool_call

Methods inherited from Raif::Agent

#final_iteration?, #model_tool_invocation_counts, #run!

Methods included from Concerns::RunWith

deserialize_run_with_value, gid_string?, locate_gid, serialize_run_with_value

Methods included from Concerns::AgentInferenceStats

#total_completion_tokens, #total_cost, #total_output_token_cost, #total_prompt_token_cost, #total_prompt_tokens, #total_tokens_sum

Methods included from Concerns::HasRuntimeDuration

#runtime_duration, #runtime_duration_seconds, #runtime_ended_at

Methods included from Concerns::HasAvailableModelTools

#available_model_tools_map

Methods included from Concerns::HasRequestedLanguage

#requested_language_name, #system_prompt_language_preference

Methods included from Concerns::HasLlm

#default_llm_model_key, #llm

Methods included from Concerns::HasPromptTemplates

#build_prompt

Methods inherited from Raif::ApplicationRecord

table_name_prefix, where_json_not_blank

Instance Method Details

#build_system_prompt ⇒ Object

A .system_prompt.erb template, when the subclass has one, replaces this default prompt. A template that renders blank, such as the comment-only stub from rails g raif:agent, falls back to the default so the agent still passes the system_prompt presence validation.



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/raif/agents/native_tool_calling_agent.rb', line 61

def build_system_prompt
  if prompt_template_exists?(:system_prompt)
    template_prompt = super
    return template_prompt if template_prompt.present?
  end

  step_two = if self.class.parallel_tool_calls
    limit = self.class.max_tool_calls_per_iteration
    batch_phrase = if limit.present?
      "up to #{limit} independent tool call#{"s" unless limit == 1}"
    else
      "several independent tool calls"
    end
    "Choose and invoke one or more tool/function calls based on that thought. You may make #{batch_phrase} " \
      "in a single step; when a call depends on another's result, make them in separate steps."
  else
    "Choose and invoke exactly one tool/function call based on that thought."
  end

  final_answer_note = if self.class.parallel_tool_calls
    "\n- Call the agent_final_answer tool by itself - do not combine it with other tool calls in the same step."
  else
    ""
  end

  <<~PROMPT.strip
    You are an AI agent that follows the ReAct (Reasoning + Acting) framework to complete tasks step by step using tool/function calls.

    At each step, you must:
    1. Think about what to do next.
    2. #{step_two}
    3. Observe the results of the tool/function call(s).
    4. Use the results to update your thought process.
    5. Repeat steps 1-4 until the task is complete.
    6. Provide a final answer to the user's request.

    For your final answer:
    - You **MUST** use the agent_final_answer tool/function to provide your final answer.#{final_answer_note}
    - Your answer should be comprehensive and directly address the user's request.

    Guidelines
    - Always think step by step
    - Be concise in your reasoning but thorough in your analysis
    - If a tool returns an error, try to understand why and adjust your approach
    - If you're unsure about something, explain your uncertainty, but do not make things up
    - Always provide a final answer that directly addresses the user's request

    Remember: Your goal is to be helpful, accurate, and efficient in solving the user's request.#{system_prompt_language_preference}
  PROMPT
end