0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Maya上で動作するチャットボットを作る

Last updated at Posted at 2025-05-10

環境

  • Windows
  • Autodesk Maya 2026

Maya上でOllamaを使用する

チャットボットを作成するために Ollama を使用します。Ollama はローカル環境で大規模言語モデル (LLM) を実行できるオープンソースのツールです。導入手順がシンプルで、Python向けのライブラリも提供されています。

Ollamaの導入

Ollamaのダウンロードページ からインストーラーを入手してインストールします。
また、PythonでOllamaを使用できるようにするため、ollama-python もインストールします。

Maya上で実行するサンプルコード

以下のコードは簡易的なサンプルです。ベストプラクティスを示したものではありませんのでご注意ください。

チャットボット用のコード

下記のコードでは、ollama.chat() を使用してユーザーの入力に対する応答を取得するシンプルなチャットボットを構築しています。モデル名とシステムプロンプトを指定することで、特定の用途に適した会話が可能です。

ChatBot
from typing import Optional

import ollama

class ChatBot:
    def __init__(self, model: str, system_prompt: Optional[str] = None) -> None:
        self._model = model
        self._system_prompt = system_prompt

    def get_response(self, question: str) -> str:
        system_message = {"role": "system", "content": self._system_prompt if self._system_prompt else ""}
        user_message = {"role": "user", "content": question}
        res = ollama.chat(model=self._model, messages=[system_message, user_message])
        return res.message.content

GUIのコード

下記のコードは、ユーザーの入力を受け取り、チャットボットからの応答を表示するシンプルなGUIを作成するものです。

GUI
from maya.app.general.mayaMixin import MayaQWidgetBaseMixin
from PySide6 import QtCore, QtGui, QtWidgets

class ChatWindow(MayaQWidgetBaseMixin, QtWidgets.QMainWindow):
    name = "ChatWindow"

    def __init__(self, chatbot: ChatBot) -> None:
        super().__init__()

        self.setObjectName(self.name)
        self.setWindowTitle(self.name)
        self.resize(600, 400)

        # メインウィジェットとレイアウトの設定
        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        layout = QtWidgets.QVBoxLayout(central_widget)

        # チャット履歴表示用のフィールド
        self._chat_display = QtWidgets.QTextEdit()
        self._chat_display.setReadOnly(True)
        self._chat_display.setPlaceholderText("応答がここに表示されます...")
        layout.addWidget(self._chat_display)

        # メッセージ入力用のフィールド
        self._message_input = QtWidgets.QTextEdit()
        self._message_input.setPlaceholderText("ここにメッセージを入力してください...")
        self._message_input.setMaximumHeight(100)
        layout.addWidget(self._message_input)

        # メッセージ送信用のボタン
        self._send_button = QtWidgets.QPushButton("送信")
        self._send_button.clicked.connect(self._on_send)
        layout.addWidget(self._send_button)

        # ChatBot のインスタンス作成
        self._chatbot = chatbot

    def _on_send(self) -> None:
        # ユーザーの入力を取得
        user_input = self._message_input.toPlainText().strip()
        if not user_input:
            return

        try:
            # 応答を出力フィールドに表示
            response = self._chatbot.get_response(user_input)
            self._chat_display.append(f"[User]\n{user_input}\n")
            self._chat_display.append(f"[Bot]\n{response}\n")

            # スクロールを一番下に移動
            self._chat_display.verticalScrollBar().setValue(self._chat_display.verticalScrollBar().maximum())

            # 入力フィールドをクリア
            self._message_input.clear()

        except Exception as e:
            self._chat_display.append(f"Error: {str(e)}\n")

メインの処理

指定したLLMモデルを用いてチャットボットを動作させるコードです。このスクリプトを実行すると、Maya上で簡単なチャットボットを利用できます。

メインの処理
def main() -> None:
    window = ChatWindow(
        ChatBot(
            model="gemma3",
            system_prompt="あなたは「Autodesk Maya」の質問の回答に特化したチャットボットです。簡潔に回答してください。",
        )
    )
    window.show()

main()

参考情報

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?