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