在 REPL 中使用 lmstudio-python
您可以在 REPL(读取-求值-输出循环)中使用 lmstudio-python 来与大语言模型(LLM)交互、管理模型等。
为了简化交互式使用,lmstudio-python 提供了一个便捷 API,它通过 atexit 钩子管理资源,允许在多个交互式命令之间共用默认的同步客户端会话。
在整个文档的示例中,此便捷 API 显示为 Python (convenience API) 选项卡(与 Python (scoped resource API) 示例并列,后者使用 with 语句以确保网络通信资源的确定性清理)。
便捷 API 允许使用标准 Python REPL 或更灵活的替代方案(如 Jupyter Notebooks)与加载到 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.虽然主要目的并非如此,但该 SDK 的异步结构化并发 API 与 python -m asyncio 启动的异步 Python REPL 兼容。例如:
# Note: assumes use of the "python -m asyncio" asynchronous REPL (or equivalent)
# Requires Python SDK version 1.5.0 or later
>>> from contextlib import AsyncExitStack
>>> import lmstudio as lms
>>> resources = AsyncExitStack()
>>> client = await resources.enter_async_context(lms.AsyncClient())
>>> loaded_models = await client.llm.list_loaded()
>>> for idx, model in enumerate(loaded_models):
... print(f"{idx:>3} {model}")
...
0 AsyncLLM(identifier='qwen2.5-7b-instruct-1m')
>>> model = loaded_models[0]
>>> chat = lms.Chat("You answer questions concisely")
>>> chat.add_user_message("Tell me three fruits")
UserMessage(content=[TextData(text='Tell me three fruits')])
>>> print(await model.respond(chat, on_message=chat.append))
Apple, banana, and orange.
>>> chat.add_user_message("Tell me three more fruits")
UserMessage(content=[TextData(text='Tell me three more fruits')])
>>> print(await model.respond(chat, on_message=chat.append))
Mango, strawberry, and pineapple.
>>> chat.add_user_message("How many fruits have you told me?")
UserMessage(content=[TextData(text='How many fruits have you told me?')])
>>> print(await model.respond(chat, on_message=chat.append))
You asked for three fruits initially, then three more, so I've listed six fruits in total.