Class: Pedicab::P

Inherits:
Object
  • Object
show all
Defined in:
lib/pedicab.rb

Overview

Main conversation interface for AI interactions Provides context management, state tracking, and elegant operator syntax

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k) ⇒ P

Initialize a new conversation instance

Examples:

ai = Pedicab::P.new('my_bot')
response = ai["Hello, world!"]

Parameters:

  • id (String) —

    Unique identifier for the conversation



56
57
58
59
60
61
62
# File 'lib/pedicab.rb', line 56

def initialize(k)
  @id = k
  @handler = lambda { |s| s.out }
  @ride = Pedicab.ride(k)
  @context = {}
  reset!
end

Instance Attribute Details

#context ⇒ Proc, Hash

Returns:

  • (Proc) —

    Response handler lambda

  • (Hash) —

    Current conversation context



48
49
50
# File 'lib/pedicab.rb', line 48

def context
  @context
end

#handler ⇒ Proc, Hash

Returns:

  • (Proc) —

    Response handler lambda

  • (Hash) —

    Current conversation context



48
49
50
# File 'lib/pedicab.rb', line 48

def handler
  @handler
end

#last ⇒ Float, ... (readonly)

Returns:

  • (Float) —

    Time taken for the last request in seconds

  • (Array<Float>) —

    Array of processing times for all requests

  • (Array<String>) —

    Array of LLM thinking processes

  • (String) —

    The last prompt sent to the model

  • (Array<String>) —

    Array of all responses received

  • (String) —

    The last response content

  • (Pedicab::Ride) —

    The associated ride instance for direct model access

  • (Array<String>) —

    Array of previous prompts



44
45
46
# File 'lib/pedicab.rb', line 44

def last
  @last
end

#out ⇒ Float, ... (readonly)

Returns:

  • (Float) —

    Time taken for the last request in seconds

  • (Array<Float>) —

    Array of processing times for all requests

  • (Array<String>) —

    Array of LLM thinking processes

  • (String) —

    The last prompt sent to the model

  • (Array<String>) —

    Array of all responses received

  • (String) —

    The last response content

  • (Pedicab::Ride) —

    The associated ride instance for direct model access

  • (Array<String>) —

    Array of previous prompts



44
45
46
# File 'lib/pedicab.rb', line 44

def out
  @out
end

#prompt ⇒ Float, ... (readonly)

Returns:

  • (Float) —

    Time taken for the last request in seconds

  • (Array<Float>) —

    Array of processing times for all requests

  • (Array<String>) —

    Array of LLM thinking processes

  • (String) —

    The last prompt sent to the model

  • (Array<String>) —

    Array of all responses received

  • (String) —

    The last response content

  • (Pedicab::Ride) —

    The associated ride instance for direct model access

  • (Array<String>) —

    Array of previous prompts



44
45
46
# File 'lib/pedicab.rb', line 44

def prompt
  @prompt
end

#response ⇒ Float, ... (readonly)

Returns:

  • (Float) —

    Time taken for the last request in seconds

  • (Array<Float>) —

    Array of processing times for all requests

  • (Array<String>) —

    Array of LLM thinking processes

  • (String) —

    The last prompt sent to the model

  • (Array<String>) —

    Array of all responses received

  • (String) —

    The last response content

  • (Pedicab::Ride) —

    The associated ride instance for direct model access

  • (Array<String>) —

    Array of previous prompts



44
45
46
# File 'lib/pedicab.rb', line 44

def response
  @response
end

#ride ⇒ Float, ... (readonly)

Returns:

  • (Float) —

    Time taken for the last request in seconds

  • (Array<Float>) —

    Array of processing times for all requests

  • (Array<String>) —

    Array of LLM thinking processes

  • (String) —

    The last prompt sent to the model

  • (Array<String>) —

    Array of all responses received

  • (String) —

    The last response content

  • (Pedicab::Ride) —

    The associated ride instance for direct model access

  • (Array<String>) —

    Array of previous prompts



44
45
46
# File 'lib/pedicab.rb', line 44

def ride
  @ride
end

#thoughts ⇒ Float, ... (readonly)

Returns:

  • (Float) —

    Time taken for the last request in seconds

  • (Array<Float>) —

    Array of processing times for all requests

  • (Array<String>) —

    Array of LLM thinking processes

  • (String) —

    The last prompt sent to the model

  • (Array<String>) —

    Array of all responses received

  • (String) —

    The last response content

  • (Pedicab::Ride) —

    The associated ride instance for direct model access

  • (Array<String>) —

    Array of previous prompts



44
45
46
# File 'lib/pedicab.rb', line 44

def thoughts
  @thoughts
end

#time ⇒ Float, ... (readonly)

Returns:

  • (Float) —

    Time taken for the last request in seconds

  • (Array<Float>) —

    Array of processing times for all requests

  • (Array<String>) —

    Array of LLM thinking processes

  • (String) —

    The last prompt sent to the model

  • (Array<String>) —

    Array of all responses received

  • (String) —

    The last response content

  • (Pedicab::Ride) —

    The associated ride instance for direct model access

  • (Array<String>) —

    Array of previous prompts



44
45
46
# File 'lib/pedicab.rb', line 44

def time
  @time
end

#took ⇒ Float, ... (readonly)

Returns:

  • (Float) —

    Time taken for the last request in seconds

  • (Array<Float>) —

    Array of processing times for all requests

  • (Array<String>) —

    Array of LLM thinking processes

  • (String) —

    The last prompt sent to the model

  • (Array<String>) —

    Array of all responses received

  • (String) —

    The last response content

  • (Pedicab::Ride) —

    The associated ride instance for direct model access

  • (Array<String>) —

    Array of previous prompts



44
45
46
# File 'lib/pedicab.rb', line 44

def took
  @took
end

Instance Method Details

#<<(input) ⇒ Object

Continue an existing conversation with context Maintains previous exchanges and builds upon them

Examples:

ai["What is Ruby?"]           # First question
ai << "What about Python?"    # Continues with context

Parameters:

  • input (String) —

    The prompt to send to the LLM

Returns:

  • (Object) —

    Result of the handler (typically the response object)



124
125
126
127
128
129
# File 'lib/pedicab.rb', line 124

def << input
  @last << @prompt if @prompt
  @prompt = input
  think(input)
  @handler.call(self)
end

#[](input) ⇒ Object

Start a fresh conversation with the given prompt Clears previous context and begins a new conversation

Examples:

response = ai["What is the meaning of life?"]
puts response.out

Parameters:

  • input (String) —

    The prompt to send to the LLM

Returns:

  • (Object) —

    Result of the handler (typically the response object)



109
110
111
112
113
114
# File 'lib/pedicab.rb', line 109

def [] input
  reset!
  @prompt = input
  think(input)
  @handler.call(self)
end

#handle(&block) ⇒ Proc

Set or execute the response handler

Examples:

# Set custom handler
ai.handle { |response| puts response.out }

# Execute handler
result = ai.handle

Parameters:

  • block (Proc, nil) —

    Optional handler that receives the response object

Returns:

  • (Proc) —

    The handler when setting, or handler result when executing



92
93
94
95
96
97
98
99
# File 'lib/pedicab.rb', line 92

def handle(&block)
  @handler = block
  if @response.length > Pedicab.memory
    @response.shift
    @last.shift
    @thoughts.shift
  end
end

#life ⇒ Float

Calculate total processing time for all requests in the conversation

Examples:

ai["Question 1"]
ai["Question 2"]
puts "Total time: #{ai.life}s"

Returns:

  • (Float) —

    Total time in seconds



138
139
140
141
142
# File 'lib/pedicab.rb', line 138

def life
  a = 0
  @time.map { |e| a += e }
  return a
end

#reset! ⇒ void

This method returns an undefined value.

Reset all conversation state, context, and history Clears prompts, responses, timing data, and ride state

Examples:

ai.reset!  # Start fresh conversation


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pedicab.rb', line 70

def reset!
  @ride.reset!
  @exe = ""
  @time = []
  @last = []
  @response = []
  @thoughts = []
  @prompt = nil
  @out = nil
  @took = 0
end