2023-11-23 10:58:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
require_relative "endpoint_compliance"
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
class HuggingFaceMock < EndpointMock
|
2023-11-23 10:58:54 -05:00
|
|
|
def response(content)
|
2024-05-07 09:02:16 -04:00
|
|
|
{
|
|
|
|
id: "chatcmpl-6sZfAb30Rnv9Q7ufzFwvQsMpjZh8S",
|
|
|
|
object: "chat.completion",
|
|
|
|
created: 1_678_464_820,
|
|
|
|
model: "Llama2-*-chat-hf",
|
|
|
|
usage: {
|
|
|
|
prompt_tokens: 337,
|
|
|
|
completion_tokens: 162,
|
|
|
|
total_tokens: 499,
|
|
|
|
},
|
|
|
|
choices: [
|
|
|
|
{ message: { role: "assistant", content: content }, finish_reason: "stop", index: 0 },
|
|
|
|
],
|
|
|
|
}
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def stub_response(prompt, response_text, tool_call: false)
|
2023-11-23 10:58:54 -05:00
|
|
|
WebMock
|
2024-07-30 12:44:57 -04:00
|
|
|
.stub_request(:post, "https://test.dev/v1/chat/completions")
|
2024-01-17 13:08:49 -05:00
|
|
|
.with(body: request_body(prompt))
|
2023-11-23 10:58:54 -05:00
|
|
|
.to_return(status: 200, body: JSON.dump(response(response_text)))
|
|
|
|
end
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
def stream_line(delta, finish_reason: nil)
|
2023-11-23 10:58:54 -05:00
|
|
|
+"data: " << {
|
2024-05-07 09:02:16 -04:00
|
|
|
id: "chatcmpl-#{SecureRandom.hex}",
|
|
|
|
object: "chat.completion.chunk",
|
|
|
|
created: 1_681_283_881,
|
|
|
|
model: "Llama2-*-chat-hf",
|
|
|
|
choices: [{ delta: { content: delta } }],
|
|
|
|
finish_reason: finish_reason,
|
|
|
|
index: 0,
|
2023-11-23 10:58:54 -05:00
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
def stub_raw(chunks)
|
2024-07-30 12:44:57 -04:00
|
|
|
WebMock.stub_request(:post, "https://test.dev/v1/chat/completions").to_return(
|
2024-05-07 09:02:16 -04:00
|
|
|
status: 200,
|
|
|
|
body: chunks,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def stub_streamed_response(prompt, deltas, tool_call: false)
|
2023-11-23 10:58:54 -05:00
|
|
|
chunks =
|
|
|
|
deltas.each_with_index.map do |_, index|
|
|
|
|
if index == (deltas.length - 1)
|
2024-05-07 09:02:16 -04:00
|
|
|
stream_line(deltas[index], finish_reason: "stop_sequence")
|
2023-11-23 10:58:54 -05:00
|
|
|
else
|
2024-05-07 09:02:16 -04:00
|
|
|
stream_line(deltas[index])
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
chunks = (chunks.join("\n\n") << "data: [DONE]").split("")
|
2023-11-23 10:58:54 -05:00
|
|
|
|
|
|
|
WebMock
|
2024-07-30 12:44:57 -04:00
|
|
|
.stub_request(:post, "https://test.dev/v1/chat/completions")
|
2024-01-17 13:08:49 -05:00
|
|
|
.with(body: request_body(prompt, stream: true))
|
2023-11-23 10:58:54 -05:00
|
|
|
.to_return(status: 200, body: chunks)
|
2024-05-07 09:02:16 -04:00
|
|
|
|
|
|
|
yield if block_given?
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
def request_body(prompt, stream: false, tool_call: false)
|
2024-01-17 13:08:49 -05:00
|
|
|
model
|
|
|
|
.default_options
|
2024-05-07 09:02:16 -04:00
|
|
|
.merge(messages: prompt)
|
|
|
|
.tap do |b|
|
2024-07-30 12:44:57 -04:00
|
|
|
b[:max_tokens] = 63_991
|
2024-05-07 09:02:16 -04:00
|
|
|
b[:stream] = true if stream
|
2024-01-17 13:08:49 -05:00
|
|
|
end
|
|
|
|
.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.describe DiscourseAi::Completions::Endpoints::HuggingFace do
|
2024-07-30 12:44:57 -04:00
|
|
|
subject(:endpoint) { described_class.new(hf_model) }
|
2024-01-17 13:08:49 -05:00
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
fab!(:hf_model)
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:user)
|
2024-01-17 13:08:49 -05:00
|
|
|
|
|
|
|
let(:hf_mock) { HuggingFaceMock.new(endpoint) }
|
|
|
|
|
|
|
|
let(:compliance) do
|
2024-07-04 12:23:03 -04:00
|
|
|
EndpointsCompliance.new(
|
|
|
|
self,
|
|
|
|
endpoint,
|
|
|
|
DiscourseAi::Completions::Dialects::OpenAiCompatible,
|
|
|
|
user,
|
|
|
|
)
|
2024-01-17 13:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#perform_completion!" do
|
|
|
|
context "when using regular mode" do
|
|
|
|
context "with simple prompts" do
|
|
|
|
it "completes a trivial prompt and logs the response" do
|
|
|
|
compliance.regular_mode_simple_prompt(hf_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when using streaming mode" do
|
|
|
|
context "with simple prompts" do
|
|
|
|
it "completes a trivial prompt and logs the response" do
|
|
|
|
compliance.streaming_mode_simple_prompt(hf_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|