-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenAI and Anthropic tests & fix some types (#1168)
- Loading branch information
1 parent
8688f98
commit 500b1fd
Showing
6 changed files
with
175 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Message } from '../shared/types'; | ||
import { experimental_buildAnthropicPrompt } from './anthropic'; | ||
|
||
describe('experimental_buildAnthropicPrompt', () => { | ||
it('should correctly format messages', () => { | ||
const messages = [ | ||
{ content: 'Hello', role: 'user' }, | ||
{ content: 'Hi there', role: 'assistant' }, | ||
{ content: 'How are you?', role: 'user' }, | ||
{ content: 'I am fine, thank you.', role: 'assistant' }, | ||
] as Pick<Message, 'content' | 'role'>[]; | ||
|
||
const result = experimental_buildAnthropicPrompt(messages); | ||
|
||
const expected = | ||
'\n\nHuman: Hello,\n\nAssistant: Hi there,\n\nHuman: How are you?,\n\nAssistant: I am fine, thank you.\n\nAssistant:'; | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { Message } from '../shared/types'; | ||
import { | ||
experimental_buildOpenAIMessages, | ||
ChatCompletionMessageParam, | ||
} from './openai'; | ||
|
||
describe('experimental_buildOpenAIMessages', () => { | ||
it('should correctly map messages to ChatCompletionMessageParam', () => { | ||
const messages: Message[] = [ | ||
{ | ||
id: '1', | ||
role: 'system', | ||
content: 'System message', | ||
}, | ||
{ | ||
id: '2', | ||
role: 'user', | ||
content: 'User message', | ||
}, | ||
{ | ||
id: '3', | ||
role: 'assistant', | ||
content: 'Assistant message', | ||
function_call: { | ||
name: 'functionName', | ||
arguments: 'arg1, arg2', | ||
}, | ||
}, | ||
{ | ||
id: '4', | ||
role: 'function', | ||
content: 'Function message', | ||
name: 'functionName', | ||
}, | ||
{ | ||
id: '5', | ||
role: 'tool', | ||
content: 'Tool message', | ||
name: 'toolName', | ||
tool_call_id: 'toolCallId', | ||
}, | ||
]; | ||
|
||
const expected: ChatCompletionMessageParam[] = [ | ||
{ | ||
role: 'system', | ||
content: 'System message', | ||
}, | ||
{ | ||
role: 'user', | ||
content: 'User message', | ||
}, | ||
{ | ||
role: 'assistant', | ||
content: 'Assistant message', | ||
function_call: { | ||
name: 'functionName', | ||
arguments: 'arg1, arg2', | ||
}, | ||
}, | ||
{ | ||
role: 'function', | ||
content: 'Function message', | ||
name: 'functionName', | ||
}, | ||
{ | ||
role: 'tool', | ||
content: 'Tool message', | ||
tool_call_id: 'toolCallId', | ||
}, | ||
]; | ||
|
||
const result = experimental_buildOpenAIMessages(messages); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it('should throw an error for invalid function call in assistant message', () => { | ||
const messages: Message[] = [ | ||
{ | ||
id: '1', | ||
role: 'assistant', | ||
content: 'Assistant message', | ||
function_call: 'invalidFunctionCall', | ||
}, | ||
]; | ||
|
||
expect(() => experimental_buildOpenAIMessages(messages)).toThrowError( | ||
'Invalid function call in message. Expected a function call object', | ||
); | ||
}); | ||
|
||
it('should throw an error for invalid function call in function message', () => { | ||
const messages: Message[] = [ | ||
{ | ||
id: '1', | ||
role: 'function', | ||
content: 'Function message', | ||
}, | ||
]; | ||
|
||
expect(() => experimental_buildOpenAIMessages(messages)).toThrowError( | ||
'Invalid function call in message. Expected a name', | ||
); | ||
}); | ||
|
||
it('should throw an error for unsupported message role', () => { | ||
const messages: Message[] = [ | ||
{ | ||
id: '1', | ||
role: 'data', | ||
content: 'Data message', | ||
}, | ||
]; | ||
|
||
expect(() => experimental_buildOpenAIMessages(messages)).toThrowError( | ||
"unsupported message role 'data'", | ||
); | ||
}); | ||
|
||
it('should throw an error for invalid tool message', () => { | ||
const messages: Message[] = [ | ||
{ | ||
id: '1', | ||
role: 'tool', | ||
content: 'Tool message', | ||
name: undefined, | ||
}, | ||
]; | ||
|
||
expect(() => experimental_buildOpenAIMessages(messages)).toThrowError( | ||
'Invalid tool message. Expected a name', | ||
); | ||
}); | ||
|
||
it('should throw an error for invalid tool message', () => { | ||
const messages: Message[] = [ | ||
{ | ||
id: '1', | ||
role: 'tool', | ||
content: 'Tool message', | ||
name: 'toolName', | ||
tool_call_id: undefined, | ||
}, | ||
]; | ||
|
||
expect(() => experimental_buildOpenAIMessages(messages)).toThrowError( | ||
'Invalid tool message. Expected a tool_call_id', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters