@@ -11,8 +11,6 @@ const helper = require('../../../lib/agent_helper')
11
11
const { req, chatRes, getExpectedResult } = require ( './common' )
12
12
13
13
tap . test ( 'LlmChatCompletionMessage' , ( t ) => {
14
- t . autoend ( )
15
-
16
14
let agent
17
15
t . beforeEach ( ( ) => {
18
16
agent = helper . loadMockedAgent ( )
@@ -104,4 +102,115 @@ tap.test('LlmChatCompletionMessage', (t) => {
104
102
t . end ( )
105
103
} )
106
104
} )
105
+
106
+ t . test ( 'should use token_count from tokenCountCallback for prompt message' , ( t ) => {
107
+ const api = helper . getAgentApi ( )
108
+ const expectedCount = 4
109
+ function cb ( model , content ) {
110
+ t . equal ( model , 'gpt-3.5-turbo-0613' )
111
+ t . equal ( content , 'What is a woodchuck?' )
112
+ return expectedCount
113
+ }
114
+ api . setLlmTokenCountCallback ( cb )
115
+ helper . runInTransaction ( agent , ( ) => {
116
+ api . startSegment ( 'fakeSegment' , false , ( ) => {
117
+ const segment = api . shim . getActiveSegment ( )
118
+ const summaryId = 'chat-summary-id'
119
+ delete chatRes . usage
120
+ const chatMessageEvent = new LlmChatCompletionMessage ( {
121
+ agent,
122
+ segment,
123
+ request : req ,
124
+ response : chatRes ,
125
+ completionId : summaryId ,
126
+ message : req . messages [ 0 ] ,
127
+ index : 0
128
+ } )
129
+ t . equal ( chatMessageEvent . token_count , expectedCount )
130
+ t . end ( )
131
+ } )
132
+ } )
133
+ } )
134
+
135
+ t . test ( 'should use token_count from tokenCountCallback for completion messages' , ( t ) => {
136
+ const api = helper . getAgentApi ( )
137
+ const expectedCount = 4
138
+ function cb ( model , content ) {
139
+ t . equal ( model , 'gpt-3.5-turbo-0613' )
140
+ t . equal ( content , 'a lot' )
141
+ return expectedCount
142
+ }
143
+ api . setLlmTokenCountCallback ( cb )
144
+ helper . runInTransaction ( agent , ( ) => {
145
+ api . startSegment ( 'fakeSegment' , false , ( ) => {
146
+ const segment = api . shim . getActiveSegment ( )
147
+ const summaryId = 'chat-summary-id'
148
+ delete chatRes . usage
149
+ const chatMessageEvent = new LlmChatCompletionMessage ( {
150
+ agent,
151
+ segment,
152
+ request : req ,
153
+ response : chatRes ,
154
+ completionId : summaryId ,
155
+ message : chatRes . choices [ 0 ] . message ,
156
+ index : 2
157
+ } )
158
+ t . equal ( chatMessageEvent . token_count , expectedCount )
159
+ t . end ( )
160
+ } )
161
+ } )
162
+ } )
163
+
164
+ t . test ( 'should not set token_count if not set in usage nor a callback registered' , ( t ) => {
165
+ const api = helper . getAgentApi ( )
166
+ helper . runInTransaction ( agent , ( ) => {
167
+ api . startSegment ( 'fakeSegment' , false , ( ) => {
168
+ const segment = api . shim . getActiveSegment ( )
169
+ const summaryId = 'chat-summary-id'
170
+ delete chatRes . usage
171
+ const chatMessageEvent = new LlmChatCompletionMessage ( {
172
+ agent,
173
+ segment,
174
+ request : req ,
175
+ response : chatRes ,
176
+ completionId : summaryId ,
177
+ message : chatRes . choices [ 0 ] . message ,
178
+ index : 2
179
+ } )
180
+ t . equal ( chatMessageEvent . token_count , undefined )
181
+ t . end ( )
182
+ } )
183
+ } )
184
+ } )
185
+
186
+ t . test (
187
+ 'should not set token_count if not set in usage nor a callback registered returns count' ,
188
+ ( t ) => {
189
+ const api = helper . getAgentApi ( )
190
+ function cb ( ) {
191
+ // empty cb
192
+ }
193
+ api . setLlmTokenCountCallback ( cb )
194
+ helper . runInTransaction ( agent , ( ) => {
195
+ api . startSegment ( 'fakeSegment' , false , ( ) => {
196
+ const segment = api . shim . getActiveSegment ( )
197
+ const summaryId = 'chat-summary-id'
198
+ delete chatRes . usage
199
+ const chatMessageEvent = new LlmChatCompletionMessage ( {
200
+ agent,
201
+ segment,
202
+ request : req ,
203
+ response : chatRes ,
204
+ completionId : summaryId ,
205
+ message : chatRes . choices [ 0 ] . message ,
206
+ index : 2
207
+ } )
208
+ t . equal ( chatMessageEvent . token_count , undefined )
209
+ t . end ( )
210
+ } )
211
+ } )
212
+ }
213
+ )
214
+
215
+ t . end ( )
107
216
} )
0 commit comments