File size: 4,794 Bytes
27f6252 | 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | """
Base class for LLM provider implementations.
Supports both sync and async methods.
"""
from abc import ABC, abstractmethod
from typing import List, Dict, Any, Optional, Iterator, AsyncIterator
import logging
import asyncio
logger = logging.getLogger(__name__)
class BaseLLM(ABC):
"""
Abstract base class for LLM providers.
All LLM providers must implement:
- generate(): Generate a complete response
- stream(): Stream response token by token (optional)
"""
@abstractmethod
def generate(
self,
user_prompt: str,
system_prompt: Optional[str] = None,
temperature: float = 0.7,
max_tokens: Optional[int] = None,
**kwargs
) -> str:
"""
Generate a response from the LLM.
Args:
user_prompt: The user's prompt, including any context.
system_prompt: System prompt to guide LLM behavior
temperature: Sampling temperature (0.0 - 1.0)
max_tokens: Maximum tokens to generate
**kwargs: Additional provider-specific parameters
Returns:
Generated response as string
"""
pass
def stream(
self,
user_prompt: str,
system_prompt: Optional[str] = None,
temperature: float = 0.7,
max_tokens: Optional[int] = None,
**kwargs
) -> Iterator[str]:
"""
Stream response token by token (optional).
Args:
Same as generate()
Yields:
Response tokens as they are generated
"""
# Default implementation: yield complete response at once
response = self.generate(
user_prompt=user_prompt,
system_prompt=system_prompt,
temperature=temperature,
max_tokens=max_tokens,
**kwargs
)
yield response
def _get_default_system_prompt(self) -> str:
"""
Get default system prompt for RAG.
Returns:
Default system prompt
"""
return """You are a helpful AI assistant. Answer the user's question based on the provided context.
If the context doesn't contain relevant information, say so. Be concise and accurate.
Guidelines:
- Use information from the context to answer
- If unsure, acknowledge uncertainty
- Cite sources when possible
- Be concise but comprehensive"""
def _get_default_vietnamese_system_prompt(self) -> str:
"""
Get default Vietnamese system prompt.
Returns:
Vietnamese system prompt
"""
return """Bạn là một trợ lý AI hữu ích. Trả lời câu hỏi của người dùng dựa trên ngữ cảnh được cung cấp.
Nếu ngữ cảnh không chứa thông tin liên quan, hãy nói rõ. Hãy ngắn gọn và chính xác.
Hướng dẫn:
- Sử dụng thông tin từ ngữ cảnh để trả lời
- Nếu không chắc chắn, hãy thừa nhận điều đó
- Trích dẫn nguồn khi có thể
- Ngắn gọn nhưng đầy đủ"""
@abstractmethod
def get_model_info(self) -> Dict[str, Any]:
"""
Get information about the LLM model.
Returns:
Dictionary with model information
"""
pass
# ==================== ASYNC METHODS ====================
async def agenerate(
self,
user_prompt: str,
system_prompt: Optional[str] = None,
temperature: float = 0.7,
max_tokens: Optional[int] = None,
**kwargs
) -> str:
"""
Async version of generate.
Default implementation wraps sync generate in thread executor.
Override this method for native async implementation.
Args:
Same as generate()
Returns:
Generated response as string
"""
return await asyncio.to_thread(
self.generate,
user_prompt,
system_prompt,
temperature,
max_tokens,
**kwargs
)
async def astream(
self,
user_prompt: str,
system_prompt: Optional[str] = None,
temperature: float = 0.7,
max_tokens: Optional[int] = None,
**kwargs
) -> AsyncIterator[str]:
"""
Async stream response token by token.
Default implementation wraps sync stream.
Override this method for native async streaming.
Args:
Same as generate()
Yields:
Response tokens as they are generated
"""
# Default: yield from sync stream in thread
for token in self.stream(
user_prompt, system_prompt, temperature, max_tokens, **kwargs
):
yield token |