@@ -7,6 +7,18 @@ def trim(messages)
77 trim_messages ( messages )
88 end
99
10+ def system_msg ( msg )
11+ msg
12+ end
13+
14+ def user_msg ( msg )
15+ msg
16+ end
17+
18+ def model_msg ( msg )
19+ msg
20+ end
21+
1022 def tokenizer
1123 DiscourseAi ::Tokenizer ::OpenAiTokenizer
1224 end
@@ -15,6 +27,57 @@ def tokenizer
1527RSpec . describe DiscourseAi ::Completions ::Dialects ::Dialect do
1628 fab! ( :llm_model )
1729
30+ describe "#translate" do
31+ let ( :five_token_msg ) { "This represents five tokens." }
32+ let ( :tools ) do
33+ [
34+ {
35+ name : "echo" ,
36+ description : "echo a string" ,
37+ parameters : [
38+ { name : "text" , type : "string" , description : "string to echo" , required : true } ,
39+ ] ,
40+ } ,
41+ ]
42+ end
43+
44+ it "injects done message when tool_choice is :none and last message follows tool pattern" do
45+ tool_call_prompt = { name : "echo" , arguments : { text : "test message" } }
46+
47+ prompt = DiscourseAi ::Completions ::Prompt . new ( "System instructions" , tools : tools )
48+ prompt . push ( type : :user , content : "echo test message" )
49+ prompt . push ( type : :tool_call , content : tool_call_prompt . to_json , id : "123" , name : "echo" )
50+ prompt . push ( type : :tool , content : "test message" . to_json , name : "echo" , id : "123" )
51+ prompt . tool_choice = :none
52+
53+ dialect = TestDialect . new ( prompt , llm_model )
54+ dialect . max_prompt_tokens = 100 # Set high enough to avoid trimming
55+
56+ translated = dialect . translate
57+
58+ expect ( translated ) . to eq (
59+ [
60+ { type : :system , content : "System instructions" } ,
61+ { type : :user , content : "echo test message" } ,
62+ {
63+ type : :tool_call ,
64+ content :
65+ "<function_calls>\n <invoke>\n <tool_name>echo</tool_name>\n <parameters>\n <text>test message</text>\n </parameters>\n </invoke>\n </function_calls>" ,
66+ id : "123" ,
67+ name : "echo" ,
68+ } ,
69+ {
70+ type : :tool ,
71+ id : "123" ,
72+ name : "echo" ,
73+ content :
74+ "<function_results>\n <result>\n <tool_name>echo</tool_name>\n <json>\n \" test message\" \n </json>\n </result>\n </function_results>\n \n #{ ::DiscourseAi ::Completions ::Dialects ::XmlTools ::DONE_MESSAGE } " ,
75+ } ,
76+ ] ,
77+ )
78+ end
79+ end
80+
1881 describe "#trim_messages" do
1982 let ( :five_token_msg ) { "This represents five tokens." }
2083
0 commit comments