|
| 1 | +import {describe, expect, test} from "vitest"; |
| 2 | +import {generateContextTextFromConversationHistory} from "../../../src/chatWrappers/generateContextTextFromConversationHistory.js"; |
| 3 | +import {ConversationInteraction, FalconChatPromptWrapper} from "../../../src/index.js"; |
| 4 | + |
| 5 | + |
| 6 | +describe("FalconChatPromptWrapper", () => { |
| 7 | + const conversationHistory: ConversationInteraction[] = [{ |
| 8 | + prompt: "Hi there!", |
| 9 | + response: "Hello!" |
| 10 | + }]; |
| 11 | + const conversationHistory2: ConversationInteraction[] = [{ |
| 12 | + prompt: "Hi there!", |
| 13 | + response: "Hello!" |
| 14 | + }, { |
| 15 | + prompt: "How are you?", |
| 16 | + response: "I'm good, how are you?" |
| 17 | + }]; |
| 18 | + |
| 19 | + test("should generate valid output for default roles", () => { |
| 20 | + const chatWrapper = new FalconChatPromptWrapper(); |
| 21 | + const {text: response} = generateContextTextFromConversationHistory(chatWrapper, conversationHistory); |
| 22 | + |
| 23 | + expect(response).toMatchInlineSnapshot(` |
| 24 | + "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible. |
| 25 | + If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. |
| 26 | + User: Hi there! |
| 27 | + Assistant: Hello! |
| 28 | + User: " |
| 29 | + `); |
| 30 | + |
| 31 | + const chatWrapper2 = new FalconChatPromptWrapper(); |
| 32 | + const {text: response2} = generateContextTextFromConversationHistory(chatWrapper2, conversationHistory2); |
| 33 | + |
| 34 | + expect(response2).toMatchInlineSnapshot(` |
| 35 | + "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible. |
| 36 | + If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. |
| 37 | + User: Hi there! |
| 38 | + Assistant: Hello! |
| 39 | + User: How are you? |
| 40 | + Assistant: I'm good, how are you? |
| 41 | + User: " |
| 42 | + `); |
| 43 | + |
| 44 | + const chatWrapper3 = new FalconChatPromptWrapper(); |
| 45 | + const {text: response3, stopStringSuffix, stopString} = generateContextTextFromConversationHistory(chatWrapper3, conversationHistory); |
| 46 | + |
| 47 | + const newPrompt = conversationHistory2[1].prompt; |
| 48 | + const wrappedNewPrompt = chatWrapper3.wrapPrompt(newPrompt, { |
| 49 | + systemPrompt: response3, |
| 50 | + promptIndex: 1, |
| 51 | + lastStopString: stopString, |
| 52 | + lastStopStringSuffix: stopStringSuffix |
| 53 | + }); |
| 54 | + |
| 55 | + expect(response3).toMatchInlineSnapshot(` |
| 56 | + "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible. |
| 57 | + If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. |
| 58 | + User: Hi there! |
| 59 | + Assistant: Hello! |
| 60 | + User: " |
| 61 | + `); |
| 62 | + |
| 63 | + expect(wrappedNewPrompt).toMatchInlineSnapshot(` |
| 64 | + "How are you? |
| 65 | + Assistant: " |
| 66 | + `); |
| 67 | + |
| 68 | + expect(response3 + wrappedNewPrompt).toMatchInlineSnapshot(` |
| 69 | + "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible. |
| 70 | + If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. |
| 71 | + User: Hi there! |
| 72 | + Assistant: Hello! |
| 73 | + User: How are you? |
| 74 | + Assistant: " |
| 75 | + `); |
| 76 | + }); |
| 77 | + |
| 78 | + test("should generate valid output for custom roles", () => { |
| 79 | + const chatWrapper = new FalconChatPromptWrapper({ |
| 80 | + instructionName: "Instruction", |
| 81 | + responseName: "Response" |
| 82 | + }); |
| 83 | + const {text: response} = generateContextTextFromConversationHistory(chatWrapper, conversationHistory); |
| 84 | + |
| 85 | + expect(response).toMatchInlineSnapshot(` |
| 86 | + "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible. |
| 87 | + If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. |
| 88 | + Instruction: Hi there! |
| 89 | + Response: Hello! |
| 90 | + Instruction: " |
| 91 | + `); |
| 92 | + |
| 93 | + const chatWrapper2 = new FalconChatPromptWrapper({ |
| 94 | + instructionName: "Instruction", |
| 95 | + responseName: "Response" |
| 96 | + }); |
| 97 | + const {text: response2} = generateContextTextFromConversationHistory(chatWrapper2, conversationHistory2); |
| 98 | + |
| 99 | + expect(response2).toMatchInlineSnapshot(` |
| 100 | + "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible. |
| 101 | + If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. |
| 102 | + Instruction: Hi there! |
| 103 | + Response: Hello! |
| 104 | + Instruction: How are you? |
| 105 | + Response: I'm good, how are you? |
| 106 | + Instruction: " |
| 107 | + `); |
| 108 | + }); |
| 109 | + |
| 110 | + test("should generate valid output for custom system prompt", () => { |
| 111 | + const chatWrapper = new FalconChatPromptWrapper(); |
| 112 | + const {text: response} = generateContextTextFromConversationHistory(chatWrapper, conversationHistory, { |
| 113 | + systemPrompt: "Below is an instruction the describes a task, Write a response the appropriately completes the request." |
| 114 | + }); |
| 115 | + |
| 116 | + expect(response).toMatchInlineSnapshot(` |
| 117 | + "Below is an instruction the describes a task, Write a response the appropriately completes the request. |
| 118 | + User: Hi there! |
| 119 | + Assistant: Hello! |
| 120 | + User: " |
| 121 | + `); |
| 122 | + |
| 123 | + const chatWrapper2 = new FalconChatPromptWrapper({ |
| 124 | + instructionName: "Instruction", |
| 125 | + responseName: "Response" |
| 126 | + }); |
| 127 | + const {text: response2} = generateContextTextFromConversationHistory(chatWrapper2, conversationHistory2, { |
| 128 | + systemPrompt: "Below is an instruction the describes a task, Write a response the appropriately completes the request." |
| 129 | + }); |
| 130 | + |
| 131 | + expect(response2).toMatchInlineSnapshot(` |
| 132 | + "Below is an instruction the describes a task, Write a response the appropriately completes the request. |
| 133 | + Instruction: Hi there! |
| 134 | + Response: Hello! |
| 135 | + Instruction: How are you? |
| 136 | + Response: I'm good, how are you? |
| 137 | + Instruction: " |
| 138 | + `); |
| 139 | + }); |
| 140 | +}); |
0 commit comments