WSLでlocal版の Open Interpreter を動かす

Open InterpreterをWSLで動かしてみたときのメモ。

https://github.com/KillianLucas/open-interpreter

# Open Interpreter のインストール

まず、新しい環境をpyenvとvenvで構築する。
Anacondaとか使う場合は適宜読み替える。既存の環境が変わっても気にしない場合はスキップする。
ただし、pythonのバージョンは3.10以上である必要がある。

```bash
> mkdir interpreter
> cd interpreter
> pyenv install 3.11.3
> pyenv global 3.11.3
> python -m venv ./venv
> . ./venv/bin/activate
```

open interpreterをインストール。

```bash
> pip install open-interpreter 
```

Code Llamaを試してみたかったので、ローカルモードで実行する。
モデルをダウンロード後、llama-cpp-pythonのインストールでエラーになる。
(なお、環境は Windows 10 上の Ubuntu 22.04.3 LTS)

```bash
> interpreter --local
(略)
  ERROR: Failed building wheel for llama-cpp-python
Failed to build llama-cpp-python
ERROR: Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based projects
```

[このあたり](https://github.com/oobabooga/text-generation-webui/issues/1534)を参考にllama-cpp-pythonをインストール。

```bash
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-11 g++-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 60 --slave /usr/bin/g++ g++ /usr/bin/g++-11
pip install --upgrade pip
pip install --upgrade setuptools wheel
sudo apt-get install build-essential
set "CMAKE_ARGS=-DLLAMA_OPENBLAS=on"
set "FORCE_CMAKE=1"
pip install llama-cpp-python --no-cache-dir
```

再度実行すると成功し、プロンプトが表示される。

```bash
> interpreter --local
(略)
▌ Model set to TheBloke/CodeLlama-13B-Instruct-GGUF

Open Interpreter will require approval before running code. Use interpreter -y to bypass this.

Press CTRL-C to exit.

>
```
# ChatGPT-4との比較
GPUのメモリの関係で動かせる限界の13B Medium Sizeしか動かせなかったが、
Code Llama と、ChatGPT-4 で出力を比べてみた。

とりあえず100番目の素数を聞いてみた。

## Code Llama 13B Medium

方法が分からなかったらしい。

```bash
> tell me the 100th prime number.
```
```python
  import math
  print(math.prime(100))
```
```bash
  Would you like to run this code? (y/n)

  y
```
```python
  import math
  print(math.prime(100))
```
```bash
  File "<stdin>", line 6, in <module>
  AttributeError: module 'math' has no attribute 'prime'


  I would like to know how to find the 100th prime number.

>
```

## ChatGPT-4

当然だが、Code Llamaより賢い。

```bash
> tell me the 100th prime number.

  Sure, I can do that. Here is the plan:

   1 Write a function to check if a number is prime.
   2 Iterate from 2 upwards, checking each number to see if it's prime.
   3 Stop when we have found the 100th prime number.

  Let's start with the first step, writing a function to check if a number is prime.
```
```python
  def is_prime(n):
      if n <= 1:
          return False
      if n == 2:
          return True
      if n % 2 == 0:
          return False
      i = 3
      while i * i <= n:
          if n % i == 0:
              return False
          i += 2
      return True

  # Test the function with a known prime number
  print(is_prime(13))
```
```bash
  Would you like to run this code? (y/n)

  y
```
```python
  def is_prime(n):
      if n <= 1:
          return False
      if n == 2:
          return True
      if n % 2 == 0:
          return False
      i = 3
      while i * i <= n:
          if n % i == 0:
              return False
          i += 2
      return True

  # Test the function with a known prime number
  print(is_prime(13))
```
```bash
  True

  The function to check if a number is prime is working correctly, as it correctly identified 13 as a prime number.

  Now, let's move on to the next step of the plan: iterating from 2 upwards, checking each number to see if it's
  prime, and stopping when we have found the 100th prime number.
```
```python
  def nth_prime(n):
      count = 0
      num = 2
      while True:
          if is_prime(num):
              count += 1
              if count == n:
                  return num
          num += 1

  # Find the 100th prime number
  print(nth_prime(100))
```
```bash
  Would you like to run this code? (y/n)

  y
```
```python
  def nth_prime(n):
      count = 0
      num = 2
      while True:
          if is_prime(num):
              count += 1
              if count == n:
                  return num
          num += 1

  # Find the 100th prime number
  print(nth_prime(100))
```
```bash
  541

  The 100th prime number is 541.
```