-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathclient.py
More file actions
84 lines (66 loc) · 2.51 KB
/
Copy pathclient.py
File metadata and controls
84 lines (66 loc) · 2.51 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
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
"""LLM Client with various providers and models
TODO: incorporate reasoning capabilities
"""
import logging
from finbot.config import settings
from finbot.core.data.models import LLMRequest, LLMResponse
logger = logging.getLogger(__name__)
class LLMClient:
"""LLM Client with configurable provider and model"""
def __init__(self):
self.provider = settings.LLM_PROVIDER.strip().lower()
self.default_model = (
settings.OLLAMA_MODEL
if self.provider == "ollama"
else settings.LLM_DEFAULT_MODEL
)
self.default_temperature = settings.LLM_DEFAULT_TEMPERATURE
self.client = self._get_client()
def _get_client(self):
"""Get the LLM client"""
if self.provider == "openai":
# pylint: disable=import-outside-toplevel
from finbot.core.llm.openai_client import OpenAIClient
return OpenAIClient()
elif self.provider == "ollama":
# pylint: disable=import-outside-toplevel
from finbot.core.llm.ollama_client import OllamaClient
return OllamaClient()
elif self.provider == "mock":
# pylint: disable=import-outside-toplevel
from finbot.core.llm.mock_client import MockLLMClient
return MockLLMClient()
raise ValueError(f"Unsupported LLM provider: {self.provider}")
async def chat(
self,
request: LLMRequest,
) -> LLMResponse:
"""
Chat with LLM
Args:
messages: List of message dicts
model: Optional model override
temperature: Optional temperature override
Returns:
LLM response string
"""
try:
if request.provider and request.provider != self.provider:
logger.warning(
"Provider mismatch, unexpected behavior may occur: "
"request.provider=%s, client.provider=%s",
request.provider,
self.provider,
)
return await self.client.chat(request)
except Exception as e: # pylint: disable=broad-exception-caught
logger.error("LLM call failed: %s", e)
return LLMResponse(
content=f"Error: LLM Provider {self.provider} unavailable - {str(e)}",
provider=self.provider,
success=False,
)
llm_client = LLMClient()
def get_llm_client() -> LLMClient:
"""Get the LLM client"""
return llm_client