mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-02 07:19:42 +00:00
Previous to this change we relied on explicit loading for a files in Discourse AI. This had a few downsides: - Busywork whenever you add a file (an extra require relative) - We were not keeping to conventions internally ... some places were OpenAI others are OpenAi - Autoloader did not work which lead to lots of full application broken reloads when developing. This moves all of DiscourseAI into a Zeitwerk compatible structure. It also leaves some minimal amount of manual loading (automation - which is loading into an existing namespace that may or may not be there) To avoid needing /lib/discourse_ai/... we mount a namespace thus we are able to keep /lib pointed at ::DiscourseAi Various files were renamed to get around zeitwerk rules and minimize usage of custom inflections Though we can get custom inflections to work it is not worth it, will require a Discourse core patch which means we create a hard dependency.
64 lines
2.7 KiB
Ruby
64 lines
2.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe DiscourseAi::Completions::Dialects::ChatGpt do
|
|
subject(:dialect) { described_class.new }
|
|
|
|
let(:prompt) do
|
|
{
|
|
insts: <<~TEXT,
|
|
I want you to act as a title generator for written pieces. I will provide you with a text,
|
|
and you will generate five attention-grabbing titles. Please keep the title concise and under 20 words,
|
|
and ensure that the meaning is maintained. Replies will utilize the language type of the topic.
|
|
TEXT
|
|
input: <<~TEXT,
|
|
Here is the text, inside <input></input> XML tags:
|
|
<input>
|
|
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.
|
|
</input>
|
|
TEXT
|
|
post_insts:
|
|
"Please put the translation between <ai></ai> tags and separate each title with a comma.",
|
|
}
|
|
end
|
|
|
|
describe "#translate" do
|
|
it "translates a prompt written in our generic format to the ChatGPT format" do
|
|
open_ai_version = [
|
|
{ role: "system", content: [prompt[:insts], prompt[:post_insts]].join("\n") },
|
|
{ role: "user", content: prompt[:input] },
|
|
]
|
|
|
|
translated = dialect.translate(prompt)
|
|
|
|
expect(translated).to contain_exactly(*open_ai_version)
|
|
end
|
|
|
|
it "include examples in the ChatGPT version" do
|
|
prompt[:examples] = [
|
|
[
|
|
"<input>In the labyrinth of time, a solitary horse, etched in gold by the setting sun, embarked on an infinite journey.</input>",
|
|
"<ai>The solitary horse.,The horse etched in gold.,A horse's infinite journey.,A horse lost in time.,A horse's last ride.</ai>",
|
|
],
|
|
]
|
|
|
|
open_ai_version = [
|
|
{ role: "system", content: [prompt[:insts], prompt[:post_insts]].join("\n") },
|
|
{ role: "user", content: prompt[:examples][0][0] },
|
|
{ role: "assistant", content: prompt[:examples][0][1] },
|
|
{ role: "user", content: prompt[:input] },
|
|
]
|
|
|
|
translated = dialect.translate(prompt)
|
|
|
|
expect(translated).to contain_exactly(*open_ai_version)
|
|
end
|
|
end
|
|
end
|