# OllamaLLM

> **Class** in `langchain_ollama`

📖 [View in docs](https://reference.langchain.com/python/langchain-ollama/llms/OllamaLLM)

Ollama large language models.

## Signature

```python
OllamaLLM()
```

## Description

**Setup:**

Install `langchain-ollama` and install/run the Ollama server locally:

```bash
pip install -U langchain-ollama
# Visit https://ollama.com/download to download and install Ollama
# (Linux users): start the server with `ollama serve`
```

Download a model to use:

```bash
ollama pull llama3.1
```

Key init args — generation params:
    model: str
        Name of the Ollama model to use (e.g. `'llama4'`).
    temperature: float | None
        Sampling temperature. Higher values make output more creative.
    num_predict: int | None
        Maximum number of tokens to predict.
    top_k: int | None
        Limits the next token selection to the K most probable tokens.
    top_p: float | None
        Nucleus sampling parameter. Higher values lead to more diverse text.
    mirostat: int | None
        Enable Mirostat sampling for controlling perplexity.
    seed: int | None
        Random number seed for generation reproducibility.

Key init args — client params:
    base_url:
        Base URL where Ollama server is hosted.
    keep_alive:
        How long the model stays loaded into memory.
    format:
        Specify the format of the output.

See full list of supported init args and their descriptions in the params section.

**Instantiate:**

```python
from langchain_ollama import OllamaLLM

model = OllamaLLM(
    model="llama3.1",
    temperature=0.7,
    num_predict=256,
    # base_url="http://localhost:11434",
    # other params...
)
```

**Invoke:**

```python
input_text = "The meaning of life is "
response = model.invoke(input_text)
print(response)
```
```txt
"a philosophical question that has been contemplated by humans for
centuries..."
```

**Stream:**

```python
for chunk in model.stream(input_text):
    print(chunk, end="")
```
```txt
a philosophical question that has been contemplated by humans for
centuries...
```

**Async:**

```python
response = await model.ainvoke(input_text)

# stream:
# async for chunk in model.astream(input_text):
#     print(chunk, end="")
```

## Extends

- `BaseLLM`

## Properties

- `model`
- `reasoning`
- `validate_model_on_init`
- `mirostat`
- `mirostat_eta`
- `mirostat_tau`
- `num_ctx`
- `num_gpu`
- `num_thread`
- `num_predict`
- `repeat_last_n`
- `repeat_penalty`
- `temperature`
- `seed`
- `stop`
- `tfs_z`
- `top_k`
- `top_p`
- `format`
- `keep_alive`
- `base_url`
- `client_kwargs`
- `async_client_kwargs`
- `sync_client_kwargs`

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/f0c5a28fa05adcda89aebcb449d897245ab21fa4/libs/partners/ollama/langchain_ollama/llms.py#L25)