Skip to content

Commit

Permalink
Clean Up Quickstart
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay committed Nov 25, 2024
1 parent d028b0b commit 78c9ac9
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 61 deletions.
6 changes: 3 additions & 3 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export default defineConfig({
text: "", // For now
collapsed: false,
items: [
{ text: "Setup", link: "setup" },
{ text: "Getting Started", link: "getting-started" },
{ text: "Overview", link: "/" },
{ text: "Quickstart", link: "quickstart" },
{ text: "Types", link: "types" },
{ text: "Context", link: "context" },
{ text: "Patterns", link: "pattern-libraries" },
{ text: "Patterns", link: "patterns" },
{ text: "Common Errors", link: "common-errors" },
],
}],
Expand Down
17 changes: 0 additions & 17 deletions docs/getting-started.md

This file was deleted.

107 changes: 107 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
prev:
text: Overview
link: overview
next:
text: Types
link: types
---

# Quickstart

## Installation

::: code-group

```sh [Node.js]
npm i structured-outputs
```

```sh [Deno]
deno add jsr:@crosshatch/[email protected]

# The following will work upon stabilization.
deno add jsr:@crosshatch/structured-outputs
```

:::

## Node.js-specific Setup

The `structured-outputs` NPM package does not contain a CommonJS build; only ES Modules are
supported. Therefore, we must specify `module` as the value of the `package.json`'s `type` field.

```diff
{
// ...
+ "type": "module"
}
```

## Basic Example

Let's generate a character with superpowers according to the following shape:

```ts twoslash
import { ResponseFormat, T } from "structured-outputs"

const Supe = T.object({
name: T.string`The super-abled character's name.`,

Check warning on line 49 in docs/quickstart.md

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (abled)
role: T.constantUnion("Hero", "Villain", "Indifferent"),
age: T.number`Between 18 and 200 years of age.`,
})
```

Create a `ResponseFormat` with the `Supe` type.

```ts twoslash
import { ResponseFormat, T } from "structured-outputs"

const Supe = T.object({
name: T.string,
role: T.constantUnion("Hero", "Villain", "Indifferent"),
age: T.number,
})
// ---cut---
const response_format = ResponseFormat("create_supe", Supe)
```

> Optionally give a description to the response format.
>
> ```ts
> const response_format = ResponseFormat("create_supe", Supe)`
> Information about a super-abled character.

Check warning on line 73 in docs/quickstart.md

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (abled)
> `
> ```
Use your OpenAI client normally, then unwrap the structured output choice with
`response_format.parseFirstChoice`.
```ts twoslash
import Openai from "openai"
import { ResponseFormat, T } from "structured-outputs"
declare const openai: Openai
declare const response_format: ResponseFormat<{
name: string
role: "Hero" | "Villain" | "Indifferent"
age: number
}>
// ---cut---
const supe = await openai.chat.completions
.create({
model: "gpt-4o-mini",
response_format,
messages: [{ role: "system", content: [] }],
})
.then(response_format.parseFirstChoice)
supe
// ^?
```
<br />
<br />
<br />
<br />
39 changes: 0 additions & 39 deletions docs/setup.md

This file was deleted.

4 changes: 2 additions & 2 deletions docs/types.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
prev:
text: Getting Started
link: getting-started
text: Quickstart
link: quickstart
next:
text: Context
link: context
Expand Down

0 comments on commit 78c9ac9

Please sign in to comment.