Local Tool Calling with llamacpp

LangChain
20 Jun 202409:13

Summary

TLDRローカルモデルの使用と機能呼び出しの重要性について説明するビデオです。ローカルモデルを使って高精度を保ちながら推論速度を上げる方法や、LLMに外部ツールをバインドして特定のタスクを実行させる仕組みについて紹介しています。Hermes 2 Pro llama 38bモデルを使った具体例を示し、ローカル環境でツールコールを実行するプロセスを詳細に解説。特に、ツールコールがエージェントにとって重要な機能であることを強調し、ローカルエージェントのセットアップの将来の可能性について触れています。

Takeaways

  • 🌐 LanceがL chainからローカルモデルのハイレベルな興味を報告しています。ローカルモデルとは、ノートPCやデバイス上でLLMを実行できる機能です。
  • 🛠️ LLMの重みを量子化または精度を32ビットから8ビットなどに下げることで、モデルサイズを小さくして推論速度を上げ、精度を最小限に損なわないようにします。
  • 📈 LLMのローカル実行はAppleデバイスなどでもサポートされ、プライバシーを保護しながらモデルを実行できます。
  • 🔧 ツール呼び出しや関数呼び出しは、LLMの機能を拡張する重要な要素で、外部のツールをLLMにバインドして使用します。
  • 🔄 LLMはツールを実行する能力はなく、入力に基づいてツールを実行するためのペイロードを返すことができます。
  • 🔗 Lanceはllama.cppという新しい統合を紹介しており、これはツール呼び出しを有効にします。
  • 📚 Lanceはローカルで実行するための適切なモデルを選択する重要性を強調しており、Hermes 2 Pro llama 38bはツール呼び出しにチューニングされています。
  • 💻 ローカルでの実行には、大きなパラメータモデルは推奨されず、8億パラメータモデルがローカルPCで実行できるサイズだと述べています。
  • 🔧 Lanceはローカル環境の設定方法や、Apple MetalなどのGPUの活用方法についてガイドを提供しています。
  • 📝 ローカルでの実行例として、LanceはノートPCでモデルを初期化し、ツール呼び出しを実演しています。
  • 🤖 ツール呼び出しは構造化された出力を生成する場合にも使用でき、ジョークの例として実際に動作を確認しています。
  • 🚀 LanceはローカルLLMの進化と、将来的にはローカルエージェントの設定にツール呼び出しを使用する可能性について触れています。

Q & A

  • ローカルモデルとは何ですか?

    -ローカルモデルとは、例えばノートパソコンやデバイス上で直接実行可能な言語モデル(LM)のことを指します。これにより、モデルのサイズを小さくしたり、推論の速度を上げたりすることができます。

  • モデルのウェイトを量子化することとは何を意味しますか?

    -モデルのウェイトを量子化とは、例えば32ビットの浮動小数点数から8ビットなどに精度を下げることで、モデルのサイズを小さくし、推論速度を上げることができます。

  • ツール呼び出しとは何を意味していますか?

    -ツール呼び出しは、言語モデル(LM)の外部で定義されたツールを呼び出すことです。LMはそのツールを実行する機能を持たないが、入力に基づいてツールを実行するためのペイロードを返すことができます。

  • llama CDPとの統合とは何を意味しますか?

    -llama CDPとの統合は、ツール呼び出しを可能にする機能を提供する、llama CDPと言語モデルを連携させることを意味します。これにより、ローカルでモデルを実行し、ツール呼び出しを活用することが可能になります。

  • Hermes 2モデルとは何ですか?

    -Hermes 2モデルは、ツール呼び出しに特化してチューニングされた8億パラメーターの言語モデルです。このモデルは、ツール呼び出しの評価で90%のスコアを獲得しており、ローカルでの実行に適しています。

  • ローカルで言語モデルを実行するためにはどのような環境設定が必要ですか?

    -ローカルで言語モデルを実行するためには、AppleのデバイスであればMetalを利用し、環境によってはGPUの利用も検討する必要があります。また、モデルの実行に必要なパッケージをインストールすることも重要です。

  • モデルの初期化とは何を意味していますか?

    -モデルの初期化とは、言語モデルを実行するための準備を整えることを意味します。これには、モデルのウェイトをロードし、必要なライブラリを初期化する作業が含まれます。

  • ツールのバインドとは何を意味していますか?

    -ツールのバインドとは、言語モデルにツールを関連付けることを意味します。これにより、LMは入力に応じてツールを実行するためのペイロードを返すことができます。

  • ローカルで実行されたモデルのパフォーマンスはどのように評価されますか?

    -ローカルで実行されたモデルのパフォーマンスは、ツール呼び出しの正確性や構造化された出力を評価する指標に基づいて評価されます。たとえば、Hermes 2モデルは、ツール呼び出しの評価で90%のスコアを獲得しています。

  • ローカルで実行される言語モデルの将来性についてどう考えていますか?

    -ローカルで実行される言語モデルは、より良いチューニングやデータに基づいて徐々に性能が向上していくと予想されます。将来的には、より大きなモデルもローカル環境で実行できるようになり、より高度な機能を提供するでしょう。

Outlines

00:00

🤖 ローカルモデルでのツール使用と機能呼び出しの紹介

Lanceは、ローカルマシン上でLLM(大規模言語モデル)を実行する能力に高い関心があることを指摘。特に、デバイス上でモデルの重みを量子化または精度を下げることで、モデルサイズを小さくして推論速度を上げ、精度を最小限に損なわないようにすることができる。このプロセスには、Appleのデバイスを含む多くのプラットフォームが対応しており、特にHermes 2 Pro llama 38bモデルはツール呼び出しに適しています。このモデルはFireworks AIと提携し、関数呼び出しの評価で90%、構造化出力の評価で84%の精度を達成している。しかし、8億パラメーターモデルはこの用途には少し小さすぎるため、より大きなラップトップで70億パラメーターモデルを実行することを提案している。

05:02

🔧 ローカル環境でのツール呼び出しの実践

Lanceは、ローカル環境でツール呼び出しを実行する方法を説明し、新しいchat llama CPPの統合を紹介。ツール呼び出しは、外部のツールに入力を渡し、モデルがその入力に基づいて必要なペイロードを返すことができる機能である。このプロセスは、モデルが文字列を文字列に変換するのではなく、入力と生の関数から抽出されたスキーマに基づいて関数を実行するペイロードを返すことに重点を置いている。Lanceは、Hermes 2 Pro llama 3モデルを使用して、ローカルで実行し、関数呼び出しの正確性と信頼性をテストした。また、ローカルで実行する際には、Appleのメタルを使用するなど、環境の設定に関するガイドも提供している。

Mindmap

Keywords

💡LLM(Large Language Model)

LLMとは、非常に大きなサイズと複雑さを持った言語モデルを指します。このビデオでは、ローカル環境でLLMを実行する能力に焦点が当てられており、例えばノートパソコン上で実行することが可能です。LLMは、自然言語処理の分野で重要な役割を果たしており、ビデオの中心となるテーマの一つです。例えば、「local models」という用語がビデオ内で使用され、ローカル環境での実行を指しています。

💡量子化(Quantization)

量子化は、機械学習モデルの重みを低いビット幅に変換することで、モデルのサイズを縮小し、推論速度を上げることができます。ビデオでは、32ビットの浮動小数点数から8ビットなどにダウンサンプリングすることで、モデルの精度をほとんど失わずにローカルでの実行が可能になるという点が強調されています。

💡推論(Inference)

推論とは、機械学習モデルが学習した知識を用いて新たなデータに対する予測を行うプロセスを指します。ビデオでは、量子化によってモデルサイズを縮小し、ローカルでの推論速度を向上させる方法が説明されています。

💡ツール呼び出し(Tool Calling)

ツール呼び出しは、外部の機能やプロセスを機械学習モデルから呼び出すことです。ビデオでは、LLMが特定の入力に基づいて必要なペイロードを返すことで、外部ツールを実行することができるという点が説明されています。これは、ビデオの主なメッセージの一つであり、ローカルでのLLM実行の柔軟性を高める重要な概念です。

💡Llama CPP

Llama CPPは、ビデオ内で紹介されているPythonのラッパーであり、Llama CDPの機能を利用することができます。ビデオでは、Llama CPPを用いてローカル環境でLLMを初期化し、ツール呼び出しを行う方法が説明されています。

💡Hermes 2 Pro

Hermes 2 Proは、ビデオ内で紹介されている8億パラメーターのモデルで、ツール呼び出しにチューニングされています。ビデオでは、このモデルがローカル環境で実行され、ツール呼び出しの能力を有していることが強調されています。

💡ローカル環境(Local Environment)

ローカル環境とは、ユーザーのコンピューターやデバイス上で直接アプリケーションやモデルを実行する環境を指します。ビデオでは、ローカル環境でのLLMの実行が主題であり、その利便性やプライバシーの保護の点が議論されています。

💡モデルのファインチューニング(Fine-tuning)

ファインチューニングは、既存の機械学習モデルに対して新しいデータセットを使用してさらに学習させ、特定のタスクに適したモデルを作成するプロセスです。ビデオでは、Hermes 2 Proがツール呼び出しに特化してファインチューニングされていることが触れられています。

💡構造化出力(Structured Output)

構造化出力とは、モデルが特定の形式やスキーマに従ってデータを出力するプロセスです。ビデオでは、ツール呼び出し機能を使って構造化データを生成することができることが示されています。

💡Lang Smith

Lang Smithは、ビデオ内で言及されているツールであり、モデルの実行やログを確認することができるインターフェースです。ビデオでは、Lang Smithを使ってローカルで実行されたLLMのログを確認し、ツール呼び出しの結果を可視化する方法が説明されています。

Highlights

ローカルモデルの高い関心:ローカルデバイスでLLMを実行する能力が高い関心を集めている。

モデルの量子化:LLMのウェイトを量子化し、精度を低下させることなくモデルサイズを縮小し、推論速度を向上させる。

ツール使用の重要性:エージェントのためのツールコーリングの重要性について議論。

Llama CPPとの統合:Llama CPPとの新しい統合がツール使用を可能にする。

Hermes 2 Pro llama 38Bモデル:ツールコーリングに最適化された8億パラメータモデル。

ツールバインディング:LLMにツールをバインドするプロセスの詳細。

基本機能のデモ:ローカルでLLMを実行し、英語からフランス語への翻訳が成功。

ツールコーリングの実例:場所と単位を入力として天気情報を取得するツールをバインド。

LangSmithとの連携:ツールコーリングの結果がLangSmithに表示される。

構造化出力のサポート:LLMが構造化出力を生成するための方法。

鳥に関するジョーク:LLMが鳥に関するジョークを生成する例。

ツールコーリングの信頼性:ツールコーリングの信頼性とパフォーマンスの改善方法。

大容量モデルの可能性:大容量モデル(70Bなど)を使用することでパフォーマンスが向上。

ローカルエージェントの設定:ツールコーリングを使用したローカルエージェントの設定についての言及。

未来の展望:より優れたローカルLLMが登場することで、ツールコーリングの能力がさらに向上する期待。

Transcripts

play00:00

hey this is Lance from L chain we' seen

play00:02

very high interest in local models so

play00:04

that's the ability to run llms locally

play00:06

for example on your laptop or maybe on

play00:08

your device um and we've done quite a

play00:11

bit of work on this we have a few videos

play00:12

focusing on ways you can incorporate

play00:14

local models and advanc workflows like

play00:16

rag but there's been one thing that's

play00:18

kind of been missing and that is this

play00:20

idea of tool use or function calling

play00:21

which can be kind of used

play00:22

interchangeably and I'm going to talk

play00:24

about that today because we just

play00:25

released an integration with llama CDP

play00:27

that enables this so first of all local

play00:30

M the big idea here is simply this you

play00:32

can take an lm's weights and you can

play00:34

basically quantize them or reduce their

play00:37

Precision for example from like say

play00:39

32bit floating Point down to say 8 bit

play00:41

or lower to reduce the model size and

play00:43

speed up inference with minimal loss and

play00:45

accuracy this allows you to run it even

play00:47

on your device so many many Apple

play00:49

devices now will support running uh

play00:52

running llms uh directly locally free

play00:54

private so this is like obviously really

play00:56

cool AMA is one of the great options for

play00:58

doing this cppp is the other one so kind

play01:01

of these are two of the more popular

play01:03

ways to do and there there are of course

play01:04

some others so I don't want to exclude

play01:06

anyone now here's the rub tool calling

play01:09

is really important for lots of things

play01:10

in particular agents and really what's

play01:11

going on with tool calling is this

play01:13

you're defining some tool so external to

play01:16

the LM so let's say in this particular

play01:18

case we have this tool called Magic

play01:20

function you take an input add two to it

play01:22

what we really want to do is bind it to

play01:24

the llm okay so we bind this tool and

play01:26

when the the input is relevant to that

play01:28

particular tool the llm knows to return

play01:32

a payload necessary to run the tool now

play01:34

this is kind of an often misconception

play01:37

the LM doesn't have the ability to like

play01:38

somehow magically run this function

play01:40

right it's string to string typed but

play01:42

what's happening is it can return the

play01:44

payload necessary to run the function

play01:46

based upon the input and the schema that

play01:48

is extracted from the raw function

play01:50

itself that's really what's going on so

play01:52

what you're getting out from a tool call

play01:54

is basically like the tool arguments and

play01:56

the tool name for the input so that's

play01:58

really what's going on it's a kind of

play02:00

central capability and kind of a high

play02:02

general interest now I want to walk you

play02:04

through the process of doing this

play02:05

locally on my laptop uh using our new

play02:09

chat llama CPP integration so I'm go

play02:10

over to the notebook here now here's

play02:12

what's very important you have to pick

play02:13

the right model that ideally supports

play02:16

tool calling so for this I hunted around

play02:18

local llama and this Hermes 2 model

play02:21

Hermes 2 Pro llama 38b has been fine-

play02:24

tuned for Tool calling you I'll share of

play02:26

course this link you can go through it

play02:28

uh but it's pretty neat so in particular

play02:30

scoring 90% on our function calling

play02:32

evaluation build partnership with

play02:33

fireworks AI um and on our and 84% on

play02:37

structured output evaluation so here's

play02:40

kind of the catch this is a very good

play02:44

this is a very good model to test I'm

play02:47

going to caveat that it's an 8 billion

play02:49

parameter model most providers I talked

play02:51

to fireworks for example that kind of

play02:53

don't recommend tool calling with 8

play02:55

billion parameter models same with Gro

play02:57

so 8 billion is definitely small but

play02:59

it's what we can runow locally on a

play03:00

laptop unless you have a larger laptop

play03:01

you can try 70 billion of course but so

play03:03

we're going to try this out um so we're

play03:05

going to be using this Hermes 2 Pro

play03:07

llama 3 Model that's been fine-tuned for

play03:09

Tool calling 8 billion parameter model

play03:12

so all I've done is when I go over to

play03:15

hugging face here I go to the files and

play03:17

versions I just download one of these

play03:18

models I already did this so I don't

play03:20

want to waste your time with that um in

play03:23

my notebook now I'm just in PIP

play03:24

installing a few a few packages here

play03:26

lent Community lb P python this is based

play03:29

of python wrapper from llama CBP allows

play03:31

you to to basically use llama CBP

play03:33

functionality so here's what I'm doing I

play03:35

specify the path to my model boom right

play03:37

here and I go ahead and and uh initi

play03:40

instantiate it so boom there we go and

play03:42

so you'll see all this spam out don't

play03:44

worry too much about it this kind of

play03:45

always is the case it'll give you a

play03:47

whole bunch of logging about the model

play03:48

you can kind of dig through it if you're

play03:50

interested um so and I will make a quick

play03:53

note I'll showare this of course check

play03:55

out our guide and running LMS locally

play03:56

there are some things you want to do

play03:57

with your environment before you try

play03:59

this for example Mac um basically that

play04:01

you're utilizing Apple metal or or the

play04:03

Apple GPU but that's kind of a separate

play04:05

Point uh and um I'll I'll point you to

play04:08

our guide to kind of go through that

play04:10

anyway so we initialized our model uh

play04:13

now try an invocation so we go ahead and

play04:15

run this you can see it's running

play04:16

locally so it takes a little bit of time

play04:18

uh and I can even go to Lang Smith

play04:20

because I should be just kind of logging

play04:22

these um to my default project let's see

play04:26

I'm in my personal user I go to rlm and

play04:29

and there it is so you can see llama

play04:31

cppp is running uh and it is still going

play04:34

okay it looks like it's done so it

play04:36

translated uh uh I love programming from

play04:39

English to French and this should be

play04:41

yeah there it is it comes through so

play04:42

okay cool so we have basic functionality

play04:45

working um let's see we can try basic

play04:47

chain to a prompt llm we can go back to

play04:51

Lang Smith we can kind of look at what's

play04:52

going on so we can look at what the

play04:54

input is input language output language

play04:56

German input I love programming uh this

play04:58

is still running and cool that's done

play05:01

and this should show up in Lang now

play05:03

there it is okay so those are

play05:06

fundamentals now here are things get

play05:07

kind of interesting tool calling like

play05:09

what we talked about here so what we're

play05:10

doing here is I'm going to go ahead and

play05:13

Define some Tool uh so for example in

play05:15

this particular case I'm defining the

play05:17

the input to the input schema to Tool

play05:19

explicitly this weather input location

play05:22

uh and uh the location and the unit

play05:25

specifi those as strings um I add those

play05:28

to the argument schema for my tool so

play05:30

this tool decorator indicates that this

play05:31

function is going to be a tool and then

play05:34

I'm going to bind that to my llm so all

play05:35

I need to do is llm which we defined up

play05:37

here right that's this guy all the way

play05:40

up that's our llm right that's just our

play05:42

model llm dot bind tools I passed the

play05:46

Tool uh function name and this is the

play05:50

catch here this is actually very

play05:51

important to get this to run reli Le at

play05:54

least in my experience and I've tested

play05:56

this only only a bit you I found that

play05:59

using us enforced invocation of the

play06:01

function certainly improves reliability

play06:04

and performance quite a bit we need to

play06:05

do evals on this to kind of confirm it

play06:08

but that's been my experience today so

play06:10

let's go ahead and try this out so I'm

play06:12

going to run this and again this might

play06:13

take a little bit of time because we're

play06:14

all running locally um it'll kind of

play06:16

spam out okay and that's that ran that's

play06:18

pretty cool so here's what's neat if I

play06:20

go to the AI message tool calls and look

play06:22

at this this is pretty nice remember

play06:25

look back at here what is really coming

play06:27

with a tool call all you're getting is

play06:29

like payload need to run the tool and

play06:30

the tool name look at this tool name get

play06:32

weather arguments location unit and it's

play06:35

parsing that just from natural language

play06:37

it even does the conversion from HCMC to

play06:40

hoi Min City pretty cool right um so

play06:42

this is actually working this is working

play06:44

locally on my laptop I can go over

play06:47

to uh y I can go over to here and yeah

play06:51

look at this this is neat right so I'm

play06:53

in lsmith here uh this is showing you

play06:56

that this particular tool is bound to my

play06:57

llm um it's cool

play07:00

this flag tells you it's called really

play07:01

cool you get here's the tool name that

play07:04

was invoked here's the payload so great

play07:06

this this is now working that's really

play07:08

nice um yeah this is showing another

play07:11

example of my little magic function um

play07:14

same idea what is Magic function of

play07:15

three and let's see yeah so you get the

play07:18

tool name magic function and then the

play07:20

arguments is Magic function three so

play07:21

that's really neat now what's another

play07:23

cool thing about this tool calling is

play07:25

kind of a very general case and it can

play07:26

be used to do kind of structured outputs

play07:28

as well uh and so we actually have a

play07:30

very nice kind of helper uh kind of

play07:32

method for that called with structured

play07:33

output again you just pass your uh your

play07:36

your output schema uh to okay in this

play07:39

particular case you actually convert it

play07:41

to a dictionary so you convert to open

play07:42

ey tool and then you pass that in to

play07:44

what structured output let's try this

play07:46

joke tell me let's try this out tell me

play07:48

a joke about birds and that is running

play07:50

and hopefully this works and maybe it's

play07:52

a funny joke okay this time it's a

play07:53

little bit flaky it doesn't give me a

play07:55

setup it only gives me a punch line

play07:56

let's try that again so you can see the

play07:58

reliability is a little bit variable

play08:00

you'd have to tune this a little bit

play08:01

maybe it's an issue of um yeah okay so

play08:04

in this case it didn't quite work quite

play08:05

as well uh but I have seen this work in

play08:08

other particular cases so uh it may

play08:10

depend a little bit on the formulation

play08:11

of the joke okay so in this particular

play08:13

case you get the setup you get the punch

play08:14

line so again uh I think it's absolutely

play08:16

worth playing with this it's really

play08:17

interesting the fact that you can run

play08:19

tool CL locally opens up a lot of cool

play08:21

things um of course if you have a larger

play08:23

laptop you actually run higher capacity

play08:25

models like 70b I think those would

play08:27

really work well I'm using 8 billion

play08:28

parameter model a little bit small for

play08:30

this particular kind of use case but it

play08:32

does appear to be working new

play08:34

integration I would definitely encourage

play08:35

playing with this and I mean here's the

play08:37

thing that I really think is important

play08:39

to note look these mods are only going

play08:41

to get better so as better L you know as

play08:42

better local LM come out better fine

play08:45

tunes find tun on more data like the

play08:47

capacities will get more and more and

play08:48

more or or kind of greater and greater

play08:49

and greater and so um maybe in some

play08:51

future videos we'll talk a little bit

play08:53

about setting up local agents using tool

play08:54

calling we've done a lot of local agents

play08:56

using Lang graph without tool calling

play08:58

but with the the ability to kind of call

play09:00

tools through LL CVP currently um we may

play09:04

be able to kind of take a stab at

play09:05

setting up some tool calling agents and

play09:07

uh we'll follow up on that in the in the

play09:09

near future thanks

Rate This

5.0 / 5 (0 votes)

Related Tags
ローカルモデルツール呼び出しL chain技術紹介LLM量子化精度低下推論速度デバイス対応AppleデバイスAIツール
Do you need a summary in English?