晴れ。 寒い日。
ここ数日は寝るときに湯たんぽを使っており、それにより内臓の冷えが解消され胃痛が和らぐ... となってほしかったがまぁそう簡単にはいかない。 逆流の感じはだいぶ治ってきたと思うし朝はゲップも少なく比較的調子がいいような気がするけど、やはり医者の言う通りしばらくはちゃんと薬を続けるのがよさそうだ。
こないだ試したアレ。 GitHub の Ollama のページを見ると使えるモデル一覧が載っていて、そこにはプログラムコードに特化した (らしい) codellama なるものがある。 phi3:mini や mistral と比べてどうだろうか。 適当に Wikipedia から持ってきた Python の関数を Emacs Lisp に書き直させてみた。
質問文 (英語が自然かどうかは知らない):
Write the following Python function in Emacs Lisp:
def fib(n: int) -> int:
if n < 2:
return n
else:
return fib(n=n-1) + fib(n=n-2)
codellama:
(defun fib (n)
(if (< n 2)
n
(+ (fib (- n 1))
(fib (- n 2)))))
mistral:
In Emacs Lisp, the equivalent of your Python function could be as follows:
(defun fib (n)
(if (or (< n 2) (not (integerp n)))
(error "Argument must be an integer greater than or equal to 2.")
(if (< n 2)
n
(+ (fib (1- n)) (fib (1- n 1))))))
This function checks if the argument is an integer greater than or equal
to 2, and if not it raises an error. If the argument is less than 2, it
returns the number itself; otherwise, it recursively calls fib with n - 1 and n - 2.
phi3:mini
(defun emacs-fib (n)
(cond ((<= n 2) n)
(t (- (+ (emacs-fib (1- n)) (emacs-fib (1- n))))))))
これは... codellama だけが正解、残りのふたつは動きやしない。 mistral のは再帰呼び出しの結果必ずエラーが出るし、phi3:mini に至っては中身の間違いに加えて括弧の数も間違っている。 AI が得意そうな Python スクリプトを入力して、AI が得意そうな Lisp を出させてみたというのに。
まぁ、phi3:mini はかなり小さなモデルなので仕方ないところはある。 mistral と codellama はどちらもパラメーター 7B (70 億) でサイズ的には変わらないが、やっぱり code と名前に付くだけのことはあるんだな。
ついでなので 3.8B (38 億) の phi4-mini も試しておくか。 phi3:mini と同じサイズながらずいぶんと進化しているらしいが...
phi4-mini
Here's how you can translate your given fib function into Emacs Lisp:
(defun my-fib (n)
"Calculate the nth Fibonacci number."
(if (< n 2)
n
(+ (my-fib (- n 1))
(my-fib (- n 2)))))
You can use M-x eval-buffer to evaluate this code in your Emacs buffer,
and then call (my-fib 5) for example. This will give you the Fibonacci
number at position 5.
Note that while it's functional, calculating large values with recursion
like fib(n) is inefficient due to its exponential time complexity; it may
cause a stack overflow or slow down significantly as n increases because
of repeated calculations already done by previous calls (this problem can
be mitigated using memoization in Emacs Lisp).
おやおや? 冗長な説明文付きの正解を出してきやがった。 パラメーターサイズだけではわからんもんだな!