This repository was archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
chore(prompts): add test #46
Open
BLamy
wants to merge
1
commit into
cfortuner:main
Choose a base branch
from
BLamy:fix-prompt-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,48 @@ | ||
| import { NoopParser, JSONParser, CSVParser, ListParser } from "@prompts/Parser" | ||
|
|
||
| test("NoopParser", () => { | ||
| expect(new NoopParser().parse("test")).toEqual("test"); | ||
| }) | ||
|
|
||
| describe("JSONParser", () => { | ||
| test("should throw error for invalid json", () => { | ||
| const parser = new JSONParser(); | ||
| expect(() => parser.parse("")).toThrow(""); | ||
| }); | ||
|
|
||
| test("should parse valid json", () => { | ||
| const parser = new JSONParser(); | ||
| expect(parser.parse('{ "hello": "world" }')).toEqual({ hello: "world" }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("CSVParser", () => { | ||
| test("should throw error for invalid csv", () => { | ||
| const parser = new CSVParser(); | ||
| // @ts-expect-error | ||
| expect(() => parser.parse(1)).toThrow("") | ||
| }); | ||
|
|
||
| test("should parse valid csv", () => { | ||
| const parser = new CSVParser(); | ||
| expect(parser.parse("a,b,c\n1,2,3")).toEqual([{ | ||
| a: "1", | ||
| b: "2", | ||
| c: "3" | ||
| }]) | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| describe("ListParser", () => { | ||
| test("should throw error for invalid json", () => { | ||
| const parser = new ListParser(); | ||
| // @ts-expect-error | ||
| expect(() => parser.parse(1)).toThrow("") | ||
| }); | ||
|
|
||
| test("should parse valid json", () => { | ||
| const parser = new ListParser(); | ||
| expect(parser.parse("a, b, c")).toEqual(["a", "b", "c"]) | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,33 +1,37 @@ | ||
| import { Prompt } from "@prompts/Prompt"; | ||
| import { prompt } from "@prompts/Prompt"; | ||
|
|
||
| const PROMPT_TEXT = "this is a {{test}}"; | ||
|
|
||
| describe("prompt", () => { | ||
| test("should throw type errors if prompt requires variables but does not provide them", () => { | ||
| // @ts-expect-error | ||
| prompt(PROMPT_TEXT, {}); | ||
|
|
||
| // @ts-expect-error | ||
| expect(prompt(PROMPT_TEXT).text).toEqual(PROMPT_TEXT); | ||
| }); | ||
|
|
||
| test("Can serialize prompt to JSON", () => { | ||
| const variables = { test: "test" }; | ||
| const p = prompt(PROMPT_TEXT, variables) | ||
| const configuration = { max_tokens: 128, stop: null, temperature: 0.7 }; | ||
| expect(p.toJSON()).toEqual({ | ||
| configuration, | ||
| variables, | ||
| text: "this is a test", | ||
| template: { | ||
| configuration, | ||
| text: PROMPT_TEXT, | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| test("Can clone prompt", () => { | ||
| const variables = { test: "test" }; | ||
| const p = prompt(PROMPT_TEXT, variables); | ||
| const pClone = p.clone({ variables: { test: "test2" } }) | ||
| expect(pClone.text).toEqual("this is a test2") | ||
| }); | ||
|
|
||
| test("Prompt Class", () => { | ||
| const p = new Prompt("this is a {{test}}", { test: "123" }); | ||
|
|
||
| // saves the template | ||
| expect(p.template).toEqual("this is a {{test}}"); | ||
|
|
||
| // Valid, returns a new prompt | ||
| const formattedPrompt = p.format({ test: "123" }); | ||
| expect(formattedPrompt.text).toEqual("this is a 123"); | ||
| expect(formattedPrompt).not.toBe(p); | ||
|
|
||
| // Invalid | ||
| // @ts-expect-error | ||
| expect(p.format({ invalid: "123" }).text).toEqual("this is a {{test}}"); | ||
| }); | ||
|
|
||
| test("Prompt without variables", () => { | ||
| const p = new Prompt("this is a test", {}); | ||
|
|
||
| // saves the template | ||
| expect(p.template).toEqual("this is a test"); | ||
|
|
||
| // Valid, returns a new prompt | ||
| const formattedPrompt = p.format({ test: "123" }); | ||
| expect(formattedPrompt.text).toEqual("this is a test"); | ||
| expect(formattedPrompt).not.toBe(p); | ||
|
|
||
| // Invalid | ||
| // @ts-expect-error | ||
| expect(p.format({ invalid: "123" }).text).toEqual("this is a test"); | ||
| }); | ||
45 changes: 45 additions & 0 deletions
45
packages/promptable/src/prompts/__tests__/prompt-templates.test.ts
This file contains hidden or 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,45 @@ | ||
| import { | ||
| QA, | ||
| ExtractText, | ||
| Summarize, | ||
| Chatbot, | ||
| ExtractJSON, | ||
| ExtractCSV, | ||
| FixMarkup, | ||
| } from "@prompts/prompt-templates"; | ||
| import { Expect, Equal } from "@utils/__tests__/type-utils.test"; | ||
| import { ExtractVariableNames } from "@utils/type-utils"; | ||
|
|
||
| test("ExtractVariableNames", () => { | ||
| // These tests will cause a compile time error if the types don't match | ||
| type tests = [ | ||
| Expect<Equal< | ||
| ExtractVariableNames<typeof QA.text>, | ||
| Array<"document" | "question"> | ||
| >>, | ||
| Expect<Equal< | ||
| ExtractVariableNames<typeof ExtractText.text>, | ||
| Array<"document" | "question"> | ||
| >>, | ||
| Expect<Equal< | ||
| ExtractVariableNames<typeof Summarize.text>, | ||
| Array<"document"> | ||
| >>, | ||
| Expect<Equal< | ||
| ExtractVariableNames<typeof Chatbot.text>, | ||
| Array<"memory" | "userInput"> | ||
| >>, | ||
| Expect<Equal< | ||
| ExtractVariableNames<typeof ExtractJSON.text>, | ||
| Array<"data" | "type"> | ||
| >>, | ||
| Expect<Equal< | ||
| ExtractVariableNames<typeof ExtractCSV.text>, | ||
| Array<"data" | "headers"> | ||
| >>, | ||
| Expect<Equal< | ||
| ExtractVariableNames<typeof FixMarkup.text>, | ||
| Array<"markupLanguage" | "documentType" | "markup"> | ||
| >>, | ||
| ]; | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cfortuner was this how you intended the
p.cloneapi to be or did you mean for it to bep.clone({ test: "test2" })