文档
lmstudio-python
(Python SDK)
lmstudio-python
提供了一套 API,用于与 LLM、嵌入模型和智能体流程进行交互。
lmstudio-python
可作为 PyPI 包提供。您可以使用 pip 进行安装。
pip install lmstudio
有关源代码和开源贡献,请访问 GitHub 上的 lmstudio-python。
import lmstudio as lms
model = lms.llm("llama-3.2-1b-instruct")
result = model.respond("What is the meaning of life?")
print(result)
上述代码需要 Llama 3.2 1B 模型。如果您没有该模型,请在终端中运行以下命令进行下载。
lms get llama-3.2-1b-instruct
在此处阅读有关 LM Studio CLI 中 lms get
的更多信息。
如上例所示,使用 LM Studio Python SDK 有两种不同的方法。
第一种是交互式便捷 API(在示例中列为“Python (便捷 API)”),它侧重于使用默认的 LM Studio 客户端实例,以便在 Python 提示符下或使用 Jupyter notebooks 时进行便捷交互。
第二种是作用域资源 API(在示例中列为“Python (作用域资源 API)”),它使用上下文管理器来确保分配的资源(例如网络连接)以确定性方式释放,而不是可能一直保持打开状态直到整个进程终止。
本页内容
安装 SDK
功能
快速示例:与 Llama 模型聊天
获取本地模型
交互式便捷性还是确定性资源管理?