This tutorial shows how to download and run the phi3:mini-128k model using Ollama and generate Java code locally.
Why Phi-3 Mini?
We use phi3:mini-128k because it is:
- Fast to run on local machines
- Uses less RAM compared to larger models
- Good enough for everyday developer tasks
- Supports a large context window (128k tokens)
This makes it ideal for laptops and local development environments.
Download the Model
Use Ollama to download the model:
ollama pull phi3:mini-128k
The model is downloaded once and stored locally for offline use.
Run the Model
Start an interactive session with the model:
ollama run phi3:mini-128k
Prompt the Model
After the model starts, enter the following prompt:
Create a Java program to calculate factorial of a given number
The model will generate a Java example similar to what you would expect from an IDE assistant or coding helper.
One-Line Command
You can also run the model with a single command:
$ ollama run phi3:mini-128k "Can you create a Java program to create factorial of a given number"
Yes, I can help with that. Here's an example code in Java:
```java
public class Factorial {
public static void main(String[] args) {
int num = 5; // Change this to the desired input value for a different result
System.out end=System.out.println("The factorial of "+num+" is: " + calculateFactorial(num));
}
public static long calculateFactorial(int n) {
if (n == 0) return 1;
else return n * calculateFactorial(n-1);
}
}
```
This code uses recursion to calculate the factorial of a given number. It takes an integer input `num`, and
recursively multiplies it with its previous values until reaching zero, returning one as the base case (since 0! =
1). You can change the value in line 3 for different results:
```java
int num = // your desired input here;
System.out.println("The factorial of "+num+" is: " + calculateFactorial(num));
```
You can also create a more efficient version using loops instead of recursion, like this code below (note that it
uses BigInteger to handle larger numbers):
```java
import java.math.BigInteger;
public class Factorial {
public static void main(String[] args) {
int num = 50; // Change this value for a different result, note the potential performance impact when
using higher values
System.out.println("The factorial of "+num+" is: " + calculateFactorial(num));
}
public static BigInteger calculateFactorial(int n) {
if (n == 0) return new BigInteger("1");
BigInteger result = new BigInteger("1");
while (n > 1) {
result = result.multiply(new BigInteger(String.valueOf(n));
n--;
}
return result;
}
}
```
Why This Matters for Developers
- No cloud APIs or API keys
- Runs fully offline
- Fast feedback for small coding tasks
- Safe for experimenting with prompts and ideas
Summary
Phi-3 Mini is a lightweight and efficient LLM that runs well on local machines. Using Ollama, developers can quickly download the model and generate useful code examples without relying on external services.
|