文档
入门
在 REPL 中使用 lmstudio-python
您可以在 REPL(Read-Eval-Print Loop,读取-求值-打印循环)中使用 lmstudio-python 与 LLM 交互、管理模型等。
为了简化交互式使用,lmstudio-python 提供了一个便利 API,通过 atexit 钩子管理其资源,允许在多个交互式命令中使用默认的同步客户端会话。
此便利 API 在整个文档的示例中以 Python(便利 API) 标签(以及使用 with 语句确保网络通信资源确定性清理的 Python(范围资源 API) 示例)显示。
便利 API 允许使用标准 Python REPL 或更灵活的替代方案(如 Juypter 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.
本页的源代码可在 GitHub 上找到