Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 540b6a7

Browse files
committed
move code to class
1 parent 8fa7fb3 commit 540b6a7

File tree

2 files changed

+103
-100
lines changed

2 files changed

+103
-100
lines changed

lib/completions/endpoints/open_ai.rb

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -97,106 +97,6 @@ def final_log_update(log)
9797
log.response_tokens = processor.completion_tokens if processor.completion_tokens
9898
end
9999

100-
class OpenAiMessageProcessor
101-
attr_reader :prompt_tokens, :completion_tokens
102-
103-
def initialize
104-
@tool = nil
105-
@tool_arguments = +""
106-
@prompt_tokens = nil
107-
@completion_tokens = nil
108-
end
109-
110-
def process_message(json)
111-
result = []
112-
tool_calls = json.dig(:choices, 0, :message, :tool_calls)
113-
114-
message = json.dig(:choices, 0, :message, :content)
115-
result << message if message.present?
116-
117-
if tool_calls.present?
118-
tool_calls.each do |tool_call|
119-
id = tool_call.dig(:id)
120-
name = tool_call.dig(:function, :name)
121-
arguments = tool_call.dig(:function, :arguments)
122-
parameters = arguments.present? ? JSON.parse(arguments, symbolize_names: true) : {}
123-
result << ToolCall.new(id: id, name: name, parameters: parameters)
124-
end
125-
end
126-
127-
update_usage(json)
128-
129-
result
130-
end
131-
132-
def process_streamed_message(json)
133-
rval = nil
134-
135-
tool_calls = json.dig(:choices, 0, :delta, :tool_calls)
136-
content = json.dig(:choices, 0, :delta, :content)
137-
138-
finished_tools = json.dig(:choices, 0, :finish_reason) || tool_calls == []
139-
140-
if tool_calls.present?
141-
id = tool_calls.dig(0, :id)
142-
name = tool_calls.dig(0, :function, :name)
143-
arguments = tool_calls.dig(0, :function, :arguments)
144-
145-
# TODO: multiple tool support may require index
146-
#index = tool_calls[0].dig(:index)
147-
148-
if id.present? && @tool && @tool.id != id
149-
process_arguments
150-
rval = @tool
151-
@tool = nil
152-
end
153-
154-
if id.present? && name.present?
155-
@tool_arguments = +""
156-
@tool = ToolCall.new(id: id, name: name)
157-
end
158-
159-
@tool_arguments << arguments.to_s
160-
elsif finished_tools && @tool
161-
parsed_args = JSON.parse(@tool_arguments, symbolize_names: true)
162-
@tool.parameters = parsed_args
163-
rval = @tool
164-
@tool = nil
165-
elsif content.present?
166-
rval = content
167-
end
168-
169-
update_usage(json)
170-
171-
rval
172-
end
173-
174-
def finish
175-
rval = []
176-
if @tool
177-
process_arguments
178-
rval << @tool
179-
@tool = nil
180-
end
181-
182-
rval
183-
end
184-
185-
private
186-
187-
def process_arguments
188-
if @tool_arguments.present?
189-
parsed_args = JSON.parse(@tool_arguments, symbolize_names: true)
190-
@tool.parameters = parsed_args
191-
@tool_arguments = nil
192-
end
193-
end
194-
195-
def update_usage(json)
196-
@prompt_tokens ||= json.dig(:usage, :prompt_tokens)
197-
@completion_tokens ||= json.dig(:usage, :completion_tokens)
198-
end
199-
end
200100

201101
def decode(response_raw)
202102
processor.process_message(JSON.parse(response_raw, symbolize_names: true))
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# frozen_string_literal: true
2+
module DiscourseAi::Completions
3+
class OpenAiMessageProcessor
4+
attr_reader :prompt_tokens, :completion_tokens
5+
6+
def initialize
7+
@tool = nil
8+
@tool_arguments = +""
9+
@prompt_tokens = nil
10+
@completion_tokens = nil
11+
end
12+
13+
def process_message(json)
14+
result = []
15+
tool_calls = json.dig(:choices, 0, :message, :tool_calls)
16+
17+
message = json.dig(:choices, 0, :message, :content)
18+
result << message if message.present?
19+
20+
if tool_calls.present?
21+
tool_calls.each do |tool_call|
22+
id = tool_call.dig(:id)
23+
name = tool_call.dig(:function, :name)
24+
arguments = tool_call.dig(:function, :arguments)
25+
parameters = arguments.present? ? JSON.parse(arguments, symbolize_names: true) : {}
26+
result << ToolCall.new(id: id, name: name, parameters: parameters)
27+
end
28+
end
29+
30+
update_usage(json)
31+
32+
result
33+
end
34+
35+
def process_streamed_message(json)
36+
rval = nil
37+
38+
tool_calls = json.dig(:choices, 0, :delta, :tool_calls)
39+
content = json.dig(:choices, 0, :delta, :content)
40+
41+
finished_tools = json.dig(:choices, 0, :finish_reason) || tool_calls == []
42+
43+
if tool_calls.present?
44+
id = tool_calls.dig(0, :id)
45+
name = tool_calls.dig(0, :function, :name)
46+
arguments = tool_calls.dig(0, :function, :arguments)
47+
48+
# TODO: multiple tool support may require index
49+
#index = tool_calls[0].dig(:index)
50+
51+
if id.present? && @tool && @tool.id != id
52+
process_arguments
53+
rval = @tool
54+
@tool = nil
55+
end
56+
57+
if id.present? && name.present?
58+
@tool_arguments = +""
59+
@tool = ToolCall.new(id: id, name: name)
60+
end
61+
62+
@tool_arguments << arguments.to_s
63+
elsif finished_tools && @tool
64+
parsed_args = JSON.parse(@tool_arguments, symbolize_names: true)
65+
@tool.parameters = parsed_args
66+
rval = @tool
67+
@tool = nil
68+
elsif content.present?
69+
rval = content
70+
end
71+
72+
update_usage(json)
73+
74+
rval
75+
end
76+
77+
def finish
78+
rval = []
79+
if @tool
80+
process_arguments
81+
rval << @tool
82+
@tool = nil
83+
end
84+
85+
rval
86+
end
87+
88+
private
89+
90+
def process_arguments
91+
if @tool_arguments.present?
92+
parsed_args = JSON.parse(@tool_arguments, symbolize_names: true)
93+
@tool.parameters = parsed_args
94+
@tool_arguments = nil
95+
end
96+
end
97+
98+
def update_usage(json)
99+
@prompt_tokens ||= json.dig(:usage, :prompt_tokens)
100+
@completion_tokens ||= json.dig(:usage, :completion_tokens)
101+
end
102+
end
103+
end

0 commit comments

Comments
 (0)