-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
145 additions
and
86 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
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
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
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,40 @@ | ||
import { toFieldType } from './prompt.js'; | ||
import type { AxField } from './sig.js'; | ||
|
||
export class AxValidationError extends Error { | ||
private field: AxField; | ||
private value: string; | ||
|
||
constructor({ | ||
message, | ||
field, | ||
value | ||
}: Readonly<{ | ||
message: string; | ||
field: AxField; | ||
value: string; | ||
}>) { | ||
super(message); | ||
this.field = field; | ||
this.value = value; | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
|
||
public getField = () => this.field; | ||
public getValue = () => this.value; | ||
|
||
public getFixingInstructions = () => { | ||
const f = this.field; | ||
|
||
const extraFields = [ | ||
{ | ||
name: `invalidField`, | ||
title: `Invalid Field`, | ||
description: `Ensure the field \`${f.title}\` is of type \`${toFieldType(f.type)}\`` | ||
} | ||
]; | ||
|
||
return extraFields; | ||
}; | ||
} |
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
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,63 @@ | ||
import { AxAI, AxChainOfThought } from '@ax-llm/ax'; | ||
|
||
// Setup the prompt program for movie reviews | ||
const gen = new AxChainOfThought<{ movieTitle: string }>( | ||
`movieTitle:string -> | ||
rating:number, | ||
genres:string[], | ||
strengths:string[], | ||
weaknesses:string[], | ||
recommendedAudience:string, | ||
verdict:string` | ||
); | ||
|
||
// Assert rating is between 1 and 10 | ||
gen.addAssert(({ rating }: Readonly<{ rating: number }>) => { | ||
if (!rating) return undefined; | ||
return rating >= 1 && rating <= 10; | ||
}, 'Rating must be between 1 and 10'); | ||
|
||
// Assert there are between 1-3 genres | ||
gen.addAssert(({ genres }: Readonly<{ genres: string[] }>) => { | ||
if (!genres) return undefined; | ||
return genres.length >= 1 && genres.length <= 3; | ||
}, 'Must specify between 1-3 genres'); | ||
|
||
// Assert strengths and weaknesses are balanced (similar length arrays) | ||
gen.addAssert( | ||
({ | ||
strengths, | ||
weaknesses | ||
}: Readonly<{ strengths: string[]; weaknesses: string[] }>) => { | ||
if (!strengths || !weaknesses) return undefined; | ||
const diff = Math.abs(strengths.length - weaknesses.length); | ||
return diff <= 1; | ||
}, | ||
'Review should be balanced with similar number of strengths and weaknesses' | ||
); | ||
|
||
// Assert verdict is not too short | ||
gen.addAssert(({ verdict }: Readonly<{ verdict: string }>) => { | ||
if (!verdict) return undefined; | ||
return verdict.length >= 50; | ||
}, 'Verdict must be at least 50 characters'); | ||
|
||
// Assert recommended audience doesn't mention specific age numbers | ||
gen.addAssert( | ||
({ recommendedAudience }: Readonly<{ recommendedAudience: string }>) => { | ||
if (!recommendedAudience) return undefined; | ||
return !/\d+/.test(recommendedAudience); | ||
}, | ||
'Use age groups (e.g. "teens", "adults") instead of specific ages' | ||
); | ||
|
||
const ai = new AxAI({ | ||
name: 'google-gemini', | ||
apiKey: process.env.GOOGLE_APIKEY as string | ||
}); | ||
// ai.setOptions({ debug: true }); | ||
|
||
// Run the program | ||
const res = await gen.forward(ai, { movieTitle: 'The Grand Budapest Hotel' }); | ||
|
||
console.log('>', res); |