|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- import { describe, it, expect } from 'vitest';
- import * as Chat from '../../../src/platforms/openai/chat';
- import { MessageRole } from '../../../src/platforms/openai';
-
- describe('OpenAI', () => {
- describe.skip('chat', () => {
- describe('normalizeChatMessage', () => {
- it('normalizes a basic string', () => {
- const message = Chat.normalizeChatMessage('This is a user message.');
-
- expect(message).toHaveLength(1);
-
- expect(message).toContainEqual({
- role: 'user',
- content: 'This is a user message.',
- });
- });
-
- it('normalizes a string array', () => {
- const message = Chat.normalizeChatMessage([
- 'This is a user message.',
- 'This is another user message.',
- ]);
-
- expect(message).toHaveLength(2);
-
- expect(message).toContainEqual({
- role: 'user',
- content: 'This is a user message.',
- });
-
- expect(message).toContainEqual({
- role: 'user',
- content: 'This is another user message.',
- });
- });
-
- it('normalizes a message object', () => {
- const message = Chat.normalizeChatMessage({
- role: MessageRole.USER,
- content: 'This is a user message.',
- });
-
- expect(message).toHaveLength(1);
-
- expect(message).toContainEqual({
- role: 'user',
- content: 'This is a user message.',
- });
- });
-
- it('normalizes a message object array', () => {
- const message = Chat.normalizeChatMessage([
- {
- role: MessageRole.USER,
- content: 'This is a user message.',
- },
- {
- role: MessageRole.USER,
- content: 'This is another user message.',
- },
- ]);
-
- expect(message).toHaveLength(2);
-
- expect(message).toContainEqual({
- role: 'user',
- content: 'This is a user message.',
- });
-
- expect(message).toContainEqual({
- role: 'user',
- content: 'This is another user message.',
- });
- });
- });
-
- describe('buildChatFromTranscript', () => {
- it('processes line breaks correctly', () => {
- const message = `
- SYSTEM: This is a system message. This is a chat from the
- user: This is a user message.
- `;
- const parameterized = Chat.buildChatFromTranscript(message);
- const prompts = parameterized({});
-
- expect(prompts[0]).toHaveLength(1);
-
- expect(prompts[0]).toContainEqual({
- role: 'system',
- content: 'This is a system message. This is a chat from the user: This is a user message.',
- });
- });
-
- it('makes distinctions between different dialogues', () => {
- const message = `
- SYSTEM: This is a system message. This is a chat from the
-
- user: This is a user message.
- `;
- const parameterized = Chat.buildChatFromTranscript(message);
- const prompts = parameterized({});
-
- expect(prompts[0]).toHaveLength(2);
-
- expect(prompts[0]).toContainEqual({
- role: 'system',
- content: 'This is a system message. This is a chat from the',
- });
-
- expect(prompts[0]).toContainEqual({
- role: 'user',
- content: 'This is a user message.',
- });
- });
-
- it('builds an array of chat messages from a single string.', () => {
- const message = `
- SYSTEM: This is a system message.
-
- USER: This is a user message.
-
- SYSTEM: This is another system message.
-
- USER: This is another user message.
-
- ASSISTANT: This is an assistant message.
- `;
- const parameterized = Chat.buildChatFromTranscript(message);
- const prompts = parameterized({});
-
- expect(prompts[0]).toHaveLength(5);
-
- expect(prompts[0]).toContainEqual({
- role: 'system',
- content: 'This is a system message.',
- });
-
- expect(prompts[0]).toContainEqual({
- role: 'user',
- content: 'This is a user message.',
- });
-
- expect(prompts[0]).toContainEqual({
- role: 'system',
- content: 'This is another system message.',
- });
-
- expect(prompts[0]).toContainEqual({
- role: 'user',
- content: 'This is another user message.',
- });
-
- expect(prompts[0]).toContainEqual({
- role: 'assistant',
- content: 'This is an assistant message.',
- });
- });
-
- it('builds multiple chat messages with a divider.', () => {
- const message = `
- SYSTEM: This is a system message.
- ---
- USER: This is a user message.
- `;
- const parameterized = Chat.buildChatFromTranscript(message);
- const prompts = parameterized({});
-
- expect(prompts).toHaveLength(2);
- expect(prompts[0]).toContainEqual({
- role: 'system',
- content: 'This is a system message.',
- });
- expect(prompts[1]).toContainEqual({
- role: 'user',
- content: 'This is a user message.',
- });
- });
-
- it('injects parameters into the chat messages.', () => {
- const message = `
- Say {{name}}. <name> <age> <foo> {{htmlChar}} \\{{escaped}}
- `;
-
- const parameterized = Chat.buildChatFromTranscript(message);
- const prompts = parameterized({ name: 'Hello', htmlChar: '<html>' });
- expect(prompts[0][0]).toEqual({
- role: 'user',
- content: 'Say Hello. <name> <age> <foo> <html> {{escaped}}',
- });
- });
- });
- });
- });
|