文档

使用 LLM 进行预测

智能体流程

文本嵌入

标记化

管理模型

模型信息

在 REPL 中使用 lmstudio-python

为了支持交互式使用,lmstudio-python 提供了一个便利 API,该 API 通过 atexit 钩子管理其资源,从而允许在多个交互式命令中使用默认的同步客户端会话。

此便利 API 在文档中的示例中以 Python (convenience API) 选项卡的形式展示(与 Python (scoped resource API) 示例并列,这些示例使用 with 语句来确保网络通信资源的确定性清理)。

此便利 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.