0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

背景

  • ローカル(Windows)で llm-jp-3.1-13b-instruct4 を CPU 実行して検証したかった
    • 量子化は軽め(IQ2_XS)を選択
  • Python(LangChain)で、Tool 対応してなくても、Tool が使えることを確認したかった

概要(TL;DR)

  • LangChain で Tool を使う方法は2系統ある
    • ネイティブ tool calling(モデルが対応している必要あり)
    • ReAct(テキストの Thought/Action/Observation をパースする)

ついでに、

  • ollama run500: unable to load model になるとき、まず GGUFのファイルサイズを疑う(途中で落ちてることがある)
  • GGUFがGB級のはずなのにMB級なら、再ダウンロードで解決しやすい

Tool Call がない場合に、Tool を使う

LangChain 側の ChatOllama ドキュメントには、Tool calling の説明があります。

  • Tool calling は Ollama側とモデル側が tool calling 対応している必要がある
  • 対応モデルの探し方(Toolsカテゴリ)も案内されている

Ollama tool calling uses the OpenAI compatible web server specification...
Make sure to select an ollama model that supports tool calling...

— LangChain Docs: ChatOllama(Tool calling)

Ollama 側の公式ブログにも「tools フィールドでツール一覧を渡す」「tool_calls が返る」などが書かれています。

To enable tool calling, provide a list of available tools via the tools field in Ollama’s API.
Supported models will now answer with a tool_calls response.

— Ollama Blog: Tool support(2024-07-25)

一方で ReAct は、Reasoning + Acting のループでツールを呼ぶパターンで、LangChain の Agents ドキュメントでも触れられています。

Agents follow the ReAct (“Reasoning + Acting”) pattern, alternating between brief reasoning steps with targeted tool calls and feeding the resulting observations...

— LangChain Docs: Agents(Tool use in the ReAct loop)

今回のモデル(llm-jp GGUF)では「ネイティブ tool calling は非対応」なので、ReAct(テキストパース)で確認。というか、出来るよね?ってことの実証コードを用意したかったというのが本来の目的だった :sweat:

躓き 1) ReAct のパースエラー:Action Input が出ない

Python で create_react_agent 相当の流れを回すと、以下エラー

Invalid Format: Missing 'Action Input:' after 'Action:'

対策:few-shotでフォーマットを「強制」する

いきなり万能に直るわけではないですが、経験的に効きやすいのはこれ。

  • システムプロンプトで 行フォーマットを固定
  • shortな few-shot を1〜2個だけ入れる
  • Action:Action Input: は必ずセット、など

ReAct は「モデルが形式を守ること」に強く依存する
守らないモデルに対しては、プロンプト以外にも

  • stop トークン調整
  • 出力のリトライ
  • そもそも tool calling 対応モデルに切り替える(本末転倒だが :sweat_smile:

躓き 2) Ollama が 500 Internal Server Error で落ちる

Ollama に GGUF を食わせて起動しようとしたら、ollama run が落ちる。

500 Internal Server Error: unable to load model

原因は「GGUFが不完全ダウンロード」だった

  • 正しいGGUFは 約4GB級(量子化にもよる)
  • なのに手元のファイルが 268MB しかなかった

つまり「モデルが壊れてる」じゃなくて「途中で落ちてた」。

Hugging Face のモデルページでは、量子化ごとのサイズ目安がまとまっているのでそこで確認
(例:2-bit の IQ2_XS が約4.24GB、Q2_K が約5.25GB など)

PowerShell 例(URLとファイル名は適宜差し替え):

# 保存先(例)
$OutFile = "C:\path\to\scripts\local_llm\llm-jp-3.1-13b-instruct4-IQ2_XS.gguf"

# ダウンロード(curl.exe を明示すると挙動が安定しやすい)
curl.exe -L -o $OutFile "https://huggingface.co/.../resolve/main/....gguf"

# サイズ確認(GB級ならOK)
(Get-Item $OutFile).Length / 1GB

ここで MB級だったら、ほぼ「途中で切れてる」です。

  • Modelfile Reference:

起動確認

ollama run llm-jp-13b "こんにちは"

ここが通れば、まず「ローカルLLMとして起動」はOK。

あとがき

ToolCall がないモデルなので、Agent が使えない、と同僚が言ってたので、
「あれ?出来るはずよね」と思い試してみたら、案の定できた。

ただ、一番躓いたのが、「Ollamaが500で落ちる」だった :laughing:
GGUFが途中で落ちてただけだった。

参考リンク

  • Ollama: Tool support(tool_calls / tools フィールド)

  • Ollama Docs(Modelfile Reference への導線あり)

  • LangChain: ChatOllama integration(セットアップ / tool calling)

  • LangChain: Agents(ReAct ループの説明あり)

  • Hugging Face: GGUF配布元(サイズ目安・IQ2_XS等)

  • Qiita Markdown(記法の注意点など)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?