跳到主要内容

LM Studio

https://lm-studio.cn/docs/basics/server

提示

我们支持所有 LM Studio 模型,只需在发送 litellm 请求时将 model=lm_studio/<any-model-on-lmstudio> 设置为前缀即可

属性详情
描述发现、下载并运行本地 LLM。
LiteLLM 上的提供商路由lm_studio/
提供商文档LM Studio ↗
支持的 OpenAI 端点/chat/completions, /embeddings, /completions

API 密钥

# env variable
os.environ['LM_STUDIO_API_BASE']
os.environ['LM_STUDIO_API_KEY'] # optional, default is empty

示例用法

from litellm import completion
import os

os.environ['LM_STUDIO_API_BASE'] = ""

response = completion(
model="lm_studio/llama-3-8b-instruct",
messages=[
{
"role": "user",
"content": "What's the weather like in Boston today in Fahrenheit?",
}
]
)
print(response)

示例用法 - 流式传输

from litellm import completion
import os

os.environ['LM_STUDIO_API_KEY'] = ""
response = completion(
model="lm_studio/llama-3-8b-instruct",
messages=[
{
"role": "user",
"content": "What's the weather like in Boston today in Fahrenheit?",
}
],
stream=True,
)

for chunk in response:
print(chunk)

与 LiteLLM 代理服务器一起使用

以下是如何使用 LiteLLM 代理服务器调用 LM Studio 模型

  1. 修改 config.yaml

    model_list:
    - model_name: my-model
    litellm_params:
    model: lm_studio/<your-model-name> # add lm_studio/ prefix to route as LM Studio provider
    api_key: api-key # api key to send your model
  1. 启动代理

    $ litellm --config /path/to/config.yaml
  2. 向 LiteLLM 代理服务器发送请求

    import openai
    client = openai.OpenAI(
    api_key="sk-1234", # pass litellm proxy key, if you're using virtual keys
    base_url="http://0.0.0.0:4000" # litellm-proxy-base url
    )

    response = client.chat.completions.create(
    model="my-model",
    messages = [
    {
    "role": "user",
    "content": "what llm are you"
    }
    ],
    )

    print(response)

支持的参数

请参阅 支持的参数 查看支持的参数。

嵌入

from litellm import embedding
import os

os.environ['LM_STUDIO_API_BASE'] = "http://localhost:8000"
response = embedding(
model="lm_studio/jina-embeddings-v3",
input=["Hello world"],
)
print(response)

结构化输出

LM Studio 通过 JSON Schema 支持结构化输出。您可以使用 response_format 传递 pydantic 模型或原始 schema。LiteLLM 将 schema 作为 { "type": "json_schema", "json_schema": {"schema": <your schema>} } 发送。

from pydantic import BaseModel
from litellm import completion

class Book(BaseModel):
title: str
author: str
year: int

response = completion(
model="lm_studio/llama-3-8b-instruct",
messages=[{"role": "user", "content": "Tell me about The Hobbit"}],
response_format=Book,
)
print(response.choices[0].message.content)