2023-06-21 10:39:51 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
describe DiscourseAi::Inference::OpenAiEmbeddings do
|
2025-01-21 12:23:19 -03:00
|
|
|
let(:api_key) { "123456" }
|
|
|
|
let(:dimensions) { 1000 }
|
|
|
|
let(:model) { "text-embedding-ada-002" }
|
|
|
|
|
2023-06-21 10:39:51 +10:00
|
|
|
it "supports azure embeddings" do
|
2025-01-21 12:23:19 -03:00
|
|
|
azure_url =
|
2023-06-21 10:39:51 +10:00
|
|
|
"https://my-company.openai.azure.com/openai/deployments/embeddings-deployment/embeddings?api-version=2023-05-15"
|
|
|
|
|
|
|
|
body_json = {
|
|
|
|
usage: {
|
|
|
|
prompt_tokens: 1,
|
|
|
|
total_tokens: 1,
|
|
|
|
},
|
|
|
|
data: [{ object: "embedding", embedding: [0.0, 0.1] }],
|
|
|
|
}.to_json
|
|
|
|
|
2025-01-21 12:23:19 -03:00
|
|
|
stub_request(:post, azure_url).with(
|
2023-06-21 10:39:51 +10:00
|
|
|
body: "{\"model\":\"text-embedding-ada-002\",\"input\":\"hello\"}",
|
|
|
|
headers: {
|
2025-01-21 12:23:19 -03:00
|
|
|
"Api-Key" => api_key,
|
2023-06-21 10:39:51 +10:00
|
|
|
"Content-Type" => "application/json",
|
|
|
|
},
|
|
|
|
).to_return(status: 200, body: body_json, headers: {})
|
|
|
|
|
2024-01-30 03:24:30 +11:00
|
|
|
result =
|
2025-01-21 12:23:19 -03:00
|
|
|
DiscourseAi::Inference::OpenAiEmbeddings.new(azure_url, api_key, model, nil).perform!("hello")
|
2023-06-21 10:39:51 +10:00
|
|
|
|
2024-11-25 13:12:43 -03:00
|
|
|
expect(result).to eq([0.0, 0.1])
|
2023-06-21 10:39:51 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports openai embeddings" do
|
2025-01-21 12:23:19 -03:00
|
|
|
url = "https://api.openai.com/v1/embeddings"
|
2023-06-21 10:39:51 +10:00
|
|
|
body_json = {
|
|
|
|
usage: {
|
|
|
|
prompt_tokens: 1,
|
|
|
|
total_tokens: 1,
|
|
|
|
},
|
|
|
|
data: [{ object: "embedding", embedding: [0.0, 0.1] }],
|
|
|
|
}.to_json
|
|
|
|
|
2025-01-21 12:23:19 -03:00
|
|
|
body = { model: model, input: "hello", dimensions: dimensions }.to_json
|
2024-01-30 03:24:30 +11:00
|
|
|
|
2025-01-21 12:23:19 -03:00
|
|
|
stub_request(:post, url).with(
|
2024-01-30 03:24:30 +11:00
|
|
|
body: body,
|
2023-06-21 10:39:51 +10:00
|
|
|
headers: {
|
2025-01-21 12:23:19 -03:00
|
|
|
"Authorization" => "Bearer #{api_key}",
|
2023-06-21 10:39:51 +10:00
|
|
|
"Content-Type" => "application/json",
|
|
|
|
},
|
|
|
|
).to_return(status: 200, body: body_json, headers: {})
|
|
|
|
|
2024-01-30 03:24:30 +11:00
|
|
|
result =
|
2025-01-21 12:23:19 -03:00
|
|
|
DiscourseAi::Inference::OpenAiEmbeddings.new(url, api_key, model, dimensions).perform!(
|
|
|
|
"hello",
|
|
|
|
)
|
2023-06-21 10:39:51 +10:00
|
|
|
|
2024-11-25 13:12:43 -03:00
|
|
|
expect(result).to eq([0.0, 0.1])
|
2023-06-21 10:39:51 +10:00
|
|
|
end
|
|
|
|
end
|