|
| 1 | +import {describe, expect, test} from 'vitest' |
| 2 | + |
| 3 | +import {createObjectPrompt, createTextPrompt} from './prompt' |
| 4 | + |
| 5 | +describe('#createTextPrompt', () => { |
| 6 | + test('can create a text prompt', () => { |
| 7 | + const prompt = createTextPrompt(mockTextPrompt) |
| 8 | + expect( |
| 9 | + prompt({ |
| 10 | + name: 'mona', |
| 11 | + }), |
| 12 | + ).toMatchObject({ |
| 13 | + model: expect.anything(), |
| 14 | + messages: [ |
| 15 | + {role: 'system', content: 'You are a helpful assistant.'}, |
| 16 | + {role: 'user', content: 'Hello mona!'}, |
| 17 | + ], |
| 18 | + temperature: 0.7, |
| 19 | + }) |
| 20 | + }) |
| 21 | + |
| 22 | + test('fails because responseFormat is not text', () => { |
| 23 | + expect(() => createTextPrompt(mockObjectPrompt)).toThrowError(/responseFormat must be "text"/) |
| 24 | + }) |
| 25 | +}) |
| 26 | + |
| 27 | +describe('createObjectPrompt', () => { |
| 28 | + test('can create a object prompt', () => { |
| 29 | + const prompt = createObjectPrompt(mockObjectPrompt) |
| 30 | + expect( |
| 31 | + prompt({ |
| 32 | + name: 'mona', |
| 33 | + }), |
| 34 | + ).toMatchObject({ |
| 35 | + model: expect.anything(), |
| 36 | + messages: [ |
| 37 | + {role: 'system', content: 'You are a helpful assistant.'}, |
| 38 | + {role: 'user', content: 'Hello mona!'}, |
| 39 | + ], |
| 40 | + temperature: 0.7, |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + test('fails because responseFormat is not json like', () => { |
| 45 | + expect(() => createObjectPrompt(mockTextPrompt)).toThrowError(/responseFormat must be either "json_object"/) |
| 46 | + }) |
| 47 | + |
| 48 | + test('allows json_object without a schema', () => { |
| 49 | + const prompt = createObjectPrompt(mockJsonSchemaPrompt) |
| 50 | + expect( |
| 51 | + prompt({ |
| 52 | + name: 'mona', |
| 53 | + }), |
| 54 | + ).toMatchObject({ |
| 55 | + model: expect.anything(), |
| 56 | + messages: [ |
| 57 | + {role: 'system', content: 'You are a helpful assistant.'}, |
| 58 | + {role: 'user', content: 'Hello mona!'}, |
| 59 | + ], |
| 60 | + temperature: 0.7, |
| 61 | + schema: expect.any(Object), |
| 62 | + schemaName: 'schema_name', |
| 63 | + }) |
| 64 | + }) |
| 65 | +}) |
| 66 | + |
| 67 | +const mockTextPrompt = { |
| 68 | + model: 'abc-123', |
| 69 | + messages: [ |
| 70 | + {role: 'system', content: 'You are a helpful assistant.'}, |
| 71 | + {role: 'user', content: 'Hello {{name}}!'}, |
| 72 | + ], |
| 73 | + modelParameters: { |
| 74 | + temperature: 0.7, |
| 75 | + }, |
| 76 | +} |
| 77 | + |
| 78 | +const mockObjectPrompt = { |
| 79 | + model: 'abc-123', |
| 80 | + messages: [ |
| 81 | + {role: 'system', content: 'You are a helpful assistant.'}, |
| 82 | + {role: 'user', content: 'Hello {{name}}!'}, |
| 83 | + ], |
| 84 | + modelParameters: { |
| 85 | + temperature: 0.7, |
| 86 | + }, |
| 87 | + responseFormat: 'json_object', |
| 88 | +} |
| 89 | + |
| 90 | +const mockJsonSchemaPrompt = { |
| 91 | + model: 'abc-123', |
| 92 | + messages: [ |
| 93 | + {role: 'system', content: 'You are a helpful assistant.'}, |
| 94 | + {role: 'user', content: 'Hello {{name}}!'}, |
| 95 | + ], |
| 96 | + modelParameters: { |
| 97 | + temperature: 0.7, |
| 98 | + }, |
| 99 | + responseFormat: 'json_schema', |
| 100 | + jsonSchema: JSON.stringify({ |
| 101 | + name: 'schema_name', |
| 102 | + schema: {}, |
| 103 | + }), |
| 104 | +} |
0 commit comments