forked from TideDra/zotero-arxiv-daily
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.py
More file actions
48 lines (43 loc) · 1.81 KB
/
llm.py
File metadata and controls
48 lines (43 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from llama_cpp import Llama
from openai import OpenAI
from loguru import logger
from time import sleep
GLOBAL_LLM = None
class LLM:
def __init__(self, api_key: str = None, base_url: str = None, model: str = None,lang: str = "English"):
if api_key:
self.llm = OpenAI(api_key=api_key, base_url=base_url)
else:
self.llm = Llama.from_pretrained(
repo_id="Qwen/Qwen2.5-3B-Instruct-GGUF",
filename="qwen2.5-3b-instruct-q4_k_m.gguf",
n_ctx=5_000,
n_threads=4,
verbose=False,
)
self.model = model
self.lang = lang
def generate(self, messages: list[dict]) -> str:
if isinstance(self.llm, OpenAI):
max_retries = 3
for attempt in range(max_retries):
try:
response = self.llm.chat.completions.create(messages=messages, temperature=0, model=self.model)
break
except Exception as e:
logger.error(f"Attempt {attempt + 1} failed: {e}")
if attempt == max_retries - 1:
raise
sleep(3)
return response.choices[0].message.content
else:
response = self.llm.create_chat_completion(messages=messages,temperature=0)
return response["choices"][0]["message"]["content"]
def set_global_llm(api_key: str = None, base_url: str = None, model: str = None, lang: str = "English"):
global GLOBAL_LLM
GLOBAL_LLM = LLM(api_key=api_key, base_url=base_url, model=model, lang=lang)
def get_llm() -> LLM:
if GLOBAL_LLM is None:
logger.info("No global LLM found, creating a default one. Use `set_global_llm` to set a custom one.")
set_global_llm()
return GLOBAL_LLM