CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-langchain--openai

OpenAI integrations for LangChain.js providing chat models, embeddings, tools, and Azure support.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

language-models.mddocs/

Language Models

Traditional text completion models for legacy workflows and specific use cases requiring the OpenAI Completions API. These models generate text continuations based on prompts.

Capabilities

OpenAI Class

The primary language model class providing access to OpenAI's text completion models.

/**
 * OpenAI text completion model wrapper
 * Uses the legacy Completions API for text generation
 */
class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions> extends LLM<CallOptions> {
  
  constructor(fields?: OpenAIInput & Partial<OpenAIInput>);

  /** Model configuration */
  model: string; // Default: "gpt-3.5-turbo-instruct"
  temperature?: number; // Sampling temperature (0-2)
  maxTokens?: number; // Maximum tokens to generate
  topP?: number; // Nucleus sampling parameter
  frequencyPenalty?: number; // Frequency penalty (-2 to 2)
  presencePenalty?: number; // Presence penalty (-2 to 2)
  n: number; // Number of completions (default: 1)
  bestOf?: number; // Generate bestOf completions server-side and return best
  batchSize: number; // Batch size for multiple prompts (default: 20)
  streaming: boolean; // Enable streaming (default: false)
  
  /** Additional parameters */
  logitBias?: Record<string, number>; // Modify likelihood of specified tokens
  suffix?: string; // Text to append after completion
  echo?: boolean; // Echo back the prompt in addition to completion
  stop?: string | string[]; // Stop sequences
  
  /** Client configuration */
  openAIApiKey?: string; // OpenAI API key
  organization?: string; // OpenAI organization ID
  baseURL?: string; // Custom base URL
  timeout?: number; // Request timeout
  maxRetries?: number; // Maximum retry attempts
  
  /** Generate text completions for multiple prompts */
  _generate(
    prompts: string[], 
    options: CallOptions, 
    runManager?: CallbackManagerForLLMRun
  ): Promise<LLMResult>;

  /** Stream response chunks for a single prompt */
  _streamResponseChunks(
    input: string, 
    options: CallOptions, 
    runManager?: CallbackManagerForLLMRun
  ): AsyncIterable<GenerationChunk>;

  /** Make completion requests with retry logic */
  completionWithRetry<T>(
    request: OpenAIClient.CompletionCreateParamsStreaming,
    options?: OpenAICallOptions
  ): Promise<T>;

  /** Get number of tokens in text */
  getNumTokens(text: string): Promise<number>;
}

OpenAI Call Options

Runtime options for completion generation.

interface OpenAICallOptions extends BaseOpenAICallOptions {
  /** Stop sequences to end generation */
  stop?: string | string[];
  
  /** Custom user identifier for abuse monitoring */
  user?: string;
  
  /** Deterministic sampling seed */
  seed?: number;
  
  /** Logit bias for token probabilities */
  logit_bias?: Record<string, number>;
}

Usage Examples

Basic Text Completion

import { OpenAI } from "@langchain/openai";

const llm = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  temperature: 0.7,
  maxTokens: 100,
  apiKey: process.env.OPENAI_API_KEY
});

// Single completion
const result = await llm.invoke("The future of artificial intelligence is");
console.log(result);

// Multiple completions
const results = await llm.generate([
  "The capital of France is",
  "The largest planet in our solar system is",
  "The inventor of the telephone was"
]);

results.generations.forEach((generation, index) => {
  console.log(`Prompt ${index + 1}: ${generation[0].text}`);
});

Streaming Completions

const streamingLLM = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  streaming: true,
  temperature: 0.5,
  maxTokens: 200
});

const stream = await streamingLLM.stream("Write a short poem about the ocean:");

for await (const chunk of stream) {
  process.stdout.write(chunk);
}

Advanced Configuration

const advancedLLM = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  
  // Generation parameters
  temperature: 0.8,
  maxTokens: 500,
  topP: 0.95,
  frequencyPenalty: 0.2,
  presencePenalty: 0.1,
  
  // Multiple completions
  n: 3, // Generate 3 completions
  bestOf: 5, // Consider 5 completions, return best 3
  
  // Control generation
  stop: ["\n\n", "###"], // Stop at double newline or ###
  suffix: "\n\nEnd of response.", // Append to completions
  echo: false, // Don't echo prompt back
  
  // Token probability modification
  logitBias: {
    "50256": -100 // Strongly discourage <|endoftext|> token
  },
  
  // Performance settings
  batchSize: 10, // Process 10 prompts at once
  timeout: 30000, // 30 second timeout
  maxRetries: 2
});

const prompts = [
  "Explain quantum computing:",
  "Describe machine learning:",
  "What is blockchain technology:"
];

const results = await advancedLLM.generate(prompts);

Custom Stop Sequences

const llm = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  temperature: 0.3,
  maxTokens: 150
});

// Stop at specific sequences
const result = await llm.invoke(
  "List the planets in our solar system:\n1.",
  {
    stop: ["\n10.", "Pluto"] // Stop before item 10 or if Pluto is mentioned
  }
);

Batch Processing

const batchLLM = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  batchSize: 5, // Process 5 prompts per API call
  temperature: 0.2,
  maxTokens: 50
});

// Generate many completions efficiently
const prompts = Array.from({ length: 20 }, (_, i) => 
  `Complete this sentence about topic ${i + 1}:`
);

const results = await batchLLM.generate(prompts);

// Access individual results
results.generations.forEach((generation, index) => {
  console.log(`Result ${index + 1}: ${generation[0].text.trim()}`);
});

// Access metadata
console.log(`Total tokens used: ${results.llmOutput?.tokenUsage?.totalTokens}`);

Error Handling and Retries

import { wrapOpenAIClientError } from "@langchain/openai";

const llm = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  maxRetries: 3,
  timeout: 10000
});

try {
  const result = await llm.invoke("Generate a creative story:");
  console.log(result);
} catch (error) {
  // Errors are automatically wrapped
  console.error("Generation failed:", error.message);
  
  if (error.code === 'rate_limit_exceeded') {
    console.log("Rate limit hit, waiting before retry...");
    // Implement exponential backoff
  }
}

Custom Prompting Patterns

const llm = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  temperature: 0.1,
  maxTokens: 200
});

// Few-shot prompting
const fewShotPrompt = `
Translate English to French:

English: Hello, how are you?
French: Bonjour, comment allez-vous?

English: What time is it?
French: Quelle heure est-il?

English: I love programming.
French:`;

const translation = await llm.invoke(fewShotPrompt);

// Chain of thought prompting
const reasoningPrompt = `
Solve this step by step:
If a train travels 60 mph for 2.5 hours, how far does it travel?

Step 1:`;

const reasoning = await llm.invoke(reasoningPrompt, {
  stop: ["Step 4:", "\n\nAnswer:"]
});

Model-Specific Features

// Using the instruct model for instruction following
const instructLLM = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  temperature: 0,
  maxTokens: 300
});

const instruction = `
Instructions: Summarize the following text in exactly 3 bullet points.

Text: Artificial intelligence (AI) is intelligence demonstrated by machines, 
in contrast to natural intelligence displayed by animals including humans. 
AI research has been defined as the field of study of intelligent agents, 
which refers to any system that perceives its environment and takes actions 
that maximize its chance of achieving its goals.

Summary:
•`;

const summary = await instructLLM.invoke(instruction);

// Using suffix for completion tasks
const completionLLM = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  temperature: 0.7,
  maxTokens: 50,
  suffix: "\n\nThis is a creative writing exercise."
});

const story = await completionLLM.invoke("Once upon a time in a distant galaxy");

Model Support

Available Models

The OpenAI LLM class primarily supports:

  • GPT-3.5 Turbo Instruct: gpt-3.5-turbo-instruct (recommended)
  • Legacy Models: Various GPT-3 models (deprecated)

Note: Most modern OpenAI models (GPT-4, GPT-4o, etc.) are chat models and should use the ChatOpenAI class instead.

Migration from Legacy Models

// Old approach with legacy models
const oldLLM = new OpenAI({
  model: "text-davinci-003", // Deprecated
  temperature: 0.7
});

// Modern approach - use ChatOpenAI for better performance
import { ChatOpenAI } from "@langchain/openai";

const modernModel = new ChatOpenAI({
  model: "gpt-3.5-turbo",
  temperature: 0.7
});

// Convert completion-style prompts to chat format
const prompt = "Explain quantum computing:";

// Legacy style
const legacyResult = await oldLLM.invoke(prompt);

// Modern style
const modernResult = await modernModel.invoke(prompt);

Performance Considerations

// Optimize for throughput
const highThroughputLLM = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  batchSize: 20, // Process many prompts together
  maxTokens: 100, // Limit response length
  temperature: 0.3 // More deterministic responses
});

// Optimize for creativity
const creativeLLM = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  temperature: 0.9, // More creative responses
  topP: 0.8, // Focus on top probability mass
  frequencyPenalty: 0.5, // Reduce repetition
  presencePenalty: 0.3 // Encourage topic diversity
});

Best Practices

When to Use Language Models vs Chat Models

Use OpenAI LLM (Completions API) for:

  • Text completion tasks
  • Legacy system integration
  • Simple prompt-response patterns
  • Code completion
  • Creative writing continuations

Use ChatOpenAI (Chat Completions API) for:

  • Conversational applications
  • Function calling
  • Structured output
  • Multi-turn dialogues
  • Modern applications (recommended)

Prompt Engineering

const llm = new OpenAI({
  model: "gpt-3.5-turbo-instruct",
  temperature: 0.2,
  maxTokens: 150
});

// Good: Clear, specific prompt with context
const goodPrompt = `
Task: Write a professional email subject line
Context: Scheduling a team meeting for next week
Requirements: Under 10 words, clear and actionable
Subject:`;

// Better: Include examples for consistency
const betterPrompt = `
Write professional email subject lines:

Meeting: Weekly team standup
Subject: Weekly Team Standup - Tuesday 2PM

Meeting: Project review session  
Subject: Q3 Project Review - Friday 3PM

Meeting: Planning for next quarter
Subject:`;

const result = await llm.invoke(betterPrompt);

docs

azure-integration.md

chat-models.md

embeddings.md

index.md

language-models.md

tools.md

types-and-configuration.md

tile.json