0
# Language Models
1
2
Traditional text completion models for legacy workflows and specific use cases requiring the OpenAI Completions API. These models generate text continuations based on prompts.
3
4
## Capabilities
5
6
### OpenAI Class
7
8
The primary language model class providing access to OpenAI's text completion models.
9
10
```typescript { .api }
11
/**
12
* OpenAI text completion model wrapper
13
* Uses the legacy Completions API for text generation
14
*/
15
class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions> extends LLM<CallOptions> {
16
17
constructor(fields?: OpenAIInput & Partial<OpenAIInput>);
18
19
/** Model configuration */
20
model: string; // Default: "gpt-3.5-turbo-instruct"
21
temperature?: number; // Sampling temperature (0-2)
22
maxTokens?: number; // Maximum tokens to generate
23
topP?: number; // Nucleus sampling parameter
24
frequencyPenalty?: number; // Frequency penalty (-2 to 2)
25
presencePenalty?: number; // Presence penalty (-2 to 2)
26
n: number; // Number of completions (default: 1)
27
bestOf?: number; // Generate bestOf completions server-side and return best
28
batchSize: number; // Batch size for multiple prompts (default: 20)
29
streaming: boolean; // Enable streaming (default: false)
30
31
/** Additional parameters */
32
logitBias?: Record<string, number>; // Modify likelihood of specified tokens
33
suffix?: string; // Text to append after completion
34
echo?: boolean; // Echo back the prompt in addition to completion
35
stop?: string | string[]; // Stop sequences
36
37
/** Client configuration */
38
openAIApiKey?: string; // OpenAI API key
39
organization?: string; // OpenAI organization ID
40
baseURL?: string; // Custom base URL
41
timeout?: number; // Request timeout
42
maxRetries?: number; // Maximum retry attempts
43
44
/** Generate text completions for multiple prompts */
45
_generate(
46
prompts: string[],
47
options: CallOptions,
48
runManager?: CallbackManagerForLLMRun
49
): Promise<LLMResult>;
50
51
/** Stream response chunks for a single prompt */
52
_streamResponseChunks(
53
input: string,
54
options: CallOptions,
55
runManager?: CallbackManagerForLLMRun
56
): AsyncIterable<GenerationChunk>;
57
58
/** Make completion requests with retry logic */
59
completionWithRetry<T>(
60
request: OpenAIClient.CompletionCreateParamsStreaming,
61
options?: OpenAICallOptions
62
): Promise<T>;
63
64
/** Get number of tokens in text */
65
getNumTokens(text: string): Promise<number>;
66
}
67
```
68
69
### OpenAI Call Options
70
71
Runtime options for completion generation.
72
73
```typescript { .api }
74
interface OpenAICallOptions extends BaseOpenAICallOptions {
75
/** Stop sequences to end generation */
76
stop?: string | string[];
77
78
/** Custom user identifier for abuse monitoring */
79
user?: string;
80
81
/** Deterministic sampling seed */
82
seed?: number;
83
84
/** Logit bias for token probabilities */
85
logit_bias?: Record<string, number>;
86
}
87
```
88
89
## Usage Examples
90
91
### Basic Text Completion
92
93
```typescript
94
import { OpenAI } from "@langchain/openai";
95
96
const llm = new OpenAI({
97
model: "gpt-3.5-turbo-instruct",
98
temperature: 0.7,
99
maxTokens: 100,
100
apiKey: process.env.OPENAI_API_KEY
101
});
102
103
// Single completion
104
const result = await llm.invoke("The future of artificial intelligence is");
105
console.log(result);
106
107
// Multiple completions
108
const results = await llm.generate([
109
"The capital of France is",
110
"The largest planet in our solar system is",
111
"The inventor of the telephone was"
112
]);
113
114
results.generations.forEach((generation, index) => {
115
console.log(`Prompt ${index + 1}: ${generation[0].text}`);
116
});
117
```
118
119
### Streaming Completions
120
121
```typescript
122
const streamingLLM = new OpenAI({
123
model: "gpt-3.5-turbo-instruct",
124
streaming: true,
125
temperature: 0.5,
126
maxTokens: 200
127
});
128
129
const stream = await streamingLLM.stream("Write a short poem about the ocean:");
130
131
for await (const chunk of stream) {
132
process.stdout.write(chunk);
133
}
134
```
135
136
### Advanced Configuration
137
138
```typescript
139
const advancedLLM = new OpenAI({
140
model: "gpt-3.5-turbo-instruct",
141
142
// Generation parameters
143
temperature: 0.8,
144
maxTokens: 500,
145
topP: 0.95,
146
frequencyPenalty: 0.2,
147
presencePenalty: 0.1,
148
149
// Multiple completions
150
n: 3, // Generate 3 completions
151
bestOf: 5, // Consider 5 completions, return best 3
152
153
// Control generation
154
stop: ["\n\n", "###"], // Stop at double newline or ###
155
suffix: "\n\nEnd of response.", // Append to completions
156
echo: false, // Don't echo prompt back
157
158
// Token probability modification
159
logitBias: {
160
"50256": -100 // Strongly discourage <|endoftext|> token
161
},
162
163
// Performance settings
164
batchSize: 10, // Process 10 prompts at once
165
timeout: 30000, // 30 second timeout
166
maxRetries: 2
167
});
168
169
const prompts = [
170
"Explain quantum computing:",
171
"Describe machine learning:",
172
"What is blockchain technology:"
173
];
174
175
const results = await advancedLLM.generate(prompts);
176
```
177
178
### Custom Stop Sequences
179
180
```typescript
181
const llm = new OpenAI({
182
model: "gpt-3.5-turbo-instruct",
183
temperature: 0.3,
184
maxTokens: 150
185
});
186
187
// Stop at specific sequences
188
const result = await llm.invoke(
189
"List the planets in our solar system:\n1.",
190
{
191
stop: ["\n10.", "Pluto"] // Stop before item 10 or if Pluto is mentioned
192
}
193
);
194
```
195
196
### Batch Processing
197
198
```typescript
199
const batchLLM = new OpenAI({
200
model: "gpt-3.5-turbo-instruct",
201
batchSize: 5, // Process 5 prompts per API call
202
temperature: 0.2,
203
maxTokens: 50
204
});
205
206
// Generate many completions efficiently
207
const prompts = Array.from({ length: 20 }, (_, i) =>
208
`Complete this sentence about topic ${i + 1}:`
209
);
210
211
const results = await batchLLM.generate(prompts);
212
213
// Access individual results
214
results.generations.forEach((generation, index) => {
215
console.log(`Result ${index + 1}: ${generation[0].text.trim()}`);
216
});
217
218
// Access metadata
219
console.log(`Total tokens used: ${results.llmOutput?.tokenUsage?.totalTokens}`);
220
```
221
222
### Error Handling and Retries
223
224
```typescript
225
import { wrapOpenAIClientError } from "@langchain/openai";
226
227
const llm = new OpenAI({
228
model: "gpt-3.5-turbo-instruct",
229
maxRetries: 3,
230
timeout: 10000
231
});
232
233
try {
234
const result = await llm.invoke("Generate a creative story:");
235
console.log(result);
236
} catch (error) {
237
// Errors are automatically wrapped
238
console.error("Generation failed:", error.message);
239
240
if (error.code === 'rate_limit_exceeded') {
241
console.log("Rate limit hit, waiting before retry...");
242
// Implement exponential backoff
243
}
244
}
245
```
246
247
### Custom Prompting Patterns
248
249
```typescript
250
const llm = new OpenAI({
251
model: "gpt-3.5-turbo-instruct",
252
temperature: 0.1,
253
maxTokens: 200
254
});
255
256
// Few-shot prompting
257
const fewShotPrompt = `
258
Translate English to French:
259
260
English: Hello, how are you?
261
French: Bonjour, comment allez-vous?
262
263
English: What time is it?
264
French: Quelle heure est-il?
265
266
English: I love programming.
267
French:`;
268
269
const translation = await llm.invoke(fewShotPrompt);
270
271
// Chain of thought prompting
272
const reasoningPrompt = `
273
Solve this step by step:
274
If a train travels 60 mph for 2.5 hours, how far does it travel?
275
276
Step 1:`;
277
278
const reasoning = await llm.invoke(reasoningPrompt, {
279
stop: ["Step 4:", "\n\nAnswer:"]
280
});
281
```
282
283
### Model-Specific Features
284
285
```typescript
286
// Using the instruct model for instruction following
287
const instructLLM = new OpenAI({
288
model: "gpt-3.5-turbo-instruct",
289
temperature: 0,
290
maxTokens: 300
291
});
292
293
const instruction = `
294
Instructions: Summarize the following text in exactly 3 bullet points.
295
296
Text: Artificial intelligence (AI) is intelligence demonstrated by machines,
297
in contrast to natural intelligence displayed by animals including humans.
298
AI research has been defined as the field of study of intelligent agents,
299
which refers to any system that perceives its environment and takes actions
300
that maximize its chance of achieving its goals.
301
302
Summary:
303
•`;
304
305
const summary = await instructLLM.invoke(instruction);
306
307
// Using suffix for completion tasks
308
const completionLLM = new OpenAI({
309
model: "gpt-3.5-turbo-instruct",
310
temperature: 0.7,
311
maxTokens: 50,
312
suffix: "\n\nThis is a creative writing exercise."
313
});
314
315
const story = await completionLLM.invoke("Once upon a time in a distant galaxy");
316
```
317
318
## Model Support
319
320
### Available Models
321
322
The OpenAI LLM class primarily supports:
323
324
- **GPT-3.5 Turbo Instruct**: `gpt-3.5-turbo-instruct` (recommended)
325
- **Legacy Models**: Various GPT-3 models (deprecated)
326
327
Note: Most modern OpenAI models (GPT-4, GPT-4o, etc.) are chat models and should use the `ChatOpenAI` class instead.
328
329
### Migration from Legacy Models
330
331
```typescript
332
// Old approach with legacy models
333
const oldLLM = new OpenAI({
334
model: "text-davinci-003", // Deprecated
335
temperature: 0.7
336
});
337
338
// Modern approach - use ChatOpenAI for better performance
339
import { ChatOpenAI } from "@langchain/openai";
340
341
const modernModel = new ChatOpenAI({
342
model: "gpt-3.5-turbo",
343
temperature: 0.7
344
});
345
346
// Convert completion-style prompts to chat format
347
const prompt = "Explain quantum computing:";
348
349
// Legacy style
350
const legacyResult = await oldLLM.invoke(prompt);
351
352
// Modern style
353
const modernResult = await modernModel.invoke(prompt);
354
```
355
356
### Performance Considerations
357
358
```typescript
359
// Optimize for throughput
360
const highThroughputLLM = new OpenAI({
361
model: "gpt-3.5-turbo-instruct",
362
batchSize: 20, // Process many prompts together
363
maxTokens: 100, // Limit response length
364
temperature: 0.3 // More deterministic responses
365
});
366
367
// Optimize for creativity
368
const creativeLLM = new OpenAI({
369
model: "gpt-3.5-turbo-instruct",
370
temperature: 0.9, // More creative responses
371
topP: 0.8, // Focus on top probability mass
372
frequencyPenalty: 0.5, // Reduce repetition
373
presencePenalty: 0.3 // Encourage topic diversity
374
});
375
```
376
377
## Best Practices
378
379
### When to Use Language Models vs Chat Models
380
381
**Use OpenAI LLM (Completions API) for:**
382
- Text completion tasks
383
- Legacy system integration
384
- Simple prompt-response patterns
385
- Code completion
386
- Creative writing continuations
387
388
**Use ChatOpenAI (Chat Completions API) for:**
389
- Conversational applications
390
- Function calling
391
- Structured output
392
- Multi-turn dialogues
393
- Modern applications (recommended)
394
395
### Prompt Engineering
396
397
```typescript
398
const llm = new OpenAI({
399
model: "gpt-3.5-turbo-instruct",
400
temperature: 0.2,
401
maxTokens: 150
402
});
403
404
// Good: Clear, specific prompt with context
405
const goodPrompt = `
406
Task: Write a professional email subject line
407
Context: Scheduling a team meeting for next week
408
Requirements: Under 10 words, clear and actionable
409
Subject:`;
410
411
// Better: Include examples for consistency
412
const betterPrompt = `
413
Write professional email subject lines:
414
415
Meeting: Weekly team standup
416
Subject: Weekly Team Standup - Tuesday 2PM
417
418
Meeting: Project review session
419
Subject: Q3 Project Review - Friday 3PM
420
421
Meeting: Planning for next quarter
422
Subject:`;
423
424
const result = await llm.invoke(betterPrompt);
425
```