heroku-ai-provider 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +501 -0
- package/dist/cjs/index.cjs +258 -0
- package/dist/cjs/index.d.cts +186 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/models/chat.cjs +1378 -0
- package/dist/cjs/models/chat.d.cts +355 -0
- package/dist/cjs/models/chat.d.ts.map +1 -0
- package/dist/cjs/models/chat.js.map +1 -0
- package/dist/cjs/models/embedding.cjs +477 -0
- package/dist/cjs/models/embedding.d.cts +320 -0
- package/dist/cjs/models/embedding.d.ts.map +1 -0
- package/dist/cjs/models/embedding.js.map +1 -0
- package/dist/cjs/utils/api-client.cjs +250 -0
- package/dist/cjs/utils/api-client.d.cts +46 -0
- package/dist/cjs/utils/api-client.d.ts.map +1 -0
- package/dist/cjs/utils/api-client.js.map +1 -0
- package/dist/cjs/utils/error-handling.cjs +258 -0
- package/dist/cjs/utils/error-handling.d.cts +31 -0
- package/dist/cjs/utils/error-handling.d.ts.map +1 -0
- package/dist/cjs/utils/error-handling.js.map +1 -0
- package/dist/cjs/utils/error-types.cjs +606 -0
- package/dist/cjs/utils/error-types.d.cts +119 -0
- package/dist/cjs/utils/error-types.d.ts.map +1 -0
- package/dist/cjs/utils/error-types.js.map +1 -0
- package/dist/cjs/utils/user-friendly-errors.cjs +247 -0
- package/dist/cjs/utils/user-friendly-errors.d.cts +52 -0
- package/dist/cjs/utils/user-friendly-errors.d.ts.map +1 -0
- package/dist/cjs/utils/user-friendly-errors.js.map +1 -0
- package/dist/esm/index.d.ts +186 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +241 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/models/chat.d.ts +355 -0
- package/dist/esm/models/chat.d.ts.map +1 -0
- package/dist/esm/models/chat.js +1374 -0
- package/dist/esm/models/chat.js.map +1 -0
- package/dist/esm/models/embedding.d.ts +320 -0
- package/dist/esm/models/embedding.d.ts.map +1 -0
- package/dist/esm/models/embedding.js +472 -0
- package/dist/esm/models/embedding.js.map +1 -0
- package/dist/esm/utils/api-client.d.ts +46 -0
- package/dist/esm/utils/api-client.d.ts.map +1 -0
- package/dist/esm/utils/api-client.js +243 -0
- package/dist/esm/utils/api-client.js.map +1 -0
- package/dist/esm/utils/error-handling.d.ts +31 -0
- package/dist/esm/utils/error-handling.d.ts.map +1 -0
- package/dist/esm/utils/error-handling.js +249 -0
- package/dist/esm/utils/error-handling.js.map +1 -0
- package/dist/esm/utils/error-types.d.ts +119 -0
- package/dist/esm/utils/error-types.d.ts.map +1 -0
- package/dist/esm/utils/error-types.js +599 -0
- package/dist/esm/utils/error-types.js.map +1 -0
- package/dist/esm/utils/user-friendly-errors.d.ts +52 -0
- package/dist/esm/utils/user-friendly-errors.d.ts.map +1 -0
- package/dist/esm/utils/user-friendly-errors.js +238 -0
- package/dist/esm/utils/user-friendly-errors.js.map +1 -0
- package/package.json +104 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
# Heroku AI SDK Provider
|
|
2
|
+
|
|
3
|
+
A TypeScript provider for the [Vercel AI SDK](https://sdk.vercel.ai/) that enables you to use Heroku's AI inference capabilities in your applications. This provider supports both chat completions and embeddings through Heroku's AI infrastructure.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🤖 **Chat Completions**: Support for Claude 3.5 Sonnet, Claude 3 Haiku, and other Heroku-hosted models
|
|
8
|
+
- 🧠 **Embeddings**: Generate vector embeddings using Cohere's multilingual embedding model
|
|
9
|
+
- 🔧 **Tool Support**: Function calling capabilities for building AI agents and assistants
|
|
10
|
+
- 📡 **Streaming**: Real-time streaming responses for chat completions
|
|
11
|
+
- 🔐 **TypeScript**: Full TypeScript support with comprehensive type definitions
|
|
12
|
+
- ⚡ **Dual Module Support**: Compatible with both ESM and CommonJS projects
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install heroku-ai-provider
|
|
18
|
+
# or
|
|
19
|
+
yarn add heroku-ai-provider
|
|
20
|
+
# or
|
|
21
|
+
pnpm add heroku-ai-provider
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Prerequisites
|
|
25
|
+
|
|
26
|
+
Before using this provider, you'll need:
|
|
27
|
+
|
|
28
|
+
1. A Heroku account with access to AI services
|
|
29
|
+
2. Heroku AI API keys for inference and/or embeddings
|
|
30
|
+
3. The Vercel AI SDK installed in your project
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install ai
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Basic Setup
|
|
37
|
+
|
|
38
|
+
### Environment Variables
|
|
39
|
+
|
|
40
|
+
Set your Heroku AI API keys as environment variables:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# For chat completions
|
|
44
|
+
HEROKU_INFERENCE_KEY=your_inference_api_key
|
|
45
|
+
|
|
46
|
+
# For embeddings
|
|
47
|
+
HEROKU_EMBEDDING_KEY=your_embedding_api_key
|
|
48
|
+
|
|
49
|
+
# Optional: Custom API endpoints
|
|
50
|
+
HEROKU_INFERENCE_URL=https://us.inference.heroku.com
|
|
51
|
+
HEROKU_EMBEDDING_URL=https://us.inference.heroku.com
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Basic Configuration
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
58
|
+
|
|
59
|
+
// Using environment variables (recommended)
|
|
60
|
+
const heroku = createHerokuProvider();
|
|
61
|
+
|
|
62
|
+
// Or with explicit configuration
|
|
63
|
+
const heroku = createHerokuProvider({
|
|
64
|
+
chatApiKey: "your_inference_api_key",
|
|
65
|
+
embeddingsApiKey: "your_embedding_api_key",
|
|
66
|
+
chatBaseUrl: "https://us.inference.heroku.com/v1/chat/completions",
|
|
67
|
+
embeddingsBaseUrl: "https://us.inference.heroku.com/v1/embeddings",
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Usage Examples
|
|
72
|
+
|
|
73
|
+
### Chat Completions
|
|
74
|
+
|
|
75
|
+
#### Basic Chat
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { generateText } from "ai";
|
|
79
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
80
|
+
|
|
81
|
+
const heroku = createHerokuProvider();
|
|
82
|
+
|
|
83
|
+
const { text } = await generateText({
|
|
84
|
+
model: heroku.chat("claude-3-5-sonnet-latest"),
|
|
85
|
+
prompt: "What is the capital of France?",
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
console.log(text); // "The capital of France is Paris."
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### Streaming Chat
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { streamText } from "ai";
|
|
95
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
96
|
+
|
|
97
|
+
const heroku = createHerokuProvider();
|
|
98
|
+
|
|
99
|
+
const { textStream } = await streamText({
|
|
100
|
+
model: heroku.chat("claude-3-haiku"),
|
|
101
|
+
prompt: "Write a short story about a robot learning to paint.",
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
for await (const delta of textStream) {
|
|
105
|
+
process.stdout.write(delta);
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### Chat with System Message
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { generateText } from "ai";
|
|
113
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
114
|
+
|
|
115
|
+
const heroku = createHerokuProvider();
|
|
116
|
+
|
|
117
|
+
const { text } = await generateText({
|
|
118
|
+
model: heroku.chat("claude-3-5-sonnet-latest"),
|
|
119
|
+
system: "You are a helpful assistant that explains complex topics simply.",
|
|
120
|
+
prompt: "Explain quantum computing",
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Tool/Function Calling
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { generateText, tool } from "ai";
|
|
128
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
129
|
+
import { z } from "zod";
|
|
130
|
+
|
|
131
|
+
const heroku = createHerokuProvider();
|
|
132
|
+
|
|
133
|
+
const { text } = await generateText({
|
|
134
|
+
model: heroku.chat("claude-3-5-sonnet-latest"),
|
|
135
|
+
prompt: "What is the weather like in New York?",
|
|
136
|
+
tools: {
|
|
137
|
+
getWeather: tool({
|
|
138
|
+
description: "Get the current weather for a location",
|
|
139
|
+
parameters: z.object({
|
|
140
|
+
location: z.string().describe("The city and state"),
|
|
141
|
+
}),
|
|
142
|
+
execute: async ({ location }) => {
|
|
143
|
+
// Simulate weather API call
|
|
144
|
+
return {
|
|
145
|
+
location,
|
|
146
|
+
temperature: 72,
|
|
147
|
+
condition: "sunny",
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
}),
|
|
151
|
+
},
|
|
152
|
+
maxSteps: 5, // Allow multi-step tool conversations
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### Advanced Tool Usage with Multiple Steps
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { generateText, tool } from "ai";
|
|
160
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
161
|
+
import { z } from "zod";
|
|
162
|
+
|
|
163
|
+
const heroku = createHerokuProvider();
|
|
164
|
+
|
|
165
|
+
const { text, steps } = await generateText({
|
|
166
|
+
model: heroku.chat("claude-3-5-sonnet-latest"),
|
|
167
|
+
prompt:
|
|
168
|
+
"Check the weather in New York and then suggest appropriate clothing.",
|
|
169
|
+
tools: {
|
|
170
|
+
getWeather: tool({
|
|
171
|
+
description: "Get the current weather for a location",
|
|
172
|
+
parameters: z.object({
|
|
173
|
+
location: z.string().describe("The city and state"),
|
|
174
|
+
}),
|
|
175
|
+
execute: async ({ location }) => {
|
|
176
|
+
return {
|
|
177
|
+
location,
|
|
178
|
+
temperature: 45,
|
|
179
|
+
condition: "rainy",
|
|
180
|
+
humidity: 80,
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
}),
|
|
184
|
+
suggestClothing: tool({
|
|
185
|
+
description: "Suggest appropriate clothing based on weather conditions",
|
|
186
|
+
parameters: z.object({
|
|
187
|
+
temperature: z.number().describe("Temperature in Fahrenheit"),
|
|
188
|
+
condition: z.string().describe("Weather condition"),
|
|
189
|
+
humidity: z.number().optional().describe("Humidity percentage"),
|
|
190
|
+
}),
|
|
191
|
+
execute: async ({ temperature, condition, humidity }) => {
|
|
192
|
+
return {
|
|
193
|
+
suggestions: [
|
|
194
|
+
"Waterproof jacket",
|
|
195
|
+
"Warm layers",
|
|
196
|
+
"Waterproof shoes",
|
|
197
|
+
"Umbrella",
|
|
198
|
+
],
|
|
199
|
+
reasoning: `Given ${temperature}°F and ${condition} weather${humidity ? ` with ${humidity}% humidity` : ""}, you'll want to stay warm and dry.`,
|
|
200
|
+
};
|
|
201
|
+
},
|
|
202
|
+
}),
|
|
203
|
+
},
|
|
204
|
+
maxSteps: 5,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
console.log("Final response:", text);
|
|
208
|
+
console.log("Tool execution steps:", steps.length);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Embeddings
|
|
212
|
+
|
|
213
|
+
#### Basic Embeddings
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
import { embed } from "ai";
|
|
217
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
218
|
+
|
|
219
|
+
const heroku = createHerokuProvider();
|
|
220
|
+
|
|
221
|
+
const { embedding } = await embed({
|
|
222
|
+
model: heroku.embedding("cohere-embed-multilingual"),
|
|
223
|
+
value: "Hello, world!",
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
console.log(embedding); // [0.1, 0.2, -0.3, ...]
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
#### Multiple Embeddings
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
import { embedMany } from "ai";
|
|
233
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
234
|
+
|
|
235
|
+
const heroku = createHerokuProvider();
|
|
236
|
+
|
|
237
|
+
const { embeddings } = await embedMany({
|
|
238
|
+
model: heroku.embedding("cohere-embed-multilingual"),
|
|
239
|
+
values: ["First document", "Second document", "Third document"],
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
console.log(embeddings.length); // 3
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
#### Using the Convenience Function
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
import { createEmbedFunction } from "heroku-ai-provider";
|
|
249
|
+
|
|
250
|
+
// Create a reusable embed function
|
|
251
|
+
const embedText = createEmbedFunction({
|
|
252
|
+
apiKey: process.env.HEROKU_EMBEDDING_KEY!,
|
|
253
|
+
model: "cohere-embed-multilingual",
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const embedding = await embedText("Hello, world!");
|
|
257
|
+
console.log(embedding); // [0.1, 0.2, -0.3, ...]
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Configuration Options
|
|
261
|
+
|
|
262
|
+
### HerokuProviderSettings
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
interface HerokuProviderSettings {
|
|
266
|
+
// API keys (falls back to environment variables)
|
|
267
|
+
chatApiKey?: string; // HEROKU_INFERENCE_KEY
|
|
268
|
+
embeddingsApiKey?: string; // HEROKU_EMBEDDING_KEY
|
|
269
|
+
|
|
270
|
+
// Base URLs (falls back to environment variables or defaults)
|
|
271
|
+
chatBaseUrl?: string; // HEROKU_INFERENCE_URL
|
|
272
|
+
embeddingsBaseUrl?: string; // HEROKU_EMBEDDING_URL
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Supported Models
|
|
277
|
+
|
|
278
|
+
#### Chat Models
|
|
279
|
+
|
|
280
|
+
- `claude-3-5-sonnet-latest` - Latest Claude 3.5 Sonnet (recommended)
|
|
281
|
+
- `claude-3-haiku` - Fast and efficient Claude 3 Haiku
|
|
282
|
+
- `claude-4-sonnet` - Claude 4 Sonnet (when available)
|
|
283
|
+
- `claude-3-7-sonnet` - Claude 3.7 Sonnet
|
|
284
|
+
- `claude-3-5-haiku` - Claude 3.5 Haiku
|
|
285
|
+
|
|
286
|
+
#### Embedding Models
|
|
287
|
+
|
|
288
|
+
- `cohere-embed-multilingual` - Multilingual embedding model by Cohere
|
|
289
|
+
|
|
290
|
+
## Framework Integration
|
|
291
|
+
|
|
292
|
+
### Next.js App Router
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
// app/api/chat/route.ts
|
|
296
|
+
import { streamText } from "ai";
|
|
297
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
298
|
+
|
|
299
|
+
const heroku = createHerokuProvider();
|
|
300
|
+
|
|
301
|
+
export async function POST(req: Request) {
|
|
302
|
+
const { messages } = await req.json();
|
|
303
|
+
|
|
304
|
+
const result = await streamText({
|
|
305
|
+
model: heroku.chat("claude-3-5-sonnet-latest"),
|
|
306
|
+
messages,
|
|
307
|
+
maxSteps: 5, // Enable multi-step tool conversations
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
return result.toDataStreamResponse();
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### Next.js with Tool Support
|
|
315
|
+
|
|
316
|
+
```typescript
|
|
317
|
+
// app/api/chat/route.ts
|
|
318
|
+
import { streamText, tool } from "ai";
|
|
319
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
320
|
+
import { z } from "zod";
|
|
321
|
+
|
|
322
|
+
const heroku = createHerokuProvider();
|
|
323
|
+
|
|
324
|
+
export async function POST(req: Request) {
|
|
325
|
+
const { messages } = await req.json();
|
|
326
|
+
|
|
327
|
+
const result = await streamText({
|
|
328
|
+
model: heroku.chat("claude-3-5-sonnet-latest"),
|
|
329
|
+
messages,
|
|
330
|
+
tools: {
|
|
331
|
+
getTime: tool({
|
|
332
|
+
description: "Get the current time",
|
|
333
|
+
parameters: z.object({
|
|
334
|
+
timezone: z
|
|
335
|
+
.string()
|
|
336
|
+
.optional()
|
|
337
|
+
.describe("Timezone (e.g., 'America/New_York')"),
|
|
338
|
+
}),
|
|
339
|
+
execute: async ({ timezone = "UTC" }) => {
|
|
340
|
+
return {
|
|
341
|
+
time: new Date().toLocaleString("en-US", { timeZone: timezone }),
|
|
342
|
+
timezone,
|
|
343
|
+
};
|
|
344
|
+
},
|
|
345
|
+
}),
|
|
346
|
+
},
|
|
347
|
+
maxSteps: 5,
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
return result.toDataStreamResponse();
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### Express.js
|
|
355
|
+
|
|
356
|
+
```typescript
|
|
357
|
+
import express from "express";
|
|
358
|
+
import { generateText } from "ai";
|
|
359
|
+
import { createHerokuProvider } from "heroku-ai-provider";
|
|
360
|
+
|
|
361
|
+
const app = express();
|
|
362
|
+
const heroku = createHerokuProvider();
|
|
363
|
+
|
|
364
|
+
app.post("/chat", async (req, res) => {
|
|
365
|
+
const { prompt } = req.body;
|
|
366
|
+
|
|
367
|
+
const { text } = await generateText({
|
|
368
|
+
model: heroku.chat("claude-3-haiku"),
|
|
369
|
+
prompt,
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
res.json({ response: text });
|
|
373
|
+
});
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
## Error Handling
|
|
377
|
+
|
|
378
|
+
The provider includes comprehensive error handling with user-friendly error messages:
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
import {
|
|
382
|
+
createHerokuProvider,
|
|
383
|
+
isConfigurationError,
|
|
384
|
+
isTemporaryServiceError,
|
|
385
|
+
} from "heroku-ai-provider";
|
|
386
|
+
|
|
387
|
+
try {
|
|
388
|
+
const heroku = createHerokuProvider();
|
|
389
|
+
const result = await generateText({
|
|
390
|
+
model: heroku.chat("claude-3-5-sonnet-latest"),
|
|
391
|
+
prompt: "Hello!",
|
|
392
|
+
});
|
|
393
|
+
} catch (error) {
|
|
394
|
+
if (isConfigurationError(error)) {
|
|
395
|
+
console.error("Configuration error:", error.message);
|
|
396
|
+
// Handle API key or URL configuration issues
|
|
397
|
+
} else if (isTemporaryServiceError(error)) {
|
|
398
|
+
console.error("Service error:", error.message);
|
|
399
|
+
// Handle temporary service issues (retry logic)
|
|
400
|
+
} else {
|
|
401
|
+
console.error("Unexpected error:", error);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
## Troubleshooting
|
|
407
|
+
|
|
408
|
+
### Common Issues
|
|
409
|
+
|
|
410
|
+
#### Authentication Errors
|
|
411
|
+
|
|
412
|
+
- **Issue**: "Chat API key is required" or "Embeddings API key is required"
|
|
413
|
+
- **Solution**: Ensure your API keys are set in environment variables or passed directly to `createHerokuProvider()`
|
|
414
|
+
|
|
415
|
+
#### Model Not Found
|
|
416
|
+
|
|
417
|
+
- **Issue**: "Unsupported chat model" or "Unsupported embedding model"
|
|
418
|
+
- **Solution**: Check that you're using a supported model from the list above
|
|
419
|
+
|
|
420
|
+
#### Network Errors
|
|
421
|
+
|
|
422
|
+
- **Issue**: Connection timeouts or network failures
|
|
423
|
+
- **Solution**: Verify your internet connection and that Heroku's AI services are accessible
|
|
424
|
+
|
|
425
|
+
#### URL Configuration
|
|
426
|
+
|
|
427
|
+
- **Issue**: "Invalid URL format" errors
|
|
428
|
+
- **Solution**: Ensure custom URLs are valid and use HTTP/HTTPS protocol
|
|
429
|
+
|
|
430
|
+
#### Tool Execution Issues
|
|
431
|
+
|
|
432
|
+
- **Issue**: Tools are called but AI doesn't provide final response
|
|
433
|
+
- **Solution**: Ensure you're using `maxSteps: 5` or higher to allow multi-step tool conversations
|
|
434
|
+
|
|
435
|
+
#### Schema Validation Errors
|
|
436
|
+
|
|
437
|
+
- **Issue**: "Unrecognized request argument" errors when using tools
|
|
438
|
+
- **Solution**: This provider automatically filters out problematic schema properties (like `$schema`) that some validation libraries add
|
|
439
|
+
|
|
440
|
+
## Contributing
|
|
441
|
+
|
|
442
|
+
We welcome contributions! Please follow these steps:
|
|
443
|
+
|
|
444
|
+
1. Fork the repository
|
|
445
|
+
2. Create a feature branch: `git checkout -b feature/amazing-feature`
|
|
446
|
+
3. Make your changes and add tests
|
|
447
|
+
4. Ensure tests pass: `npm test`
|
|
448
|
+
5. Lint your code: `npm run lint`
|
|
449
|
+
6. Commit your changes: `git commit -m 'Add amazing feature'`
|
|
450
|
+
7. Push to the branch: `git push origin feature/amazing-feature`
|
|
451
|
+
8. Open a Pull Request
|
|
452
|
+
|
|
453
|
+
### Development Setup
|
|
454
|
+
|
|
455
|
+
```bash
|
|
456
|
+
# Clone the repository
|
|
457
|
+
git clone https://github.com/julianduque/heroku-ai-provider.git
|
|
458
|
+
cd heroku-ai-provider
|
|
459
|
+
|
|
460
|
+
# Install dependencies
|
|
461
|
+
pnpm install
|
|
462
|
+
|
|
463
|
+
# Run tests
|
|
464
|
+
pnpm test
|
|
465
|
+
|
|
466
|
+
# Build the project
|
|
467
|
+
pnpm build
|
|
468
|
+
|
|
469
|
+
# Lint code
|
|
470
|
+
pnpm lint
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### Testing
|
|
474
|
+
|
|
475
|
+
The project uses Jest for testing. Run tests with:
|
|
476
|
+
|
|
477
|
+
```bash
|
|
478
|
+
# Run all tests
|
|
479
|
+
pnpm test
|
|
480
|
+
|
|
481
|
+
# Run tests in watch mode
|
|
482
|
+
pnpm test --watch
|
|
483
|
+
|
|
484
|
+
# Run tests with coverage
|
|
485
|
+
pnpm test --coverage
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
## License
|
|
489
|
+
|
|
490
|
+
This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.
|
|
491
|
+
|
|
492
|
+
## Resources
|
|
493
|
+
|
|
494
|
+
- 📖 Documentation: [Heroku AI Documentation](https://devcenter.heroku.com/categories/ai)
|
|
495
|
+
- 🐛 Issues: [GitHub Issues](https://github.com/julianduque/heroku-ai-provider/issues)
|
|
496
|
+
|
|
497
|
+
## Related Projects
|
|
498
|
+
|
|
499
|
+
- [Vercel AI SDK](https://sdk.vercel.ai/) - The AI SDK this provider integrates with
|
|
500
|
+
- [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) - Command line interface for Heroku
|
|
501
|
+
- [Heroku AI Services](https://devcenter.heroku.com/categories/ai) - Official Heroku AI documentation
|