llama-index llms ollama integration
Project Links
Meta
Author: Your Name
Requires Python: <4.0,>=3.10
Classifiers
LlamaIndex Llms Integration: Ollama
Installation
To install the required package, run:
pip install llama-index-llms-ollama
Setup
- Follow the Ollama README to set up and run a local Ollama instance.
- When the Ollama app is running on your local machine, it will serve all of your local models on
localhost:11434. - Select your model when creating the
Ollamainstance by specifyingmodel=":". - You can increase the default timeout (30 seconds) by setting
Ollama(..., request_timeout=300.0). - If you set
llm = Ollama(..., model="<model family>")without a version, it will automatically look for the latest version.
Usage
Initialize Ollama
from llama_index.llms.ollama import Ollama
llm = Ollama(model="llama3.1:latest", request_timeout=120.0)
Generate Completions
To generate a text completion for a prompt, use the complete method:
resp = llm.complete("Who is Paul Graham?")
print(resp)
Chat Responses
To send a chat message and receive a response, create a list of ChatMessage instances and use the chat method:
from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality."
),
ChatMessage(role="user", content="What is your name?"),
]
resp = llm.chat(messages)
print(resp)
Streaming Responses
Stream Complete
To stream responses for a prompt, use the stream_complete method:
response = llm.stream_complete("Who is Paul Graham?")
for r in response:
print(r.delta, end="")
Stream Chat
To stream chat responses, use the stream_chat method:
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality."
),
ChatMessage(role="user", content="What is your name?"),
]
resp = llm.stream_chat(messages)
for r in resp:
print(r.delta, end="")
JSON Mode
Ollama supports a JSON mode to ensure all responses are valid JSON, which is useful for tools that need to parse structured outputs:
llm = Ollama(model="llama3.1:latest", request_timeout=120.0, json_mode=True)
response = llm.complete(
"Who is Paul Graham? Output as a structured JSON object."
)
print(str(response))
Structured Outputs
You can attach a Pydantic class to the LLM to ensure structured outputs:
from llama_index.core.bridge.pydantic import BaseModel
from llama_index.core.tools import FunctionTool
class Song(BaseModel):
"""A song with name and artist."""
name: str
artist: str
llm = Ollama(model="llama3.1:latest", request_timeout=120.0)
sllm = llm.as_structured_llm(Song)
response = sllm.chat([ChatMessage(role="user", content="Name a random song!")])
print(
response.message.content
) # e.g., {"name": "Yesterday", "artist": "The Beatles"}
Asynchronous Chat
You can also use asynchronous chat:
response = await sllm.achat(
[ChatMessage(role="user", content="Name a random song!")]
)
print(response.message.content)
LLM Implementation example
0.10.1
Mar 20, 2026
0.10.0
Mar 12, 2026
0.9.1
Dec 19, 2025
0.9.0
Oct 27, 2025
0.8.0
Oct 04, 2025
0.7.4
Sep 25, 2025
0.7.3
Sep 12, 2025
0.7.2
Sep 08, 2025
0.7.1
Aug 14, 2025
0.7.0
Jul 30, 2025
0.6.2
Jun 07, 2025
0.6.1
May 30, 2025
0.6.0
May 30, 2025
0.5.6
May 23, 2025
0.5.5
May 22, 2025
0.5.4
Mar 27, 2025
0.5.3
Mar 05, 2025
0.5.2
Feb 09, 2025
0.5.1
Feb 05, 2025
0.5.0
Dec 08, 2024
0.4.2
Dec 05, 2024
0.4.1
Nov 22, 2024
0.4.0
Nov 18, 2024
0.3.6
Nov 07, 2024
0.3.5
Nov 06, 2024
0.3.4
Oct 08, 2024
0.3.3
Sep 26, 2024
0.3.2
Sep 13, 2024
0.3.1
Sep 02, 2024
0.3.0
Aug 22, 2024
0.2.2
Jul 25, 2024
0.2.1
Jul 25, 2024
0.2.0
Jul 25, 2024
0.1.6
Jul 19, 2024
0.1.5
May 24, 2024
0.1.4
May 17, 2024
0.1.3
Apr 29, 2024
0.1.2
Feb 21, 2024
0.1.1
Feb 12, 2024
0.1.0
Feb 10, 2024
0.0.1
Feb 03, 2024