LM Studio
https://lmstudio.ai/docs/basics/server
Tipp
Wir unterstützen ALLE LM Studio-Modelle. Setzen Sie einfach model=lm_studio/<any-model-on-lmstudio> als Präfix beim Senden von LiteLLM-Anfragen.
| Eigenschaft | Details |
|---|---|
| Beschreibung | Lokale LLMs entdecken, herunterladen und ausführen. |
| Provider-Routing in LiteLLM | lm_studio/ |
| Provider-Dokumentation | LM Studio ↗ |
| Unterstützte OpenAI-Endpunkte | /chat/completions, /embeddings, /completions |
API-Schlüssel
# env variable
os.environ['LM_STUDIO_API_BASE']
os.environ['LM_STUDIO_API_KEY'] # optional, default is empty
Beispielverwendung
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)
Beispielverwendung - Streaming
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)
Verwendung mit LiteLLM Proxy Server
So rufen Sie ein LM Studio-Modell mit dem LiteLLM Proxy Server auf
Konfigurieren Sie die 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
Starten Sie den Proxy
$ litellm --config /path/to/config.yamlAnfrage an LiteLLM Proxy Server senden
- OpenAI Python v1.0.0+
- curl
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)curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"model": "my-model",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
],
}'
Unterstützte Parameter
Siehe Unterstützte Parameter für weitere unterstützte Parameter.
Embedding
from litellm import embedding
import os
os.environ['LM_STUDIO_API_BASE'] = "https://:8000"
response = embedding(
model="lm_studio/jina-embeddings-v3",
input=["Hello world"],
)
print(response)
Strukturierte Ausgabe
LM Studio unterstützt strukturierte Ausgaben über JSON Schema. Sie können ein Pydantic-Modell oder ein rohes Schema über response_format übergeben. LiteLLM sendet das Schema als { "type": "json_schema", "json_schema": {"schema": <Ihr 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)