CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-langchain

TypeScript framework for building LLM-powered applications with agents, tools, middleware, and model interoperability

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

models.mddocs/guides/

Model Guide

This guide covers universal model initialization and configuration across 18+ LLM providers.

Basic Model Initialization

String Identifier

import { initChatModel } from "langchain";

// Initialize with provider:model format
const model = initChatModel("openai:gpt-4o");

With Configuration

import { initChatModel } from "langchain";

const model = initChatModel("openai:gpt-4o", {
  temperature: 0.7,
  maxTokens: 1000,
  timeout: 30000,
});

Using with Agent

import { createAgent, initChatModel } from "langchain";

// Option 1: Pass string directly
const agent1 = createAgent({
  model: "openai:gpt-4o",
  tools: [],
});

// Option 2: Initialize model first
const model = initChatModel("anthropic:claude-3-5-sonnet-20241022", {
  temperature: 0.5,
});

const agent2 = createAgent({
  model: model,
  tools: [],
});

Supported Providers

OpenAI

const gpt4o = initChatModel("openai:gpt-4o");
const gpt4oMini = initChatModel("openai:gpt-4o-mini");
const gpt4Turbo = initChatModel("openai:gpt-4-turbo");
const gpt35 = initChatModel("openai:gpt-3.5-turbo");

Anthropic

const sonnet = initChatModel("anthropic:claude-3-5-sonnet-20241022");
const haiku = initChatModel("anthropic:claude-3-5-haiku-20241022");
const opus = initChatModel("anthropic:claude-3-opus-20240229");

Google

// Vertex AI
const vertexPro = initChatModel("google-vertexai:gemini-1.5-pro");
const vertexFlash = initChatModel("google-vertexai:gemini-1.5-flash");

// Vertex AI (web)
const vertexWeb = initChatModel("google-vertexai-web:gemini-1.5-pro");

// Generative AI
const genaiPro = initChatModel("google-genai:gemini-1.5-pro");
const genaiFlash = initChatModel("google-genai:gemini-1.5-flash");

Other Providers

// Cohere
const commandR = initChatModel("cohere:command-r-plus");

// Mistral AI
const mistral = initChatModel("mistralai:mistral-large-latest");

// AWS Bedrock
const bedrock = initChatModel("bedrock:anthropic.claude-3-5-sonnet-20241022-v2:0");

// Ollama (local)
const llama = initChatModel("ollama:llama3.1");

// Groq
const groq = initChatModel("groq:llama-3.3-70b-versatile");

// Cerebras
const cerebras = initChatModel("cerebras:llama3.1-70b");

// DeepSeek
const deepseek = initChatModel("deepseek:deepseek-chat");

// X.AI
const grok = initChatModel("xai:grok-2-latest");

// Fireworks
const fireworks = initChatModel("fireworks:accounts/fireworks/models/llama-v3p1-70b-instruct");

// Together AI
const together = initChatModel("together:meta-llama/Llama-3-70b-chat-hf");

// Perplexity
const perplexity = initChatModel("perplexity:llama-3.1-sonar-large-128k-online");

Configuration Options

Common Parameters

const model = initChatModel("openai:gpt-4o", {
  // Temperature (0.0 to 2.0)
  temperature: 0.7,

  // Maximum tokens to generate
  maxTokens: 1000,

  // Request timeout (milliseconds)
  timeout: 30000,

  // Enable streaming
  streaming: true,

  // Top-p sampling
  topP: 0.9,

  // Frequency penalty
  frequencyPenalty: 0.5,

  // Presence penalty
  presencePenalty: 0.5,

  // Stop sequences
  stop: ["\n\n", "END"],
});

Provider-Specific Options

// OpenAI-specific
const openai = initChatModel("openai:gpt-4o", {
  temperature: 0.7,
  logitBias: { "50256": -100 },
  user: "user-123",
});

// Anthropic-specific
const anthropic = initChatModel("anthropic:claude-3-5-sonnet-20241022", {
  temperature: 0.5,
  topK: 40,
});

// Ollama-specific
const ollama = initChatModel("ollama:llama3.1", {
  temperature: 0.8,
  baseUrl: "http://localhost:11434",
});

Model Interoperability

Seamless Model Swapping

import { createAgent } from "langchain";

// Start with OpenAI
let agent = createAgent({
  model: "openai:gpt-4o",
  tools: [],
});

// Switch to Anthropic (same interface)
agent = createAgent({
  model: "anthropic:claude-3-5-sonnet-20241022",
  tools: [],
});

// Switch to local Ollama (same interface)
agent = createAgent({
  model: "ollama:llama3.1",
  tools: [],
});

// All agents work the same way
const result = await agent.invoke({
  messages: [{ role: "user", content: "Hello!" }],
});

Dynamic Model Selection

import { createAgent, initChatModel } from "langchain";

const agent = createAgent({
  model: (state) => {
    // Select model based on task complexity
    if (state.messages.length <= 2) {
      return initChatModel("openai:gpt-4o-mini"); // Fast model
    }
    return initChatModel("openai:gpt-4o"); // Powerful model
  },
  tools: [],
});

Model Fallbacks

import { initChatModel } from "langchain";

async function getModel() {
  try {
    // Try primary model
    return initChatModel("openai:gpt-4o");
  } catch (error) {
    console.log("Primary model unavailable, using fallback");
    // Fallback to alternative
    return initChatModel("anthropic:claude-3-5-sonnet-20241022");
  }
}

const model = await getModel();

Environment Setup

API Keys

# OpenAI
export OPENAI_API_KEY="sk-..."

# Anthropic
export ANTHROPIC_API_KEY="sk-ant-..."

# Google
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json"

# Cohere
export COHERE_API_KEY="..."

# Groq
export GROQ_API_KEY="gsk_..."

# Mistral
export MISTRAL_API_KEY="..."

# DeepSeek
export DEEPSEEK_API_KEY="..."

# X.AI
export XAI_API_KEY="..."

Azure OpenAI

export AZURE_OPENAI_API_KEY="..."
export AZURE_OPENAI_API_INSTANCE_NAME="..."
export AZURE_OPENAI_API_DEPLOYMENT_NAME="..."
export AZURE_OPENAI_API_VERSION="..."

AWS Bedrock

export AWS_ACCESS_KEY_ID="..."
export AWS_SECRET_ACCESS_KEY="..."
export AWS_REGION="us-east-1"

Advanced Usage

Custom Model Instances

import { ChatOpenAI } from "@langchain/openai";
import { createAgent } from "langchain";

// Create fully customized model instance
const customModel = new ChatOpenAI({
  modelName: "gpt-4o",
  temperature: 0.9,
  apiKey: process.env.CUSTOM_OPENAI_KEY,
  organization: "org-123",
  maxRetries: 5,
});

// Use with agent
const agent = createAgent({
  model: customModel,
  tools: [],
});

Model Configuration at Call Time

const model = initChatModel("openai:gpt-4o");

// Override configuration for specific call
const response = await model.invoke(
  [{ role: "user", content: "Hello" }],
  {
    temperature: 0.9, // Override default
    maxTokens: 500,
  }
);

Streaming from Models

const model = initChatModel("openai:gpt-4o", {
  streaming: true,
});

const stream = await model.stream([
  { role: "user", content: "Tell me a story" },
]);

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

Batch Processing

const model = initChatModel("openai:gpt-4o");

const results = await model.batch([
  [{ role: "user", content: "What is 2+2?" }],
  [{ role: "user", content: "What is the capital of France?" }],
  [{ role: "user", content: "Name a color" }],
]);

results.forEach((result, i) => {
  console.log(`Result ${i}:`, result.content);
});

Best Practices

Model Selection

  • Use GPT-4o or Claude 3.5 Sonnet for complex reasoning tasks
  • Use GPT-4o-mini or Claude 3.5 Haiku for simple tasks
  • Use local models (Ollama) for development and testing
  • Consider cost vs capability tradeoffs

Configuration

  • Set appropriate temperature (0 for deterministic, higher for creative)
  • Set maxTokens to prevent excessive token usage
  • Use timeouts to prevent hanging requests
  • Test with different providers to find best fit

Performance

  • Cache model instances when possible
  • Use streaming for better UX
  • Batch independent requests
  • Consider local models for high-volume use cases

Error Handling

  • Always handle initialization errors
  • Implement retry logic for transient failures
  • Have fallback models ready
  • Log errors for monitoring

Security

  • Never commit API keys to version control
  • Use environment variables for credentials
  • Rotate API keys regularly
  • Monitor API usage and costs

See Model API Reference for complete API documentation.

docs

glossary.md

index.md

quick-reference.md

task-index.md

tile.json