2023-12-26 12:49:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
require_relative "endpoint_compliance"
|
2024-01-02 09:21:13 -05:00
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
class VllmMock < EndpointMock
|
2023-12-26 12:49:55 -05:00
|
|
|
def response(content)
|
|
|
|
{
|
|
|
|
id: "cmpl-6sZfAb30Rnv9Q7ufzFwvQsMpjZh8S",
|
2024-05-07 09:02:16 -04:00
|
|
|
object: "chat.completion",
|
2023-12-26 12:49:55 -05:00
|
|
|
created: 1_678_464_820,
|
2024-01-17 13:08:49 -05:00
|
|
|
model: "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
2023-12-26 12:49:55 -05:00
|
|
|
usage: {
|
|
|
|
prompt_tokens: 337,
|
|
|
|
completion_tokens: 162,
|
|
|
|
total_tokens: 499,
|
|
|
|
},
|
2024-05-07 09:02:16 -04:00
|
|
|
choices: [
|
|
|
|
{ message: { role: "assistant", content: content }, finish_reason: "stop", index: 0 },
|
|
|
|
],
|
2023-12-26 12:49:55 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_response(prompt, response_text, tool_call: false)
|
|
|
|
WebMock
|
2024-05-07 09:02:16 -04:00
|
|
|
.stub_request(:post, "#{SiteSetting.ai_vllm_endpoint}/v1/chat/completions")
|
|
|
|
.with(body: model.default_options.merge(messages: prompt).to_json)
|
2023-12-26 12:49:55 -05:00
|
|
|
.to_return(status: 200, body: JSON.dump(response(response_text)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def stream_line(delta, finish_reason: nil)
|
|
|
|
+"data: " << {
|
|
|
|
id: "cmpl-#{SecureRandom.hex}",
|
|
|
|
created: 1_681_283_881,
|
2024-01-17 13:08:49 -05:00
|
|
|
model: "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
2024-05-07 09:02:16 -04:00
|
|
|
choices: [{ delta: { content: delta } }],
|
2023-12-26 12:49:55 -05:00
|
|
|
index: 0,
|
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_streamed_response(prompt, deltas, tool_call: false)
|
|
|
|
chunks =
|
|
|
|
deltas.each_with_index.map do |_, index|
|
|
|
|
if index == (deltas.length - 1)
|
|
|
|
stream_line(deltas[index], finish_reason: "stop_sequence")
|
|
|
|
else
|
|
|
|
stream_line(deltas[index])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
chunks = (chunks.join("\n\n") << "data: [DONE]").split("")
|
|
|
|
|
|
|
|
WebMock
|
2024-05-07 09:02:16 -04:00
|
|
|
.stub_request(:post, "#{SiteSetting.ai_vllm_endpoint}/v1/chat/completions")
|
|
|
|
.with(body: model.default_options.merge(messages: prompt, stream: true).to_json)
|
2023-12-26 12:49:55 -05:00
|
|
|
.to_return(status: 200, body: chunks)
|
|
|
|
end
|
2024-01-17 13:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.describe DiscourseAi::Completions::Endpoints::Vllm do
|
|
|
|
subject(:endpoint) do
|
|
|
|
described_class.new(
|
|
|
|
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
|
|
|
DiscourseAi::Tokenizer::MixtralTokenizer,
|
|
|
|
)
|
|
|
|
end
|
2023-12-26 12:49:55 -05:00
|
|
|
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:user)
|
2024-01-17 13:08:49 -05:00
|
|
|
|
|
|
|
let(:anthropic_mock) { VllmMock.new(endpoint) }
|
|
|
|
|
|
|
|
let(:compliance) do
|
2024-05-07 09:02:16 -04:00
|
|
|
EndpointsCompliance.new(self, endpoint, DiscourseAi::Completions::Dialects::Mistral, user)
|
2024-01-17 13:08:49 -05:00
|
|
|
end
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
let(:dialect) { DiscourseAi::Completions::Dialects::Mistral.new(generic_prompt, model_name) }
|
2024-01-17 13:08:49 -05:00
|
|
|
let(:prompt) { dialect.translate }
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
let(:request_body) { model.default_options.merge(messages: prompt).to_json }
|
|
|
|
let(:stream_request_body) { model.default_options.merge(messages: prompt, stream: true).to_json }
|
2024-01-17 13:08:49 -05:00
|
|
|
|
|
|
|
before { SiteSetting.ai_vllm_endpoint = "https://test.dev" }
|
|
|
|
|
|
|
|
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(anthropic_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with tools" do
|
|
|
|
it "returns a function invocation" do
|
|
|
|
compliance.regular_mode_tools(anthropic_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(anthropic_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with tools" do
|
|
|
|
it "returns a function invoncation" do
|
|
|
|
compliance.streaming_mode_tools(anthropic_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-12-26 12:49:55 -05:00
|
|
|
end
|