|
| 1 | +import { createPromptConfigurationFixture } from '../../../test/__fixtures__/promptConfiguration' |
| 2 | +import { createPromptMessageFixture } from '../../../test/__fixtures__/promptMessage' |
| 3 | +import { createPromptToolFixture } from '../../../test/__fixtures__/promptTool' |
| 4 | +import { PromptMessageRoleEnum, PromptTool } from '../../types' |
| 5 | +import { mapMessagesToOpenAI, mapPromptToOpenAIConfig, mapToolChoiceToOpenAI } from '../openAi' |
| 6 | + |
| 7 | +describe('openAi helpers', () => { |
| 8 | + describe('mapMessagesToOpenAI', () => { |
| 9 | + const mockMessages = [ |
| 10 | + createPromptMessageFixture({ role: PromptMessageRoleEnum.TOOL, content: 'content1', toolCallId: 'id1' }), |
| 11 | + createPromptMessageFixture({ role: PromptMessageRoleEnum.ASSISTANT, content: 'content2' }), |
| 12 | + createPromptMessageFixture({ role: PromptMessageRoleEnum.USER, content: 'content3' }), |
| 13 | + createPromptMessageFixture({ role: PromptMessageRoleEnum.SYSTEM, content: 'content4' }) |
| 14 | + ] |
| 15 | + |
| 16 | + it('should correctly map tool messages', () => { |
| 17 | + const result = mapMessagesToOpenAI([mockMessages[0]]) |
| 18 | + expect(result[0]).toEqual({ |
| 19 | + role: 'tool', |
| 20 | + content: 'content1', |
| 21 | + tool_call_id: 'id1' |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + it('should correctly map assistant messages', () => { |
| 26 | + const result = mapMessagesToOpenAI([mockMessages[1]]) |
| 27 | + expect(result[0]).toEqual({ |
| 28 | + role: 'assistant', |
| 29 | + content: 'content2', |
| 30 | + name: undefined, |
| 31 | + tool_calls: undefined |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + it('should correctly map user messages', () => { |
| 36 | + const result = mapMessagesToOpenAI([mockMessages[2]]) |
| 37 | + expect(result[0]).toEqual({ |
| 38 | + role: 'user', |
| 39 | + name: undefined, |
| 40 | + content: 'content3' |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should correctly map system messages', () => { |
| 45 | + const result = mapMessagesToOpenAI([mockMessages[3]]) |
| 46 | + expect(result[0]).toEqual({ |
| 47 | + role: 'system', |
| 48 | + content: 'content4' |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should throw an error for invalid message roles', () => { |
| 53 | + expect(() => mapMessagesToOpenAI([createPromptMessageFixture({ role: 'invalid', content: 'content5' } as any)])).toThrow( |
| 54 | + 'Invalid message role: invalid' |
| 55 | + ) |
| 56 | + }) |
| 57 | + |
| 58 | + // Additional test cases for missing fields and other edge cases can be added here. |
| 59 | + }) |
| 60 | + |
| 61 | + describe('mapPromptToOpenAIConfig', () => { |
| 62 | + const mockPromptConfig = createPromptConfigurationFixture() |
| 63 | + |
| 64 | + it('should map the configuration to OpenAI parameters correctly', () => { |
| 65 | + const result = mapPromptToOpenAIConfig(mockPromptConfig) |
| 66 | + expect(result).toEqual({ |
| 67 | + messages: [{ role: 'user', name: undefined, content: 'Hello world' }], |
| 68 | + model: 'text-davinci-002', |
| 69 | + top_p: 0.5, |
| 70 | + max_tokens: 150, |
| 71 | + temperature: 0.7, |
| 72 | + seed: 42, |
| 73 | + presence_penalty: 0.1, |
| 74 | + frequency_penalty: 0.1, |
| 75 | + tool_choice: 'none', |
| 76 | + response_format: { |
| 77 | + type: 'json_object' |
| 78 | + }, |
| 79 | + tools: undefined |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + // Additional test cases to test other configurations and scenarios can be added here. |
| 84 | + }) |
| 85 | + |
| 86 | + describe('mapToolChoiceToOpenAI', () => { |
| 87 | + it('should return "auto" if toolChoice is "auto" or there are tools available and toolChoice is undefined', () => { |
| 88 | + const tools = [createPromptToolFixture()] |
| 89 | + expect(mapToolChoiceToOpenAI(tools, 'auto')).toBe('auto') |
| 90 | + expect(mapToolChoiceToOpenAI(tools)).toBe('auto') |
| 91 | + }) |
| 92 | + |
| 93 | + it('should return "none" if there are no tools or toolChoice is "none"', () => { |
| 94 | + const tools: PromptTool[] = [] |
| 95 | + expect(mapToolChoiceToOpenAI(tools, 'none')).toBe('none') |
| 96 | + expect(mapToolChoiceToOpenAI(tools)).toBe('none') |
| 97 | + expect(mapToolChoiceToOpenAI(tools, 'auto')).toBe('none') |
| 98 | + }) |
| 99 | + |
| 100 | + it('should return tool function object if a valid toolChoice matches a tool', () => { |
| 101 | + const tools = [createPromptToolFixture({ name: 'exampleTool' })] |
| 102 | + expect(mapToolChoiceToOpenAI(tools, 'exampleTool')).toEqual({ |
| 103 | + type: 'function', |
| 104 | + function: { name: 'exampleTool' } |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + it('should return "none" if the toolChoice does not match any tool', () => { |
| 109 | + const tools = [createPromptToolFixture({ name: 'exampleTool' })] |
| 110 | + expect(mapToolChoiceToOpenAI(tools, 'nonexistentTool')).toBe('none') |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments