From e8d302ace933f6772d8917adb9661cef1f7568e5 Mon Sep 17 00:00:00 2001 From: pelikhan Date: Thu, 13 Jun 2024 22:53:05 -0700 Subject: [PATCH] more docs --- .../content/docs/reference/scripts/prompt.md | 49 ++++++++--- .../reference/scripts/response-priming.md | 59 +++++++++++++- .../docs/reference/scripts/schemas.mdx | 81 +++++++++++++++++-- 3 files changed, 168 insertions(+), 21 deletions(-) diff --git a/docs/src/content/docs/reference/scripts/prompt.md b/docs/src/content/docs/reference/scripts/prompt.md index 18d511b4b1..1d17798bac 100644 --- a/docs/src/content/docs/reference/scripts/prompt.md +++ b/docs/src/content/docs/reference/scripts/prompt.md @@ -1,31 +1,62 @@ --- title: Prompt ($) sidebar: - order: 2 -description: Learn how to use the tagged template literal for dynamic prompt generation in GenAI scripts. -keywords: tagged template, prompt expansion, dynamic prompts, JavaScript templates, GenAI scripting + order: 2 +description: Learn how to use the tagged template literal for dynamic prompt + generation in GenAI scripts. +keywords: tagged template, prompt expansion, dynamic prompts, JavaScript + templates, GenAI scripting +genaiscript: + model: openai:gpt-3.5-turbo + --- The `$` is a JavaScript [tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) that expands the string into the final prompt. -```javascript title="example.genai.js" -... +```js title="example.genai.js" assistant=false user=true $`You are a helpful assistant.` ``` -```txt title="Final prompt" + + +
+👤 user + + +```markdown wrap You are a helpful assistant. ``` + +
+ + + + + + ## Inline expressions You can weave expressions in the template using `${...}`. Expression can be promises and will be awaited when rendering the final prompt. -```js title="example.genai.js" +```js title="example.genai.js" assistant=false user=true $`Today is ${new Date().toDateString()}.` ``` -```txt title="Final prompt" -Today is Fri Mar 01 2024. + + +
+👤 user + + +```markdown wrap +Today is Thu Jun 13 2024. ``` + +
+ + + + + diff --git a/docs/src/content/docs/reference/scripts/response-priming.md b/docs/src/content/docs/reference/scripts/response-priming.md index 2542eee137..af9bd99a04 100644 --- a/docs/src/content/docs/reference/scripts/response-priming.md +++ b/docs/src/content/docs/reference/scripts/response-priming.md @@ -1,9 +1,14 @@ --- title: Response Priming sidebar: - order: 100 -description: Learn how to prime LLM responses with specific syntax or format using the writeText function in scripts. -keywords: response priming, LLM syntax, script formatting, writeText function, assistant message + order: 100 +description: Learn how to prime LLM responses with specific syntax or format + using the writeText function in scripts. +keywords: response priming, LLM syntax, script formatting, writeText function, + assistant message +genaiscript: + model: openai:gpt-3.5-turbo + --- It is possible to provide the start of the LLM response (`assistant` message) in the script. @@ -12,13 +17,59 @@ This allows to steer the answer of the LLM to a specify syntax or format. Use `writeText` with the `{assistant: true}` option to provide the assistant text. ```js -$`Answer with a JSON array. Do not emit the enclosing markdown.` +$`List 5 colors. Answer with a JSON array. Do not emit the enclosing markdown.` // help the LLM by starting the JSON array syntax // in the assistant response writeText(`[`, { assistant: true }) ``` + + +
+👤 user + + +```markdown wrap +List 5 colors. Answer with a JSON array. Do not emit the enclosing markdown. +``` + + +
+ + +
+🤖 assistant + + +```markdown wrap +[ +``` + + +
+ + +
+🤖 assistant + + +```markdown wrap +"red", +"blue", +"green", +"yellow", +"purple" +] +``` + + +
+ + + + + ### How does it work? Internally when invoking the LLM, an additional message is added to the query as if the LLM had generated this content. diff --git a/docs/src/content/docs/reference/scripts/schemas.mdx b/docs/src/content/docs/reference/scripts/schemas.mdx index ce985f3b14..a341a98cac 100644 --- a/docs/src/content/docs/reference/scripts/schemas.mdx +++ b/docs/src/content/docs/reference/scripts/schemas.mdx @@ -1,9 +1,14 @@ --- title: Data Schemas sidebar: - order: 6 -description: Learn how to define and use data schemas for structured output in JSON/YAML with LLM, including validation and repair techniques. -keywords: data schemas, JSON schema, YAML validation, LLM structured output, schema repair + order: 6 +description: Learn how to define and use data schemas for structured output in + JSON/YAML with LLM, including validation and repair techniques. +keywords: data schemas, JSON schema, YAML validation, LLM structured output, + schema repair +genaiscript: + model: openai:gpt-3.5-turbo + --- import { Card } from '@astrojs/starlight/components'; @@ -18,7 +23,7 @@ specific data format later on. Use `defSchema` to define a JSON/YAML schema for the prompt output. -```javascript +```js user=true const schema = defSchema("CITY_SCHEMA", { type: "array", description: "A list of cities with population and elevation information.", @@ -34,15 +39,75 @@ const schema = defSchema("CITY_SCHEMA", { } }) -$`Generate ... using JSON compliant with ${schema}.` +$`Generate data using JSON compliant with ${schema}.` +``` + +{/* genaiscript output start */} + +
+👤 user + + +````markdown wrap +CITY_SCHEMA: +```typescript-schema +// A list of cities with population and elevation information. +type CITY_SCHEMA = Array<{ + // The name of the city. + name: string, + // The population of the city. + population: number, + // The URL of the city's Wikipedia page. + url: string, + }> ``` +Generate data using JSON compliant with CITY_SCHEMA. +```` + + +
+ + +
+🤖 assistant + + +````markdown wrap +File ./data.json: +```json schema=CITY_SCHEMA +[ + { + "name": "New York", + "population": 8398748, + "url": "https://en.wikipedia.org/wiki/New_York_City" + }, + { + "name": "Los Angeles", + "population": 3990456, + "url": "https://en.wikipedia.org/wiki/Los_Angeles" + }, + { + "name": "Chicago", + "population": 2705994, + "url": "https://en.wikipedia.org/wiki/Chicago" + } +] +``` +```` + + +
+ +{/* genaiscript output end */} + + ### Prompt encoding Following the ["All You Need Is Types" approach](https://microsoft.github.io/TypeChat/docs/introduction/) from TypeChat, the schema is converted TypeScript types before being injected in the LLM prompt. -```typescript +```ts // A list of cities with population and elevation information. type CITY_SCHEMA = Array<{ // The name of the city. @@ -56,7 +121,7 @@ type CITY_SCHEMA = Array<{ You can change this behavior by using the `{ format: "json" }` option. -```javascript +```js const schema = defSchema("CITY_SCHEMA", {...}, { format: "json" }) ``` @@ -102,7 +167,7 @@ in the output folder as well. ``` - prompt (rendered as typescript): -```typescript +```ts // A list of cities with population and elevation information. type CITY_SCHEMA = Array<{ // The name of the city.