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
|
2023-12-06 09:22:42 -05:00
|
|
|
.stub_request(:post, "#{SiteSetting.ai_hugging_face_api_url}")
|
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)
|
|
|
|
WebMock.stub_request(:post, "#{SiteSetting.ai_hugging_face_api_url}").to_return(
|
|
|
|
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
|
2023-12-06 09:22:42 -05:00
|
|
|
.stub_request(:post, "#{SiteSetting.ai_hugging_face_api_url}")
|
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|
|
|
|
|
b[:max_tokens] = (SiteSetting.ai_hugging_face_token_limit || 4_000) -
|
2024-01-17 13:08:49 -05:00
|
|
|
model.prompt_size(prompt)
|
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
|
|
|
|
subject(:endpoint) do
|
2024-05-13 11:46:42 -04:00
|
|
|
described_class.new(
|
|
|
|
"mistralai/Mistral-7B-Instruct-v0.2",
|
|
|
|
DiscourseAi::Tokenizer::MixtralTokenizer,
|
|
|
|
)
|
2024-01-17 13:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before { SiteSetting.ai_hugging_face_api_url = "https://test.dev" }
|
|
|
|
|
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
|
|
|
|
|
|
|
|
context "with tools" do
|
|
|
|
it "returns a function invocation" do
|
|
|
|
compliance.regular_mode_tools(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
|
|
|
|
|
|
|
|
context "with tools" do
|
2024-01-19 06:51:26 -05:00
|
|
|
it "returns a function invocation" do
|
2024-01-17 13:08:49 -05:00
|
|
|
compliance.streaming_mode_tools(hf_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|