add lm studio option

This commit is contained in:
2026-04-08 10:17:20 -04:00
parent 08c5757b31
commit c7c4019ecc
6 changed files with 112 additions and 13 deletions
+63
View File
@@ -81,3 +81,66 @@ class LLMEngine:
if remainder:
yield remainder
SYSTEM_PROMPT = (
"You are a helpful voice assistant. Keep your responses concise and natural "
"for spoken conversation. Respond in 1-3 short sentences. "
"Do not use markdown, bullet points, code blocks, emojis, or any "
"formatting that doesn't work in speech."
)
class LMStudioEngine:
"""LLM engine that delegates to an LM Studio server via its OpenAI-compatible API."""
def __init__(self, base_url: str, model: str):
self.base_url = base_url.rstrip("/")
self.model = model
def generate(self, messages: list[dict], max_new_tokens: int = 256) -> str:
import requests
payload_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
payload_messages.extend(messages)
body: dict = {
"messages": payload_messages,
"max_tokens": max_new_tokens,
"temperature": 0.7,
"stream": False,
}
if self.model:
body["model"] = self.model
resp = requests.post(
f"{self.base_url}/v1/chat/completions",
json=body,
timeout=30,
)
resp.raise_for_status()
response = resp.json()["choices"][0]["message"]["content"].strip()
log.info(f"LM Studio response: {response}")
return response
async def generate_sentences(
self,
messages: list[dict],
cancel_event: threading.Event | None = None,
) -> AsyncIterator[str]:
"""Generate response and yield it sentence by sentence for TTS pipelining."""
import asyncio
response = await asyncio.to_thread(self.generate, messages)
if cancel_event and cancel_event.is_set():
return
sentences, remainder = split_sentences(response)
for sentence in sentences:
if cancel_event and cancel_event.is_set():
return
yield sentence
if remainder:
yield remainder