Stability AIが公開している「stable-code-instruct-3b」にPythonスクリプトを書いてもらいました。

はじめに

「stable-code-3b」という大規模言語モデルは以前から公開されていました。
今回「instruct」モデルが新たに公開されたので使ってみました。

私の認識が間違えていなかったら「instruct」モデルは質問に答えてくれるモデルです。

通常の大規模言語モデルは最初の数単語を渡して続きを書いてくれます。そのモデルに追加学習して質問や指示に答えてくれるようにしたのが「instruct」モデルです。質問文(または指示文)を理解して返答する必要があるので通常モデルよりやや複雑だと思います。

その他に会話ができる「chat」モデルというのもあったりします。たぶん会話の内容を記憶しながら質問に答えてくれる(チャットする)モデルだと思います。

ほとんどの大規模言語モデルは「〇〇b」という単語が後ろについています。今回の場合は「3b」です。この「b」はビリオン(10億)を表していて、このモデルのパラメーター数が約30億であることを示しています。30億は他のモデルに比べて非常に少ないです。

「stable-code-instruct-3b」は軽量だけど高性能のモデルのようです。

ライセンス

商用利用にはStability AIメンバーシップへの加入が必要とのことです。

注意して下さい。

目的

サンプルがHuggingFaceに載っていたのでそれを少し変更(HTML→Python)して実行してみました。

日本語は使えません。

指示

Write a simple application in pyside6. When a user clicks the button, it shows a random joke from a list of 4 jokes. I want to use QMainWindow.

結果

完璧に動作するスクリプトを書いてくれました。

import sys
from random import randint
from PySide6 import QtCore, QtWidgets

class JokeButton(QtWidgets.QPushButton):
    def __init__(self, parent=None):
        super().__init__("Tell Joke", parent)
        self.clicked.connect(self.show_joke)

    def show_joke(self):
        jokes = [
            "Why don't scientists trust atoms? Because they make up everything!",
            "Why did the chicken go to the seance? To talk to the other side!",
            "Why did the scarecrow win an award? Because he was outstanding in his field!",
            "Why did the tomato turn red? Because it saw the salad dressing!",
        ]
        random_joke_index = randint(0, len(jokes) - 1)
        QtWidgets.QMessageBox.information(
            self, "Random Joke", jokes[random_joke_index]
        )

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Random Joke")
        self.setGeometry(100, 100, 400, 200)

        self.joke_button = JokeButton(self)
        self.joke_button.move(160, 50)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

Pythonスクリプト

モデルをダウンロードしてローカル環境で実行しました。

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

# downloaded from https://huggingface.co/stabilityai/stable-code-instruct-3b
model_id = "llm/stable-code-instruct-3b"
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, trust_remote_code=True)
model.eval()
model = model.cuda()

messages = [
    {
        "role": "system",
        "content": "You are a helpful and polite assistant",
    },
    {
        "role": "user",
        "content": "Write a simple application in pyside6. When a user clicks the button, it shows a random joke from a list of 4 jokes. I want to use QMainWindow."
    },
]

prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)

inputs = tokenizer([prompt], return_tensors="pt").to(model.device)

tokens = model.generate(
    **inputs,
    max_new_tokens=1024,
    temperature=0.5,
    top_p=0.95,
    top_k=100,
    do_sample=True,
    use_cache=True
)

output = tokenizer.batch_decode(tokens[:, inputs.input_ids.shape[-1]:], skip_special_tokens=False)[0]

print(output)




このエントリーをはてなブックマークに追加