Skip to content
代码片段 群组 项目
提交 4830d389 编辑于 作者: Patrick Bajao's avatar Patrick Bajao
浏览文件

Do not parse SSE events if not streaming

When response of Anthropic model includes `data:` and even if
the request isn't streaming, `parse_sse_events` will still try to
parse the response. It can fail since the format of the response
is not really in the proper SSE format expected. We also don't need
to parse SSE events if we're not streaming.

To fix that, we check if call for `perform_completion_request` and
`perform_messages_request` have `stream` as `true`. If not, then
we skip calling `parse_sse_events`.

Changelog: fixed
EE: true
上级 662fd875
No related branches found
No related tags found
无相关合并请求
......@@ -101,6 +101,7 @@ def messages_stream(messages:, **options)
def perform_completion_request(prompt:, options:)
logger.info(message: "Performing request to Anthropic", options: options)
timeout = options.delete(:timeout) || DEFAULT_TIMEOUT
stream = options.fetch(:stream, false)
Gitlab::HTTP.post(
"#{url}/v1/complete",
......@@ -108,8 +109,10 @@ def perform_completion_request(prompt:, options:)
body: request_body(prompt: prompt, options: options).to_json,
timeout: timeout,
allow_local_requests: true,
stream_body: options.fetch(:stream, false)
stream_body: stream
) do |fragment|
next unless stream
parse_sse_events(fragment).each do |parsed_event|
yield parsed_event if block_given?
end
......@@ -119,6 +122,7 @@ def perform_completion_request(prompt:, options:)
def perform_messages_request(messages:, options:)
logger.info(message: "Performing request to Anthropic", options: options)
timeout = options.delete(:timeout) || DEFAULT_TIMEOUT
stream = options.fetch(:stream, false)
response = Gitlab::HTTP.post(
"#{url}/v1/messages",
......@@ -126,8 +130,10 @@ def perform_messages_request(messages:, options:)
body: request_body_for_messages(messages: messages, options: options).to_json,
timeout: timeout,
allow_local_requests: true,
stream_body: options.fetch(:stream, false)
stream_body: stream
) do |fragment|
next unless stream
parse_sse_events(fragment).each do |parsed_event|
yield parsed_event if block_given?
end
......
......@@ -33,7 +33,7 @@
let(:expected_response) do
{
'completion' => 'Completion Response',
'completion' => 'data: { response: Completion Response }',
'stop' => nil,
'stop_reason' => 'max_tokens',
'truncated' => false,
......@@ -108,11 +108,11 @@
stub_const("Gitlab::Llm::Concerns::ExponentialBackoff::INITIAL_DELAY", 0.0)
end
it_behaves_like 'tracks events for AI requests', 2, 4
it_behaves_like 'tracks events for AI requests', 2, 9
end
end
it_behaves_like 'tracks events for AI requests', 2, 4
it_behaves_like 'tracks events for AI requests', 2, 9
it 'logs the response' do
complete
......@@ -353,7 +353,7 @@
'content' => [
{
'type' => 'text',
'text' => 'Completion Response'
'text' => 'data: { response: Completion Response }'
}
],
'stop_reason' => 'end_turn',
......@@ -426,11 +426,11 @@
stub_const("Gitlab::Llm::Concerns::ExponentialBackoff::INITIAL_DELAY", 0.0)
end
it_behaves_like 'tracks events for AI requests', 2, 4
it_behaves_like 'tracks events for AI requests', 2, 9
end
end
it_behaves_like 'tracks events for AI requests', 2, 4
it_behaves_like 'tracks events for AI requests', 2, 9
it 'logs the response' do
messages_complete
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册