langchain.js
    Preparing search index...

    Ollama chat model integration.

    Setup: Install @langchain/ollama and the Ollama app.

    npm install @langchain/ollama
    

    Runtime args can be passed as the second argument to any of the base runnable methods .invoke. .stream, .batch, etc. They can also be passed via .withConfig, or the second arg in .bindTools, like shown in the examples below:

    // When calling `.withConfig`, call options should be passed via the first argument
    const llmWithArgsBound = llm.withConfig({
    stop: ["\n"],
    });

    // When calling `.bindTools`, call options should be passed via the second argument
    const llmWithTools = llm.bindTools(
    [...],
    {
    stop: ["\n"],
    }
    );
    Instantiate
    import { ChatOllama } from '@langchain/ollama';

    const llm = new ChatOllama({
    model: "llama-3.1:8b",
    temperature: 0,
    // other params...
    });

    Invoking
    const input = `Translate "I love programming" into French.`;

    // Models also accept a list of chat messages or a formatted prompt
    const result = await llm.invoke(input);
    console.log(result);
    AIMessage {
      "content": "The translation of \"I love programming\" into French is:\n\n\"J'adore programmer.\"",
      "additional_kwargs": {},
      "response_metadata": {
        "model": "llama3.1:8b",
        "created_at": "2024-08-12T22:12:23.09468Z",
        "done_reason": "stop",
        "done": true,
        "total_duration": 3715571291,
        "load_duration": 35244375,
        "prompt_eval_count": 19,
        "prompt_eval_duration": 3092116000,
        "eval_count": 20,
        "eval_duration": 585789000
      },
      "tool_calls": [],
      "invalid_tool_calls": [],
      "usage_metadata": {
        "input_tokens": 19,
        "output_tokens": 20,
        "total_tokens": 39
      }
    }
    

    Streaming Chunks
    for await (const chunk of await llm.stream(input)) {
    console.log(chunk);
    }
    AIMessageChunk {
      "content": "The",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": []
    }
    AIMessageChunk {
      "content": " translation",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": []
    }
    AIMessageChunk {
      "content": " of",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": []
    }
    AIMessageChunk {
      "content": " \"",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": []
    }
    AIMessageChunk {
      "content": "I",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": []
    }
    ...
    AIMessageChunk {
      "content": "",
      "additional_kwargs": {},
      "response_metadata": {},
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": []
    }
    AIMessageChunk {
      "content": "",
      "additional_kwargs": {},
      "response_metadata": {
        "model": "llama3.1:8b",
        "created_at": "2024-08-12T22:13:22.22423Z",
        "done_reason": "stop",
        "done": true,
        "total_duration": 8599883208,
        "load_duration": 35975875,
        "prompt_eval_count": 19,
        "prompt_eval_duration": 7918195000,
        "eval_count": 20,
        "eval_duration": 643569000
      },
      "tool_calls": [],
      "tool_call_chunks": [],
      "invalid_tool_calls": [],
      "usage_metadata": {
        "input_tokens": 19,
        "output_tokens": 20,
        "total_tokens": 39
      }
    }
    

    Bind tools
    import { z } from 'zod';

    const GetWeather = {
    name: "GetWeather",
    description: "Get the current weather in a given location",
    schema: z.object({
    location: z.string().describe("The city and state, e.g. San Francisco, CA")
    }),
    }

    const GetPopulation = {
    name: "GetPopulation",
    description: "Get the current population in a given location",
    schema: z.object({
    location: z.string().describe("The city and state, e.g. San Francisco, CA")
    }),
    }

    const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
    const aiMsg = await llmWithTools.invoke(
    "Which city is hotter today and which is bigger: LA or NY?"
    );
    console.log(aiMsg.tool_calls);
    [
      {
        name: 'GetWeather',
        args: { location: 'Los Angeles, CA' },
        id: '49410cad-2163-415e-bdcd-d26938a9c8c5',
        type: 'tool_call'
      },
      {
        name: 'GetPopulation',
        args: { location: 'New York, NY' },
        id: '39e230e4-63ec-4fae-9df0-21c3abe735ad',
        type: 'tool_call'
      }
    ]
    

    Structured Output
    import { z } from 'zod';

    const Joke = z.object({
    setup: z.string().describe("The setup of the joke"),
    punchline: z.string().describe("The punchline to the joke"),
    rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
    }).describe('Joke to tell user.');

    const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
    const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
    console.log(jokeResult);
    {
      punchline: 'Why did the cat join a band? Because it wanted to be the purr-cussionist!',
      rating: 8,
      setup: 'A cat walks into a music store and asks the owner...'
    }
    

    Usage Metadata
    const aiMsgForMetadata = await llm.invoke(input);
    console.log(aiMsgForMetadata.usage_metadata);
    { input_tokens: 19, output_tokens: 20, total_tokens: 39 }
    

    Response Metadata
    const aiMsgForResponseMetadata = await llm.invoke(input);
    console.log(aiMsgForResponseMetadata.response_metadata);
    {
      model: 'llama3.1:8b',
      created_at: '2024-08-12T22:17:42.274795Z',
      done_reason: 'stop',
      done: true,
      total_duration: 6767071209,
      load_duration: 31628209,
      prompt_eval_count: 19,
      prompt_eval_duration: 6124504000,
      eval_count: 20,
      eval_duration: 608785000
    }
    

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    • Parameters

      Returns ChatOllama

    Properties

    baseUrl: string = "http://127.0.0.1:11434"

    The host URL of the Ollama server.

    "http://127.0.0.1:11434"
    
    checkOrPullModel: boolean = false

    Whether or not to check the model exists on the local machine before invoking it. If set to true, the model will be pulled if it does not exist.

    false
    
    client: Ollama
    embeddingOnly?: boolean
    f16Kv?: boolean
    format?: string | Record<string, any>
    frequencyPenalty?: number
    keepAlive?: string | number
    "5m"
    
    logitsAll?: boolean
    lowVram?: boolean
    mainGpu?: number
    mirostat?: number
    mirostatEta?: number
    mirostatTau?: number
    model: string = "llama3"

    The model to invoke. If the model does not exist, it will be pulled.

    "llama3"
    
    numa?: boolean
    numBatch?: number
    numCtx?: number
    numGpu?: number
    numKeep?: number
    numPredict?: number
    numThread?: number
    penalizeNewline?: boolean
    presencePenalty?: number
    repeatLastN?: number
    repeatPenalty?: number
    seed?: number
    streaming?: boolean
    temperature?: number
    tfsZ?: number
    topK?: number
    topP?: number
    typicalP?: number
    useMlock?: boolean
    useMmap?: boolean
    vocabOnly?: boolean

    Methods

    • Parameters

      • messages: BaseMessage[]
      • options: unknown
      • OptionalrunManager: any

      Returns Promise<ChatResult>

    • Returns string

    • Parameters

      • messages: BaseMessage[]
      • options: unknown
      • OptionalrunManager: any

      Returns AsyncGenerator<ChatGenerationChunk>

    • Parameters

      • tools: BindToolsInput[]
      • Optionalkwargs: Partial<unknown>

      Returns Runnable<BaseLanguageModelInput, AIMessageChunk, ChatOllamaCallOptions>

    • Parameters

      • options: unknown

      Returns LangSmithParams

    • Parameters

      • Optionaloptions: unknown

      Returns Omit<OllamaChatRequest, "messages">

    • Download a model onto the local machine.

      Parameters

      • model: string

        The name of the model to download.

      • Optionaloptions: PullModelOptions

        Options for pulling the model.

      Returns Promise<void>

    • Type Parameters

      • RunOutput extends Record<string, any> = Record<string, any>

      Parameters

      • outputSchema: any
      • Optionalconfig: any

      Returns Runnable<BaseLanguageModelInput, RunOutput>

    • Type Parameters

      • RunOutput extends Record<string, any> = Record<string, any>

      Parameters

      • outputSchema: any
      • Optionalconfig: any

      Returns Runnable<BaseLanguageModelInput, { parsed: RunOutput; raw: BaseMessage }>

    • Type Parameters

      • RunOutput extends Record<string, any> = Record<string, any>

      Parameters

      • outputSchema: any
      • Optionalconfig: any

      Returns any

    • Returns string