Skip to content

Commit

Permalink
more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 14, 2024
1 parent 4e05ec6 commit e8d302a
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 21 deletions.
49 changes: 40 additions & 9 deletions docs/src/content/docs/reference/scripts/prompt.md
Original file line number Diff line number Diff line change
@@ -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"
<!-- genaiscript output start -->

<details open>
<summary>👤 user</summary>


```markdown wrap
You are a helpful assistant.
```


</details>

<!-- genaiscript output end -->




## 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.
<!-- genaiscript output start -->

<details open>
<summary>👤 user</summary>


```markdown wrap
Today is Thu Jun 13 2024.
```


</details>

<!-- genaiscript output end -->



59 changes: 55 additions & 4 deletions docs/src/content/docs/reference/scripts/response-priming.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 })
```

<!-- genaiscript output start -->

<details>
<summary>👤 user</summary>


```markdown wrap
List 5 colors. Answer with a JSON array. Do not emit the enclosing markdown.
```


</details>


<details open>
<summary>🤖 assistant</summary>


```markdown wrap
[
```


</details>


<details open>
<summary>🤖 assistant</summary>


```markdown wrap
"red",
"blue",
"green",
"yellow",
"purple"
]
```


</details>

<!-- genaiscript output end -->



### 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.
Expand Down
81 changes: 73 additions & 8 deletions docs/src/content/docs/reference/scripts/schemas.mdx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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.",
Expand All @@ -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 */}

<details open>
<summary>👤 user</summary>


````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.
````


</details>


<details open>
<summary>🤖 assistant</summary>


````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"
}
]
```
````


</details>

{/* 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.
Expand All @@ -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" })
```

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e8d302a

Please sign in to comment.