日本の鉄道知識に特化したAI「JapanRailwayLLM」を作った/I Built “JapanRailwayLLM,” an AI Specialized in Japanese Railway Knowledge

日本の鉄道知識に特化したAI「JapanRailwayLLM」を作った

突然ですが、鉄道運転シミュレータ「BVE Trainsim(作者:mackoy様)」の路線や車両を制作したことはありますか?

「構文が独特で覚えるのが大変」「数万行あるMapファイルのエラー原因が分からない」「技術資料や省令を読み解くのが面倒」……など、BVEのデータ制作は、界隈でも「圧倒的に骨の折れる作業」として知られています。

SNSや制作記を覗いてみても、作者の皆さんからはこんなリアルな苦労の声が絶えません。

  • 「数万行のMapファイルに200行の修正が必要で手作業は限界」
  • 「事前調査やGoogle Earthでの位置測定だけで半年かかった」
  • 「線路脇だけでなく、田畑に囲まれた遠景まで作り込まないと殺風景で誤魔化せない」
  • 「車両制作も運転台写真の合成、音源編集、TASCや保安装置プラグインの設定でやる事が山積み」

このように、1分の走行区間を作るのすら膨大な労力と専門知識が必要であり、挫折や制作中断の大きな原因となっています。

さらに、世の中には様々なAI(LLM)が存在しますが、ChatGPTやClaudeなどの汎用AIに質問しても、BVE独自の構文(BveText2.00 等)や、国土交通省が定める「鉄道に関する技術上の基準を定める省令」といった専門的な質問には正確に答えてくれません。

そこで今回、BVE制作者の負担を少しでも減らすため、開発過程の異なる2つのAIモデルを用意しました。最初にLlamaベースのモデルを構築しましたが、起動自体に失敗しました。そこでGemma 2をベースに、鉄道の法令と規格だけを外部知識として参照するRAG版を構築し、LM Studioで試用しました。しかし、LM Studioで何度か試したところハルシネーション(事実と異なる回答)が頻発したため、現在も調整を進めています。また、方針を見直し、車両形式などの細かな情報を除く鉄道知識を一通り取り入れ、BVE路線製作への貢献を目指すGemma 4ベースのファインチューニング版を新たに作成しました。

この記事では、開発の背景、起動に失敗したLlamaベースの初期モデル、Gemma 2ベースで構築した鉄道法令・規格限定のRAG版、その試用で判明したハルシネーションと現在の調整状況、さらにハルシネーションの頻発を受けて作成したGemma 4ベースのファインチューニング版、そして開発の裏側での試行錯誤をご紹介します。

開発の背景:なぜ作ろうと思ったのか?

開発の動機は極めてシンプルです。

  1. BVE Trainsimのデータ制作が難易度高め

マップファイルの距離程指定や複雑な構文ルール、各種定義ファイル(Structure, Signal, Soundなど)の連携、車両の性能・パネル・音の設定など、挫折ポイントが山ほどあります。

 

  1. 一般的なAIは「日本の鉄道」や「BVE構文」に弱い

汎用AIは一般的なプログラミング言語(PythonやJavaScriptなど)には強いものの、BVE特有の構文や日本の鉄道技術基準などのマニアックな専門知識にはハルシネーション(嘘の回答)を起こしがちです。

 

「それなら、信頼できる情報を集めてAIに直接読み込ませ、正確に答えてくれる環境を作ればいいのでは?」と考えたのがスタートでした。

 

どうやって学習させた?(RAG構築の仕組み)

公開しているモデルは2種類です。開発当初はLlamaベースのモデルを構築しましたが、起動自体に失敗しました。そこで、1つ目としてGemma 2をベースに、鉄道の法令と規格に限定したドキュメントを外部知識として検索・参照するRAG版を構築しました。ただし、LM Studioで何度か試したところハルシネーション(事実と異なる回答)が頻発したため、現在も精度向上に向けた調整を進めています。2つ目のファインチューニング版は、この結果を踏まえてGemma 4をベースに作成したもので、車両形式などの細かな情報を除く鉄道知識を一通り取り入れ、BVE路線製作への活用を想定しています。

(※開発の流れは、最初にLlamaベースのモデルを構築したものの起動自体に失敗し、次にGemma 2ベースでRAG版を構築してLM Studioで何度か試したところハルシネーションが頻発しました。現在も調整を進める一方で、方針を見直してGemma 4ベースのファインチューニング版を作成した、という順序です。比較や検証ができるよう、現在はRAG版とファインチューニング版の両方を公開しています。)





1. 参照させた資料と前処理(データ整理の工夫)

情報の「信憑性」に徹底的にこだわり、必要な基礎知識や公式マニュアルなどの各種資料を一度Word等にきれいに取りまとめた上でテキスト抽出・ベクトル化を行い、データベース(ChromaDB)に組み込みました。

取りまとめた主な情報源

·         鉄道に関する技術上の基準を定める省令(e-Gov法令検索)

·         鉄道技術基準に関する解釈・解説資料

·         その他、鉄道に関する法令・規格の資料

Web上の情報をそのまま読み込ませるのではなく、表記の揺れや不要なレイアウトを事前に整理・整形(前処理)してから学習データ化することで、AIがより正確かつノイズなしで情報を検索・参照できるように工夫しています。鉄道の基礎知識無い状態だと

2. 使用した技術スタック

·         ベースモデル:

·         Gemma 2(RAG版)、Gemma 4(ファインチューニング版)。開発初期のLlamaベースモデルは起動自体に失敗

  • 埋め込みモデル(Embedding):
  • intfloat/multilingual-e5-base
  • ベクトルデータベース:
  • ChromaDB
  • フレームワーク:
  • LangChain

RAG版では、ユーザーが鉄道の法令や規格について質問すると、取りまとめたデータベースから該当資料を検索・参照して回答を生成します。ファインチューニング版は、車両形式などの細かな情報を除く鉄道知識を広く活用し、BVE路線製作に関する情報整理や検討を支援する用途を想定しています。

開発裏話:Google Colabでのメモリと依存関係との戦い

開発環境には Google Colab を使用しましたが、一筋縄ではいきませんでした……。よく「AIを使えば開発が楽になる」と言われますが、実際に作ってみると泥臭い作業の連続です。

  • GPUのないパソコンでも動かせれるコンセプト

BVETrainsimのやる人の中でGPU搭載しない普通のノートパソコンで製作する人もいるので、ハイスペックじゃなくても動かせれるというのを念頭に置いて作成しました。

自分のメインPCみたいに構築する人自体マイノリティーだろう。

  • GPUメモリ不足(OOM)との戦い

当初は8Bクラスのモデル(Llama-3-ELYZA-JP-8B)などで試走したものの、T4 GPUのVRAM上限に達してしまいエラーを連発。モデルの変更や軽量化を余儀なくされました。

最初にLlamaベースのモデルを構築しましたが、起動自体に失敗しました。そのためGemma 2をベースにRAG版を構築し、LM Studioで何度か試しましたが、今度はハルシネーション(事実と異なる回答)が頻発したため、現在も調整を進めています。

この結果から方針を改め、Gemma 4をベースに、車両形式などの細かな情報を除く鉄道知識を一通り取り入れたファインチューニング版を作成しました。BVE路線製作に必要な情報整理や検討を支援し、制作に貢献できるモデルを目指しています。

  • 依存ライブラリのバグ・バージョン競合

protobuf や transformers、torchvision などのライブラリ同士がバージョン互換エラーを起こし、何度もアンインストール&再インストールを繰り返して環境を修復しました。

  • llama.cpp による GGUF 化と量子化

最終的に軽量かつ高速に動作させるため、llama.cppをCPUビルドし、モデルを GGUF (f16) 形式に変換後、Q4_K_M 形式へ量子化。約5.7GBまで軽量化して Hugging Face にアップロードすることに成功しました!

現在公開中のモデル・データセット

今回構築したベースモデルやベクトルデータセットは、すべて Hugging Face 上で公開しています。

·         Gemma 2ベース

https://huggingface.co/galakenya/JapanRailwayLLM(モデル自体には鉄道知識を追加学習させず、質問のたびに鉄道の法令・規格を外部資料から検索して参照するRAG方式です。参照範囲は法令・規格に限られ、LM Studioで何度か試したところハルシネーションが頻発したため、現在も調整を進めています)

量が少ないのと思ったほど手ごたえ無かったのでgemma2をベースにしたファインチューニングを実施

※ファインチューニングも行うようになり苦戦中

10000ステップでゲームだとβ版までにこぎつけれたらしい。

·         Gemma 4ベース

https://huggingface.co/galakenya/JapanRailwaysimLLM(外部資料をその都度検索するRAG版とは異なり、車両形式などの細かな情報を除く鉄道の基礎知識をモデル自体に幅広く取り込むようファインチューニングしたモデルです。BVE路線製作など、鉄道シミュレーションゲームの制作支援を目的としています)

こちらに関してはまだほぼ未完成

期待する活用方法と今後の可能性

現在は基礎的な構文と省令データの取り込みが完了した状態です。自分自身でここからの機能拡張や継続的な開発を計画しているわけではありませんが、今回公開したモデルやデータセットをベースに、コミュニティや他の開発者の方々が自由に活用・応用していっていただければと思っています。

例えば、以下のようなツールや応用のアイデアが考えられます。

  • 構文エラー自動診断プラグイン

サウンド定義やストラクチャ定義、Mapファイルの記述ミスを自動検知して修正案を提示するエディタ拡張。

  • マップファイルのリファクタリング・自動生成ツール

冗長になった数万行のコードを最適化したり、記述補助を行うスクリプトの構築。

  • 拡張版RAGデータベースの構築

より広範な鉄道技術資料や私鉄独自の保安装置仕様などを追加インデックス化。

BVE制作者の皆さんが手作業の泥臭い構文エラーや座標計算に悩まされず、「面白い路線・リアルな景観づくり」というクリエイティブな作業に集中できる環境づくりの一助となれば幸いです。興味のある方はぜひモデルやデータセットをダウンロードして、実験やツール開発に活用してみてください!

コメント大歓迎です!

モデルを試した感想、不具合、ハルシネーションの事例、改善案などがありましたら、ぜひコメントでお知らせください。今後の調整の参考にさせていただきます。

使用に関してはgemmaと同様になるのでJapanRailwaySimLLMとしての利用制限は特にありません 。

※gemma利用規約についてかみ砕いた事が日本語で書かれている記事


 

I Built “JapanRailwayLLM,” an AI Specialized in Japanese Railway Knowledge

Have you ever created a route or rolling stock for the railway driving simulator BVE Trainsim (created by mackoy)?

“The syntax is unique and difficult to learn,” “I cannot identify the cause of an error in a Map file containing tens of thousands of lines,” and “Interpreting technical documents and ministerial ordinances is tedious”—BVE data creation is widely known in the community as an overwhelmingly labor-intensive task.

A look at social media and development logs reveals a steady stream of candid accounts from creators about the difficulties they face.

·         “A Map file with tens of thousands of lines needed 200 lines of corrections; manual work had reached its limit.”

·         “Preliminary research and location measurements using Google Earth alone took six months.”

·         “It is not enough to build only the area beside the tracks; without distant scenery surrounded by fields, the landscape looks empty and cannot be disguised.”

·         “Rolling-stock production also involves a mountain of work, including compositing cab photographs, editing audio, and configuring TASC and safety-system plug-ins.”

As these examples show, creating even one minute of track requires enormous effort and specialized knowledge, making frustration and abandoned projects common.

Many AI systems (LLMs) are available today, but general-purpose AI such as ChatGPT and Claude cannot reliably answer specialized questions about BVE-specific syntax, including BveText2.00, or Japan’s Ministerial Ordinance Providing for the Technical Standard on Railways, established by the Ministry of Land, Infrastructure, Transport and Tourism.

To reduce the burden on BVE creators, I prepared two AI models developed through different processes. I first built a Llama-based model, but it failed even to launch. I then built a Gemma 2-based RAG version that uses only railway laws and standards as external knowledge, and tested it in LM Studio. However, repeated testing produced frequent hallucinations—answers that were factually incorrect—so I am still working on adjustments. I also reconsidered the approach and created a new Gemma 4-based fine-tuned version. It incorporates a broad range of railway knowledge, excluding detailed information such as individual rolling-stock types, and aims to contribute to BVE route development.

This article covers the background of the project; the initial Llama-based model that failed to launch; the Gemma 2-based RAG version limited to railway laws and standards; the hallucinations discovered during testing and the ongoing adjustments; the Gemma 4-based fine-tuned version created in response to those frequent hallucinations; and the trial and error behind the development process.

Background: Why Did I Decide to Build It?

The motivation was very simple.

1.       Creating Data for BVE Trainsim Is Highly Challenging

There are countless stumbling blocks: specifying distances in Map files, following complex syntax rules, coordinating definition files such as Structure, Signal, and Sound, and configuring vehicle performance, panels, and audio.

 

2.       General-Purpose AI Struggles with Japanese Railways and BVE Syntax

Although general-purpose AI is good at common programming languages such as Python and JavaScript, it tends to hallucinate—provide false answers—when dealing with niche expertise such as BVE-specific syntax and Japanese railway technical standards.

 

The project began with a simple thought: “What if I collected reliable information, fed it directly to an AI, and created an environment in which it could answer accurately?”

 

How Was It Trained? (How the RAG System Works)

Two models are publicly available. At the beginning of development, I built a Llama-based model, but it failed even to launch. As the first release, I therefore created a Gemma 2-based RAG version that searches and references documents limited to railway laws and standards as external knowledge. However, repeated tests in LM Studio produced frequent hallucinations—factually incorrect answers—so I am still making adjustments to improve accuracy. Based on those results, the second model is a Gemma 4-based fine-tuned version. It incorporates a broad range of railway knowledge, excluding detailed information such as individual rolling-stock types, and is intended for use in BVE route development.

Note: The development sequence was as follows. First, I built a Llama-based model, but it failed even to launch. Next, I built a Gemma 2-based RAG version and tested it several times in LM Studio, where hallucinations occurred frequently. While continuing to adjust that version, I reconsidered the approach and created a Gemma 4-based fine-tuned version. Both the RAG and fine-tuned versions are currently available so that users can compare and evaluate them.

1. Reference Materials and Preprocessing (How the Data Was Organized)

I placed the highest priority on information reliability. After compiling the necessary foundational knowledge, official manuals, and other materials neatly in Word and similar tools, I extracted and vectorized the text and incorporated it into a ChromaDB database.

Main Sources Compiled

• Ministerial Ordinance Providing for the Technical Standard on Railways (e-Gov Law Search)

• Interpretation and explanatory materials concerning railway technical standards

• Other materials on railway laws and standards

Rather than importing online information as-is, I first standardized inconsistent wording and removed unnecessary layout elements through preprocessing. Turning the cleaned material into training data helps the AI search and reference information more accurately and with less noise. Without foundational railway knowledge, this would be difficult.

2. Technology Stack

• Base models:

• Gemma 2 (RAG version) and Gemma 4 (fine-tuned version). The Llama-based model used early in development failed to launch.

·         Embedding model:

·         intfloat/multilingual-e5-base

·         Vector database:

·         ChromaDB

·         Framework:

·         LangChain

In the RAG version, when a user asks about railway laws or standards, the system searches the compiled database for relevant documents, references them, and generates an answer. The fine-tuned version is designed to draw on broad railway knowledge—excluding detailed information such as individual rolling-stock types—to support research and planning for BVE route development.

Behind the Scenes: Battling Memory Limits and Dependencies in Google Colab

I used Google Colab as the development environment, but the process was far from straightforward. People often say that AI makes development easier, yet actually building this system involved one painstaking task after another.

·         A Concept Designed to Run Even on Computers without a GPU

Some BVE Trainsim creators work on ordinary laptops without dedicated GPUs, so I designed the model with the goal of running even on non-high-end hardware.

People who build systems like my main PC are probably a minority.

·         Battling GPU Memory Shortages (OOM Errors)

Initially, I tested 8B-class models such as Llama-3-ELYZA-JP-8B, but repeatedly encountered errors after reaching the VRAM limit of the T4 GPU. I was forced to change models and reduce their size.

I first built a Llama-based model, but it failed even to launch. I therefore created a Gemma 2-based RAG version and tested it several times in LM Studio. This time, however, hallucinations—factually incorrect answers—occurred frequently, so I am still working on adjustments.

Based on those results, I changed direction and created a Gemma 4-based fine-tuned version incorporating a broad range of railway knowledge, excluding detailed information such as individual rolling-stock types. The goal is a model that can organize and assess the information needed for BVE route development and thereby contribute to the creation process.

·         Dependency Bugs and Version Conflicts

Libraries such as protobuf, transformers, and torchvision caused version-compatibility errors with one another. I had to repair the environment by repeatedly uninstalling and reinstalling them.

·         GGUF Conversion and Quantization with llama.cpp

To achieve lightweight, fast operation, I ultimately built llama.cpp for the CPU, converted the model to GGUF (f16), and then quantized it to Q4_K_M. I reduced it to approximately 5.7 GB and successfully uploaded it to Hugging Face.

Models and Datasets Currently Available

All the base models and vector datasets created for this project are publicly available on Hugging Face.

• Gemma 2-Based RAG Version

https://huggingface.co/galakenya/JapanRailwayLLM (Rather than additionally training the model itself on railway knowledge, this RAG system searches and references external sources on railway laws and standards whenever a question is asked. Its reference scope is limited to laws and standards. Repeated tests in LM Studio produced frequent hallucinations, so adjustments are still underway.)

• Gemma 4-Based Fine-Tuned Version

https://huggingface.co/galakenya/JapanRailwaysimLLM (Unlike the RAG version, which searches external materials for each query, this model was fine-tuned to incorporate a broad range of foundational railway knowledge, excluding detailed information such as individual rolling-stock types. Its purpose is to support the development of railway simulation games, including BVE route production.)

Potential Uses and Future Possibilities

At present, the incorporation of basic syntax and ministerial-ordinance data has been completed. I am not personally planning further feature expansion or continuous development from this point, but I hope the community and other developers will freely use and adapt the models and datasets released here as a foundation.

For example, the following tools and applications could be developed.

·         Automatic Syntax-Error Diagnostic Plug-In

An editor extension that automatically detects errors in Sound and Structure definitions and Map files, then suggests corrections.

·         Map-File Refactoring and Automatic Generation Tool

A script that optimizes bloated code spanning tens of thousands of lines and assists with writing.

·         Expanded RAG Database

Additional indexing of a wider range of railway technical documents and specifications for proprietary safety systems used by private railway operators.

I hope this project helps create an environment in which BVE creators can focus on the creative work of building interesting routes and realistic scenery, rather than struggling with tedious manual syntax errors and coordinate calculations. If you are interested, please download the models and datasets and use them for experiments or tool development.

Comments Are Very Welcome!

If you have tried the models, please leave a comment with your impressions, bugs, examples of hallucinations, or suggestions for improvement. I will use your feedback as a reference for future adjustments.

Usage is governed by the same terms as Gemma, so JapanRailwaySimLLM imposes no additional usage restrictions of its own.

Note: A Japanese-language article that explains the Gemma Terms of Use in accessible language

 


コメント

コメントを投稿

このブログの人気の投稿

名鉄の存廃問題と廃線の決まった路線(2025年2月時点)/Meitetsu's survival issues and the routes that were decided to be discontinued (as of February 2025)

JR東日本の小規模駅で進む「簡易Suica改札機」導入とは?背景・現地の様子・今後の展望/What’s Behind JR East’s “Simplified Suica Gate” Rollout at Small Stations?