Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Node.jsでllama_2_.pdf
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Optimisuke
February 21, 2024
Technology
230
0
Share
Node.jsでllama_2_.pdf
kansai.ts #5 2024/02/21 で発表したスライドです。
Optimisuke
February 21, 2024
More Decks by Optimisuke
See All by Optimisuke
量子コンピュータ勉強会#1
optimisuke
0
38
LangChainやるならPythonよりTypeScriptの方がいんじゃね?
optimisuke
1
1.3k
オンオフの切り替え
optimisuke
0
62
Other Decks in Technology
See All in Technology
続 運用改善、不都合な真実 〜 物理制約のない運用改善はほとんど無価値 / 20260518-ssmjp-kaizen-no-value-without-physical-constraints
opelab
2
220
Vision Banana: Image Generators are Generalist Vision Learners
kzykmyzw
0
380
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
15
100k
AIを賢くしたいなら、まずは人間の改善ループから
subroh0508
0
120
20260515 ログイン機能だけではないアカウント管理を全体で考える~サービス設計者向け~
oidfj
0
620
AI-Assisted Contributions and Maintainer Load - PyCon US 2026
pauloxnet
1
150
(きっとたぶん)人材育成や教育のような何かの話
sejima
0
750
クラウドネイティブ DB はいかにして制約を 克服したか? 〜進化歴史から紐解く、スケーラブルアーキテクチャ設計指針〜
hacomono
PRO
6
1k
写真で見るAWS Summit Singapore 2026
k_adachi_01
0
110
AWS WAFの運用を地道に改善し、自社で運用可能にするプラクティス
andpad
1
220
カオナビに Suspenseを導入するまで / The Road to Suspense at kaonavi
kaonavi
1
450
Every Conversation Counts
kawaguti
PRO
0
230
Featured
See All Featured
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
150
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2k
Raft: Consensus for Rubyists
vanstee
141
7.4k
Faster Mobile Websites
deanohume
310
31k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Un-Boring Meetings
codingconduct
0
290
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.5k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
550
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
130
Ruling the World: When Life Gets Gamed
codingconduct
0
230
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
780
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.7k
Transcript
Node.jsでllama 2🦙
はじめに ローカルでもLarge Language Models (LLM) を動かしてみたい MetaのLlama 2は色々できるらしい Pythonでも良いけど、TypeScriptでもやり たい
generated by chatgpt
Llama 2 MetaのLlama 2は、研究用途と商業用途 の両方に対応したオープンソースの大規 模言語モデル いろんなライブラリ・サービスがあってエコ システムがいい感じ rinna、ELYZA、stability.ai による日本語モ
デルも公開されている https://llama.meta.com/ generated by chatgpt
llama.cpp LlamaをC/C++で実装したもの。Apple silicon のGPUにも対応している。 https://github.com/ggerganov/llama.cpp?tab=readme-ov-file
node-llama-cpp Node.jsでllama.cppを動かすためのライブラリ Node-APIを使ってllama.cppを呼び出している Node-APIはネイティブアドオンの作成を可能にす るNode.jsのAPI。ネイティブアドオンとは、CやC++ などのコンパイル言語で書かれ、Node.jsの JavaScriptランタイムと統合されるモジュールのこ と。 https://github.com/withcatai/node-llama-cpp https://nodejs.org/api/n-api.html
🦜🔗 LangChain.js LangChainは、大規模言語モデルを活用し たアプリケーションの構築を支援するフレー ムワーク LangChain.jsはJavaScript/TypeScript版 (Python版がメイン) kansai.ts #4で話した https://www.langchain.com/
https://github.com/langchain-ai/langchainjs https://speakerdeck.com/optimisuke/langchainyarunarapythonyoritypescriptnofang-gainziyane
試してみた generated by chatgpt
node-llama-cpp 試してみた import { fileURLToPath } from "url"; import path
from "path"; import { LlamaModel, LlamaContext, LlamaChatSession } from "node-llama-cpp"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const model = new LlamaModel({modelPath: path.join(__dirname, "models", "ELYZA-japanese-Llama-2-7b-fast-instruct-q4_K_M.gguf")}); const context = new LlamaContext({ model }); const session = new LlamaChatSession({ context }); const q1 = "元気?"; console.log("User: " + q1); const a1 = await session.prompt(q1); console.log("AI: " + a1);
LangChain.js 試してみた import { LlamaCpp } from "@langchain/community/llms/llama_cpp"; const llamaPath
= "/Users/hoge/hello-node-llama-cpp/models/ELYZA-japanese-Llama-2-7b-fast-instru ct-q4_K_M.gguf"; const model = new LlamaCpp({ modelPath: llamaPath }); const question = "ラマって何?"; console.log(`User: ${question}`); const response = await model.invoke(question); console.log(`AI : ${response}`);
LangChain.js 試してみた import { ChatLlamaCpp } from "@langchain/community/chat_models/llama_cpp"; const llamaPath
= "/Users/hoge/hello-node-llama-cpp/models/ELYZA-japanese-Llama-2-7b-fast-instru ct-q4_K_M.gguf"; const model = new ChatLlamaCpp({ modelPath: llamaPath, temperature: 0.7 }); const stream = await model.stream("ラマって何?"); for await (const chunk of stream) { console.log(chunk.content); }
コードを見てみた generated by chatgpt
node-llama-cppのコードを見てみた class LLAMAModel : public Napi::ObjectWrap<LLAMAModel> { public: llama_model_params model_params;
llama_model* model; LLAMAModel(const Napi::CallbackInfo& info) : Napi::ObjectWrap<LLAMAModel>(info) { model_params = llama_model_default_params(); // Get the model path std::string modelPath = info[0].As<Napi::String>().Utf8Value(); llama_backend_init(false); model = llama_load_model_from_file(modelPath.c_str(), model_params); } };
node-llama-cppのコードを見てみた execute_process(COMMAND node -p "require('node-addon-api').include.slice(1,-1)" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE NODE_ADDON_API_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
include_directories(${NODE_ADDON_API_DIR} ${CMAKE_JS_INC}) include_directories("llama.cpp") file(GLOB SOURCE_FILES "addon.cpp") target_link_libraries(${PROJECT_NAME} "llama") execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
node-llama-cppのコードを見てみた import {createRequire} from "module"; const require = createRequire(import.meta.url); export
async function loadBin(): Promise<LlamaCppNodeModule> { const usedBinFlag = await getUsedBinFlag(); if (usedBinFlag === "prebuiltBinaries") { const prebuildBinPath = await getPrebuildBinPath(); return require(prebuildBinPath); } }
LangChain.jsのコードを見てみた import { LlamaModel, LlamaContext, LlamaChatSession } from "node-llama-cpp"; import
{ LlamaBaseCppInputs, createLlamaModel, createLlamaContext, createLlamaSession} from "../utils/llama_cpp.js"; export class LlamaCpp extends LLM<LlamaCppCallOptions> { _model: LlamaModel; _context: LlamaContext; _session: LlamaChatSession; constructor(inputs: LlamaCppInputs) { this._model = createLlamaModel(inputs); this._context = createLlamaContext(this._model, inputs); this._session = createLlamaSession(this._context); } async _call(prompt: string): Promise<string> { const promptOptions = {}; const completion = await this._session.prompt(prompt, promptOptions); return completion; } } https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-community/src/llms/llama_cpp.ts
llama.cppのコードを見てみた ifdef LLAMA_METAL MK_CPPFLAGS += -DGGML_USE_METAL MK_LDFLAGS += -framework Foundation
-framework Metal -framework MetalKit OBJS += ggml-metal.o ifdef LLAMA_METAL_NDEBUG MK_CPPFLAGS += -DGGML_METAL_NDEBUG endif endif # LLAMA_METAL ifdef LLAMA_METAL ggml-metal.o: ggml-metal.m ggml-metal.h $(CC) $(CFLAGS) -c $< -o $@ endif # LLAMA_METAL https://github.com/ggerganov/llama.cpp/blob/master/Makefile
参考:Ollama🦙 https://ollama.ai/
おわりに Llama 2すごい llama.cppすごい Node-APIすごい LLMはPythonじゃなくてもいんじゃね? generated by chatgpt