Implement LLM abstraction layer
Browse files- core/llm.py +57 -0
core/llm.py
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import openai
|
| 3 |
+
from utils.config import config
|
| 4 |
+
|
| 5 |
+
class LLMClient:
|
| 6 |
+
def __init__(self, provider="ollama", model_name=None):
|
| 7 |
+
self.provider = provider
|
| 8 |
+
self.model_name = model_name or config.local_model_name
|
| 9 |
+
|
| 10 |
+
# Set up OpenAI client for Hugging Face endpoint
|
| 11 |
+
self.hf_client = openai.OpenAI(
|
| 12 |
+
base_url=config.hf_api_url,
|
| 13 |
+
api_key=config.hf_token
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def generate(self, prompt, max_tokens=8192, stream=True):
|
| 17 |
+
if self.provider == "ollama":
|
| 18 |
+
return self._generate_ollama(prompt, max_tokens, stream)
|
| 19 |
+
elif self.provider == "huggingface":
|
| 20 |
+
return self._generate_hf(prompt, max_tokens, stream)
|
| 21 |
+
else:
|
| 22 |
+
raise ValueError(f"Unsupported provider: {self.provider}")
|
| 23 |
+
|
| 24 |
+
def _generate_ollama(self, prompt, max_tokens, stream):
|
| 25 |
+
url = f"{config.ollama_host}/api/generate"
|
| 26 |
+
payload = {
|
| 27 |
+
"model": self.model_name,
|
| 28 |
+
"prompt": prompt,
|
| 29 |
+
"stream": stream
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
with requests.post(url, json=payload, stream=stream) as response:
|
| 34 |
+
if response.status_code != 200:
|
| 35 |
+
raise Exception(f"Ollama API error: {response.text}")
|
| 36 |
+
|
| 37 |
+
if stream:
|
| 38 |
+
return (chunk.decode("utf-8") for chunk in response.iter_content())
|
| 39 |
+
else:
|
| 40 |
+
return response.json()["response"]
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise Exception(f"Ollama request failed: {e}")
|
| 43 |
+
|
| 44 |
+
def _generate_hf(self, prompt, max_tokens, stream):
|
| 45 |
+
try:
|
| 46 |
+
response = self.hf_client.chat.completions.create(
|
| 47 |
+
model=self.model_name,
|
| 48 |
+
messages=[{"role": "user", "content": prompt}],
|
| 49 |
+
max_tokens=max_tokens,
|
| 50 |
+
stream=stream
|
| 51 |
+
)
|
| 52 |
+
if stream:
|
| 53 |
+
return (chunk.choices[0].delta.content or "" for chunk in response)
|
| 54 |
+
else:
|
| 55 |
+
return response.choices[0].text
|
| 56 |
+
except Exception as e:
|
| 57 |
+
raise Exception(f"Hugging Face API error: {e}")
|