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