-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse step response when JSON schema (#10)
* Parse step response when JSON schema * Add tests
- Loading branch information
Showing
3 changed files
with
147 additions
and
12 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
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,133 @@ | ||
import CompileError from '$promptl/error/error' | ||
import { complete, getExpectedError } from "$promptl/compiler/test/helpers"; | ||
import { removeCommonIndent } from "$promptl/compiler/utils"; | ||
import { Chain } from "$promptl/index"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
describe("step tags", async () => { | ||
it("does not create a variable from response if not specified", async () => { | ||
const mock = vi.fn(); | ||
const prompt = removeCommonIndent(` | ||
<step> | ||
Ensure truthfulness of the following statement, give a reason and a confidence score. | ||
Statement: fake statement | ||
</step> | ||
<step> | ||
Now correct the statement if it is not true. | ||
</step> | ||
`); | ||
|
||
const chain = new Chain({ prompt, parameters: { mock }}); | ||
await complete({ chain, callback: async () => ` | ||
The statement is not true because it is fake. My confidence score is 100. | ||
`.trim()}); | ||
|
||
expect(mock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("creates a text variable from response if specified", async () => { | ||
const mock = vi.fn(); | ||
const prompt = removeCommonIndent(` | ||
<step as="analysis"> | ||
Ensure truthfulness of the following statement, give a reason and a confidence score. | ||
Statement: fake statement | ||
</step> | ||
<step> | ||
{{ mock(analysis) }} | ||
Now correct the statement if it is not true. | ||
</step> | ||
`); | ||
|
||
const chain = new Chain({ prompt, parameters: { mock }}); | ||
await complete({ chain, callback: async () => ` | ||
The statement is not true because it is fake. My confidence score is 100. | ||
`.trim()}); | ||
|
||
expect(mock).toHaveBeenCalledWith("The statement is not true because it is fake. My confidence score is 100."); | ||
}); | ||
|
||
it("creates an object variable from response if specified and schema is provided", async () => { | ||
const mock = vi.fn(); | ||
const prompt = removeCommonIndent(` | ||
<step as="analysis" schema={{{type: "object", properties: {truthful: {type: "boolean"}, reason: {type: "string"}, confidence: {type: "integer"}}, required: ["truthful", "reason", "confidence"]}}}> | ||
Ensure truthfulness of the following statement, give a reason and a confidence score. | ||
Statement: fake statement | ||
</step> | ||
<step> | ||
{{ mock(analysis) }} | ||
{{ if !analysis.truthful && analysis.confidence > 50 }} | ||
Correct the statement taking into account the reason: '{{ analysis.reason }}'. | ||
{{ endif }} | ||
</step> | ||
`); | ||
|
||
const chain = new Chain({ prompt, parameters: { mock }}); | ||
const { messages } = await complete({ chain, callback: async () => ` | ||
{ | ||
"truthful": false, | ||
"reason": "It is fake", | ||
"confidence": 100 | ||
} | ||
`.trim()}); | ||
|
||
expect(mock).toHaveBeenCalledWith({ | ||
truthful: false, | ||
reason: "It is fake", | ||
confidence: 100 | ||
}); | ||
expect(messages[2]!.content).toEqual("Correct the statement taking into account the reason: 'It is fake'."); | ||
}); | ||
|
||
it("fails creating an object variable from response if specified and schema is provided but response is invalid", async () => { | ||
const mock = vi.fn(); | ||
const prompt = removeCommonIndent(` | ||
<step as="analysis" schema={{{type: "object", properties: {truthful: {type: "boolean"}, reason: {type: "string"}, confidence: {type: "integer"}}, required: ["truthful", "reason", "confidence"]}}}> | ||
Ensure truthfulness of the following statement, give a reason and a confidence score. | ||
Statement: fake statement | ||
</step> | ||
<step> | ||
{{ mock(analysis) }} | ||
{{ if !analysis.truthful && analysis.confidence > 50 }} | ||
Correct the statement taking into account the reason: '{{ analysis.reason }}'. | ||
{{ endif }} | ||
</step> | ||
`); | ||
|
||
const chain = new Chain({ prompt, parameters: { mock }}); | ||
const error = await getExpectedError(() => complete({ chain, callback: async () => ` | ||
Bad JSON. | ||
`.trim()}), CompileError) | ||
expect(error.code).toBe('function-call-error') | ||
|
||
expect(mock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("creates a raw variable from response if specified", async () => { | ||
const mock = vi.fn(); | ||
const prompt = removeCommonIndent(` | ||
<step raw="analysis"> | ||
Ensure truthfulness of the following statement, give a reason and a confidence score. | ||
Statement: fake statement | ||
</step> | ||
<step> | ||
{{ mock(analysis) }} | ||
Now correct the statement if it is not true. | ||
</step> | ||
`); | ||
|
||
const chain = new Chain({ prompt, parameters: { mock }}); | ||
await complete({ chain, callback: async () => ` | ||
The statement is not true because it is fake. My confidence score is 100. | ||
`.trim()}); | ||
|
||
expect(mock).toHaveBeenCalledWith({ | ||
role: "assistant", | ||
content: [ | ||
{ | ||
type: "text", | ||
text: "The statement is not true because it is fake. My confidence score is 100.", | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
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