0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CLOUDFLAREでLLMを動かす

Posted at

記事の対象者

  • CLOUDFLAREの使い方全般を知りたい人向けではなく、CLOUDFLAREでLLMを動かしてみたい(特殊な?)人向けです。

CLOUDFLAREとは?

筆者自身も詳しくはないですが、調べた限りで超ざっくり言えば、「本来はCDN(Contents Delivery Network)の機能を持つが、一部AIモデルも使えるSaaS」です。

AIモデルの利用に関しては、上限がありますが、無料で使うことが可能です。料金体系についてですが、Neuronという独自の課金体系が存在しており、1日10,000 Neuronまでは無料です。Neuronの使用量は(イギリス時間で)0:00にリセットされます。

CLOUDFLAREでLLMを動かすための手順

ここからCLOUDFLAREでLLMを動かすための手順を解説します。

流れとしては、ざっくりと、アカウントを作成->APIトークンを作成->REST APIでLLMを呼び出すという流れです。

アカウントを作成

このページからアカウントをまずは作成します。 Googleアカウントを持っている人はGoogleアカウントを使うのが手軽だと思います。

▼アカウント作成画面
image.png

▼アカウント作成後に表示されるダッシュボード画面
image.png

もしかしたらこのような画面が出るかもしれないですが、AIモデルを動かしたいだけであれば、ドメインを購入/登録する必要はないです。
image.png

APIトークンを作成

▼ 画面左にあるメニューを下にスクロールして、 AI > WorkersAIをクリックする
image.png

▼ "REST API"を選択すると下のような画面へ遷移する
Untitled Diagram.drawio.png

▼ APIトークンを作成する(特に変更せずそのままでOKです)

※作成したAPIトークンは無くさないでください。

プログラムを作成する

基本的には、画面上に表示されているpythonコードを利用すれば良いです。

Untitled Diagram-Page-3.drawio.png

アカウントIDやAPIトークン、モデルを変数として別で書き出しておきたい場合は、以下のようにしておくと便利だと思います。

ACCOUNT_ID = "<ご自身のアカウントID>"
API_TOKEN = "<作成したAPIトークン>"
MODEL = "<利用したいモデル>" # 例えば、"llama-4-scout-17b-16e-instruct"

import requests


API_BASE_URL = f"https://api.cloudflare.com/client/v4/accounts/{ACCOUND_ID}/ai/run/"
headers = {"Authorization": f"Bearer {API_TOKEN}"}


def run(model, inputs):
    input = { "messages": inputs }
    response = requests.post(f"{API_BASE_URL}{model}", headers=headers, json=input)
    return response.json()


inputs = [
    { "role": "system", "content": "You are a friendly assistan that helps write stories" },
    { "role": "user", "content": "Write a short story about a llama that goes on a journey to find an orange cloud "}
];
output = run(f"@cf/meta/{MODEL}", inputs)
print(output)

▼ 出力

{'result': {'response': "Once upon a time, in a lush green valley surrounded by towering mountains, there lived a curious llama named Lola. Lola was known for her bright, inquisitive eyes and her love for exploring the world around her. One day, while gazing up at the sky, Lola spotted something that caught her attention - a fluffy orange cloud drifting lazily across the horizon.\n\nIntrigued by the cloud's vibrant color, Lola decided that she had to see it up close. She packed a small bag with some snacks and her favorite blanket, and set off on an adventure to find the orange cloud.\n\nAs she trekked through the valley, Lola encountered all sorts of creatures, from chatty birds to playful rabbits. But none of them knew where the orange cloud was or how to get to it. Undeterred, Lola pressed on, her determination driving her forward.\n\nAfter many hours of walking, Lola reached the foot of a tall mountain. She knew that the cloud must be hiding somewhere on its slopes, so she began to climb. The air grew thinner and the path grew steeper, but Lola persevered, her llama legs strong and sure.\n\nAs she ascended higher, the clouds grew thicker and the wind picked up. Lola's ears flapped in the", 'usage': {'prompt_tokens': 43, 'completion_tokens': 256, 'total_tokens': 299}}, 'success': True, 'errors': [], 'messages': []}

まとめ

今回はCLOUDFLAREでローカルLLMを動かしてみました。
利用量の上限はあるものの、無料で試せるのはありがたいですね。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?