tinycodinglm / lmstudio /lmstudio-0060.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
1.92 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How do I generate text embeddings with LM Studio's OpenAI-compatible POST /v1/embeddings endpoint? Show single and batch embeddings with the OpenAI Python SDK. Include code examples.

Assistant

LM Studio embeddings via OpenAI-compatible POST /v1/embeddings.
Load an embedding model first, then use base_url="http://localhost:1234/v1".

python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")

def get_embedding(text: str, model: str) -> list[float]:
    text = text.replace("\n", " ")
    return client.embeddings.create(input=[text], model=model).data[0].embedding

MODEL = "text-embedding-nomic-embed-text-v1.5"  # example; use your loaded id
emb = get_embedding("What is the meaning of life?", MODEL)
print(f"dim={len(emb)} first5={emb[:5]}")

Batch embeddings:

python
resp = client.embeddings.create(
    model=MODEL,
    input=["Hello world", "How are you?", "Goodbye!"],
)
for i, d in enumerate(resp.data):
    print(f"{i}: dim={len(d.embedding)}")

Use cases: semantic search via cosine similarity, RAG, clustering. Dimension depends on model (e.g. 768 for nomic v1.5). Use GET /v1/models to confirm the embedding model id you loaded.