2023-03-15 16:02:20 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class OpenAiCompletionsInferenceStubs
|
2023-03-22 15:00:28 -04:00
|
|
|
TRANSLATE = "translate"
|
|
|
|
PROOFREAD = "proofread"
|
|
|
|
GENERATE_TITLES = "generate_titles"
|
2023-09-25 14:12:54 -04:00
|
|
|
CUSTOM_PROMPT = "custom_prompt"
|
2023-10-23 10:41:36 -04:00
|
|
|
EXPLAIN = "explain"
|
2023-03-22 15:00:28 -04:00
|
|
|
|
2023-03-15 16:02:20 -04:00
|
|
|
class << self
|
2023-04-10 10:04:42 -04:00
|
|
|
def text_mode_to_id(mode)
|
|
|
|
case mode
|
|
|
|
when TRANSLATE
|
|
|
|
-1
|
|
|
|
when PROOFREAD
|
|
|
|
-3
|
|
|
|
when GENERATE_TITLES
|
|
|
|
-2
|
2023-09-25 14:12:54 -04:00
|
|
|
when CUSTOM_PROMPT
|
|
|
|
-5
|
2023-10-23 10:41:36 -04:00
|
|
|
when EXPLAIN
|
|
|
|
-4
|
2023-04-10 10:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-15 16:02:20 -04:00
|
|
|
def spanish_text
|
|
|
|
<<~STRING
|
2023-04-21 02:54:25 -04:00
|
|
|
Para que su horror sea perfecto, César, acosado al pie de la estatua por lo impacientes puñales de sus amigos,
|
2023-03-15 16:02:20 -04:00
|
|
|
descubre entre las caras y los aceros la de Marco Bruto, su protegido, acaso su hijo,
|
|
|
|
y ya no se defiende y exclama: ¡Tú también, hijo mío! Shakespeare y Quevedo recogen el patético grito.
|
2023-04-21 02:54:25 -04:00
|
|
|
|
|
|
|
Al destino le agradan las repeticiones, las variantes, las simetrías; diecinueve siglos después,
|
|
|
|
en el sur de la provincia de Buenos Aires, un gaucho es agredido por otros gauchos y, al caer,
|
|
|
|
reconoce a un ahijado suyo y le dice con mansa reconvención y lenta sorpresa (estas palabras hay que oírlas, no leerlas):
|
2023-03-15 16:02:20 -04:00
|
|
|
¡Pero, che! Lo matan y no sabe que muere para que se repita una escena.
|
|
|
|
STRING
|
|
|
|
end
|
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
def custom_prompt_input
|
|
|
|
"Translate to French"
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_prompt_response
|
|
|
|
<<~STRING
|
|
|
|
Le destin favorise les répétitions, les variantes, les symétries ;
|
2023-09-25 16:26:00 -04:00
|
|
|
dix-neuf siècles plus tard, dans la province du sud de Buenos Aires,
|
|
|
|
un gaucho est attaqué par d'autres gauchos et, en tombant,
|
|
|
|
reconnaît un filleul à lui et dit avec doux reproche et surprise lente (ces mots doivent être entendus, pas lus)
|
|
|
|
: 'Mais, mon ami!' Il est tué et ne sait pas qu'il meurt pour qu'une scène puisse être répétée.
|
2023-09-25 14:12:54 -04:00
|
|
|
STRING
|
|
|
|
end
|
|
|
|
|
2023-03-15 16:02:20 -04:00
|
|
|
def translated_response
|
|
|
|
<<~STRING
|
|
|
|
"To perfect his horror, Caesar, surrounded at the base of the statue by the impatient daggers of his friends,
|
|
|
|
discovers among the faces and blades that of Marcus Brutus, his protege, perhaps his son, and he no longer
|
|
|
|
defends himself, but instead exclaims: 'You too, my son!' Shakespeare and Quevedo capture the pathetic cry.
|
|
|
|
|
|
|
|
Destiny favors repetitions, variants, symmetries; nineteen centuries later, in the southern province of Buenos Aires,
|
|
|
|
a gaucho is attacked by other gauchos and, as he falls, recognizes a godson of his and says with gentle rebuke and
|
|
|
|
slow surprise (these words must be heard, not read): 'But, my friend!' He is killed and does not know that he
|
|
|
|
dies so that a scene may be repeated."
|
|
|
|
STRING
|
|
|
|
end
|
|
|
|
|
|
|
|
def generated_titles
|
|
|
|
<<~STRING
|
|
|
|
1. "The Life and Death of a Nameless Gaucho"
|
|
|
|
2. "The Faith of Iron and Courage: A Gaucho's Legacy"
|
|
|
|
3. "The Quiet Piece that Moves Literature: A Gaucho's Story"
|
|
|
|
4. "The Unknown Hero: A Gaucho's Sacrifice for Country"
|
|
|
|
5. "From Dust to Legacy: The Enduring Name of a Gaucho"
|
|
|
|
STRING
|
|
|
|
end
|
|
|
|
|
|
|
|
def proofread_response
|
|
|
|
<<~STRING
|
|
|
|
"This excerpt explores the idea of repetition and symmetry in tragic events. The author highlights two instances
|
|
|
|
where someone is betrayed by a close friend or protege, uttering a similar phrase of surprise and disappointment
|
|
|
|
before their untimely death. The first example refers to Julius Caesar, who upon realizing that one of his own
|
|
|
|
friends and proteges, Marcus Brutus, is among his assassins, exclaims \"You too, my son!\" The second example
|
2023-04-21 02:54:25 -04:00
|
|
|
is of a gaucho in Buenos Aires, who recognizes his godson among his attackers and utters the words of rebuke
|
2023-03-15 16:02:20 -04:00
|
|
|
and surprise, \"But, my friend!\" before he is killed. The author suggests that these tragedies occur so that
|
|
|
|
a scene may be repeated, emphasizing the cyclical nature of history and the inevitability of certain events."
|
|
|
|
STRING
|
|
|
|
end
|
|
|
|
|
2023-10-23 10:41:36 -04:00
|
|
|
def explain_response
|
|
|
|
<<~STRING
|
|
|
|
"In this context, \"pie\" refers to a baked dessert typically consisting of a pastry crust and filling.
|
|
|
|
The person states they enjoy eating pie, considering it a good dessert. They note that some people wastefully
|
|
|
|
throw pie at others, but the person themselves chooses to eat the pie rather than throwing it. Overall, \"pie\"
|
|
|
|
is being used to refer the the baked dessert food item."
|
|
|
|
STRING
|
|
|
|
end
|
|
|
|
|
2023-03-15 16:02:20 -04:00
|
|
|
def response(content)
|
|
|
|
{
|
|
|
|
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: [
|
|
|
|
{ message: { role: "assistant", content: content }, finish_reason: "stop", index: 0 },
|
|
|
|
],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_text_for(type)
|
|
|
|
case type
|
2023-03-22 15:00:28 -04:00
|
|
|
when TRANSLATE
|
2023-03-15 16:02:20 -04:00
|
|
|
translated_response
|
2023-03-22 15:00:28 -04:00
|
|
|
when PROOFREAD
|
2023-03-15 16:02:20 -04:00
|
|
|
proofread_response
|
2023-03-22 15:00:28 -04:00
|
|
|
when GENERATE_TITLES
|
2023-03-15 16:02:20 -04:00
|
|
|
generated_titles
|
2023-09-25 14:12:54 -04:00
|
|
|
when CUSTOM_PROMPT
|
|
|
|
custom_prompt_response
|
2023-10-23 10:41:36 -04:00
|
|
|
when EXPLAIN
|
|
|
|
explain_response
|
2023-03-15 16:02:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_prompt(type)
|
2023-09-25 14:12:54 -04:00
|
|
|
user_input = type == TRANSLATE ? spanish_text : translated_response
|
|
|
|
id = text_mode_to_id(type)
|
|
|
|
|
|
|
|
if type == CUSTOM_PROMPT
|
|
|
|
user_input = { mode: id, text: translated_response, custom_prompt: "Translate to French" }
|
|
|
|
elsif type == TRANSLATE
|
|
|
|
user_input = { mode: id, text: spanish_text, custom_prompt: "" }
|
|
|
|
else
|
|
|
|
user_input = { mode: id, text: translated_response, custom_prompt: "" }
|
|
|
|
end
|
2023-03-17 14:14:19 -04:00
|
|
|
|
2023-09-25 14:12:54 -04:00
|
|
|
prompt_messages = CompletionPrompt.find_by(id: id).messages_with_user_input(user_input)
|
2023-03-15 16:02:20 -04:00
|
|
|
|
2023-05-05 14:28:31 -04:00
|
|
|
stub_response(prompt_messages, response_text_for(type))
|
|
|
|
end
|
|
|
|
|
2023-05-31 19:10:33 -04:00
|
|
|
def stub_response(messages, response_text, model: nil, req_opts: {})
|
2023-05-05 14:28:31 -04:00
|
|
|
WebMock
|
|
|
|
.stub_request(:post, "https://api.openai.com/v1/chat/completions")
|
2023-05-31 19:10:33 -04:00
|
|
|
.with(body: { model: model || "gpt-3.5-turbo", messages: messages }.merge(req_opts).to_json)
|
2023-05-05 14:28:31 -04:00
|
|
|
.to_return(status: 200, body: JSON.dump(response(response_text)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def stream_line(finish_reason: nil, delta: {})
|
|
|
|
+"data: " << {
|
|
|
|
id: "chatcmpl-#{SecureRandom.hex}",
|
|
|
|
object: "chat.completion.chunk",
|
|
|
|
created: 1_681_283_881,
|
|
|
|
model: "gpt-3.5-turbo-0301",
|
|
|
|
choices: [{ delta: delta }],
|
|
|
|
finish_reason: finish_reason,
|
|
|
|
index: 0,
|
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
2023-05-31 19:10:33 -04:00
|
|
|
def stub_streamed_response(messages, deltas, model: nil, req_opts: {})
|
2023-05-05 14:28:31 -04:00
|
|
|
chunks = deltas.map { |d| stream_line(delta: d) }
|
|
|
|
chunks << stream_line(finish_reason: "stop")
|
|
|
|
chunks << "[DONE]"
|
|
|
|
chunks = chunks.join("\n\n")
|
|
|
|
|
2023-03-15 16:02:20 -04:00
|
|
|
WebMock
|
|
|
|
.stub_request(:post, "https://api.openai.com/v1/chat/completions")
|
2023-05-31 19:10:33 -04:00
|
|
|
.with(body: { model: model || "gpt-3.5-turbo", messages: messages }.merge(req_opts).to_json)
|
2023-05-05 14:28:31 -04:00
|
|
|
.to_return(status: 200, body: chunks)
|
2023-03-15 16:02:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|