1
- import { describe , it , expect } from 'vitest'
2
- import {
3
- buildMessages ,
4
- buildResponseFormat ,
5
- buildInferenceRequest
6
- } from '../src/helpers'
7
- import { PromptConfig } from '../src/prompt'
1
+ import { describe , it , expect } from 'vitest'
2
+ import { buildMessages , buildResponseFormat , buildInferenceRequest } from '../src/helpers'
3
+ import { PromptConfig } from '../src/prompt'
8
4
9
5
describe ( 'helpers.ts - inference request building' , ( ) => {
10
6
describe ( 'buildMessages' , ( ) => {
11
7
it ( 'should build messages from prompt config' , ( ) => {
12
8
const promptConfig : PromptConfig = {
13
9
messages : [
14
- { role : 'system' , content : 'System message' } ,
15
- { role : 'user' , content : 'User message' }
16
- ]
10
+ { role : 'system' , content : 'System message' } ,
11
+ { role : 'user' , content : 'User message' } ,
12
+ ] ,
17
13
}
18
14
19
15
const result = buildMessages ( promptConfig )
20
16
expect ( result ) . toEqual ( [
21
- { role : 'system' , content : 'System message' } ,
22
- { role : 'user' , content : 'User message' }
17
+ { role : 'system' , content : 'System message' } ,
18
+ { role : 'user' , content : 'User message' } ,
23
19
] )
24
20
} )
25
21
26
22
it ( 'should build messages from legacy format' , ( ) => {
27
23
const result = buildMessages ( undefined , 'System prompt' , 'User prompt' )
28
24
expect ( result ) . toEqual ( [
29
- { role : 'system' , content : 'System prompt' } ,
30
- { role : 'user' , content : 'User prompt' }
25
+ { role : 'system' , content : 'System prompt' } ,
26
+ { role : 'user' , content : 'User prompt' } ,
31
27
] )
32
28
} )
33
29
34
30
it ( 'should use default system prompt when none provided' , ( ) => {
35
31
const result = buildMessages ( undefined , undefined , 'User prompt' )
36
32
expect ( result ) . toEqual ( [
37
- { role : 'system' , content : 'You are a helpful assistant' } ,
38
- { role : 'user' , content : 'User prompt' }
33
+ { role : 'system' , content : 'You are a helpful assistant' } ,
34
+ { role : 'user' , content : 'User prompt' } ,
39
35
] )
40
36
} )
41
37
} )
@@ -47,24 +43,24 @@ describe('helpers.ts - inference request building', () => {
47
43
responseFormat : 'json_schema' ,
48
44
jsonSchema : JSON . stringify ( {
49
45
name : 'test_schema' ,
50
- schema : { type : 'object' }
51
- } )
46
+ schema : { type : 'object' } ,
47
+ } ) ,
52
48
}
53
49
54
50
const result = buildResponseFormat ( promptConfig )
55
51
expect ( result ) . toEqual ( {
56
52
type : 'json_schema' ,
57
53
json_schema : {
58
54
name : 'test_schema' ,
59
- schema : { type : 'object' }
60
- }
55
+ schema : { type : 'object' } ,
56
+ } ,
61
57
} )
62
58
} )
63
59
64
60
it ( 'should return undefined for text format' , ( ) => {
65
61
const promptConfig : PromptConfig = {
66
62
messages : [ ] ,
67
- responseFormat : 'text'
63
+ responseFormat : 'text' ,
68
64
}
69
65
70
66
const result = buildResponseFormat ( promptConfig )
@@ -73,7 +69,7 @@ describe('helpers.ts - inference request building', () => {
73
69
74
70
it ( 'should return undefined when no response format specified' , ( ) => {
75
71
const promptConfig : PromptConfig = {
76
- messages : [ ]
72
+ messages : [ ] ,
77
73
}
78
74
79
75
const result = buildResponseFormat ( promptConfig )
@@ -84,27 +80,25 @@ describe('helpers.ts - inference request building', () => {
84
80
const promptConfig : PromptConfig = {
85
81
messages : [ ] ,
86
82
responseFormat : 'json_schema' ,
87
- jsonSchema : 'invalid json'
83
+ jsonSchema : 'invalid json' ,
88
84
}
89
85
90
- expect ( ( ) => buildResponseFormat ( promptConfig ) ) . toThrow (
91
- 'Invalid JSON schema'
92
- )
86
+ expect ( ( ) => buildResponseFormat ( promptConfig ) ) . toThrow ( 'Invalid JSON schema' )
93
87
} )
94
88
} )
95
89
96
90
describe ( 'buildInferenceRequest' , ( ) => {
97
91
it ( 'should build complete inference request from prompt config' , ( ) => {
98
92
const promptConfig : PromptConfig = {
99
93
messages : [
100
- { role : 'system' , content : 'System message' } ,
101
- { role : 'user' , content : 'User message' }
94
+ { role : 'system' , content : 'System message' } ,
95
+ { role : 'user' , content : 'User message' } ,
102
96
] ,
103
97
responseFormat : 'json_schema' ,
104
98
jsonSchema : JSON . stringify ( {
105
99
name : 'test_schema' ,
106
- schema : { type : 'object' }
107
- } )
100
+ schema : { type : 'object' } ,
101
+ } ) ,
108
102
}
109
103
110
104
const result = buildInferenceRequest (
@@ -114,13 +108,13 @@ describe('helpers.ts - inference request building', () => {
114
108
'gpt-4' ,
115
109
100 ,
116
110
'https://api.test.com' ,
117
- 'test-token'
111
+ 'test-token' ,
118
112
)
119
113
120
114
expect ( result ) . toEqual ( {
121
115
messages : [
122
- { role : 'system' , content : 'System message' } ,
123
- { role : 'user' , content : 'User message' }
116
+ { role : 'system' , content : 'System message' } ,
117
+ { role : 'user' , content : 'User message' } ,
124
118
] ,
125
119
modelName : 'gpt-4' ,
126
120
maxTokens : 100 ,
@@ -130,9 +124,9 @@ describe('helpers.ts - inference request building', () => {
130
124
type : 'json_schema' ,
131
125
json_schema : {
132
126
name : 'test_schema' ,
133
- schema : { type : 'object' }
134
- }
135
- }
127
+ schema : { type : 'object' } ,
128
+ } ,
129
+ } ,
136
130
} )
137
131
} )
138
132
@@ -144,19 +138,19 @@ describe('helpers.ts - inference request building', () => {
144
138
'gpt-4' ,
145
139
100 ,
146
140
'https://api.test.com' ,
147
- 'test-token'
141
+ 'test-token' ,
148
142
)
149
143
150
144
expect ( result ) . toEqual ( {
151
145
messages : [
152
- { role : 'system' , content : 'System prompt' } ,
153
- { role : 'user' , content : 'User prompt' }
146
+ { role : 'system' , content : 'System prompt' } ,
147
+ { role : 'user' , content : 'User prompt' } ,
154
148
] ,
155
149
modelName : 'gpt-4' ,
156
150
maxTokens : 100 ,
157
151
endpoint : 'https://api.test.com' ,
158
152
token : 'test-token' ,
159
- responseFormat : undefined
153
+ responseFormat : undefined ,
160
154
} )
161
155
} )
162
156
} )
0 commit comments