Skip to content

Commit

Permalink
Add Markdown and plaintext output systems (#929)
Browse files Browse the repository at this point in the history
* feat: ➕ Add Markdown and plaintext output systems

* feat: ✨ add plaintext output script and documentation

* fix basic test

* feat: 🍰 update recipe generation with carrot focus

* docs: ✏️ rename system prompt and add automation info
  • Loading branch information
pelikhan authored Dec 11, 2024
1 parent ecc5ce5 commit 610a432
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/src/content/docs/reference/scripts/prompty.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ This is what the `basic.prompty` file compiles to:

```js wrap title="basic.prompty.genai.mts"
script({
model: "azure:gpt-4o",
model: "openai:gpt-4o",
title: "Basic Prompt",
description: "A basic prompt that uses the chat API to answer questions",
parameters: {
Expand Down
59 changes: 53 additions & 6 deletions docs/src/content/docs/reference/scripts/system.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ System prompts are scripts that are executed and injected before the main prompt
- system prompts must use the `system` instead of `script`
- system prompts are executed with the same environment as the main prompt

```js title="system.zscot.genai.js" "system"
```js title="system.zero_shot_cot.genai.js" "system"
system({
title: "Zero-shot Chain of Thought",
})
Expand All @@ -22,10 +22,10 @@ $`Let's think step by step.`

To use system prompts in script, populate the `system` field with script identifiers.

```js title="myscript.genai.js" 'system: ["system.zscot"]'
```js title="myscript.genai.js" 'system: ["system.zero_shot_cot"]'
script({
...,
system: ["system.zscot"]
system: ["system.zero_shot_cot"]
})
$`Let's think step by step.`
```
Expand Down Expand Up @@ -73,6 +73,22 @@ script({ ...,
})
```

## Automated System Prompts

When unspecified, GenAIScript inspects the source code of the script
to determine a reasonable set of system prompts ([source code](https://github.com/microsoft/genaiscript/blob/main/packages/core/src/systems.ts)).

The default mix is

- system
- system.output_markdown
- system.explanations
- system.safety_jailbreak
- system.safety_harmful_content
- system.safety_protected_material

On top of the default, injects other system scripts based on keyword matching.

## Builtin System Prompts

GenAIScript comes with a number of system prompt that support features like creating files, extracting diffs or
Expand All @@ -88,9 +104,7 @@ Base system prompt

`````js wrap title="system"
system({ title: "Base system prompt" })
$`- You are concise.
- Answer in markdown.
`
$`- You are concise.`

`````

Expand Down Expand Up @@ -2567,6 +2581,39 @@ defTool(
`````
### `system.output_markdown`
Base system prompt
`````js wrap title="system.output_markdown"
system({ title: "Base system prompt" })
$`## Markdown Output
Respond in Markdown (GitHub Flavored Markdown also supported).`
`````
### `system.output_plaintext`
Plain text output
`````js wrap title="system.output_plaintext"
system({ title: "Plain text output" })
$`## Plain Text Output
Respond in plain text. Do not wrap result in Markdown code fences or XML tags.
`
`````
### `system.planner`
Instruct to make a plan
Expand Down
22 changes: 19 additions & 3 deletions packages/core/bundleprompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ System prompts are scripts that are executed and injected before the main prompt
- system prompts must use the \`system\` instead of \`script\`
- system prompts are executed with the same environment as the main prompt
\`\`\`js title="system.zscot.genai.js" "system"
\`\`\`js title="system.zero_shot_cot.genai.js" "system"
system({
title: "Zero-shot Chain of Thought",
})
Expand All @@ -145,10 +145,10 @@ $\`Let's think step by step.\`
To use system prompts in script, populate the \`system\` field with script identifiers.
\`\`\`js title="myscript.genai.js" 'system: ["system.zscot"]'
\`\`\`js title="myscript.genai.js" 'system: ["system.zero_shot_cot"]'
script({
...,
system: ["system.zscot"]
system: ["system.zero_shot_cot"]
})
$\`Let's think step by step.\`
\`\`\`
Expand Down Expand Up @@ -196,6 +196,22 @@ script({ ...,
})
\`\`\`
## Automated System Prompts
When unspecified, GenAIScript inspects the source code of the script
to determine a reasonable set of system prompts ([source code](https://github.com/microsoft/genaiscript/blob/main/packages/core/src/systems.ts)).
The default mix is
- system
- system.output_markdown
- system.explanations
- system.safety_jailbreak
- system.safety_harmful_content
- system.safety_protected_material
On top of the default, injects other system scripts based on keyword matching.
## Builtin System Prompts
GenAIScript comes with a number of system prompt that support features like creating files, extracting diffs or
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export const VISION_MODEL_ID = "vision"
export const DEFAULT_FENCE_FORMAT: FenceFormat = "xml"
export const DEFAULT_MODEL = "openai:gpt-4o"
export const DEFAULT_MODEL_CANDIDATES = [
"azure:gpt-4o",
"azure_serverless:gpt-4o",
DEFAULT_MODEL,
"google:gemini-1.5-pro-latest",
Expand All @@ -68,7 +67,6 @@ export const DEFAULT_MODEL_CANDIDATES = [
]
export const DEFAULT_VISION_MODEL = "openai:gpt-4o"
export const DEFAULT_VISION_MODEL_CANDIDATES = [
"azure:gpt-4o",
"azure_serverless:gpt-4o",
DEFAULT_MODEL,
"google:gemini-1.5-flash-latest",
Expand All @@ -77,7 +75,6 @@ export const DEFAULT_VISION_MODEL_CANDIDATES = [
]
export const DEFAULT_SMALL_MODEL = "openai:gpt-4o-mini"
export const DEFAULT_SMALL_MODEL_CANDIDATES = [
"azure:gpt-4o-mini",
"azure_serverless:gpt-4o-mini",
DEFAULT_SMALL_MODEL,
"google:gemini-1.5-flash-latest",
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/genaisrc/system.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
system({ title: "Base system prompt" })
$`- You are concise.
- Answer in markdown.
`
$`- You are concise.`
3 changes: 3 additions & 0 deletions packages/core/src/genaisrc/system.output_markdown.genai.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
system({ title: "Base system prompt" })
$`## Markdown Output
Respond in Markdown (GitHub Flavored Markdown also supported).`
4 changes: 4 additions & 0 deletions packages/core/src/genaisrc/system.output_plaintext.genai.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
system({ title: "Plain text output" })
$`## Plain Text Output
Respond in plain text. Do not wrap result in Markdown code fences or XML tags.
`
1 change: 0 additions & 1 deletion packages/core/src/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
ImageContent,
EmbeddedResource,
} from "@modelcontextprotocol/sdk/types.js"
import { YAMLStringify } from "./yaml"

export async function startMcpServer(
serverConfig: McpServerConfig,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/systems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function resolveSystems(
// Default systems if no responseType is specified
if (!script.responseType) {
systems.push("system")
systems.push("system.output_markdown")
systems.push("system.explanations")
systems.push("system.safety_jailbreak")
systems.push("system.safety_harmful_content")
Expand Down
9 changes: 9 additions & 0 deletions packages/sample/genaisrc/plaintext.genai.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
script({
model: "small",
system: ["system", "system.output_plaintext"],
tests: {
keywords: "carrots",
},
})

$`Generate a random recipe with carrots.`
2 changes: 1 addition & 1 deletion packages/sample/src/templates/basic.prompty
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ model:
api: chat
configuration:
type: azure_openai
azure_deployment: gpt-4o
azure_deployment: gpt-4o_2024-08-06
parameters:
max_tokens: 128
temperature: 0.2
Expand Down

0 comments on commit 610a432

Please sign in to comment.