2023-12-15 12:32:01 -05:00
# frozen_string_literal: true
2024-01-12 12:36:44 -05:00
require_relative " dialect_context "
2023-12-15 12:32:01 -05:00
2024-01-12 12:36:44 -05:00
RSpec . describe DiscourseAi :: Completions :: Dialects :: Gemini do
2024-05-22 02:35:29 -04:00
let ( :model_name ) { " gemini-1.5-pro " }
2024-01-12 12:36:44 -05:00
let ( :context ) { DialectContext . new ( described_class , model_name ) }
2023-12-15 12:32:01 -05:00
describe " # translate " do
it " translates a prompt written in our generic format to the Gemini format " do
2024-05-22 02:35:29 -04:00
gemini_version = {
messages : [ { role : " user " , parts : [ { text : context . simple_user_input } ] } ] ,
system_instruction : context . system_insts ,
}
2023-12-15 12:32:01 -05:00
2024-01-12 12:36:44 -05:00
translated = context . system_user_scenario
2023-12-15 12:32:01 -05:00
expect ( translated ) . to eq ( gemini_version )
end
2024-02-27 02:24:30 -05:00
it " injects model after tool call " do
expect ( context . image_generation_scenario ) . to eq (
2024-05-22 02:35:29 -04:00
{
messages : [
{ role : " user " , parts : [ { text : " draw a cat " } ] } ,
{
role : " model " ,
parts : [ { functionCall : { name : " draw " , args : { picture : " Cat " } } } ] ,
} ,
{
role : " function " ,
parts : [
{
functionResponse : {
name : " tool_id " ,
response : {
content : " \" I'm a tool result \" " ,
} ,
} ,
2024-02-27 02:24:30 -05:00
} ,
2024-05-22 02:35:29 -04:00
] ,
2024-02-27 02:24:30 -05:00
} ,
2024-05-22 02:35:29 -04:00
{ role : " model " , parts : { text : " Ok. " } } ,
{ role : " user " , parts : [ { text : " draw another cat " } ] } ,
] ,
system_instruction : context . system_insts ,
} ,
2024-02-27 02:24:30 -05:00
)
end
2024-01-12 12:36:44 -05:00
it " translates tool_call and tool messages " do
expect ( context . multi_turn_scenario ) . to eq (
2024-05-22 02:35:29 -04:00
{
messages : [
{ role : " user " , parts : [ { text : " This is a message by a user " } ] } ,
{
role : " model " ,
parts : [ { text : " I'm a previous bot reply, that's why there's no user " } ] ,
2024-01-12 12:36:44 -05:00
} ,
2024-05-22 02:35:29 -04:00
{ role : " user " , parts : [ { text : " This is a new message by a user " } ] } ,
{
role : " model " ,
parts : [
{ functionCall : { name : " get_weather " , args : { location : " Sydney " , unit : " c " } } } ,
] ,
2024-01-12 12:36:44 -05:00
} ,
2024-05-22 02:35:29 -04:00
{
role : " function " ,
parts : [
{
functionResponse : {
name : " get_weather " ,
response : {
content : " \" I'm a tool result \" " ,
} ,
} ,
2023-12-18 16:06:01 -05:00
} ,
2024-05-22 02:35:29 -04:00
] ,
2024-01-04 16:15:34 -05:00
} ,
2024-05-22 02:35:29 -04:00
] ,
system_instruction :
" I want you to act as a title generator for written pieces. I will provide you with a text, \n and you will generate five attention-grabbing titles. Please keep the title concise and under 20 words, \n and ensure that the meaning is maintained. Replies will utilize the language type of the topic. \n " ,
} ,
2023-12-18 16:06:01 -05:00
)
end
it " trims content if it's getting too long " do
2024-05-22 02:35:29 -04:00
# testing truncation on 800k tokens is slow use model with less
context = DialectContext . new ( described_class , " gemini-pro " )
2024-01-12 12:36:44 -05:00
translated = context . long_user_input_scenario ( length : 5_000 )
2024-01-08 08:28:03 -05:00
2024-05-22 02:35:29 -04:00
expect ( translated [ :messages ] . last [ :role ] ) . to eq ( " user " )
expect ( translated [ :messages ] . last . dig ( :parts , :text ) . length ) . to be <
2024-01-12 12:36:44 -05:00
context . long_message_text ( length : 5_000 ) . length
2024-01-05 13:21:14 -05:00
end
2023-12-18 16:06:01 -05:00
end
describe " # tools " do
it " returns a list of available tools " do
gemini_tools = {
function_declarations : [
{
name : " get_weather " ,
description : " Get the weather in a city " ,
2024-01-04 16:15:34 -05:00
parameters : {
type : " object " ,
required : %w[ location unit ] ,
properties : {
" location " = > {
type : " string " ,
description : " the city name " ,
} ,
" unit " = > {
type : " string " ,
description : " the unit of measurement celcius c or fahrenheit f " ,
enum : %w[ c f ] ,
} ,
2023-12-18 16:06:01 -05:00
} ,
2024-01-04 16:15:34 -05:00
} ,
2023-12-18 16:06:01 -05:00
} ,
] ,
}
2024-01-12 12:36:44 -05:00
expect ( context . dialect_tools ) . to contain_exactly ( gemini_tools )
2023-12-18 16:06:01 -05:00
end
end
2023-12-15 12:32:01 -05:00
end