Skip to content

Commit

Permalink
support disabling explanataions
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jan 17, 2025
1 parent e4a0848 commit 3842b00
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
15 changes: 14 additions & 1 deletion docs/src/content/docs/reference/scripts/classify.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,27 @@ Each label id should be a single word that encodes into a single token. This all
A `other` label can be automatically added to the list
of label to give an escape route for the LLM when it is not able to classify the text.

```js
```js "other: true"
const res = await classify(
"...",
{ ... },
{ other: true }
)
```

## Explanations

By default, the classification prompt is tuned to return a token (`maxToken: 1`) as the label.
You can enable emitting a justification before returning the label.

```js "explanation: true"
const res = await classify(
"...",
{ ... },
{ explanation: true }
)
```

## Model and other options

The `classify` function uses the `classify` [model alias](/genaiscript/reference/scripts/model-aliases) by default.
Expand Down
16 changes: 12 additions & 4 deletions packages/cli/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export type ClassifyOptions = {
* Inject a 'other' label
*/
other?: boolean
/**
* Explain answers before returning token
*/
explanations?: boolean
/**
* Options runPrompt context
*/
ctx?: ChatGenerationContext
} & Omit<PromptGeneratorOptions, "choices">

Expand All @@ -40,7 +47,7 @@ export async function classify<L extends Record<string, string>>(
answer: string
logprobs?: Record<keyof typeof labels | "other", Logprob>
}> {
const { other, ...rest } = options || {}
const { other, explanations, ...rest } = options || {}

const entries = Object.entries({
...labels,
Expand Down Expand Up @@ -71,8 +78,8 @@ You must classify the data as one of the following labels.
${entries.map(([id, descr]) => `- Label '${id}': ${descr}`).join("\n")}
## Output
Provide a single sentence justification for your choice.
and output the label as a single word on the last line. Do not emit "Label".
${explanations ? "Provide a single short sentence justification for your choice." : ""}
Output the label as a single word on the last line (do not emit "Label").
`
_.fence(
Expand All @@ -83,7 +90,7 @@ DATA:
Why did the chicken cross the road? Because moo.
Output:
It's a classic joke but the ending does not relate to the start of the joke.
${explanations ? "It's a classic joke but the ending does not relate to the start of the joke." : ""}
no
`,
Expand All @@ -98,6 +105,7 @@ no
label: `classify ${choices.join(", ")}`,
logprobs: true,
topLogprobs: Math.min(3, choices.length),
maxTokens: explanations ? 100 : 1,
system: [
"system.output_plaintext",
"system.safety_jailbreak",
Expand Down
3 changes: 2 additions & 1 deletion packages/sample/genaisrc/classify.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const joke = await classify(
{
yes: "funny",
no: "not funny",
}
},
{ explanations: true }
)

console.log(joke)
Expand Down

0 comments on commit 3842b00

Please sign in to comment.