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
Optimisuke
February 21, 2024
Technology
0
210
Node.jsでllama_2_.pdf
kansai.ts #5 2024/02/21 で発表したスライドです。
Optimisuke
February 21, 2024
Tweet
Share
More Decks by Optimisuke
See All by Optimisuke
量子コンピュータ勉強会#1
optimisuke
0
21
LangChainやるならPythonよりTypeScriptの方がいんじゃね?
optimisuke
1
1.1k
オンオフの切り替え
optimisuke
0
59
Other Decks in Technology
See All in Technology
ZOZOマッチのアーキテクチャと技術構成
zozotech
PRO
3
1.2k
ヒューリスティック評価を用いたゲームQA実践事例
gree_tech
PRO
0
430
ガチな登山用デバイスからこんにちは
halka
1
200
攻撃と防御で実践するプロダクトセキュリティ演習~導入パート~
recruitengineers
PRO
4
1.8k
DuckDB-Wasmを使って ブラウザ上でRDBMSを動かす
hacusk
1
140
衝突して強くなる! BLUE GIANTと アジャイルチームの共通点とは ― いきいきと活気に満ちたグルーヴあるチームを作るコツ ― / BLUE GIANT and Agile Teams
naitosatoshi
0
290
Kubernetes における cgroup driver のしくみ: runwasi の bugfix より
z63d
2
120
「魔法少女まどか☆マギカ Magia Exedra」のグローバル展開を支える、開発チームと翻訳チームの「意識しない協創」を実現するローカライズシステム
gree_tech
PRO
0
440
モダンフロントエンド 開発研修
recruitengineers
PRO
10
6.2k
JuniorからSeniorまで: DevOpsエンジニアの成長ロードマップ
yuriemori
2
350
大「個人開発サービス」時代に僕たちはどう生きるか
sotarok
15
7.4k
おやつは300円まで!の最適化を模索してみた
techtekt
PRO
0
260
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
800
Side Projects
sachag
455
43k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Thoughts on Productivity
jonyablonski
69
4.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
A designer walks into a library…
pauljervisheath
207
24k
Balancing Empowerment & Direction
lara
3
610
Facilitating Awesome Meetings
lara
55
6.5k
The Invisible Side of Design
smashingmag
301
51k
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