文档

入门

代理流程

文本嵌入

分词

管理模型

模型信息

取消预测

使用流式 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())