# ChatOllama

> **Class** in `@langchain/ollama`

📖 [View in docs](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama)

Ollama chat model integration.

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

```bash
npm install @langchain/ollama
export OLLAMA_BASE_URL="http://127.0.0.1:11434" # Optional; defaults to http://127.0.0.1:11434 if not set
```

## [Constructor args](https://api.js.langchain.com/classes/_langchain_ollama.ChatOllama.html#constructor)

## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_ollama.ChatOllamaCallOptions.html)

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:

```typescript
// 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"],
  }
);
```

## Examples

<details open>
<summary><strong>Instantiate</strong></summary>

```typescript
import { ChatOllama } from '@langchain/ollama';

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

<br />

<details>
<summary><strong>Invoking</strong></summary>

```typescript
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);
```

```txt
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
  }
}
```
</details>

<br />

<details>
<summary><strong>Streaming Chunks</strong></summary>

```typescript
for await (const chunk of await llm.stream(input)) {
  console.log(chunk);
}
```

```txt
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
  }
}
```
</details>

<br />

<details>
<summary><strong>Bind tools</strong></summary>

```typescript
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);
```

```txt
[
  {
    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'
  }
]
```
</details>

<br />

<details>
<summary><strong>Structured Output</strong></summary>

```typescript
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);
```

```txt
{
  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...'
}
```
</details>

<br />

<details>
<summary><strong>Usage Metadata</strong></summary>

```typescript
const aiMsgForMetadata = await llm.invoke(input);
console.log(aiMsgForMetadata.usage_metadata);
```

```txt
{ input_tokens: 19, output_tokens: 20, total_tokens: 39 }
```
</details>

<br />

<details>
<summary><strong>Response Metadata</strong></summary>

```typescript
const aiMsgForResponseMetadata = await llm.invoke(input);
console.log(aiMsgForResponseMetadata.response_metadata);
```

```txt
{
  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
}
```
</details>

<br />

## Signature

```javascript
class ChatOllama
```

## Extends

- `BaseChatModel<ChatOllamaCallOptions, AIMessageChunk>`

## Implements

- `ChatOllamaInput`

## Constructors

- [`constructor()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/constructor)

## Properties

- `baseUrl`
- `cache`
- `callbacks`
- `caller`
- `checkOrPullModel`
- `client`
- `disableStreaming`
- `embeddingOnly`
- `f16Kv`
- `format`
- `frequencyPenalty`
- `keepAlive`
- `lc_kwargs`
- `lc_namespace`
- `lc_runnable`
- `lc_serializable`
- `logitsAll`
- `lowVram`
- `mainGpu`
- `metadata`
- `mirostat`
- `mirostatEta`
- `mirostatTau`
- `model`
- `name`
- `numa`
- `numBatch`
- `numCtx`
- `numGpu`
- `numKeep`
- `numPredict`
- `numThread`
- `outputVersion`
- `ParsedCallOptions`
- `penalizeNewline`
- `presencePenalty`
- `repeatLastN`
- `repeatPenalty`
- `seed`
- `streaming`
- `tags`
- `temperature`
- `tfsZ`
- `think`
- `topK`
- `topP`
- `typicalP`
- `useMlock`
- `useMmap`
- `verbose`
- `vocabOnly`
- `callKeys`
- `lc_aliases`
- `lc_attributes`
- `lc_id`
- `lc_secrets`
- `lc_serializable_keys`
- `profile`

## Methods

- [`_addVersion()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_addVersion)
- [`_batchWithConfig()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_batchWithConfig)
- [`_callWithConfig()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_callWithConfig)
- [`_combineLLMOutput()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_combineLLMOutput)
- [`_filterInvocationParamsForTracing()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_filterInvocationParamsForTracing)
- [`_generate()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_generate)
- [`_generateCached()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_generateCached)
- [`_getOptionsList()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_getOptionsList)
- [`_getSerializedCacheKeyParametersForCall()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_getSerializedCacheKeyParametersForCall)
- [`_identifyingParams()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_identifyingParams)
- [`_llmType()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_llmType)
- [`_modelType()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_modelType)
- [`_separateRunnableConfigFromCallOptions()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_separateRunnableConfigFromCallOptions)
- [`_separateRunnableConfigFromCallOptionsCompat()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_separateRunnableConfigFromCallOptionsCompat)
- [`_streamChatModelEvents()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_streamChatModelEvents)
- [`_streamIterator()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_streamIterator)
- [`_streamLog()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_streamLog)
- [`_streamResponseChunks()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_streamResponseChunks)
- [`_transformStreamWithConfig()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_transformStreamWithConfig)
- [`assign()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/assign)
- [`asTool()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/asTool)
- [`batch()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/batch)
- [`bindTools()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/bindTools)
- [`generate()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/generate)
- [`generatePrompt()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/generatePrompt)
- [`getGraph()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/getGraph)
- [`getLsParams()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/getLsParams)
- [`getLsParamsWithDefaults()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/getLsParamsWithDefaults)
- [`getName()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/getName)
- [`getNumTokens()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/getNumTokens)
- [`invocationParams()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/invocationParams)
- [`invoke()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/invoke)
- [`pick()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/pick)
- [`pipe()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/pipe)
- [`pull()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/pull)
- [`serialize()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/serialize)
- [`stream()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/stream)
- [`streamEvents()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/streamEvents)
- [`streamLog()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/streamLog)
- [`streamV2()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/streamV2)
- [`toJSON()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/toJSON)
- [`toJSONNotImplemented()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/toJSONNotImplemented)
- [`transform()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/transform)
- [`withConfig()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/withConfig)
- [`withFallbacks()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/withFallbacks)
- [`withListeners()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/withListeners)
- [`withRetry()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/withRetry)
- [`withStructuredOutput()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/withStructuredOutput)
- [`_convertInputToPromptValue()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/_convertInputToPromptValue)
- [`deserialize()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/deserialize)
- [`isRunnable()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/isRunnable)
- [`lc_name()`](https://reference.langchain.com/javascript/langchain-ollama/ChatOllama/lc_name)

---

[View source on GitHub](https://github.com/langchain-ai/langchainjs/blob/c5e896270207fcd3bdf1d1877e80b94c72d35f1e/libs/providers/langchain-ollama/src/chat_models.ts#L420)