2023-11-23 12:58:54 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative "endpoint_examples"
|
|
|
|
|
2023-11-29 15:17:46 +11:00
|
|
|
RSpec.describe DiscourseAi::Completions::Endpoints::OpenAi do
|
2023-11-23 12:58:54 -03:00
|
|
|
subject(:model) { described_class.new(model_name, DiscourseAi::Tokenizer::OpenAiTokenizer) }
|
|
|
|
|
|
|
|
let(:model_name) { "gpt-3.5-turbo" }
|
2023-12-18 18:06:01 -03:00
|
|
|
let(:generic_prompt) { { insts: "You are a helpful bot.", input: "write 3 words" } }
|
|
|
|
let(:dialect) { DiscourseAi::Completions::Dialects::ChatGpt.new(generic_prompt, model_name) }
|
|
|
|
let(:prompt) { dialect.translate }
|
|
|
|
|
|
|
|
let(:tool_deltas) do
|
2023-11-23 12:58:54 -03:00
|
|
|
[
|
2023-12-18 18:06:01 -03:00
|
|
|
{ id: "get_weather", name: "get_weather", arguments: {} },
|
|
|
|
{ id: "get_weather", name: "get_weather", arguments: { location: "" } },
|
|
|
|
{ id: "get_weather", name: "get_weather", arguments: { location: "Sydney", unit: "c" } },
|
2023-11-23 12:58:54 -03:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2023-12-18 18:06:01 -03:00
|
|
|
let(:tool_call) do
|
|
|
|
{ id: "get_weather", name: "get_weather", arguments: { location: "Sydney", unit: "c" } }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:request_body) do
|
|
|
|
model
|
|
|
|
.default_options
|
|
|
|
.merge(messages: prompt)
|
|
|
|
.tap do |b|
|
|
|
|
b[:tools] = generic_prompt[:tools].map do |t|
|
|
|
|
{ type: "function", tool: t }
|
|
|
|
end if generic_prompt[:tools]
|
|
|
|
end
|
|
|
|
.to_json
|
|
|
|
end
|
|
|
|
let(:stream_request_body) do
|
|
|
|
model
|
|
|
|
.default_options
|
|
|
|
.merge(messages: prompt, stream: true)
|
|
|
|
.tap do |b|
|
|
|
|
b[:tools] = generic_prompt[:tools].map do |t|
|
|
|
|
{ type: "function", tool: t }
|
|
|
|
end if generic_prompt[:tools]
|
|
|
|
end
|
|
|
|
.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def response(content, tool_call: false)
|
|
|
|
message_content =
|
|
|
|
if tool_call
|
|
|
|
{ tool_calls: [{ function: content }] }
|
|
|
|
else
|
|
|
|
{ content: content }
|
|
|
|
end
|
2023-11-23 12:58:54 -03:00
|
|
|
|
|
|
|
{
|
|
|
|
id: "chatcmpl-6sZfAb30Rnv9Q7ufzFwvQsMpjZh8S",
|
|
|
|
object: "chat.completion",
|
|
|
|
created: 1_678_464_820,
|
|
|
|
model: "gpt-3.5-turbo-0301",
|
|
|
|
usage: {
|
|
|
|
prompt_tokens: 337,
|
|
|
|
completion_tokens: 162,
|
|
|
|
total_tokens: 499,
|
|
|
|
},
|
|
|
|
choices: [
|
2023-12-18 18:06:01 -03:00
|
|
|
{ message: { role: "assistant" }.merge(message_content), finish_reason: "stop", index: 0 },
|
2023-11-23 12:58:54 -03:00
|
|
|
],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-12-18 18:06:01 -03:00
|
|
|
def stub_response(prompt, response_text, tool_call: false)
|
2023-11-23 12:58:54 -03:00
|
|
|
WebMock
|
|
|
|
.stub_request(:post, "https://api.openai.com/v1/chat/completions")
|
2023-12-18 18:06:01 -03:00
|
|
|
.with(body: request_body)
|
|
|
|
.to_return(status: 200, body: JSON.dump(response(response_text, tool_call: tool_call)))
|
2023-11-23 12:58:54 -03:00
|
|
|
end
|
|
|
|
|
2023-12-18 18:06:01 -03:00
|
|
|
def stream_line(delta, finish_reason: nil, tool_call: false)
|
|
|
|
message_content =
|
|
|
|
if tool_call
|
|
|
|
{ tool_calls: [{ function: delta }] }
|
|
|
|
else
|
|
|
|
{ content: delta }
|
|
|
|
end
|
|
|
|
|
2023-11-23 12:58:54 -03:00
|
|
|
+"data: " << {
|
|
|
|
id: "chatcmpl-#{SecureRandom.hex}",
|
|
|
|
object: "chat.completion.chunk",
|
|
|
|
created: 1_681_283_881,
|
|
|
|
model: "gpt-3.5-turbo-0301",
|
2023-12-18 18:06:01 -03:00
|
|
|
choices: [{ delta: message_content }],
|
2023-11-23 12:58:54 -03:00
|
|
|
finish_reason: finish_reason,
|
|
|
|
index: 0,
|
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
2023-12-18 18:06:01 -03:00
|
|
|
def stub_streamed_response(prompt, deltas, tool_call: false)
|
2023-11-23 12:58:54 -03:00
|
|
|
chunks =
|
|
|
|
deltas.each_with_index.map do |_, index|
|
|
|
|
if index == (deltas.length - 1)
|
2023-12-18 18:06:01 -03:00
|
|
|
stream_line(deltas[index], finish_reason: "stop_sequence", tool_call: tool_call)
|
2023-11-23 12:58:54 -03:00
|
|
|
else
|
2023-12-18 18:06:01 -03:00
|
|
|
stream_line(deltas[index], tool_call: tool_call)
|
2023-11-23 12:58:54 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-18 18:06:01 -03:00
|
|
|
chunks = (chunks.join("\n\n") << "data: [DONE]").split("")
|
2023-11-23 12:58:54 -03:00
|
|
|
|
|
|
|
WebMock
|
|
|
|
.stub_request(:post, "https://api.openai.com/v1/chat/completions")
|
2023-12-18 18:06:01 -03:00
|
|
|
.with(body: stream_request_body)
|
2023-11-23 12:58:54 -03:00
|
|
|
.to_return(status: 200, body: chunks)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like "an endpoint that can communicate with a completion service"
|
|
|
|
end
|