# frozen_string_literal: true RSpec.describe DiscourseAi::Completions::Dialects::Claude do subject(:dialect) { described_class.new(prompt, "claude-2") } let(:tool) do { name: "get_weather", description: "Get the weather in a city", parameters: [ { name: "location", type: "string", description: "the city name", required: true }, { name: "unit", type: "string", description: "the unit of measurement celcius c or fahrenheit f", enum: %w[c f], required: true, }, ], } end 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 XML tags: 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. TEXT post_insts: "Please put the translation between tags and separate each title with a comma.", } end describe "#translate" do it "translates a prompt written in our generic format to Claude's format" do anthropic_version = (<<~TEXT).strip + " " #{prompt[:insts]} Human: #{prompt[:input]} #{prompt[:post_insts]} Assistant: TEXT translated = dialect.translate expect(translated).to eq(anthropic_version) end it "knows how to translate examples to Claude's format" do prompt[:examples] = [ [ "In the labyrinth of time, a solitary horse, etched in gold by the setting sun, embarked on an infinite journey.", "The solitary horse.,The horse etched in gold.,A horse's infinite journey.,A horse lost in time.,A horse's last ride.", ], ] anthropic_version = (<<~TEXT).strip + " " #{prompt[:insts]} H: #{prompt[:examples][0][0]} A: #{prompt[:examples][0][1]} Human: #{prompt[:input]} #{prompt[:post_insts]} Assistant: TEXT translated = dialect.translate expect(translated).to eq(anthropic_version) end it "include tools inside the prompt" do prompt[:tools] = [tool] anthropic_version = (<<~TEXT).strip + " " #{prompt[:insts]} #{DiscourseAi::Completions::Dialects::Claude.tool_preamble} #{dialect.tools} Human: #{prompt[:input]} #{prompt[:post_insts]} Assistant: TEXT translated = dialect.translate expect(translated).to eq(anthropic_version) end it "includes all the right newlines" do prompt.clear prompt.merge!( { insts: "You are an artist", conversation_context: [ { content: "draw another funny cat", type: "user", name: "sam" }, { content: "ok", type: "assistant" }, { content: "draw a funny cat", type: "user", name: "sam" }, ], }, ) expected = (<<~TEXT).strip + " " You are an artist Human: draw a funny cat Assistant: ok Human: draw another funny cat Assistant: TEXT expect(dialect.translate).to eq(expected) end end describe "#conversation_context" do let(:context) do [ { type: "user", name: "user1", content: "This is a new message by a user" }, { type: "assistant", content: "I'm a previous bot reply, that's why there's no user" }, { type: "tool", name: "tool_id", content: "I'm a tool result" }, ] end it "adds conversation in reverse order (first == newer)" do prompt[:conversation_context] = context expected = (<<~TEXT).strip Assistant: tool_id #{context.last[:content]} Assistant: #{context.second[:content]} Human: #{context.first[:content]} TEXT translated_context = dialect.conversation_context expect(translated_context).to eq(expected) end it "trims content if it's getting too long" do context.last[:content] = context.last[:content] * 20_000 prompt[:conversation_context] = context translated_context = dialect.conversation_context expect(translated_context.length).to be < context.last[:content].length end end describe "#tools" do it "translates tools to the tool syntax" do prompt[:tools] = [tool] translated_tool = <<~TEXT get_weather Get the weather in a city location string the city name true unit string the unit of measurement celcius c or fahrenheit f true c,f TEXT expect(dialect.tools).to eq(translated_tool) end end end