Many-in-one AI client.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chat.test.ts 5.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { describe, it, expect } from 'vitest';
  2. import * as Chat from '../../../src/platforms/openai/chat';
  3. import { MessageRole } from '../../../src/platforms/openai';
  4. describe('OpenAI', () => {
  5. describe.skip('chat', () => {
  6. describe('normalizeChatMessage', () => {
  7. it('normalizes a basic string', () => {
  8. const message = Chat.normalizeChatMessage('This is a user message.');
  9. expect(message).toHaveLength(1);
  10. expect(message).toContainEqual({
  11. role: 'user',
  12. content: 'This is a user message.',
  13. });
  14. });
  15. it('normalizes a string array', () => {
  16. const message = Chat.normalizeChatMessage([
  17. 'This is a user message.',
  18. 'This is another user message.',
  19. ]);
  20. expect(message).toHaveLength(2);
  21. expect(message).toContainEqual({
  22. role: 'user',
  23. content: 'This is a user message.',
  24. });
  25. expect(message).toContainEqual({
  26. role: 'user',
  27. content: 'This is another user message.',
  28. });
  29. });
  30. it('normalizes a message object', () => {
  31. const message = Chat.normalizeChatMessage({
  32. role: MessageRole.USER,
  33. content: 'This is a user message.',
  34. });
  35. expect(message).toHaveLength(1);
  36. expect(message).toContainEqual({
  37. role: 'user',
  38. content: 'This is a user message.',
  39. });
  40. });
  41. it('normalizes a message object array', () => {
  42. const message = Chat.normalizeChatMessage([
  43. {
  44. role: MessageRole.USER,
  45. content: 'This is a user message.',
  46. },
  47. {
  48. role: MessageRole.USER,
  49. content: 'This is another user message.',
  50. },
  51. ]);
  52. expect(message).toHaveLength(2);
  53. expect(message).toContainEqual({
  54. role: 'user',
  55. content: 'This is a user message.',
  56. });
  57. expect(message).toContainEqual({
  58. role: 'user',
  59. content: 'This is another user message.',
  60. });
  61. });
  62. });
  63. describe('buildChatFromTranscript', () => {
  64. it('processes line breaks correctly', () => {
  65. const message = `
  66. SYSTEM: This is a system message. This is a chat from the
  67. user: This is a user message.
  68. `;
  69. const parameterized = Chat.buildChatFromTranscript(message);
  70. const prompts = parameterized({});
  71. expect(prompts[0]).toHaveLength(1);
  72. expect(prompts[0]).toContainEqual({
  73. role: 'system',
  74. content: 'This is a system message. This is a chat from the user: This is a user message.',
  75. });
  76. });
  77. it('makes distinctions between different dialogues', () => {
  78. const message = `
  79. SYSTEM: This is a system message. This is a chat from the
  80. user: This is a user message.
  81. `;
  82. const parameterized = Chat.buildChatFromTranscript(message);
  83. const prompts = parameterized({});
  84. expect(prompts[0]).toHaveLength(2);
  85. expect(prompts[0]).toContainEqual({
  86. role: 'system',
  87. content: 'This is a system message. This is a chat from the',
  88. });
  89. expect(prompts[0]).toContainEqual({
  90. role: 'user',
  91. content: 'This is a user message.',
  92. });
  93. });
  94. it('builds an array of chat messages from a single string.', () => {
  95. const message = `
  96. SYSTEM: This is a system message.
  97. USER: This is a user message.
  98. SYSTEM: This is another system message.
  99. USER: This is another user message.
  100. ASSISTANT: This is an assistant message.
  101. `;
  102. const parameterized = Chat.buildChatFromTranscript(message);
  103. const prompts = parameterized({});
  104. expect(prompts[0]).toHaveLength(5);
  105. expect(prompts[0]).toContainEqual({
  106. role: 'system',
  107. content: 'This is a system message.',
  108. });
  109. expect(prompts[0]).toContainEqual({
  110. role: 'user',
  111. content: 'This is a user message.',
  112. });
  113. expect(prompts[0]).toContainEqual({
  114. role: 'system',
  115. content: 'This is another system message.',
  116. });
  117. expect(prompts[0]).toContainEqual({
  118. role: 'user',
  119. content: 'This is another user message.',
  120. });
  121. expect(prompts[0]).toContainEqual({
  122. role: 'assistant',
  123. content: 'This is an assistant message.',
  124. });
  125. });
  126. it('builds multiple chat messages with a divider.', () => {
  127. const message = `
  128. SYSTEM: This is a system message.
  129. ---
  130. USER: This is a user message.
  131. `;
  132. const parameterized = Chat.buildChatFromTranscript(message);
  133. const prompts = parameterized({});
  134. expect(prompts).toHaveLength(2);
  135. expect(prompts[0]).toContainEqual({
  136. role: 'system',
  137. content: 'This is a system message.',
  138. });
  139. expect(prompts[1]).toContainEqual({
  140. role: 'user',
  141. content: 'This is a user message.',
  142. });
  143. });
  144. it('injects parameters into the chat messages.', () => {
  145. const message = `
  146. Say {{name}}. <name> <age> <foo> {{htmlChar}} \\{{escaped}}
  147. `;
  148. const parameterized = Chat.buildChatFromTranscript(message);
  149. const prompts = parameterized({ name: 'Hello', htmlChar: '<html>' });
  150. expect(prompts[0][0]).toEqual({
  151. role: 'user',
  152. content: 'Say Hello. <name> <age> <foo> <html> {{escaped}}',
  153. });
  154. });
  155. });
  156. });
  157. });