Skip to content

Commit 32a8302

Browse files
committed
Release 1.0.0
1 parent af8d0a6 commit 32a8302

File tree

347 files changed

+35351
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

347 files changed

+35351
-17
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
title: Assistant
3+
---
4+
The `Assistant` module provides a powerful API that allows users to request structured JSON data from the assistant. This functionality can be used for automation tasks such as extracting bill details, categorizing expenses, or parsing text data.
5+
6+
## `isAvailable` Variable
7+
8+
### Description
9+
Indicates whether the Assistant API is available.
10+
* This status depends on the selected AI provider and whether a valid API Key is configured.
11+
* If the appropriate API Key is not provided, the Assistant API will be unavailable.
12+
13+
## `requestStructuredData` Method
14+
15+
### Description
16+
`requestStructuredData` allows users to send a text prompt to the assistant and receive structured data in JSON format based on a defined schema.
17+
18+
### Syntax
19+
```ts
20+
function requestStructuredData<R>(
21+
prompt: string,
22+
schema: JSONSchemaArray | JSONSchemaObject
23+
): Promise<R>
24+
```
25+
26+
### Parameters
27+
- `prompt` (`string`): The input prompt describing the content to be parsed.
28+
- `schema` (`JSONSchemaArray | JSONSchemaObject`): The expected output JSON schema, defining the structure of the returned data.
29+
30+
### Return Value
31+
Returns a `Promise` resolving to structured JSON data that matches the `schema` definition, with a type of `R`.
32+
33+
---
34+
35+
## JSON Schema Definition
36+
The `schema` parameter in the `requestStructuredData` method defines the structure of the returned JSON data, with the following types:
37+
38+
### `JSONSchemaType`
39+
```ts
40+
type JSONSchemaType = JSONSchemaPrimitive | JSONSchemaArray | JSONSchemaObject
41+
```
42+
43+
Primitive data type definition:
44+
```ts
45+
type JSONSchemaPrimitive = {
46+
type: "string" | "number" | "boolean"
47+
required?: boolean
48+
description: string
49+
}
50+
```
51+
52+
Array type definition:
53+
```ts
54+
type JSONSchemaArray = {
55+
type: "array"
56+
items: JSONSchemaType
57+
required?: boolean
58+
description: string
59+
}
60+
```
61+
62+
Object type definition:
63+
```ts
64+
type JSONSchemaObject = {
65+
type: "object"
66+
properties: Record<string, JSONSchemaType>
67+
required?: boolean
68+
description: string
69+
}
70+
```
71+
72+
### Example
73+
```ts
74+
const schema: JSONSchemaObject = {
75+
type: "object",
76+
properties: {
77+
totalAmount: {
78+
type: "number",
79+
required: true,
80+
description: "Total bill amount"
81+
},
82+
category: {
83+
type: "string",
84+
required: true,
85+
description: "Bill category"
86+
},
87+
date: {
88+
type: "string",
89+
required: false,
90+
description: "Bill date"
91+
},
92+
location: {
93+
type: "string",
94+
required: false,
95+
description: "Bill location"
96+
}
97+
}
98+
}
99+
```
100+
101+
---
102+
103+
## Example Usage
104+
105+
### Parsing Bill Information
106+
Suppose we have a bill, and we need to extract its amount, date, category, and location.
107+
108+
```ts
109+
const someBillDetails = `
110+
- Amount: $15.00
111+
- Date: 2024-03-11 14:30
112+
- Location: City Center Parking Lot
113+
- Category: Parking
114+
`
115+
116+
const prompt = `Please parse the following bill and output the structured data: ${someBillDetails}`
117+
118+
const data = await Assistant.requestStructuredData(
119+
prompt,
120+
schema
121+
)
122+
console.log(data)
123+
```
124+
125+
### Possible Output
126+
```json
127+
{
128+
"totalAmount": 15.00,
129+
"category": "Parking",
130+
"date": "2024-03-11 14:30",
131+
"location": "City Center Parking Lot"
132+
}
133+
```
134+
135+
---
136+
137+
## Usage Considerations
138+
1. **Ensure the `schema` is correctly defined**: The JSON schema should match the expected data format.
139+
2. **Use `required` attributes carefully**: If a field is essential, set `required: true`.
140+
3. **Provide a clear `prompt`**: A detailed `prompt` improves the accuracy of the assistant's response.
141+
4. **Error Handling**: Since `requestStructuredData` returns a `Promise`, handle potential errors using `try-catch`.
142+
143+
Example error handling:
144+
```ts
145+
try {
146+
const data = await Assistant.requestStructuredData(
147+
prompt,
148+
schema
149+
)
150+
console.log("Parsed result:", data)
151+
} catch (error) {
152+
console.error("Parsing failed:", error)
153+
}
154+
```

0 commit comments

Comments
 (0)