lollms-client 0.32.1__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of lollms-client might be problematic. Click here for more details.
- lollms_client/__init__.py +1 -1
- lollms_client/llm_bindings/azure_openai/__init__.py +6 -10
- lollms_client/llm_bindings/claude/__init__.py +4 -7
- lollms_client/llm_bindings/gemini/__init__.py +3 -7
- lollms_client/llm_bindings/grok/__init__.py +3 -7
- lollms_client/llm_bindings/groq/__init__.py +4 -7
- lollms_client/llm_bindings/hugging_face_inference_api/__init__.py +4 -6
- lollms_client/llm_bindings/litellm/__init__.py +15 -6
- lollms_client/llm_bindings/llamacpp/__init__.py +214 -388
- lollms_client/llm_bindings/lollms/__init__.py +24 -14
- lollms_client/llm_bindings/lollms_webui/__init__.py +6 -12
- lollms_client/llm_bindings/mistral/__init__.py +58 -29
- lollms_client/llm_bindings/ollama/__init__.py +6 -11
- lollms_client/llm_bindings/open_router/__init__.py +45 -14
- lollms_client/llm_bindings/openai/__init__.py +7 -14
- lollms_client/llm_bindings/openllm/__init__.py +12 -12
- lollms_client/llm_bindings/pythonllamacpp/__init__.py +1 -1
- lollms_client/llm_bindings/tensor_rt/__init__.py +8 -13
- lollms_client/llm_bindings/transformers/__init__.py +14 -6
- lollms_client/llm_bindings/vllm/__init__.py +16 -12
- lollms_client/lollms_core.py +296 -487
- lollms_client/lollms_discussion.py +436 -78
- lollms_client/lollms_llm_binding.py +223 -11
- lollms_client/lollms_mcp_binding.py +33 -2
- lollms_client/mcp_bindings/local_mcp/__init__.py +3 -2
- lollms_client/mcp_bindings/remote_mcp/__init__.py +6 -5
- lollms_client/mcp_bindings/standard_mcp/__init__.py +3 -5
- lollms_client/stt_bindings/lollms/__init__.py +6 -8
- lollms_client/stt_bindings/whisper/__init__.py +2 -4
- lollms_client/stt_bindings/whispercpp/__init__.py +15 -16
- lollms_client/tti_bindings/dalle/__init__.py +29 -28
- lollms_client/tti_bindings/diffusers/__init__.py +25 -21
- lollms_client/tti_bindings/gemini/__init__.py +215 -0
- lollms_client/tti_bindings/lollms/__init__.py +8 -9
- lollms_client-1.0.0.dist-info/METADATA +1214 -0
- lollms_client-1.0.0.dist-info/RECORD +69 -0
- {lollms_client-0.32.1.dist-info → lollms_client-1.0.0.dist-info}/top_level.txt +0 -2
- examples/article_summary/article_summary.py +0 -58
- examples/console_discussion/console_app.py +0 -266
- examples/console_discussion.py +0 -448
- examples/deep_analyze/deep_analyse.py +0 -30
- examples/deep_analyze/deep_analyze_multiple_files.py +0 -32
- examples/function_calling_with_local_custom_mcp.py +0 -250
- examples/generate_a_benchmark_for_safe_store.py +0 -89
- examples/generate_and_speak/generate_and_speak.py +0 -251
- examples/generate_game_sfx/generate_game_fx.py +0 -240
- examples/generate_text_with_multihop_rag_example.py +0 -210
- examples/gradio_chat_app.py +0 -228
- examples/gradio_lollms_chat.py +0 -259
- examples/internet_search_with_rag.py +0 -226
- examples/lollms_chat/calculator.py +0 -59
- examples/lollms_chat/derivative.py +0 -48
- examples/lollms_chat/test_openai_compatible_with_lollms_chat.py +0 -12
- examples/lollms_discussions_test.py +0 -155
- examples/mcp_examples/external_mcp.py +0 -267
- examples/mcp_examples/local_mcp.py +0 -171
- examples/mcp_examples/openai_mcp.py +0 -203
- examples/mcp_examples/run_remote_mcp_example_v2.py +0 -290
- examples/mcp_examples/run_standard_mcp_example.py +0 -204
- examples/simple_text_gen_test.py +0 -173
- examples/simple_text_gen_with_image_test.py +0 -178
- examples/test_local_models/local_chat.py +0 -9
- examples/text_2_audio.py +0 -77
- examples/text_2_image.py +0 -144
- examples/text_2_image_diffusers.py +0 -274
- examples/text_and_image_2_audio.py +0 -59
- examples/text_gen.py +0 -30
- examples/text_gen_system_prompt.py +0 -29
- lollms_client-0.32.1.dist-info/METADATA +0 -854
- lollms_client-0.32.1.dist-info/RECORD +0 -101
- test/test_lollms_discussion.py +0 -368
- {lollms_client-0.32.1.dist-info → lollms_client-1.0.0.dist-info}/WHEEL +0 -0
- {lollms_client-0.32.1.dist-info → lollms_client-1.0.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -26,11 +26,6 @@ class LollmsBinding(LollmsLLMBinding):
|
|
|
26
26
|
|
|
27
27
|
|
|
28
28
|
def __init__(self,
|
|
29
|
-
host_address: str = "http://localhost:9642", #This is the default local installation
|
|
30
|
-
model_name: str = "",
|
|
31
|
-
service_key: str|None = None, # a key generated on the lollms interface (it is advised to use LOLLMS_API_KEY environment variable instead)
|
|
32
|
-
verify_ssl_certificate: bool = True,
|
|
33
|
-
default_completion_format: ELF_COMPLETION_FORMAT = ELF_COMPLETION_FORMAT.Chat,
|
|
34
29
|
**kwargs):
|
|
35
30
|
"""
|
|
36
31
|
Initialize the OpenAI binding.
|
|
@@ -43,20 +38,35 @@ class LollmsBinding(LollmsLLMBinding):
|
|
|
43
38
|
verify_ssl_certificate (bool): Whether to verify SSL certificates. Defaults to True.
|
|
44
39
|
personality (Optional[int]): Ignored parameter for compatibility with LollmsLLMBinding.
|
|
45
40
|
"""
|
|
46
|
-
super().__init__(
|
|
47
|
-
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
self.model_name=model_name
|
|
51
|
-
self.service_key=service_key
|
|
52
|
-
self.verify_ssl_certificate=verify_ssl_certificate
|
|
53
|
-
self.default_completion_format=default_completion_format
|
|
41
|
+
super().__init__(BindingName, **kwargs)
|
|
42
|
+
self.host_address=kwargs.get("host_address","http://localhost:9642/v1").rstrip("/")
|
|
43
|
+
if not self.host_address.endswith("v1"):
|
|
44
|
+
self.host_address += "/v1"
|
|
45
|
+
self.model_name=kwargs.get("model_name")
|
|
46
|
+
self.service_key=kwargs.get("service_key")
|
|
47
|
+
self.verify_ssl_certificate=kwargs.get("verify_ssl_certificate", True)
|
|
48
|
+
self.default_completion_format=kwargs.get("default_completion_format", ELF_COMPLETION_FORMAT.Chat)
|
|
54
49
|
|
|
55
50
|
if not self.service_key:
|
|
56
51
|
self.service_key = os.getenv("LOLLMS_API_KEY", self.service_key)
|
|
57
|
-
self.client = openai.OpenAI(api_key=self.service_key, base_url=None if host_address is None else host_address if len(host_address)>0 else None)
|
|
52
|
+
self.client = openai.OpenAI(api_key=self.service_key, base_url=None if self.host_address is None else self.host_address if len(self.host_address)>0 else None)
|
|
58
53
|
self.completion_format = ELF_COMPLETION_FORMAT.Chat
|
|
59
54
|
|
|
55
|
+
def lollms_listMountedPersonalities(self, host_address:str|None=None):
|
|
56
|
+
host_address = host_address if host_address else self.host_address
|
|
57
|
+
url = f"{host_address}/list_mounted_personalities"
|
|
58
|
+
|
|
59
|
+
response = requests.get(url)
|
|
60
|
+
|
|
61
|
+
if response.status_code == 200:
|
|
62
|
+
try:
|
|
63
|
+
text = json.loads(response.content.decode("utf-8"))
|
|
64
|
+
return text
|
|
65
|
+
except Exception as ex:
|
|
66
|
+
return {"status": False, "error": str(ex)}
|
|
67
|
+
else:
|
|
68
|
+
return {"status": False, "error": response.text}
|
|
69
|
+
|
|
60
70
|
|
|
61
71
|
def generate_text(self,
|
|
62
72
|
prompt: str,
|
|
@@ -18,10 +18,6 @@ class LollmsWebuiLLMBinding(LollmsLLMBinding):
|
|
|
18
18
|
DEFAULT_HOST_ADDRESS = "http://localhost:9600"
|
|
19
19
|
|
|
20
20
|
def __init__(self,
|
|
21
|
-
host_address: str = None,
|
|
22
|
-
model_name: str = "",
|
|
23
|
-
service_key: str = None,
|
|
24
|
-
verify_ssl_certificate: bool = True,
|
|
25
21
|
personality: Optional[int] = None,
|
|
26
22
|
**kwargs
|
|
27
23
|
):
|
|
@@ -35,16 +31,14 @@ class LollmsWebuiLLMBinding(LollmsLLMBinding):
|
|
|
35
31
|
verify_ssl_certificate (bool): Whether to verify SSL certificates. Defaults to True.
|
|
36
32
|
personality (Optional[int]): Personality ID for generation. Defaults to None.
|
|
37
33
|
"""
|
|
38
|
-
super().__init__(
|
|
39
|
-
|
|
40
|
-
)
|
|
41
|
-
|
|
34
|
+
super().__init__(BindingName, **kwargs)
|
|
35
|
+
host_address = kwargs.get("host_address")
|
|
42
36
|
self.host_address=host_address if host_address is not None else self.DEFAULT_HOST_ADDRESS
|
|
43
|
-
self.model_name=model_name
|
|
44
|
-
self.service_key=service_key
|
|
45
|
-
self.verify_ssl_certificate=verify_ssl_certificate
|
|
37
|
+
self.model_name=kwargs.get("model_name")
|
|
38
|
+
self.service_key=kwargs.get("service_key")
|
|
39
|
+
self.verify_ssl_certificate=kwargs.get("verify_ssl_certificate")
|
|
46
40
|
self.default_completion_format=kwargs.get("default_completion_format",ELF_COMPLETION_FORMAT.Chat)
|
|
47
|
-
self.personality = personality
|
|
41
|
+
self.personality = kwargs.get("personality")
|
|
48
42
|
self.model = None
|
|
49
43
|
|
|
50
44
|
def generate_text(self,
|
|
@@ -11,11 +11,11 @@ import pipmaster as pm
|
|
|
11
11
|
# Ensure the required packages are installed
|
|
12
12
|
pm.ensure_packages(["mistralai", "pillow", "tiktoken"])
|
|
13
13
|
|
|
14
|
-
from mistralai
|
|
15
|
-
from mistralai.models.chat_completion import ChatMessage
|
|
14
|
+
from mistralai import Mistral
|
|
16
15
|
from PIL import Image, ImageDraw
|
|
17
16
|
import tiktoken
|
|
18
17
|
|
|
18
|
+
|
|
19
19
|
BindingName = "MistralBinding"
|
|
20
20
|
|
|
21
21
|
class MistralBinding(LollmsLLMBinding):
|
|
@@ -27,8 +27,6 @@ class MistralBinding(LollmsLLMBinding):
|
|
|
27
27
|
"""
|
|
28
28
|
|
|
29
29
|
def __init__(self,
|
|
30
|
-
model_name: str = "mistral-large-latest",
|
|
31
|
-
mistral_api_key: str = None,
|
|
32
30
|
**kwargs
|
|
33
31
|
):
|
|
34
32
|
"""
|
|
@@ -38,15 +36,15 @@ class MistralBinding(LollmsLLMBinding):
|
|
|
38
36
|
model_name (str): The name of the Mistral model to use.
|
|
39
37
|
mistral_api_key (str): The API key for the Mistral service.
|
|
40
38
|
"""
|
|
41
|
-
super().__init__(
|
|
42
|
-
self.model_name = model_name
|
|
43
|
-
self.mistral_api_key =
|
|
39
|
+
super().__init__(BindingName, **kwargs)
|
|
40
|
+
self.model_name = kwargs.get("model_name","mistral-large-latest")
|
|
41
|
+
self.mistral_api_key = kwargs.get("service_key") or os.getenv("MISTRAL_API_KEY")
|
|
44
42
|
|
|
45
43
|
if not self.mistral_api_key:
|
|
46
44
|
raise ValueError("Mistral API key is required. Set it via 'mistral_api_key' or MISTRAL_API_KEY env var.")
|
|
47
45
|
|
|
48
46
|
try:
|
|
49
|
-
self.client =
|
|
47
|
+
self.client = Mistral(api_key=self.mistral_api_key)
|
|
50
48
|
except Exception as e:
|
|
51
49
|
ASCIIColors.error(f"Failed to configure Mistral client: {e}")
|
|
52
50
|
self.client = None
|
|
@@ -64,35 +62,67 @@ class MistralBinding(LollmsLLMBinding):
|
|
|
64
62
|
if n_predict is not None: params['max_tokens'] = n_predict
|
|
65
63
|
if seed is not None: params['random_seed'] = seed # Mistral uses 'random_seed'
|
|
66
64
|
return params
|
|
65
|
+
|
|
67
66
|
|
|
68
|
-
def _prepare_messages(self, discussion: LollmsDiscussion, branch_tip_id: Optional[str] = None) -> List[
|
|
69
|
-
"""Prepares the message list for the
|
|
67
|
+
def _prepare_messages(self, discussion: LollmsDiscussion, branch_tip_id: Optional[str] = None) -> List[Dict[str, any]]:
|
|
68
|
+
"""Prepares the message list for the API from a LollmsDiscussion."""
|
|
70
69
|
history = []
|
|
71
70
|
if discussion.system_prompt:
|
|
72
|
-
|
|
73
|
-
# A lone system message is not ideal. We will prepend it to the first user message.
|
|
74
|
-
# However, for API consistency, we will treat it as a separate message if it exists.
|
|
75
|
-
# The official client will likely handle this.
|
|
76
|
-
history.append(ChatMessage(role="system", content=discussion.system_prompt))
|
|
71
|
+
history.append({"role": "system", "content": discussion.system_prompt})
|
|
77
72
|
|
|
78
73
|
for msg in discussion.get_messages(branch_tip_id):
|
|
79
74
|
role = 'user' if msg.sender_type == "user" else 'assistant'
|
|
80
|
-
# Note:
|
|
75
|
+
# Note: Vision support depends on the specific model being called via OpenRouter.
|
|
76
|
+
# We will not implement it in this generic binding to avoid complexity,
|
|
77
|
+
# as different models might expect different formats.
|
|
81
78
|
if msg.content:
|
|
82
|
-
history.append(
|
|
79
|
+
history.append({'role': role, 'content': msg.content})
|
|
83
80
|
return history
|
|
84
81
|
|
|
85
|
-
def generate_text(self,
|
|
82
|
+
def generate_text(self,
|
|
83
|
+
prompt: str,
|
|
84
|
+
images: Optional[List[str]] = None,
|
|
85
|
+
system_prompt: str = "",
|
|
86
|
+
n_predict: Optional[int] = None,
|
|
87
|
+
stream: Optional[bool] = None,
|
|
88
|
+
temperature: float = 0.7, # Ollama default is 0.8, common default 0.7
|
|
89
|
+
top_k: int = 40, # Ollama default is 40
|
|
90
|
+
top_p: float = 0.9, # Ollama default is 0.9
|
|
91
|
+
repeat_penalty: float = 1.1, # Ollama default is 1.1
|
|
92
|
+
repeat_last_n: int = 64, # Ollama default is 64
|
|
93
|
+
seed: Optional[int] = None,
|
|
94
|
+
n_threads: Optional[int] = None,
|
|
95
|
+
ctx_size: int | None = None,
|
|
96
|
+
streaming_callback: Optional[Callable[[str, MSG_TYPE], None]] = None,
|
|
97
|
+
split: Optional[bool] = False, # put to true if the prompt is a discussion
|
|
98
|
+
user_keyword: Optional[str] = "!@>user:",
|
|
99
|
+
ai_keyword: Optional[str] = "!@>assistant:",
|
|
100
|
+
**kwargs
|
|
101
|
+
) -> Union[str, dict]:
|
|
86
102
|
"""
|
|
87
|
-
Generate text using
|
|
103
|
+
Generate text using OpenRouter. This is a wrapper around the chat method.
|
|
88
104
|
"""
|
|
89
|
-
temp_discussion = LollmsDiscussion
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
temp_discussion.system_prompt = kwargs.get("system_prompt")
|
|
105
|
+
temp_discussion = LollmsDiscussion(None)
|
|
106
|
+
temp_discussion.add_message(sender="user", content=prompt, images=images or [])
|
|
107
|
+
if system_prompt:
|
|
108
|
+
temp_discussion.system_prompt = system_prompt
|
|
94
109
|
|
|
95
|
-
return self.chat(temp_discussion,
|
|
110
|
+
return self.chat(temp_discussion,
|
|
111
|
+
n_predict=n_predict,
|
|
112
|
+
stream=stream,
|
|
113
|
+
temperature=temperature,
|
|
114
|
+
top_k=top_k,
|
|
115
|
+
top_p=top_p,
|
|
116
|
+
repeat_penalty=repeat_penalty,
|
|
117
|
+
repeat_last_n=repeat_last_n,
|
|
118
|
+
seed=seed,
|
|
119
|
+
n_threads=n_threads,
|
|
120
|
+
ctx_size=ctx_size,
|
|
121
|
+
streaming_callback=streaming_callback,
|
|
122
|
+
split=split,
|
|
123
|
+
user_keyword=user_keyword,
|
|
124
|
+
ai_keyword=ai_keyword,
|
|
125
|
+
**kwargs)
|
|
96
126
|
|
|
97
127
|
def chat(self,
|
|
98
128
|
discussion: LollmsDiscussion,
|
|
@@ -117,7 +147,7 @@ class MistralBinding(LollmsLLMBinding):
|
|
|
117
147
|
|
|
118
148
|
try:
|
|
119
149
|
if stream:
|
|
120
|
-
response = self.client.
|
|
150
|
+
response = self.client.chat.stream(
|
|
121
151
|
model=self.model_name,
|
|
122
152
|
messages=messages,
|
|
123
153
|
**api_params
|
|
@@ -131,7 +161,7 @@ class MistralBinding(LollmsLLMBinding):
|
|
|
131
161
|
break
|
|
132
162
|
return full_response_text
|
|
133
163
|
else:
|
|
134
|
-
response = self.client.chat(
|
|
164
|
+
response = self.client.chat.complete(
|
|
135
165
|
model=self.model_name,
|
|
136
166
|
messages=messages,
|
|
137
167
|
**api_params
|
|
@@ -201,7 +231,7 @@ class MistralBinding(LollmsLLMBinding):
|
|
|
201
231
|
return []
|
|
202
232
|
try:
|
|
203
233
|
ASCIIColors.debug("Listing Mistral models...")
|
|
204
|
-
models = self.client.
|
|
234
|
+
models = self.client.models.list()
|
|
205
235
|
model_info_list = []
|
|
206
236
|
for m in models.data:
|
|
207
237
|
model_info_list.append({
|
|
@@ -264,7 +294,6 @@ if __name__ == '__main__':
|
|
|
264
294
|
ASCIIColors.cyan("\n--- Text Generation (Streaming) ---")
|
|
265
295
|
full_streamed_text = ""
|
|
266
296
|
def stream_callback(chunk: str, msg_type: int):
|
|
267
|
-
nonlocal full_streamed_text
|
|
268
297
|
ASCIIColors.green(chunk, end="", flush=True)
|
|
269
298
|
full_streamed_text += chunk
|
|
270
299
|
return True
|
|
@@ -67,10 +67,6 @@ class OllamaBinding(LollmsLLMBinding):
|
|
|
67
67
|
DEFAULT_HOST_ADDRESS = "http://localhost:11434"
|
|
68
68
|
|
|
69
69
|
def __init__(self,
|
|
70
|
-
host_address: str = None,
|
|
71
|
-
model_name: str = "",
|
|
72
|
-
service_key: str = None,
|
|
73
|
-
verify_ssl_certificate: bool = True,
|
|
74
70
|
**kwargs
|
|
75
71
|
):
|
|
76
72
|
"""
|
|
@@ -83,14 +79,13 @@ class OllamaBinding(LollmsLLMBinding):
|
|
|
83
79
|
verify_ssl_certificate (bool): Whether to verify SSL certificates. Defaults to True.
|
|
84
80
|
default_completion_format (ELF_COMPLETION_FORMAT): Default completion format.
|
|
85
81
|
"""
|
|
82
|
+
host_address = kwargs.get("host_address")
|
|
86
83
|
_host_address = host_address if host_address is not None else self.DEFAULT_HOST_ADDRESS
|
|
87
|
-
super().__init__(
|
|
88
|
-
binding_name=BindingName, # Use the module-level BindingName
|
|
89
|
-
)
|
|
84
|
+
super().__init__(BindingName, **kwargs)
|
|
90
85
|
self.host_address=_host_address
|
|
91
|
-
self.model_name=model_name
|
|
92
|
-
self.service_key=service_key
|
|
93
|
-
self.verify_ssl_certificate=verify_ssl_certificate
|
|
86
|
+
self.model_name=kwargs.get("model_name")
|
|
87
|
+
self.service_key=kwargs.get("service_key")
|
|
88
|
+
self.verify_ssl_certificate=kwargs.get("verify_ssl_certificate", True)
|
|
94
89
|
self.default_completion_format=kwargs.get("default_completion_format",ELF_COMPLETION_FORMAT.Chat)
|
|
95
90
|
|
|
96
91
|
if ollama is None:
|
|
@@ -376,7 +371,7 @@ class OllamaBinding(LollmsLLMBinding):
|
|
|
376
371
|
# 2. Build the generation options dictionary
|
|
377
372
|
options = {
|
|
378
373
|
'num_predict': n_predict,
|
|
379
|
-
'temperature': float(temperature),
|
|
374
|
+
'temperature': float(temperature) if temperature else None,
|
|
380
375
|
'top_k': top_k,
|
|
381
376
|
'top_p': top_p,
|
|
382
377
|
'repeat_penalty': repeat_penalty,
|
|
@@ -28,8 +28,6 @@ class OpenRouterBinding(LollmsLLMBinding):
|
|
|
28
28
|
BASE_URL = "https://openrouter.ai/api/v1"
|
|
29
29
|
|
|
30
30
|
def __init__(self,
|
|
31
|
-
model_name: str = "google/gemini-flash-1.5", # A good, fast default
|
|
32
|
-
open_router_api_key: str = None,
|
|
33
31
|
**kwargs
|
|
34
32
|
):
|
|
35
33
|
"""
|
|
@@ -37,11 +35,11 @@ class OpenRouterBinding(LollmsLLMBinding):
|
|
|
37
35
|
|
|
38
36
|
Args:
|
|
39
37
|
model_name (str): The name of the model to use from OpenRouter (e.g., 'anthropic/claude-3-haiku-20240307').
|
|
40
|
-
|
|
38
|
+
service_key (str): The API key for the OpenRouter service.
|
|
41
39
|
"""
|
|
42
|
-
super().__init__(
|
|
43
|
-
self.model_name = model_name
|
|
44
|
-
self.api_key =
|
|
40
|
+
super().__init__(BindingName, **kwargs)
|
|
41
|
+
self.model_name = kwargs.get("model_name","google/gemini-flash-1.5")
|
|
42
|
+
self.api_key = kwargs.get("service_key") or os.getenv("OPENROUTER_API_KEY")
|
|
45
43
|
|
|
46
44
|
if not self.api_key:
|
|
47
45
|
raise ValueError("OpenRouter API key is required. Set it via 'open_router_api_key' or OPENROUTER_API_KEY env var.")
|
|
@@ -84,17 +82,50 @@ class OpenRouterBinding(LollmsLLMBinding):
|
|
|
84
82
|
history.append({'role': role, 'content': msg.content})
|
|
85
83
|
return history
|
|
86
84
|
|
|
87
|
-
def generate_text(self,
|
|
85
|
+
def generate_text(self,
|
|
86
|
+
prompt: str,
|
|
87
|
+
images: Optional[List[str]] = None,
|
|
88
|
+
system_prompt: str = "",
|
|
89
|
+
n_predict: Optional[int] = None,
|
|
90
|
+
stream: Optional[bool] = None,
|
|
91
|
+
temperature: float = 0.7, # Ollama default is 0.8, common default 0.7
|
|
92
|
+
top_k: int = 40, # Ollama default is 40
|
|
93
|
+
top_p: float = 0.9, # Ollama default is 0.9
|
|
94
|
+
repeat_penalty: float = 1.1, # Ollama default is 1.1
|
|
95
|
+
repeat_last_n: int = 64, # Ollama default is 64
|
|
96
|
+
seed: Optional[int] = None,
|
|
97
|
+
n_threads: Optional[int] = None,
|
|
98
|
+
ctx_size: int | None = None,
|
|
99
|
+
streaming_callback: Optional[Callable[[str, MSG_TYPE], None]] = None,
|
|
100
|
+
split: Optional[bool] = False, # put to true if the prompt is a discussion
|
|
101
|
+
user_keyword: Optional[str] = "!@>user:",
|
|
102
|
+
ai_keyword: Optional[str] = "!@>assistant:",
|
|
103
|
+
**kwargs
|
|
104
|
+
) -> Union[str, dict]:
|
|
88
105
|
"""
|
|
89
106
|
Generate text using OpenRouter. This is a wrapper around the chat method.
|
|
90
107
|
"""
|
|
91
|
-
temp_discussion = LollmsDiscussion
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
temp_discussion.system_prompt = kwargs.get("system_prompt")
|
|
108
|
+
temp_discussion = LollmsDiscussion(None)
|
|
109
|
+
temp_discussion.add_message(sender="user", content=prompt, images=images or [])
|
|
110
|
+
if system_prompt:
|
|
111
|
+
temp_discussion.system_prompt = system_prompt
|
|
96
112
|
|
|
97
|
-
return self.chat(temp_discussion,
|
|
113
|
+
return self.chat(temp_discussion,
|
|
114
|
+
n_predict=n_predict,
|
|
115
|
+
stream=stream,
|
|
116
|
+
temperature=temperature,
|
|
117
|
+
top_k=top_k,
|
|
118
|
+
top_p=top_p,
|
|
119
|
+
repeat_penalty=repeat_penalty,
|
|
120
|
+
repeat_last_n=repeat_last_n,
|
|
121
|
+
seed=seed,
|
|
122
|
+
n_threads=n_threads,
|
|
123
|
+
ctx_size=ctx_size,
|
|
124
|
+
streaming_callback=streaming_callback,
|
|
125
|
+
split=split,
|
|
126
|
+
user_keyword=user_keyword,
|
|
127
|
+
ai_keyword=ai_keyword,
|
|
128
|
+
**kwargs)
|
|
98
129
|
|
|
99
130
|
def chat(self,
|
|
100
131
|
discussion: LollmsDiscussion,
|
|
@@ -121,7 +152,7 @@ class OpenRouterBinding(LollmsLLMBinding):
|
|
|
121
152
|
response = self.client.chat.completions.create(
|
|
122
153
|
model=self.model_name,
|
|
123
154
|
messages=messages,
|
|
124
|
-
stream=stream,
|
|
155
|
+
stream=stream if stream else False,
|
|
125
156
|
**api_params
|
|
126
157
|
)
|
|
127
158
|
|
|
@@ -27,11 +27,6 @@ class OpenAIBinding(LollmsLLMBinding):
|
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
def __init__(self,
|
|
30
|
-
host_address: str = None,
|
|
31
|
-
model_name: str = "",
|
|
32
|
-
service_key: str = None,
|
|
33
|
-
verify_ssl_certificate: bool = True,
|
|
34
|
-
default_completion_format: ELF_COMPLETION_FORMAT = ELF_COMPLETION_FORMAT.Chat,
|
|
35
30
|
**kwargs):
|
|
36
31
|
"""
|
|
37
32
|
Initialize the OpenAI binding.
|
|
@@ -43,18 +38,16 @@ class OpenAIBinding(LollmsLLMBinding):
|
|
|
43
38
|
verify_ssl_certificate (bool): Whether to verify SSL certificates. Defaults to True.
|
|
44
39
|
personality (Optional[int]): Ignored parameter for compatibility with LollmsLLMBinding.
|
|
45
40
|
"""
|
|
46
|
-
super().__init__(
|
|
47
|
-
|
|
48
|
-
)
|
|
49
|
-
self.
|
|
50
|
-
self.
|
|
51
|
-
self.
|
|
52
|
-
self.verify_ssl_certificate=verify_ssl_certificate
|
|
53
|
-
self.default_completion_format=default_completion_format
|
|
41
|
+
super().__init__(BindingName, **kwargs)
|
|
42
|
+
self.host_address=kwargs.get("host_address")
|
|
43
|
+
self.model_name=kwargs.get("model_name")
|
|
44
|
+
self.service_key=kwargs.get("service_key")
|
|
45
|
+
self.verify_ssl_certificate=kwargs.get("verify_ssl_certificate", True)
|
|
46
|
+
self.default_completion_format=kwargs.get("default_completion_format", ELF_COMPLETION_FORMAT.Chat)
|
|
54
47
|
|
|
55
48
|
if not self.service_key:
|
|
56
49
|
self.service_key = os.getenv("OPENAI_API_KEY", self.service_key)
|
|
57
|
-
self.client = openai.OpenAI(api_key=self.service_key, base_url=None if host_address is None else host_address if len(host_address)>0 else None)
|
|
50
|
+
self.client = openai.OpenAI(api_key=self.service_key, base_url=None if self.host_address is None else self.host_address if len(self.host_address)>0 else None)
|
|
58
51
|
self.completion_format = ELF_COMPLETION_FORMAT.Chat
|
|
59
52
|
|
|
60
53
|
def _build_openai_params(self, messages: list, **kwargs) -> dict:
|
|
@@ -61,23 +61,23 @@ class OpenLLMBinding(LollmsLLMBinding):
|
|
|
61
61
|
DEFAULT_HOST_ADDRESS = "http://localhost:3000" # Default OpenLLM server address
|
|
62
62
|
|
|
63
63
|
def __init__(self,
|
|
64
|
-
host_address: str = None,
|
|
65
|
-
model_name: str = "", # Informational, as client connects to specific model server
|
|
66
|
-
# service_key and verify_ssl_certificate are not directly used by openllm.client.HTTPClient constructor
|
|
67
|
-
# but kept for potential future extensions or custom client logic.
|
|
68
|
-
service_key: Optional[str] = None,
|
|
69
|
-
verify_ssl_certificate: bool = True,
|
|
70
|
-
timeout: int = 120, # Timeout for client requests
|
|
71
64
|
**kwargs
|
|
72
65
|
):
|
|
66
|
+
""" Initialize the OpenLLM binding.
|
|
67
|
+
Args:
|
|
68
|
+
host_address (str): The address of the OpenLLM server (default: http://localhost:3000).
|
|
69
|
+
model_name (str): The name of the model to connect to. This is primarily for informational purposes.
|
|
70
|
+
service_key (Optional[str]): Optional service key for authentication, not used by openllm client.
|
|
71
|
+
verify_ssl_certificate (bool): Whether to verify SSL certificates (default: True).
|
|
72
|
+
timeout (int): Timeout for client requests in seconds (default: 120).
|
|
73
|
+
"""
|
|
74
|
+
host_address = kwargs.get("host_address")
|
|
73
75
|
_host_address = host_address if host_address is not None else self.DEFAULT_HOST_ADDRESS
|
|
74
|
-
super().__init__(
|
|
75
|
-
binding_name=BindingName,
|
|
76
|
-
)
|
|
76
|
+
super().__init__(BindingName, **kwargs)
|
|
77
77
|
self.host_address = _host_address
|
|
78
|
-
self.model_name = model_name # Can be set by load_model or from config
|
|
78
|
+
self.model_name = kwargs.get("model_name") # Can be set by load_model or from config
|
|
79
79
|
self.default_completion_format=kwargs.get("default_completion_format",ELF_COMPLETION_FORMAT.Chat)
|
|
80
|
-
self.timeout = timeout
|
|
80
|
+
self.timeout = kwargs.get("timeout")
|
|
81
81
|
|
|
82
82
|
if openllm is None or openllm.client is None:
|
|
83
83
|
raise ImportError("OpenLLM library is not installed or client module not found. Please run 'pip install openllm'.")
|
|
@@ -89,7 +89,7 @@ class PythonLlamaCppBinding(LollmsLLMBinding):
|
|
|
89
89
|
**kwargs
|
|
90
90
|
):
|
|
91
91
|
|
|
92
|
-
super().__init__(
|
|
92
|
+
super().__init__(BindingName, **kwargs)
|
|
93
93
|
|
|
94
94
|
if Llama is None: # Check if import failed
|
|
95
95
|
raise ImportError("Llama-cpp-python library is not available. Please install it.")
|
|
@@ -232,25 +232,20 @@ def resolve_hf_model_path(model_id_or_gguf_id: str, models_base_path: Path) -> P
|
|
|
232
232
|
# --- VLLM Binding Class ---
|
|
233
233
|
class VLLMBinding(LollmsLLMBinding):
|
|
234
234
|
def __init__(self,
|
|
235
|
-
models_folder: Optional[Union[str, Path]] = None,
|
|
236
|
-
model_name: str = "",
|
|
237
|
-
service_key: Optional[str] = None,
|
|
238
|
-
verify_ssl_certificate: bool = True,
|
|
239
|
-
default_completion_format: ELF_COMPLETION_FORMAT = ELF_COMPLETION_FORMAT.Chat,
|
|
240
235
|
**kwargs
|
|
241
236
|
):
|
|
242
237
|
if not _vllm_deps_installed:
|
|
243
238
|
raise ImportError(f"vLLM or its dependencies not installed. Binding unusable. Error: {_vllm_installation_error}")
|
|
244
239
|
if engine_manager is None:
|
|
245
240
|
raise RuntimeError("VLLMEngineManager failed to initialize. Binding unusable.")
|
|
246
|
-
|
|
241
|
+
models_folder = kwargs.get("models_folder")
|
|
247
242
|
_models_folder = Path(models_folder) if models_folder is not None else DEFAULT_models_folder
|
|
248
243
|
_models_folder.mkdir(parents=True, exist_ok=True)
|
|
249
244
|
|
|
250
|
-
super().__init__(BindingName)
|
|
251
|
-
self.models_folder=
|
|
252
|
-
self.model_name=model_name
|
|
253
|
-
self.default_completion_format=default_completion_format
|
|
245
|
+
super().__init__(BindingName, **kwargs)
|
|
246
|
+
self.models_folder= _models_folder
|
|
247
|
+
self.model_name=kwargs.get("model_name", "")
|
|
248
|
+
self.default_completion_format=kwargs.get("default_completion_format", ELF_COMPLETION_FORMAT.Chat)
|
|
254
249
|
|
|
255
250
|
|
|
256
251
|
self.models_folder: Path = _models_folder
|
|
@@ -261,11 +256,11 @@ class VLLMBinding(LollmsLLMBinding):
|
|
|
261
256
|
self.current_engine_params: Optional[Dict[str, Any]] = None
|
|
262
257
|
self.vllm_engine_kwargs_config = kwargs.copy()
|
|
263
258
|
|
|
264
|
-
if model_name:
|
|
259
|
+
if self.model_name:
|
|
265
260
|
try:
|
|
266
|
-
self.load_model(model_name)
|
|
261
|
+
self.load_model(self.model_name)
|
|
267
262
|
except Exception as e:
|
|
268
|
-
ASCIIColors.error(f"Auto-load model '{model_name}' failed: {e}")
|
|
263
|
+
ASCIIColors.error(f"Auto-load model '{self.model_name}' failed: {e}")
|
|
269
264
|
trace_exception(e)
|
|
270
265
|
|
|
271
266
|
def _get_vllm_engine_params_for_load(self) -> Dict[str, Any]:
|
|
@@ -91,14 +91,22 @@ class HuggingFaceHubBinding(LollmsLLMBinding):
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
def __init__(self,
|
|
94
|
-
model_name_or_id: str, # Can be HF Hub ID or local folder name relative to models_path
|
|
95
|
-
models_path: Union[str, Path],
|
|
96
|
-
config: Optional[Dict[str, Any]] = None,
|
|
97
|
-
default_completion_format: ELF_COMPLETION_FORMAT = ELF_COMPLETION_FORMAT.Chat,
|
|
98
94
|
**kwargs # Overrides for config_args
|
|
99
95
|
):
|
|
100
|
-
|
|
101
|
-
|
|
96
|
+
"""
|
|
97
|
+
Initializes the Hugging Face Hub binding.
|
|
98
|
+
Args:
|
|
99
|
+
model_name (str): Hugging Face Hub model ID or local folder name.
|
|
100
|
+
models_path (str or Path): Path to the directory containing local models.
|
|
101
|
+
config (Optional[Dict[str, Any]]): Optional configuration dictionary to override defaults.
|
|
102
|
+
default_completion_format (ELF_COMPLETION_FORMAT): Default format for text generation.
|
|
103
|
+
"""
|
|
104
|
+
super().__init__(BindingName, **kwargs)
|
|
105
|
+
|
|
106
|
+
model_name_or_id = kwargs.get("model_name")
|
|
107
|
+
models_path = kwargs.get("models_path")
|
|
108
|
+
config = kwargs.get("config")
|
|
109
|
+
default_completion_format = kwargs.get("default_completion_format", ELF_COMPLETION_FORMAT.Chat)
|
|
102
110
|
|
|
103
111
|
if torch is None or transformers is None: # Check if core imports failed
|
|
104
112
|
raise ImportError("Core libraries (torch, transformers) not available. Binding cannot function.")
|
|
@@ -232,24 +232,28 @@ def resolve_hf_model_path(model_id_or_gguf_id: str, models_base_path: Path) -> P
|
|
|
232
232
|
# --- VLLM Binding Class ---
|
|
233
233
|
class VLLMBinding(LollmsLLMBinding):
|
|
234
234
|
def __init__(self,
|
|
235
|
-
models_folder: Optional[Union[str, Path]] = None,
|
|
236
|
-
model_name: str = "",
|
|
237
|
-
service_key: Optional[str] = None,
|
|
238
|
-
verify_ssl_certificate: bool = True,
|
|
239
|
-
default_completion_format: ELF_COMPLETION_FORMAT = ELF_COMPLETION_FORMAT.Chat,
|
|
240
235
|
**kwargs
|
|
241
236
|
):
|
|
237
|
+
"""Initializes the VLLM binding with the specified model and configuration.
|
|
238
|
+
Args:
|
|
239
|
+
models_folder (Optional[Union[str, Path]]): Path to the directory containing local models.²
|
|
240
|
+
model_name (str): Name or ID of the model to load. Can be a Hugging Face Hub ID or local folder name.
|
|
241
|
+
service_key (Optional[str]): Service key for authentication (not used in this binding).
|
|
242
|
+
verify_ssl_certificate (bool): Whether to verify SSL certificates (not used in this binding).
|
|
243
|
+
default_completion_format (ELF_COMPLETION_FORMAT): Default format for text generation.
|
|
244
|
+
"""
|
|
242
245
|
if not _vllm_deps_installed:
|
|
243
246
|
raise ImportError(f"vLLM or its dependencies not installed. Binding unusable. Error: {_vllm_installation_error}")
|
|
244
247
|
if engine_manager is None:
|
|
245
248
|
raise RuntimeError("VLLMEngineManager failed to initialize. Binding unusable.")
|
|
246
|
-
|
|
249
|
+
models_folder = kwargs.get("models_folder")
|
|
250
|
+
default_completion_format = kwargs.get("default_completion_format", ELF_COMPLETION_FORMAT.Chat)
|
|
247
251
|
_models_folder = Path(models_folder) if models_folder is not None else DEFAULT_models_folder
|
|
248
252
|
_models_folder.mkdir(parents=True, exist_ok=True)
|
|
249
253
|
|
|
250
|
-
super().__init__(BindingName)
|
|
251
|
-
self.models_folder= models_folder
|
|
252
|
-
self.model_name=model_name
|
|
254
|
+
super().__init__(BindingName, **kwargs)
|
|
255
|
+
self.models_folder= kwargs.get("models_folder")
|
|
256
|
+
self.model_name=kwargs.get("model_name")
|
|
253
257
|
self.default_completion_format=default_completion_format
|
|
254
258
|
|
|
255
259
|
|
|
@@ -261,11 +265,11 @@ class VLLMBinding(LollmsLLMBinding):
|
|
|
261
265
|
self.current_engine_params: Optional[Dict[str, Any]] = None
|
|
262
266
|
self.vllm_engine_kwargs_config = kwargs.copy()
|
|
263
267
|
|
|
264
|
-
if model_name:
|
|
268
|
+
if self.model_name:
|
|
265
269
|
try:
|
|
266
|
-
self.load_model(model_name)
|
|
270
|
+
self.load_model(self.model_name)
|
|
267
271
|
except Exception as e:
|
|
268
|
-
ASCIIColors.error(f"Auto-load model '{model_name}' failed: {e}")
|
|
272
|
+
ASCIIColors.error(f"Auto-load model '{self.model_name}' failed: {e}")
|
|
269
273
|
trace_exception(e)
|
|
270
274
|
|
|
271
275
|
def _get_vllm_engine_params_for_load(self) -> Dict[str, Any]:
|