|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import abc |
| 4 | +import re |
| 5 | +from typing import Any, List, Optional |
| 6 | + |
| 7 | +from graphgen.bases.base_tokenizer import BaseTokenizer |
| 8 | +from graphgen.bases.datatypes import Token |
| 9 | + |
| 10 | + |
| 11 | +class BaseLLMClient(abc.ABC): |
| 12 | + """ |
| 13 | + LLM client base class, agnostic to specific backends (OpenAI / Ollama / ...). |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + *, |
| 19 | + system_prompt: str = "", |
| 20 | + temperature: float = 0.0, |
| 21 | + max_tokens: int = 4096, |
| 22 | + repetition_penalty: float = 1.05, |
| 23 | + top_p: float = 0.95, |
| 24 | + top_k: int = 50, |
| 25 | + tokenizer: Optional[BaseTokenizer] = None, |
| 26 | + **kwargs: Any, |
| 27 | + ): |
| 28 | + self.system_prompt = system_prompt |
| 29 | + self.temperature = temperature |
| 30 | + self.max_tokens = max_tokens |
| 31 | + self.repetition_penalty = repetition_penalty |
| 32 | + self.top_p = top_p |
| 33 | + self.top_k = top_k |
| 34 | + self.tokenizer = tokenizer |
| 35 | + |
| 36 | + for k, v in kwargs.items(): |
| 37 | + setattr(self, k, v) |
| 38 | + |
| 39 | + @abc.abstractmethod |
| 40 | + async def generate_answer( |
| 41 | + self, text: str, history: Optional[List[str]] = None, **extra: Any |
| 42 | + ) -> str: |
| 43 | + """Generate answer from the model.""" |
| 44 | + raise NotImplementedError |
| 45 | + |
| 46 | + @abc.abstractmethod |
| 47 | + async def generate_topk_per_token( |
| 48 | + self, text: str, history: Optional[List[str]] = None, **extra: Any |
| 49 | + ) -> List[Token]: |
| 50 | + """Generate top-k tokens for the next token prediction.""" |
| 51 | + raise NotImplementedError |
| 52 | + |
| 53 | + @abc.abstractmethod |
| 54 | + async def generate_inputs_prob( |
| 55 | + self, text: str, history: Optional[List[str]] = None, **extra: Any |
| 56 | + ) -> List[Token]: |
| 57 | + """Generate probabilities for each token in the input.""" |
| 58 | + raise NotImplementedError |
| 59 | + |
| 60 | + def count_tokens(self, text: str) -> int: |
| 61 | + """Count the number of tokens in the text.""" |
| 62 | + if self.tokenizer is None: |
| 63 | + raise ValueError("Tokenizer is not set. Please provide a tokenizer to use count_tokens.") |
| 64 | + return len(self.tokenizer.encode(text)) |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def filter_think_tags(text: str, think_tag: str = "think") -> str: |
| 68 | + """ |
| 69 | + Remove <think> tags from the text. |
| 70 | + If the text contains <think> and </think>, it removes everything between them and the tags themselves. |
| 71 | + """ |
| 72 | + think_pattern = re.compile(rf"<{think_tag}>.*?</{think_tag}>", re.DOTALL) |
| 73 | + filtered_text = think_pattern.sub("", text).strip() |
| 74 | + return filtered_text if filtered_text else text.strip() |
0 commit comments