chub-dev 0.1.0 → 0.1.2-beta.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/README.md +55 -0
- package/bin/chub-mcp +2 -0
- package/dist/airtable/docs/database/javascript/DOC.md +1437 -0
- package/dist/airtable/docs/database/python/DOC.md +1735 -0
- package/dist/amplitude/docs/analytics/javascript/DOC.md +1282 -0
- package/dist/amplitude/docs/analytics/python/DOC.md +1199 -0
- package/dist/anthropic/docs/claude-api/javascript/DOC.md +503 -0
- package/dist/anthropic/docs/claude-api/python/DOC.md +389 -0
- package/dist/asana/docs/tasks/DOC.md +1396 -0
- package/dist/assemblyai/docs/transcription/DOC.md +1043 -0
- package/dist/atlassian/docs/confluence/javascript/DOC.md +1347 -0
- package/dist/atlassian/docs/confluence/python/DOC.md +1604 -0
- package/dist/auth0/docs/identity/javascript/DOC.md +968 -0
- package/dist/auth0/docs/identity/python/DOC.md +1199 -0
- package/dist/aws/docs/s3/javascript/DOC.md +1773 -0
- package/dist/aws/docs/s3/python/DOC.md +1807 -0
- package/dist/binance/docs/trading/javascript/DOC.md +1315 -0
- package/dist/binance/docs/trading/python/DOC.md +1454 -0
- package/dist/braintree/docs/gateway/javascript/DOC.md +1278 -0
- package/dist/braintree/docs/gateway/python/DOC.md +1179 -0
- package/dist/chromadb/docs/embeddings-db/javascript/DOC.md +1263 -0
- package/dist/chromadb/docs/embeddings-db/python/DOC.md +1707 -0
- package/dist/clerk/docs/auth/javascript/DOC.md +1220 -0
- package/dist/clerk/docs/auth/python/DOC.md +274 -0
- package/dist/cloudflare/docs/workers/javascript/DOC.md +918 -0
- package/dist/cloudflare/docs/workers/python/DOC.md +994 -0
- package/dist/cockroachdb/docs/distributed-db/DOC.md +1500 -0
- package/dist/cohere/docs/llm/DOC.md +1335 -0
- package/dist/datadog/docs/monitoring/javascript/DOC.md +1740 -0
- package/dist/datadog/docs/monitoring/python/DOC.md +1815 -0
- package/dist/deepgram/docs/speech/javascript/DOC.md +885 -0
- package/dist/deepgram/docs/speech/python/DOC.md +685 -0
- package/dist/deepl/docs/translation/javascript/DOC.md +887 -0
- package/dist/deepl/docs/translation/python/DOC.md +944 -0
- package/dist/deepseek/docs/llm/DOC.md +1220 -0
- package/dist/directus/docs/headless-cms/javascript/DOC.md +1128 -0
- package/dist/directus/docs/headless-cms/python/DOC.md +1276 -0
- package/dist/discord/docs/bot/javascript/DOC.md +1090 -0
- package/dist/discord/docs/bot/python/DOC.md +1130 -0
- package/dist/elasticsearch/docs/search/DOC.md +1634 -0
- package/dist/elevenlabs/docs/text-to-speech/javascript/DOC.md +336 -0
- package/dist/elevenlabs/docs/text-to-speech/python/DOC.md +552 -0
- package/dist/firebase/docs/auth/DOC.md +1015 -0
- package/dist/gemini/docs/genai/javascript/DOC.md +691 -0
- package/dist/gemini/docs/genai/python/DOC.md +555 -0
- package/dist/github/docs/octokit/DOC.md +1560 -0
- package/dist/google/docs/bigquery/javascript/DOC.md +1688 -0
- package/dist/google/docs/bigquery/python/DOC.md +1503 -0
- package/dist/hubspot/docs/crm/javascript/DOC.md +1805 -0
- package/dist/hubspot/docs/crm/python/DOC.md +2033 -0
- package/dist/huggingface/docs/transformers/DOC.md +948 -0
- package/dist/intercom/docs/messaging/javascript/DOC.md +1844 -0
- package/dist/intercom/docs/messaging/python/DOC.md +1797 -0
- package/dist/jira/docs/issues/javascript/DOC.md +1420 -0
- package/dist/jira/docs/issues/python/DOC.md +1492 -0
- package/dist/kafka/docs/streaming/javascript/DOC.md +1671 -0
- package/dist/kafka/docs/streaming/python/DOC.md +1464 -0
- package/dist/landingai-ade/docs/api/DOC.md +620 -0
- package/dist/landingai-ade/docs/sdk/python/DOC.md +489 -0
- package/dist/landingai-ade/docs/sdk/typescript/DOC.md +542 -0
- package/dist/landingai-ade/skills/SKILL.md +489 -0
- package/dist/launchdarkly/docs/feature-flags/javascript/DOC.md +1191 -0
- package/dist/launchdarkly/docs/feature-flags/python/DOC.md +1671 -0
- package/dist/linear/docs/tracker/DOC.md +1554 -0
- package/dist/livekit/docs/realtime/javascript/DOC.md +303 -0
- package/dist/livekit/docs/realtime/python/DOC.md +163 -0
- package/dist/mailchimp/docs/marketing/DOC.md +1420 -0
- package/dist/meilisearch/docs/search/DOC.md +1241 -0
- package/dist/microsoft/docs/onedrive/javascript/DOC.md +1421 -0
- package/dist/microsoft/docs/onedrive/python/DOC.md +1549 -0
- package/dist/mongodb/docs/atlas/DOC.md +2041 -0
- package/dist/notion/docs/workspace-api/javascript/DOC.md +1435 -0
- package/dist/notion/docs/workspace-api/python/DOC.md +1400 -0
- package/dist/okta/docs/identity/javascript/DOC.md +1171 -0
- package/dist/okta/docs/identity/python/DOC.md +1401 -0
- package/dist/openai/docs/chat/javascript/DOC.md +407 -0
- package/dist/openai/docs/chat/python/DOC.md +568 -0
- package/dist/paypal/docs/checkout/DOC.md +278 -0
- package/dist/pinecone/docs/sdk/javascript/DOC.md +984 -0
- package/dist/pinecone/docs/sdk/python/DOC.md +1395 -0
- package/dist/plaid/docs/banking/javascript/DOC.md +1163 -0
- package/dist/plaid/docs/banking/python/DOC.md +1203 -0
- package/dist/playwright-community/skills/login-flows/SKILL.md +108 -0
- package/dist/postmark/docs/transactional-email/DOC.md +1168 -0
- package/dist/prisma/docs/orm/javascript/DOC.md +1419 -0
- package/dist/prisma/docs/orm/python/DOC.md +1317 -0
- package/dist/qdrant/docs/vector-search/javascript/DOC.md +1221 -0
- package/dist/qdrant/docs/vector-search/python/DOC.md +1653 -0
- package/dist/rabbitmq/docs/message-queue/javascript/DOC.md +1193 -0
- package/dist/rabbitmq/docs/message-queue/python/DOC.md +1243 -0
- package/dist/razorpay/docs/payments/javascript/DOC.md +1219 -0
- package/dist/razorpay/docs/payments/python/DOC.md +1330 -0
- package/dist/redis/docs/key-value/javascript/DOC.md +1851 -0
- package/dist/redis/docs/key-value/python/DOC.md +2054 -0
- package/dist/registry.json +2817 -0
- package/dist/replicate/docs/model-hosting/DOC.md +1318 -0
- package/dist/resend/docs/email/DOC.md +1271 -0
- package/dist/salesforce/docs/crm/javascript/DOC.md +1241 -0
- package/dist/salesforce/docs/crm/python/DOC.md +1183 -0
- package/dist/search-index.json +1 -0
- package/dist/sendgrid/docs/email-api/javascript/DOC.md +371 -0
- package/dist/sendgrid/docs/email-api/python/DOC.md +656 -0
- package/dist/sentry/docs/error-tracking/javascript/DOC.md +1073 -0
- package/dist/sentry/docs/error-tracking/python/DOC.md +1309 -0
- package/dist/shopify/docs/storefront/DOC.md +457 -0
- package/dist/slack/docs/workspace/javascript/DOC.md +933 -0
- package/dist/slack/docs/workspace/python/DOC.md +271 -0
- package/dist/square/docs/payments/javascript/DOC.md +1855 -0
- package/dist/square/docs/payments/python/DOC.md +1728 -0
- package/dist/stripe/docs/api/DOC.md +1727 -0
- package/dist/stripe/docs/payments/DOC.md +1726 -0
- package/dist/stytch/docs/auth/javascript/DOC.md +1813 -0
- package/dist/stytch/docs/auth/python/DOC.md +1962 -0
- package/dist/supabase/docs/client/DOC.md +1606 -0
- package/dist/twilio/docs/messaging/python/DOC.md +469 -0
- package/dist/twilio/docs/messaging/typescript/DOC.md +946 -0
- package/dist/vercel/docs/platform/DOC.md +1940 -0
- package/dist/weaviate/docs/vector-db/javascript/DOC.md +1268 -0
- package/dist/weaviate/docs/vector-db/python/DOC.md +1388 -0
- package/dist/zendesk/docs/support/javascript/DOC.md +2150 -0
- package/dist/zendesk/docs/support/python/DOC.md +2297 -0
- package/package.json +22 -6
- package/skills/get-api-docs/SKILL.md +84 -0
- package/src/commands/annotate.js +83 -0
- package/src/commands/build.js +12 -1
- package/src/commands/feedback.js +150 -0
- package/src/commands/get.js +83 -42
- package/src/commands/search.js +7 -0
- package/src/index.js +43 -17
- package/src/lib/analytics.js +90 -0
- package/src/lib/annotations.js +57 -0
- package/src/lib/bm25.js +170 -0
- package/src/lib/cache.js +69 -6
- package/src/lib/config.js +8 -3
- package/src/lib/identity.js +99 -0
- package/src/lib/registry.js +103 -20
- package/src/lib/telemetry.js +86 -0
- package/src/mcp/server.js +177 -0
- package/src/mcp/tools.js +251 -0
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: chat
|
|
3
|
+
description: "OpenAI API for text generation, chat completions, streaming, function calling, vision, embeddings, and assistants"
|
|
4
|
+
metadata:
|
|
5
|
+
languages: "javascript"
|
|
6
|
+
versions: "6.27.0"
|
|
7
|
+
updated-on: "2026-03-06"
|
|
8
|
+
source: maintainer
|
|
9
|
+
tags: "openai,chat,llm,ai"
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# OpenAI API Coding Guidelines (JavaScript/TypeScript)
|
|
13
|
+
|
|
14
|
+
You are an OpenAI API coding expert. Help me with writing code using the OpenAI API calling the official libraries and SDKs.
|
|
15
|
+
|
|
16
|
+
## Golden Rule: Use the Correct and Current SDK
|
|
17
|
+
|
|
18
|
+
Always use the official OpenAI Node.js SDK for all OpenAI API interactions.
|
|
19
|
+
|
|
20
|
+
- **Library Name:** OpenAI Node.js SDK
|
|
21
|
+
- **NPM Package:** `openai`
|
|
22
|
+
- **JSR Package:** `@openai/openai`
|
|
23
|
+
|
|
24
|
+
**Installation:**
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# NPM
|
|
28
|
+
npm install openai
|
|
29
|
+
|
|
30
|
+
# JSR (Deno/Node.js)
|
|
31
|
+
deno add jsr:@openai/openai
|
|
32
|
+
npx jsr add @openai/openai
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Import Patterns:**
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
// Correct - ES6 import
|
|
39
|
+
import OpenAI from 'openai';
|
|
40
|
+
|
|
41
|
+
// Correct - with additional utilities
|
|
42
|
+
import OpenAI, { toFile } from 'openai';
|
|
43
|
+
|
|
44
|
+
// JSR import for Deno
|
|
45
|
+
import OpenAI from 'jsr:@openai/openai';
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Initialization and API Key
|
|
49
|
+
|
|
50
|
+
The OpenAI library requires creating an `OpenAI` client instance for all API calls.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import OpenAI from 'openai';
|
|
54
|
+
|
|
55
|
+
// Uses OPENAI_API_KEY environment variable automatically
|
|
56
|
+
const client = new OpenAI({
|
|
57
|
+
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Or pass API key directly
|
|
61
|
+
const client = new OpenAI({
|
|
62
|
+
apiKey: 'your-api-key-here'
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Models (as of March 2026)
|
|
67
|
+
|
|
68
|
+
Default choices:
|
|
69
|
+
- **General Text Tasks:** `gpt-5.4` (frontier) or `gpt-4.1` (non-reasoning)
|
|
70
|
+
- **Complex Reasoning Tasks:** `gpt-5.4` or `gpt-5.4-pro`
|
|
71
|
+
- **Fast & Cost-Efficient:** `gpt-5-mini` or `gpt-4.1-mini`
|
|
72
|
+
- **Cheapest / Fastest:** `gpt-5-nano` or `gpt-4.1-nano`
|
|
73
|
+
- **Audio Processing:** `gpt-audio` or `gpt-audio-mini`
|
|
74
|
+
- **Vision Tasks:** `gpt-5.4` or `gpt-4.1`
|
|
75
|
+
- **Agentic Coding:** `gpt-5.3-codex`
|
|
76
|
+
- **Search (Chat Completions):** `gpt-5-search-api`, `gpt-4o-search-preview`, or `gpt-4o-mini-search-preview`
|
|
77
|
+
|
|
78
|
+
Frontier (reasoning, configurable effort):
|
|
79
|
+
- `gpt-5.4`, `gpt-5.4-2026-03-05`, `gpt-5.4-pro`, `gpt-5.4-pro-2026-03-05`
|
|
80
|
+
- `gpt-5.2`, `gpt-5.2-2025-12-11`, `gpt-5.2-pro`
|
|
81
|
+
- `gpt-5.1`, `gpt-5.1-2025-11-13`, `gpt-5.1-pro`
|
|
82
|
+
- `gpt-5`, `gpt-5-2025-08-07`, `gpt-5-pro`
|
|
83
|
+
- `gpt-5-mini`, `gpt-5-mini-2025-08-07`
|
|
84
|
+
- `gpt-5-nano`, `gpt-5-nano-2025-08-07`
|
|
85
|
+
|
|
86
|
+
Non-reasoning:
|
|
87
|
+
- `gpt-4.1`, `gpt-4.1-2025-04-14`
|
|
88
|
+
- `gpt-4.1-mini`, `gpt-4.1-mini-2025-04-14`
|
|
89
|
+
- `gpt-4.1-nano`, `gpt-4.1-nano-2025-04-14`
|
|
90
|
+
|
|
91
|
+
Reasoning (o-series, succeeded by GPT-5):
|
|
92
|
+
- `o3`, `o3-2025-04-16`, `o3-pro`, `o3-pro-2025-06-10`
|
|
93
|
+
- `o4-mini`, `o4-mini-2025-04-16`
|
|
94
|
+
- `o3-mini`, `o3-mini-2025-01-31`
|
|
95
|
+
- `o1`, `o1-2024-12-17`
|
|
96
|
+
|
|
97
|
+
Deep research: `o3-deep-research`, `o4-mini-deep-research`
|
|
98
|
+
|
|
99
|
+
Codex (agentic coding, Responses API only):
|
|
100
|
+
- `gpt-5.3-codex`, `gpt-5.2-codex`, `gpt-5.1-codex`, `gpt-5.1-codex-max`, `gpt-5.1-codex-mini`, `gpt-5-codex`
|
|
101
|
+
|
|
102
|
+
Audio chat: `gpt-audio`, `gpt-audio-2025-08-28`, `gpt-audio-mini`
|
|
103
|
+
Realtime: `gpt-realtime`, `gpt-realtime-2025-08-28`, `gpt-realtime-mini`
|
|
104
|
+
TTS: `gpt-4o-mini-tts`, `gpt-4o-mini-tts-2025-12-15`, `tts-1`, `tts-1-hd`
|
|
105
|
+
STT: `gpt-4o-transcribe`, `gpt-4o-mini-transcribe`, `gpt-4o-mini-transcribe-2025-12-15`, `gpt-4o-transcribe-diarize`, `whisper-1`
|
|
106
|
+
Image generation: `gpt-image-1.5`, `gpt-image-1.5-2025-12-16`, `gpt-image-1`, `gpt-image-1-mini`, `chatgpt-image-latest`
|
|
107
|
+
Embeddings: `text-embedding-3-large`, `text-embedding-3-small`, `text-embedding-ada-002`
|
|
108
|
+
Moderation: `omni-moderation-latest`
|
|
109
|
+
Search (Chat Completions only): `gpt-5-search-api`, `gpt-4o-search-preview`, `gpt-4o-mini-search-preview`
|
|
110
|
+
|
|
111
|
+
Legacy (still available): `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo`, `gpt-3.5-turbo`
|
|
112
|
+
|
|
113
|
+
Deprecated (shutdown scheduled):
|
|
114
|
+
- `dall-e-3`, `dall-e-2` → May 12, 2026 (use `gpt-image-1`)
|
|
115
|
+
- `o1-preview`, `o1-mini` → deprecated (use `o3` or `gpt-5`)
|
|
116
|
+
- `codex-mini-latest` → shut down Feb 12, 2026
|
|
117
|
+
- `chatgpt-4o-latest` → shut down Feb 17, 2026
|
|
118
|
+
- `gpt-4o-realtime-preview` → Mar 24, 2026 (use `gpt-realtime`)
|
|
119
|
+
- `gpt-4o-mini-audio-preview` → Mar 24, 2026 (use `gpt-audio-mini`)
|
|
120
|
+
- `gpt-4.5-preview` → deprecated
|
|
121
|
+
- Assistants API → sunset Aug 26, 2026 (migrate to Responses API)
|
|
122
|
+
|
|
123
|
+
## Primary APIs
|
|
124
|
+
|
|
125
|
+
### Responses API (Recommended)
|
|
126
|
+
|
|
127
|
+
The Responses API is the primary interface for text generation.
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import OpenAI from 'openai';
|
|
131
|
+
|
|
132
|
+
const client = new OpenAI({
|
|
133
|
+
apiKey: process.env['OPENAI_API_KEY'],
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const response = await client.responses.create({
|
|
137
|
+
model: 'gpt-5.4',
|
|
138
|
+
instructions: 'You are a coding assistant that talks like a pirate',
|
|
139
|
+
input: 'Are semicolons optional in JavaScript?',
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
console.log(response.output_text);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Chat Completions API (Legacy but Supported)
|
|
146
|
+
|
|
147
|
+
The Chat Completions API remains fully supported for existing applications.
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import OpenAI from 'openai';
|
|
151
|
+
|
|
152
|
+
const client = new OpenAI({
|
|
153
|
+
apiKey: process.env['OPENAI_API_KEY'],
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const completion = await client.chat.completions.create({
|
|
157
|
+
model: 'gpt-5.4',
|
|
158
|
+
messages: [
|
|
159
|
+
{ role: 'developer', content: 'Talk like a pirate.' },
|
|
160
|
+
{ role: 'user', content: 'Are semicolons optional in JavaScript?' },
|
|
161
|
+
],
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
console.log(completion.choices[0].message.content);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## API Resources Structure
|
|
168
|
+
|
|
169
|
+
The OpenAI client organizes endpoints into logical resource groupings:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
// Core API resources available on client
|
|
173
|
+
client.completions // Text completions
|
|
174
|
+
client.chat // Chat completions
|
|
175
|
+
client.embeddings // Text embeddings
|
|
176
|
+
client.files // File management
|
|
177
|
+
client.images // Image generation
|
|
178
|
+
client.audio // Audio processing
|
|
179
|
+
client.moderations // Content moderation
|
|
180
|
+
client.models // Model information
|
|
181
|
+
client.fineTuning // Fine-tuning jobs
|
|
182
|
+
client.graders // Model evaluation
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Streaming Responses
|
|
186
|
+
|
|
187
|
+
Both Responses and Chat Completions APIs support streaming for real-time output.
|
|
188
|
+
|
|
189
|
+
### Responses API Streaming
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import OpenAI from 'openai';
|
|
193
|
+
|
|
194
|
+
const client = new OpenAI();
|
|
195
|
+
|
|
196
|
+
const stream = await client.responses.create({
|
|
197
|
+
model: 'gpt-5.4',
|
|
198
|
+
input: 'Say "Sheep sleep deep" ten times fast!',
|
|
199
|
+
stream: true,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
for await (const event of stream) {
|
|
203
|
+
console.log(event);
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Chat Completions Streaming
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
const stream = await client.chat.completions.create({
|
|
211
|
+
model: 'gpt-5.4',
|
|
212
|
+
messages: [{ role: 'user', content: 'Count to 10' }],
|
|
213
|
+
stream: true,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
for await (const chunk of stream) {
|
|
217
|
+
process.stdout.write(chunk.choices[0]?.delta?.content || '');
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## File Uploads
|
|
222
|
+
|
|
223
|
+
The library supports multiple file upload formats for various use cases.
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import fs from 'fs';
|
|
227
|
+
import OpenAI, { toFile } from 'openai';
|
|
228
|
+
|
|
229
|
+
const client = new OpenAI();
|
|
230
|
+
|
|
231
|
+
// Method 1: Node.js fs.ReadStream (recommended for Node.js)
|
|
232
|
+
await client.files.create({
|
|
233
|
+
file: fs.createReadStream('input.jsonl'),
|
|
234
|
+
purpose: 'fine-tune'
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Method 2: Web File API
|
|
238
|
+
await client.files.create({
|
|
239
|
+
file: new File(['my bytes'], 'input.jsonl'),
|
|
240
|
+
purpose: 'fine-tune'
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// Method 3: Fetch Response
|
|
244
|
+
await client.files.create({
|
|
245
|
+
file: await fetch('https://somesite/input.jsonl'),
|
|
246
|
+
purpose: 'fine-tune'
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// Method 4: toFile helper utility
|
|
250
|
+
await client.files.create({
|
|
251
|
+
file: await toFile(Buffer.from('my bytes'), 'input.jsonl'),
|
|
252
|
+
purpose: 'fine-tune',
|
|
253
|
+
});
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Advanced Configuration
|
|
257
|
+
|
|
258
|
+
### Function Calling (Tools)
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
const completion = await client.chat.completions.create({
|
|
262
|
+
model: 'gpt-5.4',
|
|
263
|
+
messages: [{ role: 'user', content: 'What is the weather like today?' }],
|
|
264
|
+
tools: [
|
|
265
|
+
{
|
|
266
|
+
type: 'function',
|
|
267
|
+
function: {
|
|
268
|
+
name: 'get_current_weather',
|
|
269
|
+
description: 'Get the current weather in a given location',
|
|
270
|
+
parameters: {
|
|
271
|
+
type: 'object',
|
|
272
|
+
properties: {
|
|
273
|
+
location: {
|
|
274
|
+
type: 'string',
|
|
275
|
+
description: 'The city and state, e.g. San Francisco, CA',
|
|
276
|
+
},
|
|
277
|
+
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
|
|
278
|
+
},
|
|
279
|
+
required: ['location'],
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
],
|
|
284
|
+
tool_choice: 'auto',
|
|
285
|
+
});
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Temperature and Sampling Parameters
|
|
289
|
+
|
|
290
|
+
Configure model behavior using parameters in the chat completions API:
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
const completion = await client.chat.completions.create({
|
|
294
|
+
model: 'gpt-5.4',
|
|
295
|
+
messages: [{ role: 'user', content: 'Write a creative story' }],
|
|
296
|
+
temperature: 0.8, // Higher = more creative (0-2)
|
|
297
|
+
max_tokens: 1000, // Maximum response length
|
|
298
|
+
top_p: 0.9, // Nucleus sampling
|
|
299
|
+
frequency_penalty: 0.1, // Reduce repetition
|
|
300
|
+
presence_penalty: 0.1, // Encourage new topics
|
|
301
|
+
});
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Structured Outputs (JSON Mode)
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
const completion = await client.chat.completions.create({
|
|
308
|
+
model: 'gpt-5.4',
|
|
309
|
+
messages: [
|
|
310
|
+
{ role: 'user', content: 'Extract the name and age from: "John is 30 years old"' }
|
|
311
|
+
],
|
|
312
|
+
response_format: {
|
|
313
|
+
type: 'json_object'
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
const result = JSON.parse(completion.choices[0].message.content);
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## Error Handling
|
|
321
|
+
|
|
322
|
+
The library provides specific error types for different failure scenarios:
|
|
323
|
+
|
|
324
|
+
```typescript
|
|
325
|
+
import OpenAI from 'openai';
|
|
326
|
+
|
|
327
|
+
const client = new OpenAI();
|
|
328
|
+
|
|
329
|
+
try {
|
|
330
|
+
const completion = await client.chat.completions.create({
|
|
331
|
+
model: 'gpt-5.4',
|
|
332
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
333
|
+
});
|
|
334
|
+
} catch (error) {
|
|
335
|
+
if (error instanceof OpenAI.RateLimitError) {
|
|
336
|
+
console.log('Rate limit exceeded');
|
|
337
|
+
} else if (error instanceof OpenAI.AuthenticationError) {
|
|
338
|
+
console.log('Invalid API key');
|
|
339
|
+
} else if (error instanceof OpenAI.APIError) {
|
|
340
|
+
console.log(error.status); // HTTP status code
|
|
341
|
+
console.log(error.name); // Error name
|
|
342
|
+
console.log(error.headers); // Response headers
|
|
343
|
+
} else {
|
|
344
|
+
console.log('Unexpected error:', error);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
## Common Patterns
|
|
350
|
+
|
|
351
|
+
### Retry Logic with Exponential Backoff
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
async function createCompletionWithRetry(messages: any[], maxRetries = 3) {
|
|
355
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
356
|
+
try {
|
|
357
|
+
return await client.chat.completions.create({
|
|
358
|
+
model: 'gpt-5.4',
|
|
359
|
+
messages,
|
|
360
|
+
});
|
|
361
|
+
} catch (error) {
|
|
362
|
+
if (error instanceof OpenAI.RateLimitError && attempt < maxRetries) {
|
|
363
|
+
const delay = Math.pow(2, attempt) * 1000; // Exponential backoff
|
|
364
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
throw error;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### Conversation Management
|
|
374
|
+
|
|
375
|
+
```typescript
|
|
376
|
+
class ChatSession {
|
|
377
|
+
private messages: OpenAI.Chat.ChatCompletionMessageParam[] = [];
|
|
378
|
+
|
|
379
|
+
constructor(private client: OpenAI, systemPrompt?: string) {
|
|
380
|
+
if (systemPrompt) {
|
|
381
|
+
this.messages.push({ role: 'system', content: systemPrompt });
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async sendMessage(content: string) {
|
|
386
|
+
this.messages.push({ role: 'user', content });
|
|
387
|
+
|
|
388
|
+
const completion = await this.client.chat.completions.create({
|
|
389
|
+
model: 'gpt-5.4',
|
|
390
|
+
messages: this.messages,
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
const response = completion.choices[0].message;
|
|
394
|
+
this.messages.push(response);
|
|
395
|
+
|
|
396
|
+
return response.content;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
## Useful Links
|
|
402
|
+
|
|
403
|
+
- **Documentation:** https://platform.openai.com/docs/api-reference
|
|
404
|
+
- **API Keys:** https://platform.openai.com/api-keys
|
|
405
|
+
- **Models:** https://platform.openai.com/docs/models
|
|
406
|
+
- **Pricing:** https://openai.com/pricing
|
|
407
|
+
- **Rate Limits:** https://platform.openai.com/docs/guides/rate-limits
|