官方

此篇博文说明如何使用农Ollama下载并运行phi3:mini-128k,然后用本地大模型生成java代码。

选用Phi-3 Mini的原因:

  1. 本地运行快
  2. 使用内存少。
  3. 足够满足日常开发任务。
  4. 支持128k的上下文窗口。

Phi-3 Mini适合笔记本和本地环境。

下载模型

使用Ollama命令下载模型

ollama pull phi3:mini-128k

下载好的模型即可离线使用。

运行模型

与大模型开启一次交互会话

ollama run phi3:mini-128k

提示模型

模型启动后,输入下面提示词:

Create a Java program to calculate factorial of a given number

模型将会生成你想要的Java示例代码。

结果

运行后命令后,得到如下:

$ 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;
    }
}
```

测试

输入:

更多推荐