ドキュメント
REPLでのlmstudio-python
の使用
インタラクティブな使用を可能にするため、lmstudio-python
は、atexit
フックを介してリソースを管理する便利なAPIを提供しており、これによりデフォルトの同期クライアントセッションを複数のインタラクティブコマンドで利用できます。
この便利なAPIは、ドキュメント全体の例において、Python (convenience API)
タブとして示されています(ネットワーク通信リソースの確実なクリーンアップのためにwith
ステートメントを使用するPython (scoped resource API)
の例も併記されています)。
この便利なAPIにより、標準のPython REPLや、Jupyter Notebookのようなより柔軟な代替環境を使用して、LM StudioにロードされたAIモデルと対話できます。例:
>>> import lmstudio as lms
>>> loaded_models = lms.list_loaded_models()
>>> for idx, model in enumerate(loaded_models):
... print(f"{idx:>3} {model}")
...
0 LLM(identifier='qwen2.5-7b-instruct')
>>> model = loaded_models[0]
>>> chat = lms.Chat("You answer questions concisely")
>>> chat = lms.Chat("You answer questions concisely")
>>> chat.add_user_message("Tell me three fruits")
UserMessage(content=[TextData(text='Tell me three fruits')])
>>> print(model.respond(chat, on_message=chat.append))
Banana, apple, orange.
>>> chat.add_user_message("Tell me three more fruits")
UserMessage(content=[TextData(text='Tell me three more fruits')])
>>> print(model.respond(chat, on_message=chat.append))
Mango, strawberry, avocado.
>>> chat.add_user_message("How many fruits have you told me?")
UserMessage(content=[TextData(text='How many fruits have you told me?')])
>>> print(model.respond(chat, on_message=chat.append))
You asked for three initial fruits and three more, so I've listed a total of six fruits.