py-adtools 0.1.5__py3-none-any.whl → 0.1.7__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 py-adtools might be problematic. Click here for more details.

adtools/lm/__init__.py ADDED
@@ -0,0 +1,6 @@
1
+ try:
2
+ import openai
3
+ except ImportError:
4
+ raise ImportError('Python package "openai" is not installed.')
5
+
6
+ from .lm_base import LanguageModel
adtools/lm/lm_base.py ADDED
@@ -0,0 +1,39 @@
1
+ """
2
+ Copyright (c) 2025 Rui Zhang <rzhang.cs@gmail.com>
3
+
4
+ NOTICE: This code is under MIT license. This code is intended for academic/research purposes only.
5
+ Commercial use of this software or its derivatives requires prior written permission.
6
+ """
7
+
8
+ from abc import abstractmethod
9
+ from typing import List
10
+
11
+ import openai.types.chat
12
+
13
+
14
+ class LanguageModel:
15
+ """Base class for language model interface."""
16
+
17
+ @abstractmethod
18
+ def chat_completion(
19
+ self,
20
+ message: str | List[openai.types.chat.ChatCompletionMessageParam],
21
+ max_tokens: int,
22
+ timeout_seconds: float,
23
+ *args,
24
+ **kwargs
25
+ ):
26
+ """Send a chat completion query with OpenAI format to the vLLM server. Return the response content.
27
+ Args:
28
+ message: The message in str or openai format.
29
+ max_tokens: The maximum number of tokens to generate.
30
+ timeout_seconds: The timeout seconds.
31
+ """
32
+ pass
33
+
34
+ def close(self):
35
+ """Release resources (if necessary)."""
36
+ pass
37
+
38
+ def __del__(self):
39
+ self.close()
@@ -0,0 +1,72 @@
1
+ """
2
+ Copyright (c) 2025 Rui Zhang <rzhang.cs@gmail.com>
3
+
4
+ NOTICE: This code is under MIT license. This code is intended for academic/research purposes only.
5
+ Commercial use of this software or its derivatives requires prior written permission.
6
+ """
7
+
8
+ import os
9
+ from typing import List
10
+
11
+ import openai.types.chat
12
+
13
+ from .lm_base import LanguageModel
14
+
15
+
16
+ class OpenAIAPI(LanguageModel):
17
+ def __init__(
18
+ self,
19
+ model: str,
20
+ base_url: str = None,
21
+ api_key: str = None,
22
+ **openai_init_kwargs
23
+ ):
24
+ super().__init__()
25
+ # If base_url is set to None, find 'OPENAI_BASE_URL' in environment variables
26
+ if base_url is None:
27
+ if 'OPENAI_BASE_URL' not in os.environ:
28
+ raise RuntimeError('If "base_url" is None, the environment variable OPENAI_BASE_URL must be set.')
29
+ else:
30
+ base_url = os.environ['OPENAI_BASE_URL']
31
+
32
+ # If api_key is set to None, find 'OPENAI_API_KEY' in environment variables
33
+ if api_key is None:
34
+ if 'OPENAI_API_KEY' not in os.environ:
35
+ raise RuntimeError('If "api_key" is None, OPENAI_API_KEY must be set.')
36
+ else:
37
+ api_key = os.environ['OPENAI_API_KEY']
38
+
39
+ self._model = model
40
+ self._client = openai.OpenAI(
41
+ api_key=api_key,
42
+ base_url=base_url,
43
+ **openai_init_kwargs
44
+ )
45
+
46
+ def chat_completion(
47
+ self,
48
+ message: str | List[openai.types.chat.ChatCompletionMessageParam],
49
+ max_tokens: int,
50
+ timeout_seconds: float,
51
+ *args,
52
+ **kwargs
53
+ ):
54
+ """Send a chat completion query with OpenAI format to the vLLM server. Return the response content.
55
+ Args:
56
+ message: The message in str or openai format.
57
+ max_tokens: The maximum number of tokens to generate.
58
+ timeout_seconds: The timeout seconds.
59
+ """
60
+ if isinstance(message, str):
61
+ message = [{'role': 'user', 'content': message.strip()}]
62
+
63
+ response = self._client.chat.completions.create(
64
+ model=self._model,
65
+ messages=message,
66
+ stream=False,
67
+ max_tokens=max_tokens,
68
+ timeout=timeout_seconds,
69
+ *args,
70
+ **kwargs,
71
+ )
72
+ return response.choices[0].message.content
@@ -5,103 +5,27 @@ NOTICE: This code is under MIT license. This code is intended for academic/resea
5
5
  Commercial use of this software or its derivatives requires prior written permission.
6
6
  """
7
7
 
8
- from abc import abstractmethod
8
+ try:
9
+ import vllm
10
+ except ImportError:
11
+ raise ImportError('Python package "vllm" is not installed.')
12
+
13
+ try:
14
+ import requests
15
+ except ImportError:
16
+ raise ImportError('Python package "requests" is not installed.')
17
+
9
18
  from typing import Optional, List, Literal, Dict, Any
10
19
  import os
11
20
  import subprocess
12
21
  import sys
13
22
  from pathlib import Path
14
23
  import psutil
15
- import requests
16
24
  import time
17
25
 
18
26
  import openai.types.chat
19
27
 
20
- __all__ = ['LanguageModel', 'OpenAIAPI', 'VLLMServer']
21
-
22
-
23
- class LanguageModel:
24
- """Base class for language model interface."""
25
-
26
- @abstractmethod
27
- def chat_completion(
28
- self,
29
- message: str | List[openai.types.chat.ChatCompletionMessageParam],
30
- max_tokens: int,
31
- timeout_seconds: float,
32
- *args,
33
- **kwargs
34
- ):
35
- """Send a chat completion query with OpenAI format to the vLLM server. Return the response content.
36
- Args:
37
- message: The message in str or openai format.
38
- max_tokens: The maximum number of tokens to generate.
39
- timeout_seconds: The timeout seconds.
40
- """
41
- pass
42
-
43
- def close(self):
44
- """Release resources (if necessary)."""
45
- pass
46
-
47
-
48
- class OpenAIAPI(LanguageModel):
49
- def __init__(
50
- self,
51
- model: str,
52
- base_url: str = None,
53
- api_key: str = None,
54
- **openai_init_kwargs
55
- ):
56
- super().__init__()
57
- # If base_url is set to None, find 'OPENAI_BASE_URL' in environment variables
58
- if base_url is None:
59
- if 'OPENAI_BASE_URL' not in os.environ:
60
- raise RuntimeError('If "base_url" is None, the environment variable OPENAI_BASE_URL must be set.')
61
- else:
62
- base_url = os.environ['OPENAI_BASE_URL']
63
-
64
- # If api_key is set to None, find 'OPENAI_API_KEY' in environment variables
65
- if api_key is None:
66
- if 'OPENAI_API_KEY' not in os.environ:
67
- raise RuntimeError('If "api_key" is None, OPENAI_API_KEY must be set.')
68
- else:
69
- api_key = os.environ['OPENAI_API_KEY']
70
-
71
- self._model = model
72
- self._client = openai.OpenAI(
73
- api_key=api_key,
74
- base_url=base_url,
75
- **openai_init_kwargs
76
- )
77
-
78
- def chat_completion(
79
- self,
80
- message: str | List[openai.types.chat.ChatCompletionMessageParam],
81
- max_tokens: int,
82
- timeout_seconds: float,
83
- *args,
84
- **kwargs
85
- ):
86
- """Send a chat completion query with OpenAI format to the vLLM server. Return the response content.
87
- Args:
88
- message: The message in str or openai format.
89
- max_tokens: The maximum number of tokens to generate.
90
- timeout_seconds: The timeout seconds.
91
- """
92
- if isinstance(message, str):
93
- message = [{'role': 'user', 'content': message.strip()}]
94
-
95
- response = self._client.chat.completions.create(
96
- model=self._model,
97
- messages=message,
98
- stream=False,
99
- max_tokens=max_tokens,
100
- timeout=timeout_seconds,
101
- *args,
102
- **kwargs,
103
- )
104
- return response.choices[0].message.content
28
+ from .lm_base import LanguageModel
105
29
 
106
30
 
107
31
  def _print_cmd_list(cmd_list, gpus, host, port):
@@ -115,24 +39,26 @@ def _print_cmd_list(cmd_list, gpus, host, port):
115
39
  print('=' * 80 + '\n', flush=True)
116
40
 
117
41
 
118
- class VLLMServer:
119
- def __init__(self,
120
- model_path: str,
121
- port: int,
122
- gpus: int | list[int],
123
- tokenizer_path: Optional[str] = None,
124
- max_model_len: int = 16384,
125
- max_lora_rank: Optional[int] = None,
126
- host: str = '0.0.0.0',
127
- mem_util: float = 0.85,
128
- deploy_timeout_seconds: int = 600,
129
- enforce_eager: bool = False,
130
- vllm_log_level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] = 'INFO',
131
- silent_mode: bool = False,
132
- env_variable_dict: Optional[Dict[str, str]] = None,
133
- vllm_serve_args: Optional[List[str]] = None,
134
- vllm_serve_kwargs: Optional[Dict[str, str]] = None,
135
- chat_template_kwargs: Optional[Dict[str, Any]] = None):
42
+ class VLLMServer(LanguageModel):
43
+ def __init__(
44
+ self,
45
+ model_path: str,
46
+ port: int,
47
+ gpus: int | list[int],
48
+ tokenizer_path: Optional[str] = None,
49
+ max_model_len: int = 16384,
50
+ max_lora_rank: Optional[int] = None,
51
+ host: str = '0.0.0.0',
52
+ mem_util: float = 0.85,
53
+ deploy_timeout_seconds: int = 600,
54
+ enforce_eager: bool = False,
55
+ vllm_log_level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] = 'INFO',
56
+ silent_mode: bool = False,
57
+ env_variable_dict: Optional[Dict[str, str]] = None,
58
+ vllm_serve_args: Optional[List[str]] = None,
59
+ vllm_serve_kwargs: Optional[Dict[str, str]] = None,
60
+ chat_template_kwargs: Optional[Dict[str, Any]] = None
61
+ ):
136
62
  """Deploy an LLM on specified GPUs.
137
63
  Args:
138
64
  model_path: Path to the model to deploy.
@@ -234,8 +160,8 @@ class VLLMServer:
234
160
  env['CUDA_VISIBLE_DEVICES'] = gpus
235
161
  env['VLLM_LOGGING_LEVEL'] = self._vllm_log_level
236
162
 
237
- # FIXME: These code are required for my machine :-(
238
- # FIXME: This may due to the bad NCCL environment configuration :-(
163
+ # FIXME: These code are required for my machine :(
164
+ # FIXME: This may due to the bad NCCL configuration :(
239
165
  if isinstance(self._gpus, list) and len(self._gpus) > 1:
240
166
  # set NCCL environment variable
241
167
  env['NCCL_P2P_DISABLE'] = '1'
@@ -352,24 +278,26 @@ class VLLMServer:
352
278
  print(f'[vLLM] Failed to load LoRA adapter. '
353
279
  f'Status code: {response.status_code}, Response: {response.text}')
354
280
  return True
355
- except requests.exceptions.RequestException as e:
281
+ except requests.exceptions.RequestException:
356
282
  continue
357
283
 
358
- print(f'[vLLM] Error loading LoRA adapter: {str(e)}')
284
+ print(f'[vLLM] Error loading LoRA adapter.')
359
285
  return False
360
286
 
361
287
  def close(self):
362
288
  """Shut down vLLM server and kill all vLLM processes."""
363
289
  self._kill_vllm_process()
364
290
 
365
- def chat_completion(self,
366
- message: str | List[openai.types.chat.ChatCompletionMessageParam],
367
- max_tokens: Optional[int] = None,
368
- timeout_seconds: Optional[int] = None,
369
- lora_name: Optional[str] = None,
370
- temperature: float = 0.9,
371
- top_p: float = 0.9,
372
- chat_template_kwargs: Optional[Dict[str, Any]] = None) -> str:
291
+ def chat_completion(
292
+ self,
293
+ message: str | List[openai.types.chat.ChatCompletionMessageParam],
294
+ max_tokens: Optional[int] = None,
295
+ timeout_seconds: Optional[int] = None,
296
+ lora_name: Optional[str] = None,
297
+ temperature: float = 0.9,
298
+ top_p: float = 0.9,
299
+ chat_template_kwargs: Optional[Dict[str, Any]] = None
300
+ ) -> str:
373
301
  """Send a chat completion query with OpenAI format to the vLLM server. Return the response content.
374
302
  Args:
375
303
  message: The message in str or openai format.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: py-adtools
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: Useful tools for parsing and evaluating Python programs for LLM-based algorithm design.
5
5
  Home-page: https://github.com/RayZhhh/py-adtools
6
6
  Author: Rui Zhang
@@ -13,6 +13,7 @@ Requires-Python: >=3.10
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE
15
15
  Requires-Dist: psutil
16
+ Requires-Dist: openai
16
17
  Dynamic: author
17
18
  Dynamic: author-email
18
19
  Dynamic: classifier
@@ -0,0 +1,13 @@
1
+ adtools/__init__.py,sha256=kbxntZFeCcURiIypNOdMWyeKPdlzRsWOB-K7z6HNCsc,150
2
+ adtools/evaluator.py,sha256=weA6zR1WyUE3f5pt7wQYF1ukmkA-e2kDLaogbDmG_Ig,9154
3
+ adtools/evaluator_pool.py,sha256=v_NZibN4VI3STVUZt6ARdyoB4Z061xAefZlH8lkWsjE,2972
4
+ adtools/py_code.py,sha256=FZfkp-IZ4zpOjrWe6svKNJsQhVANaTTkE0l0mc4aMW8,14277
5
+ adtools/lm/__init__.py,sha256=PZf5Lraly9rAWz-cxOSLCvZ9OZ4EI8aQEluetvNX8LM,146
6
+ adtools/lm/lm_base.py,sha256=KtO7KTrrMW7oWN-BhncoIOsbOVQsSc-0gNCYtvR6Sog,1105
7
+ adtools/lm/openai_api.py,sha256=LcfLkNOBrJTdsp0zcUjaCelIcQK5XknpHWrlB0S67_k,2390
8
+ adtools/lm/vllm_server.py,sha256=BPZoTS77wNJDcJ_0FO2QFyZTf6WR0isYKMuTctqKEU8,12942
9
+ py_adtools-0.1.7.dist-info/licenses/LICENSE,sha256=E5GGyecx3y5h2gcEGQloF-rDY9wbaef5IHjRsvtFbt8,1065
10
+ py_adtools-0.1.7.dist-info/METADATA,sha256=952zQgrdHcpBYGmnYqfwlVFfMubvvSYixg-weIf4eWU,6386
11
+ py_adtools-0.1.7.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
12
+ py_adtools-0.1.7.dist-info/top_level.txt,sha256=X2kKzmJFDAKR2FWCij5pfMG9pVVjVUomyl4e-1VLXIk,8
13
+ py_adtools-0.1.7.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- adtools/__init__.py,sha256=kbxntZFeCcURiIypNOdMWyeKPdlzRsWOB-K7z6HNCsc,150
2
- adtools/evaluator.py,sha256=weA6zR1WyUE3f5pt7wQYF1ukmkA-e2kDLaogbDmG_Ig,9154
3
- adtools/evaluator_pool.py,sha256=v_NZibN4VI3STVUZt6ARdyoB4Z061xAefZlH8lkWsjE,2972
4
- adtools/lm_base.py,sha256=fXltOlJpxy_b4ByX7nSIGXcMnH71W81tuzLsRtq_7JE,15709
5
- adtools/py_code.py,sha256=FZfkp-IZ4zpOjrWe6svKNJsQhVANaTTkE0l0mc4aMW8,14277
6
- py_adtools-0.1.5.dist-info/licenses/LICENSE,sha256=E5GGyecx3y5h2gcEGQloF-rDY9wbaef5IHjRsvtFbt8,1065
7
- py_adtools-0.1.5.dist-info/METADATA,sha256=iR6GXsszQb8aFq8SQiXFWU_dUTTJ83MyhtEUfDEK93g,6364
8
- py_adtools-0.1.5.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
9
- py_adtools-0.1.5.dist-info/top_level.txt,sha256=X2kKzmJFDAKR2FWCij5pfMG9pVVjVUomyl4e-1VLXIk,8
10
- py_adtools-0.1.5.dist-info/RECORD,,