Ollama offers multiple ways to interact with its models, with the most common being through command-line inference operations.

Command-Line Interaction

The simplest way to interact with the model is directly through the command line.

Running the Model

Use the ollama run command to start the model and enter interactive mode:

1
ollama run <model-name>

For example, to download and run the deepseek-r1:1.5b model:

1
ollama run deepseek-r1:1.5b

Once the model is running, you can directly input questions or instructions, and the model will generate responses in real-time.

Example Interaction:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
>>> Hi, can you help me write some code?
<think>

</think>

Of course! I'd be happy to help. What do you need assistance with? For example:

- Do you want to learn a new programming language?
- Need help with setting up a particular software or tool?
- Or simply need some code snippets for a specific task?

Let me know, and I can provide detailed guidance!

>>> Write a Python "Hello, World!" program.
<think>
Alright, the user asked for a Python "Hello, World!" program. Okay, so they want me to write that in Python.
Let's break this down.

First, I need to recall what the "Hello, World!" program does. It prints the string "Hello, World!" to the
console. That’s pretty straightforward.

I should think about how to implement this in Python. Well, one common way is using the print function. So,
something like `print("Hello, World!")`.

But wait, maybe they want me to write a program that demonstrates more concepts. Let's see—maybe I can include
variables or loops? But for now, just printing "Hello, World!" should be simple enough.

I should also consider best practices. Do I need to handle any edge cases? Well, if it's always the same
string, no, but in a real program, maybe the message changes. But for this case, staying simple is probably
best.

Let me outline the steps:

1. Print the word "Hello".
2. Print a comma.
3. Print space.
4. Continue to print ", World!".

That covers the entire string. I can write it concisely using the print function multiple times.

Is there another way to do this? Maybe using f-strings or triple quotes for better readability? Let me think...
Both are similar in Python and not too hard.

But perhaps sticking with the basic print function is simplest for now. It’s easy to read and maintain.

I should also mention that the string can be any text, so "Hello, World!" works well but could change if
needed. But for this case, it's just a placeholder.

In terms of code length, I want it concise yet clear. Using multiple print statements is straightforward and
efficient.

Alright, let me put it all together in Python syntax.
</think>

Certainly! Here’s how to write the "Hello, World!" program in Python:

```python
print("Hello, World!")
\```

This will output:
\```
Hello, World!
\```

Let me know if you'd like more examples or have a specific task!

>>>

Exiting Interactive Mode

To exit the interactive mode, type /bye or press Ctrl+D.

Single Command Interaction

If you only need the model to generate a single response, you can pass the input directly through the command line.

Using Pipe Input

You can pipe input to the model:

1
echo "Who are you?" | ollama run deepseek-r1:1.5b

Output:

1
2
3
4
5
6
7
8
9
C:\Users\Administrator>echo "Who are you?" | ollama run deepseek-r1:1.5b
<think>

</think>

Greetings! I'm DeepSeek-R1, an artificial intelligence assistant created by DeepSeek. I'm at your service and would be delighted to assist you with any inquiries or tasks you may have.


C:\Users\Administrator>

Using Command-Line Arguments

You can also pass input directly as a command-line argument:

1
ollama run deepseek-r1:1.5b "Python hello world code?"

Output:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
C:\Users\Administrator>ollama run deepseek-r1:1.5b "Python hello world code?"
<think>

</think>

Of course! Here's a simple "Hello World" program in Python:

```python
print("Hello, World!")
\```

When you run this code, it will output:

\```
Hello, World!
\```

Let me know if you'd like to see a more complex example or different functionality!


C:\Users\Administrator>
\```



## Multi-Turn Conversations
Ollama supports multi-turn conversations, allowing the model to remember context.

**Example:**

```plain
>>> Hi, can you help me write some Python code?
<think>
Okay, the user asked if I can help them write some Python code. I need to respond by offering assistance with any specific tasks they have in mind.

I should let them know I'm here to assist with programming, problem-solving, or just general support. Maybe I'll suggest a few things to get them started.

I want to make sure my response is welcoming and encourages them to provide more details about what they need help with.
</think>

Of course! I'd be happy to help you write Python code. What specific tasks do you need assistance with? Whether it's solving a problem, implementing a feature, or just getting some advice, let me know so
I can assist effectively!

>>> I want to write a function to calculate the Fibonacci sequence.
<think>
Alright, the user wants a function to calculate the Fibonacci sequence. I should create a clear and functional Python function for them.

First, I'll outline the steps: the Fibonacci sequence starts with 0 and 1, each subsequent number is the sum of the previous two. The function will take an integer n and return the nth term in the
sequence.

I need to handle cases where n is larger than a certain value, like when it's over 47 because beyond that, the numbers get too big for Python's integers to manage efficiently. I'll also consider using
memoization to optimize performance by storing previously calculated values.

I should write a helper function inside fib to make the code cleaner and more readable. This helper will compute the nth Fibonacci number using an iterative approach with caching.

Finally, I'll test the function with known inputs like n=0, 1, 2, etc., to ensure it works correctly.
</think>

Sure! Let's create a Python function called `fibonacci` that calculates the nth term in the Fibonacci sequence. The Fibonacci sequence is defined by starting with 0 and 1, where each subsequent number is
the sum of the previous two numbers.

Here's how we can implement this:

```python
def fibonacci(n):
# Check if n is a positive integer
if not isinstance(n, int) or n < 0:
raise ValueError("n must be a non-negative integer")

# Base cases: F(0) = 0 and F(1) = 1
if n == 0:
return 0
elif n == 1:
return 1

# Create a memoization table to store previously computed values
fib_sequence = [0, 1] # Initialize with F(0), F(1)

# Iterate from 2 to n-1 to compute the sequence up to F(n)
for i in range(2, n):
next_val = fib_sequence[i-2] + fib_sequence[i-1]
fib_sequence.append(next_val)

return fib_sequence[n - 1]

# Test the function
print(fibonacci(0)) # Output: 0
print(fibonacci(1)) # Output: 1
print(fibonacci(5)) # Output: 5 (F(5) = F(4) + F(3) = 3 + 2)
\```

### Explanation:
1. **Base Cases**: The function starts with the base cases where `n=0` returns 0 and `n=1` returns 1.
2. **Memoization**: We use a list called `fib_sequence` to store each computed Fibonacci number, which allows us to reuse previously calculated values and improve efficiency.
3. **Iterative Calculation**: For numbers larger than the base cases, we iterate from 2 up to `n-1`, computing each value as the sum of the previous two in the sequence and appending it to the list.
4. **Return Result**: Finally, we return the nth Fibonacci number by accessing the (n-1)th index of the `fib_sequence` list.

This implementation is efficient for calculating Fibonacci numbers up to n=47 or beyond due to its use of memoization.

>>> Send a message (/? for help)

File Input

You can also pass the contents of a file as input to the model.

For example, if the content of the input.txt file is:

1
Write a Python function to calculate the Fibonacci sequence.

You can pass the file content as input:

1
ollama run deepseek-r1:1.5b < input.txt

Custom Prompts

You can define custom prompts or system instructions using a Modelfile, allowing the model to follow specific rules during interactions.

Creating a Custom Model

Write a Modelfile:

1
2
FROM deepseek-r1:1.5b
SYSTEM "You are a programming assistant specialized in helping users write code."

Then create a custom model:

1
ollama create coder-assistant -f Modelfile

The output looks something like this:

1
2
3
4
5
6
7
8
gathering model components
using existing layer sha256:aabd4debf0c8f08881923f2c25fc0fdeed24435271c2b3e92c4af36704040dbc
using existing layer sha256:369ca498f347f710d068cbb38bf0b8692dd3fa30f30ca2ff755e211c94768150
using existing layer sha256:6e4c38e1172f42fdbff13edf9a7a017679fb82b0fde415a3e8b3c31c6ed4a4e4
creating new layer sha256:1036dcfe8bb2a3490d61eb73e932ffa8ae134954295de3e1e575191fbb26ea66
using existing layer sha256:f4d24e9138dd4603380add165d2b0d970bef471fac194b436ebd50e6147c6588
writing manifest
success

Run the custom model:

1
ollama run coder-assistant

For example:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
C:\Users\Administrator\Downloads>ollama run coder-assistant
>>> write a Python function that calculates the number of divisors of a given number
<think>
Okay, I need to write a Python function called count_divisors that takes a number as input and returns the total number of its divisors. Let me think about how to approach this.

First, what is a divisor? It's any integer that can divide the number without leaving a remainder. So for example, the divisors of 6 are 1, 2, 3, and 6.

I remember from math class that one way to find all divisors of a number efficiently is by checking divisibility up to the square root of the number. This works because if there's a divisor larger than
the square root, it must pair with a smaller divisor. So I can iterate from 1 up to sqrt(n) and check for divisors.

Wait, but what about numbers that are perfect squares? They have an odd square root, so in those cases, I need to make sure not to double count by handling the square root separately.

Let me outline the steps:

1. Initialize a counter to keep track of how many divisors there are.
2. Iterate from 1 up to the integer part of sqrt(n).
3. For each number i in this range, check if n is divisible by i (i.e., n % i == 0).
4. If it is divisible, add both i and n/i to the count, but only once if they are different.
5. Handle the case where i is exactly sqrt(n) separately to avoid adding the same divisor twice.

Wait, another point: when I find a pair (i, j) such that i * j = n, I should make sure not to count both unless necessary. So for each i from 1 to sqrt(n), if it divides n, then if i is different from
n/i, we count both. If i and n/i are the same (like when n is a perfect square), we only count once.

Alternatively, another approach is to check each number starting from 1 up to sqrt(n). For each divisor i found, increment the count by 2 unless i is exactly sqrt(n) in which case we only add 1. But I
think handling it as adding both when possible might be clearer for someone learning.

Let me test this logic with an example. Take n = 6:

- Start with i=1: 6 % 1 ==0, so check if 1 and 6/1 are different (which they are). Count +=2 → count is now 2.
- Next i=2: 6%2==0, 2 !=3. So again, add 2 → count becomes 4.
- i=3: sqrt(6) ≈2.45, so we stop before i=3? Wait, no, wait. The loop should run from 1 up to int(sqrt(n)) inclusive.

Wait, in the example for n=6, sqrt(6) is about 2.449, so the integer part is 2. So i will go up to 2, and we don't get to i=3 because that's beyond the square root. But wait, if I do int(math.sqrt(n)),
which in Python is done by import math.

Wait, no, actually, for n=6, sqrt(n) is approximately 2.45, so int(sqrt(6)) would be 2. So i will run from 1 to 2 inclusive. Then after that, if there's a pair beyond the square root, we need to account
for it.

Wait no, in this case, when n=6 and i is 2, 6%2 ==0. But then j would be 3, which is greater than sqrt(6), so our current approach of checking only up to sqrt(n) doesn't account for the larger divisor.
So perhaps we need to adjust.

Wait, another way: when I find a divisor i in the loop (up to sqrt(n)), if i != n//i, then both are divisors and should be counted. If they are equal, count just once.

So maybe I can restructure the code:

Initialize count = 0

for i from 1 to int(math.sqrt(n)) + 1: # Wait, no, that would go beyond.

Wait, better to run i up to and including sqrt(n). So for example, n=6, sqrt(6) is about 2.45, so the integer part is 2. So we loop from 1 to 2 inclusive.

In each iteration:

if i divides n (n % i ==0), then j = n // i

if j !=i → count +=2
else → count +=1

Wait, but in this case, when I'm looping up to sqrt(n) and for each i that divides n, if it's not equal to j, we add 2. If it is equal to j, which can only happen when the divisor is exactly at sqrt(n),
then we add 1.

But wait, let me test this logic with n=6:

i starts at 1: 6%1==0 → j=6. 6 !=1 → count +=2 (count becomes 2)
i=2: 6%2==0 → j=3. 3 !=2 → count +=2 (count is now 4)

So the function would return 4, which is correct because divisors of 6 are 1,2,3,6.

Another test case: n=4

sqrt(4)=2, so i runs from 1 to 2.

i=1 → j=4 → add 2 → count=2
i=2 → j=2 → since equal, add 1 → count=3. Divisors are 1,2,4.

So that works.

Another test case: n=9

sqrt(9)=3, i runs to 3.

i=1→j=9→add 2
i=3→j=3→add1
Total count is 3 → divisors of 9 are 1,3,9. Correct.

So the logic seems solid.

Now, what about n=0? Well, in mathematics, 0 has infinitely many divisors because any number can divide zero. But since we're dealing with integers and trying to compute this function, perhaps it's
better to handle edge cases where n is less than or equal to zero.

Wait, let me think: if the input is a negative number, like -6, what should be done? Divisors of 6 include both positive and negative numbers. For example, divisors of -6 are ±1, ±2, ±3, ±6. So in
that case, the total number would be 8.

But wait, perhaps I'm supposed to count all integer divisors, including negatives. But for efficiency, we can note that if n is negative, it's equivalent to its absolute value when considering divisors.
Because any divisor of a positive number is also a divisor (with sign) of its negative counterpart.

So the function should probably take the absolute value of the input n, but then in our code, since n can't be zero (as per division by zero), perhaps it's better to handle that case first.

But according to the problem statement, I'm not sure whether n can be 0. But let me think: if someone passes 0 as a number, what is expected? Well, every non-zero integer divides zero, so there are
infinitely many divisors. So in this function, perhaps we should return 0 or raise an error for n=0. Alternatively, perhaps it's better to assume that the input will be a positive integer, and handle
negatives by taking their absolute value.

But since the problem statement does not specify this, I'll proceed with assuming that the input is a non-zero integer, as per programming challenges often do. So in code:

First, import math

def count_divisors(n):
if n == 0:
return 0
# handle negative numbers by taking absolute value
n = abs(n)
count =0
sqrt_n = int(math.sqrt(n))
for i in range(1, sqrt_n +1): # Loop up to and including sqrt_n
if n %i ==0:
j = n // i
if i !=j:
count +=2
else:
count +=1
return count

Wait, but let me test this for n=6:

sqrt(6)=2.45 → int is 2.

Loop runs i from 1 to 2.

i=1: j=6 → add 2 → count=2
i=2: j=3 → add 2 → count=4

Correct.

Another test case, n=0: function returns 0. But since in code we return 0 for n==0 and let it be handled by the user if n is zero. Maybe that's better than returning an error because the problem
statement didn't specify handling n=0.

So this should work.
</think>

To calculate the number of divisors of a given non-zero integer, follow these steps:

1. **Handle Special Cases**: If the input number is 0, return 0 since every non-zero number divides 0 infinitely many times, but practically, we'll handle it by returning 0 for this problem.
2. **Take Absolute Value**: Convert the number to its absolute value to simplify divisor calculation since divisors of a negative number are the same as their positive counterparts.
3. **Initialize Counters**: Set up a counter to keep track of the total number of divisors.
4. **Calculate Divisors**: Loop from 1 to the square root of the number (inclusive). For each integer `i` in this range:
- If `i` is a divisor of the number, calculate the corresponding divisor `j = number / i`.
- If `i` and `j` are equal, add 1 to the count. Otherwise, add 2 to the count (since both `i` and `j`) are valid divisors.
5. **Return Result**: After processing all potential divisors up to the square root, return the total count of divisors.

Here's a Python implementation that follows these steps:

```python
import math

def count_divisors(n):
if n == 0:
return 0 # Handle zero as per problem statement

n = abs(n) # Convert to positive number for divisor calculation purposes

total_divisors = 0
sqrt_n = int(math.sqrt(n))

for i in range(1, sqrt_n + 1):
if n % i == 0:
j = n // i
if i != j:
total_divisors += 2
else:
total_divisors += 1

return total_divisors

# Example usage:
print(count_divisors(6)) # Output: 4 (divisors are 1, 2, 3, 6)
`` `

This function efficiently calculates the number of divisors by leveraging mathematical properties and reducing redundant checks.

>>>