Class: Bristow::Agent
- Inherits:
-
Object
show all
- Includes:
- Sgetter
- Defined in:
- lib/bristow/agent.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#chat(messages, &block) ⇒ Object
-
#formatted_functions ⇒ Object
-
#handle_function_call(name, arguments) ⇒ Object
-
#initialize(agent_name: self.class.agent_name, description: self.class.description, system_message: self.class.system_message, functions: self.class.functions.dup, provider: self.class.provider, model: self.class.model, client: self.class.client, logger: self.class.logger, termination: self.class.termination) ⇒ Agent
constructor
Methods included from Sgetter
included
Constructor Details
#initialize(agent_name: self.class.agent_name, description: self.class.description, system_message: self.class.system_message, functions: self.class.functions.dup, provider: self.class.provider, model: self.class.model, client: self.class.client, logger: self.class.logger, termination: self.class.termination) ⇒ Agent
Returns a new instance of Agent.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/bristow/agent.rb', line 18
def initialize(
agent_name: self.class.agent_name,
description: self.class.description,
system_message: self.class.system_message,
functions: self.class.functions.dup,
provider: self.class.provider,
model: self.class.model,
client: self.class.client,
logger: self.class.logger,
termination: self.class.termination
)
@agent_name = agent_name
@description = description
@system_message = system_message
@functions = functions
@provider = provider
@model = model
@client = client || Bristow.configuration.client_for(@provider)
@logger = logger
@chat_history = []
@termination = termination
end
|
Instance Attribute Details
#chat_history ⇒ Object
Returns the value of attribute chat_history.
14
15
16
|
# File 'lib/bristow/agent.rb', line 14
def chat_history
@chat_history
end
|
Class Method Details
.chat ⇒ Object
51
52
53
|
# File 'lib/bristow/agent.rb', line 51
def self.chat(...)
new.chat(...)
end
|
Instance Method Details
#chat(messages, &block) ⇒ Object
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
97
98
99
100
101
102
103
104
|
# File 'lib/bristow/agent.rb', line 55
def chat(messages, &block)
messages = [{ role: "user", content: messages }] if messages.is_a?(String)
messages = messages.dup
messages.unshift(system_message_hash) if system_message
@chat_history = messages.dup
while termination.continue?(messages) do
params = {
model: model,
messages: messages
}
if functions.any?
params.merge!(formatted_functions)
end
logger.debug("Calling #{provider} API with params: #{params}")
response_message = if block_given?
client.stream_chat(params, &block)
else
client.chat(params)
end
messages << response_message
@chat_history << response_message
break unless client.is_function_call?(response_message)
result = handle_function_call(
client.function_name(response_message),
client.function_arguments(response_message)
)
yield "\n[Function Call: #{response_message}]\n" if block_given?
yield "#{result.to_json}\n" if block_given?
messages << client.format_function_response(response_message, result)
end
messages
rescue Faraday::BadRequestError, Faraday::ResourceNotFound => e
logger.error("Error calling OpenAI API: #{e.response[:body]}")
raise
end
|
47
48
49
|
# File 'lib/bristow/agent.rb', line 47
def formatted_functions
client.format_functions(functions)
end
|
#handle_function_call(name, arguments) ⇒ Object
41
42
43
44
45
|
# File 'lib/bristow/agent.rb', line 41
def handle_function_call(name, arguments)
function = functions.find { |f| f.function_name == name }
raise ArgumentError, "Function #{name} not found" unless function
function.call(**arguments.transform_keys(&:to_sym))
end
|