文档

基础知识

取消预测

lmstudio-python 中停止正在进行的预测

使用流式 API 的一个好处是能够根据无法使用 stopStringsmaxPredictedTokens 配置设置表示的条件取消预测请求。

以下代码片段演示了根据应用程序指定取消条件(例如轮询由另一个线程设置的事件)取消请求。

import lmstudio as lms
model = lms.llm()

prediction_stream = model.respond_stream("What is the meaning of life?")
cancelled = False
for fragment in prediction_stream:
    if ...: # Cancellation condition will be app specific
        cancelled = True
        prediction_stream.cancel()
        # Note: it is recommended to let the iteration complete,
        # as doing so allows the partial result to be recorded.
        # Breaking the loop *is* permitted, but means the partial result
        # and final prediction stats won't be available to the client
# The stream allows the prediction result to be retrieved after iteration
if not cancelled:
    print(prediction_stream.result())

本页的源代码可在 GitHub 上获取