13
13
14
14
let ( :strategy ) { DiscourseAi ::Summarization . topic_summary ( topic ) }
15
15
16
+ let ( :raw_summary ) { "{\" summary\" :\" #{ clean_summary } \" }" }
17
+ let ( :clean_summary ) { "This is the final summary" }
18
+
16
19
describe "#summarize" do
17
20
subject ( :summarization ) { described_class . new ( strategy , user ) }
18
21
@@ -30,10 +33,10 @@ def assert_summary_is_cached(topic, summary_response)
30
33
let ( :summary ) { "This is the final summary" }
31
34
32
35
it "caches the summary" do
33
- DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ summary ] ) do
36
+ DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ raw_summary ] ) do
34
37
section = summarization . summarize
35
- expect ( section . summarized_text ) . to eq ( summary )
36
- assert_summary_is_cached ( topic , summary )
38
+ expect ( section . summarized_text ) . to eq ( clean_summary )
39
+ assert_summary_is_cached ( topic , clean_summary )
37
40
end
38
41
end
39
42
@@ -54,7 +57,6 @@ def assert_summary_is_cached(topic, summary_response)
54
57
55
58
describe "invalidating cached summaries" do
56
59
let ( :cached_text ) { "This is a cached summary" }
57
- let ( :updated_summary ) { "This is the final summary" }
58
60
59
61
def cached_summary
60
62
AiSummary . find_by ( target : topic , summary_type : AiSummary . summary_types [ :complete ] )
@@ -65,7 +67,9 @@ def cached_summary
65
67
# once it is cached with_prepared_responses will not work as expected
66
68
# since it is glued to the old llm instance
67
69
# so we create the cached summary totally independantly
68
- DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ cached_text ] ) do
70
+ DiscourseAi ::Completions ::Llm . with_prepared_responses (
71
+ [ "{\" summary\" : \" #{ cached_text } \" }" ] ,
72
+ ) do
69
73
strategy = DiscourseAi ::Summarization . topic_summary ( topic )
70
74
described_class . new ( strategy , user ) . summarize
71
75
end
@@ -86,10 +90,10 @@ def cached_summary
86
90
before { cached_summary . update! ( original_content_sha : "outdated_sha" ) }
87
91
88
92
it "returns a new summary" do
89
- DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ updated_summary ] ) do
93
+ DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ raw_summary ] ) do
90
94
section = summarization . summarize
91
95
92
- expect ( section . summarized_text ) . to eq ( updated_summary )
96
+ expect ( section . summarized_text ) . to eq ( clean_summary )
93
97
end
94
98
end
95
99
@@ -106,10 +110,10 @@ def cached_summary
106
110
end
107
111
108
112
it "returns a new summary if the skip_age_check flag is passed" do
109
- DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ updated_summary ] ) do
113
+ DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ raw_summary ] ) do
110
114
section = summarization . summarize ( skip_age_check : true )
111
115
112
- expect ( section . summarized_text ) . to eq ( updated_summary )
116
+ expect ( section . summarized_text ) . to eq ( clean_summary )
113
117
end
114
118
end
115
119
end
@@ -118,16 +122,15 @@ def cached_summary
118
122
end
119
123
120
124
describe "stream partial updates" do
121
- let ( :summary ) { "This is the final summary" }
122
-
123
125
it "receives a blk that is passed to the underlying strategy and called with partial summaries" do
124
126
partial_result = +""
125
127
126
- DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ summary ] ) do
128
+ DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ raw_summary ] ) do
127
129
summarization . summarize { |partial_summary | partial_result << partial_summary }
128
130
end
129
131
130
- expect ( partial_result ) . to eq ( summary )
132
+ # In a real world example, this is removed in the returned AiSummary obj.
133
+ expect ( partial_result . chomp ( "\" }" ) ) . to eq ( clean_summary )
131
134
end
132
135
end
133
136
end
0 commit comments