LangChain Classic

1.0.3 · deprecated · verified Sun Apr 05

LangChain Classic is a legacy Python package providing the original API for building applications with Large Language Models (LLMs) through composability. It represents the state of the `langchain` library prior to its major architectural refactor (pre-0.1.0/0.2.0, before v1.0), which split the project into `langchain-core`, `langchain-community`, and partner packages. This package is in maintenance mode for backward compatibility and does not receive new features; it is not recommended for new projects.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic LLM interaction using an `LLMChain` with an OpenAI model and a `PromptTemplate`, typical of `langchain-classic` usage. Ensure `OPENAI_API_KEY` is set in your environment variables.

import os
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

# Set your OpenAI API key as an environment variable (e.g., export OPENAI_API_KEY="sk-...")
# For this example, we use os.environ.get to prevent errors if the key is not set.
openai_api_key = os.environ.get('OPENAI_API_KEY', '')

if not openai_api_key:
    print("Warning: OPENAI_API_KEY environment variable not set. Please set it to run the example.")
    print("Skipping LLM interaction.")
else:
    llm = OpenAI(openai_api_key=openai_api_key, temperature=0.7)

    prompt = PromptTemplate(
        input_variables=["topic"],
        template="Tell me a short, funny joke about {topic}."
    )

    chain = LLMChain(llm=llm, prompt=prompt)

    try:
        response = chain.run("developers")
        print(f"Joke about developers:\n{response}")
    except Exception as e:
        print(f"An error occurred during LLM interaction: {e}")
        print("Please ensure your OPENAI_API_KEY is valid and has access to the OpenAI API.")

view raw JSON →