Learning how to connect Ollama.
Let's connect Ollama on Server.
Learning how to connect Ollama.
How to connect Ollama on a server
Ollama has been briefly mentioned in previous posts.
LangChain using ChatOllama
Learning how to use prompts with ChatOllama
As explained in Learning how to use prompts with ChatOllama, let’s take another look at how to use Ollama. What if Ollama is set up on a server instead of your local PC? How should you connect to it? It’s very simple.
When calling ChatOllama, just provide the server address where Ollama is running as the base_url
.
ChatOllama(model="gemma2:latest")
default base_url : http://localhost:11434, http://127.0.0.1:11434
ChatOllama(model="gemma2:latest", base_url="http://1.2.3.4:11434")
Applying PromptTemplate
- Learning how to use prompts with ChatOllama in previous posts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from langchain_ollama import ChatOllama
from ai.local.langchain_brainai import stream_response, invoke_response
from langchain_core.prompts import PromptTemplate
selected_model = "gemma2:latest"
# Note. base_url
llm = ChatOllama(model=selected_model, base_url="http://1.2.3.4:11434")
template = """
#System:
You are a friendly AI assistant. Your name is DS2Man. Please answer questions briefly.
#Question:
{question}
#Answer:
"""
# The format below also works.
# template = """You are a friendly AI assistant. Your name is DS2Man. Please answer questions briefly.
# {question}
# """
prompt = PromptTemplate.from_template(
template
)
chain = prompt | llm
question = "What is the capital of the United States?"
response = chain.stream({"question": question})
stream_response(response)
# response = chain.invoke({"question":question})
# invoke_response(response, "")
1
The capital of the United States is Washington, D.C.
Applying ChatPromptTemplate
- Learning how to use prompts with ChatOllama in previous posts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from langchain_ollama import ChatOllama
from ai.local.langchain_brainai import stream_response, invoke_response
from langchain_core.prompts import ChatPromptTemplate
selected_model = "gemma2:latest"
llm = ChatOllama(model=selected_model, base_url="http://1.2.3.4:11434")
template = [
("system", "You are a friendly AI assistant. Your name is DS2Man. Please answer questions briefly."),
("human", "{question}")
]
prompt = ChatPromptTemplate.from_messages(
template
)
chain = prompt | llm
question = "What is the capital of the United States?"
response = chain.stream({"question": question})
stream_response(response)
# response = chain.invoke({"question": question})
# invoke_response(response, "")
1
The capital of the United States is Washington, D.C.
This post is licensed under CC BY 4.0 by the author.