Class ChatAnthropicMessages<CallOptions>

Wrapper around Anthropic large language models.

To use this package, you should have an Anthropic API key set as an environment variable named ANTHROPIC_API_KEY or passed into the constructor.

Any parameters that are valid to be passed to anthropic.messages can be passed through invocationKwargs, even if not explicitly available on this class.

import { ChatAnthropic } from "@langchain/anthropic";

const model = new ChatAnthropic({
temperature: 0.9,
apiKey: 'YOUR-API-KEY',
});
const res = await model.invoke({ input: 'Hello!' });
console.log(res);

Type Parameters

Hierarchy (view full)

Implements

Constructors

Properties

clientOptions: ClientOptions

Overridable Anthropic ClientOptions

maxTokens: number = 2048

A maximum number of tokens to generate before stopping.

model: string = "claude-2.1"

Model name to use

modelName: string = "claude-2.1"

Model name to use

streamUsage: boolean = true

Whether or not to include token usage data in streamed chunks.

true
streaming: boolean = false

Whether to stream the results or not

temperature: number = 1

Amount of randomness injected into the response. Ranges from 0 to 1. Use temp closer to 0 for analytical / multiple choice, and temp closer to 1 for creative and generative tasks.

topK: number = -1

Only sample from the top K options for each subsequent token. Used to remove "long tail" low probability responses. Defaults to -1, which disables it.

topP: number = -1

Does nucleus sampling, in which we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. Defaults to -1, which disables it. Note that you should either alter temperature or top_p, but not both.

anthropicApiKey?: string

Anthropic API key

apiKey?: string

Anthropic API key

apiUrl?: string
invocationKwargs?: Kwargs

Holds any additional parameters that are valid to pass to anthropic.messages that are not explicitly specified on this class.

stopSequences?: string[]

A list of strings upon which to stop generating. You probably want ["\n\nHuman:"], as that's the cue for the next turn in the dialog agent.

batchClient: Anthropic
streamingClient: Anthropic

Methods

  • Formats LangChain StructuredTools to AnthropicTools.

    Parameters

    • tools: undefined | any[]

      The tools to format

    Returns undefined | Tool[]

    The formatted tools, or undefined if none are passed.

    If a mix of AnthropicTools and StructuredTools are passed.

  • Get the identifying parameters for the model

    Returns {
        max_tokens: number;
        model:
            | string & {}
            | "claude-2.1"
            | "claude-3-opus-20240229"
            | "claude-3-sonnet-20240229"
            | "claude-3-haiku-20240307"
            | "claude-2.0"
            | "claude-instant-1.2";
        model_name: string;
        metadata?: Metadata;
        stop_sequences?: string[];
        stream?: boolean;
        system?: string;
        temperature?: number;
        tool_choice?: ToolChoiceAuto | ToolChoiceAny | ToolChoiceTool;
        tools?: Tool[];
        top_k?: number;
        top_p?: number;
    }

    • max_tokens: number

      The maximum number of tokens to generate before stopping.

      Note that our models may stop before reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate.

      Different models have different maximum values for this parameter. See models for details.

    • model:
          | string & {}
          | "claude-2.1"
          | "claude-3-opus-20240229"
          | "claude-3-sonnet-20240229"
          | "claude-3-haiku-20240307"
          | "claude-2.0"
          | "claude-instant-1.2"

      The model that will complete your prompt.

      See models for additional details and options.

    • model_name: string
    • Optionalmetadata?: Metadata

      An object describing metadata about the request.

    • Optionalstop_sequences?: string[]

      Custom text sequences that will cause the model to stop generating.

      Our models will normally stop when they have naturally completed their turn, which will result in a response stop_reason of "end_turn".

      If you want the model to stop generating when it encounters custom strings of text, you can use the stop_sequences parameter. If the model encounters one of the custom sequences, the response stop_reason value will be "stop_sequence" and the response stop_sequence value will contain the matched stop sequence.

    • Optionalstream?: boolean

      Whether to incrementally stream the response using server-sent events.

      See streaming for details.

    • Optionalsystem?: string

      System prompt.

      A system prompt is a way of providing context and instructions to Claude, such as specifying a particular goal or role. See our guide to system prompts.

    • Optionaltemperature?: number

      Amount of randomness injected into the response.

      Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks.

      Note that even with temperature of 0.0, the results will not be fully deterministic.

    • Optionaltool_choice?: ToolChoiceAuto | ToolChoiceAny | ToolChoiceTool

      How the model should use the provided tools. The model can use a specific tool, any available tool, or decide by itself.

    • Optionaltools?: Tool[]

      Definitions of tools that the model may use.

      If you include tools in your API request, the model may return tool_use content blocks that represent the model's use of those tools. You can then run those tools using the tool input generated by the model and then optionally return results back to the model using tool_result content blocks.

      Each tool definition includes:

      • name: Name of the tool.
      • description: Optional, but strongly-recommended description of the tool.
      • input_schema: JSON schema for the tool input shape that the model will produce in tool_use output content blocks.

      For example, if you defined tools as:

      [
      {
      "name": "get_stock_price",
      "description": "Get the current stock price for a given ticker symbol.",
      "input_schema": {
      "type": "object",
      "properties": {
      "ticker": {
      "type": "string",
      "description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
      }
      },
      "required": ["ticker"]
      }
      }
      ]

      And then asked the model "What's the S&P 500 at today?", the model might produce tool_use content blocks in the response like this:

      [
      {
      "type": "tool_use",
      "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV",
      "name": "get_stock_price",
      "input": { "ticker": "^GSPC" }
      }
      ]

      You might then run your get_stock_price tool with {"ticker": "^GSPC"} as an input, and return the following back to the model in a subsequent user message:

      [
      {
      "type": "tool_result",
      "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV",
      "content": "259.75 USD"
      }
      ]

      Tools can be used for workflows that include running client-side tools and functions, or more generally whenever you want the model to produce a particular JSON structure of output.

      See our guide for more details.

    • Optionaltop_k?: number

      Only sample from the top K options for each subsequent token.

      Used to remove "long tail" low probability responses. Learn more technical details here.

      Recommended for advanced use cases only. You usually only need to use temperature.

    • Optionaltop_p?: number

      Use nucleus sampling.

      In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both.

      Recommended for advanced use cases only. You usually only need to use temperature.

  • Get the parameters used to invoke the model

    Parameters

    • Optionaloptions: unknown

    Returns Omit<MessageCreateParamsNonStreaming | MessageCreateParamsStreaming, "messages"> & Kwargs

  • Creates a streaming request with retry.

    Parameters

    • request: MessageCreateParamsStreaming & Kwargs

      The parameters for creating a completion.

    • Optionaloptions: AnthropicRequestOptions

    Returns Promise<Stream<RawMessageStreamEvent>>

    A streaming request.