-
Notifications
You must be signed in to change notification settings - Fork 8
Add startIndex and endIndex to compile error. #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andresgutgon
merged 1 commit into
main
from
feature/add-start-index-and-end-index-to-errors
Jun 18, 2025
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| 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"; | ||
| import { complete } 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(); | ||
| 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. | ||
|
|
@@ -15,18 +15,22 @@ describe("step tags", async () => { | |
| <step> | ||
| Now correct the statement if it is not true. | ||
| </step> | ||
| `); | ||
| `) | ||
|
|
||
| const chain = new Chain({ prompt, parameters: { mock }}); | ||
| await complete({ chain, callback: async () => ` | ||
| 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()}); | ||
| `.trim(), | ||
| }) | ||
|
|
||
| expect(mock).not.toHaveBeenCalled(); | ||
| }); | ||
| expect(mock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("creates a text variable from response if specified", async () => { | ||
| const mock = vi.fn(); | ||
| 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. | ||
|
|
@@ -36,18 +40,24 @@ describe("step tags", async () => { | |
| {{ mock(analysis) }} | ||
| Now correct the statement if it is not true. | ||
| </step> | ||
| `); | ||
| `) | ||
|
|
||
| const chain = new Chain({ prompt, parameters: { mock }}); | ||
| await complete({ chain, callback: async () => ` | ||
| 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()}); | ||
| `.trim(), | ||
| }) | ||
|
|
||
| expect(mock).toHaveBeenCalledWith("The statement is not true because it is fake. My confidence score is 100."); | ||
| }); | ||
| 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(); | ||
| 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. | ||
|
|
@@ -59,27 +69,33 @@ describe("step tags", async () => { | |
| 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 () => ` | ||
| const chain = new Chain({ prompt, parameters: { mock } }) | ||
| const { messages } = await complete({ | ||
| chain, | ||
| callback: async () => | ||
| ` | ||
| { | ||
| "truthful": false, | ||
| "reason": "It is fake", | ||
| "confidence": 100 | ||
| } | ||
| `.trim()}); | ||
| `.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'."); | ||
| }); | ||
| 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(); | ||
| 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. | ||
|
|
@@ -91,19 +107,29 @@ describe("step tags", async () => { | |
| 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 () => ` | ||
| const chain = new Chain({ prompt, parameters: { mock } }) | ||
| let error: CompileError | ||
| try { | ||
| await complete({ | ||
| chain, | ||
| callback: async () => | ||
| ` | ||
| Bad JSON. | ||
| `.trim()}), CompileError) | ||
| expect(error.code).toBe('invalid-step-response-format') | ||
| `.trim(), | ||
| }) | ||
| } catch (e) { | ||
| error = e as CompileError | ||
| expect(e).toBeInstanceOf(CompileError) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the change. The abstraction we have |
||
| } | ||
|
|
||
| expect(mock).not.toHaveBeenCalled(); | ||
| }); | ||
| expect(error!.code).toBe('invalid-step-response-format') | ||
| expect(mock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("creates a raw variable from response if specified", async () => { | ||
| const mock = vi.fn(); | ||
| 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. | ||
|
|
@@ -113,21 +139,25 @@ describe("step tags", async () => { | |
| {{ mock(analysis) }} | ||
| Now correct the statement if it is not true. | ||
| </step> | ||
| `); | ||
| `) | ||
|
|
||
| const chain = new Chain({ prompt, parameters: { mock }}); | ||
| await complete({ chain, callback: async () => ` | ||
| 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()}); | ||
| `.trim(), | ||
| }) | ||
|
|
||
| expect(mock).toHaveBeenCalledWith({ | ||
| role: "assistant", | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: "text", | ||
| text: "The statement is not true because it is fake. My confidence score is 100.", | ||
| type: 'text', | ||
| text: 'The statement is not true because it is fake. My confidence score is 100.', | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
| }); | ||
| }) | ||
| }) | ||
| }) | ||
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
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.
This file was not prettified. Therefore all these changes