hjxdl 0.2.42__py3-none-any.whl → 0.2.43__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.
hdl/_version.py CHANGED
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '0.2.42'
16
- __version_tuple__ = version_tuple = (0, 2, 42)
15
+ __version__ = version = '0.2.43'
16
+ __version_tuple__ = version_tuple = (0, 2, 43)
@@ -90,6 +90,22 @@ FN_DESC = {
90
90
  }
91
91
  }
92
92
 
93
+ """,
94
+ "wolfram_alpha_calculate": """
95
+ ## 函数名:wolfram_alpha_calculate
96
+ 描述:当用户要求计算一个数学表达式时,调用此工具,计算这个表达式,返回计算结果。
97
+ 触发条件:用户要求计算一个数学表达式
98
+ 参数:
99
+ # query (str): 用户要求的数学表达式
100
+ 返回值 (str): 计算结果
101
+ 需要返回的json
102
+ {
103
+ "function_name": "wolfram_alpha_calculate",
104
+ "params":
105
+ {
106
+ "query": <expression>
107
+ }
108
+ }
93
109
  """,
94
110
 
95
111
  "default": None
@@ -16,3 +16,13 @@ FN_TEMPLATE = """
16
16
  每个函数都有特定的参数和要求说明,确保仔细遵循每个功能的说明。根据最后用户的问题和函数的触发条件判断要执行的任务选择合适的一个函数。请严格遵守每个函数的触发条件,以说明中的JSON格式提供函数调用所需要的参数,其中参数的具体值从用户的提问中获取,并且不能带“<>”符号:
17
17
 
18
18
  """
19
+
20
+ COT_TEMPLATE = """
21
+ 你是一个专家级AI助手,有能针对一个问题分解成合适的步骤并逐步解释推理过程的能力。
22
+ 对于每一步,提供标题来描述您在该步骤中要做什么,以及内容。决定是否需要另一个步骤或是否准备好给出最终答案。
23
+ 以JSON格式回复,包括'title'、'tool','tool_param','content'和'next_action',
24
+ 其中'title'键的值为此步骤的任务,'content'键的值为此步骤的具体操作,而'next_station'键的值只能是'continue'或'final_answer'两种),
25
+ 若此步骤需要调用下文所述的工具,则'tool'键的值为工具名称,而'tool_param'键的值为工具参数,即把下文中工具描述中的返回 json 作为'tool_param'的值;
26
+ 若此步骤不需要调用下文所述的工具,则'tool'键的值为null,而'tool_param'键的值为null。
27
+ 你的回答中应只包含一段 json,且不包含其他多余文字。
28
+ """
@@ -5,16 +5,14 @@ import math
5
5
 
6
6
  def execute_code(code):
7
7
  """
8
- Executes the given Python code string in a subprocess.
8
+ Executes the given Python code string in a subprocess and returns the execution result.
9
9
 
10
- This function uses the subprocess to execute Python code strings, limiting the execution time to prevent infinite loops,
11
- and captures the execution output and errors for returning results.
12
-
13
- Parameters:
14
- code (str): The Python code string to be executed.
10
+ Args:
11
+ code (str): The Python code to be executed, provided as a string.
15
12
 
16
13
  Returns:
17
- str: The output result of executing the code or an error message.
14
+ str: The output result of the code execution. If there is an error during execution,
15
+ it returns an error message describing the error.
18
16
  """
19
17
  try:
20
18
  # Execute the code in a subprocess for safety
@@ -43,18 +41,16 @@ def execute_code(code):
43
41
 
44
42
  def calculate(expression):
45
43
  """
46
- Calculates the result of a mathematical expression.
44
+ Safely evaluates a mathematical expression.
47
45
 
48
- This function evaluates a mathematical expression string passed as an argument and returns the result.
49
- In order to prevent security risks, the use of built-in functions is restricted during evaluation,
50
- allowing only the 'math' module to be accessed. If an exception occurs during evaluation,
51
- it returns the corresponding error message.
46
+ This function receives a string containing a mathematical expression and evaluates it safely, allowing access only to the math module.
47
+ If an error occurs during evaluation, such as a syntax error or an undefined function, the function catches the exception and returns an error message.
52
48
 
53
- Parameters:
54
- expression (str): The mathematical expression to calculate.
49
+ Args:
50
+ expression (str): The mathematical expression to evaluate.
55
51
 
56
52
  Returns:
57
- The result of the calculation or an error message if an exception occurs.
53
+ The result of the expression evaluation or an error message if an exception occurs.
58
54
  """
59
55
  try:
60
56
  # Evaluate the expression with restricted built-in functions and access to the math module
hdl/utils/llm/chat.py CHANGED
@@ -6,7 +6,7 @@ import subprocess
6
6
 
7
7
 
8
8
  from openai import OpenAI
9
- from ..desc.template import FN_TEMPLATE
9
+ from ..desc.template import FN_TEMPLATE, COT_TEMPLATE
10
10
  from ..desc.func_desc import FN_DESC
11
11
  import json
12
12
  # import traceback
@@ -135,11 +135,15 @@ class OpenAI_M():
135
135
  *args,
136
136
  **kwargs
137
137
  )
138
- self.tools = tools
139
- self.tool_desc = FN_DESC
138
+ self.tools: list = tools
139
+ self.tool_desc: dict = FN_DESC
140
140
  if tool_desc is not None:
141
141
  self.tool_desc = self.tool_desc | tool_desc
142
142
 
143
+ self.tool_desc_str = "\n".join(
144
+ [self.tool_desc.get(tool.__name__, "") for tool in self.tools]
145
+ )
146
+
143
147
  def get_thought_chain(
144
148
  self,
145
149
  prompt,
@@ -164,7 +168,7 @@ class OpenAI_M():
164
168
 
165
169
  def get_resp(
166
170
  self,
167
- prompt : str,
171
+ prompt: str,
168
172
  sys_info: str = None,
169
173
  assis_info: str = None,
170
174
  images: list = None,
@@ -174,30 +178,38 @@ class OpenAI_M():
174
178
  stream: bool = True,
175
179
  **kwargs: t.Any,
176
180
  ):
177
- """Get response from chatbot based on the provided prompt and optional images.
178
-
179
- Args:
180
- prompt (str): The prompt to provide to the chatbot.
181
- images (list, optional): List of images to include in the response. Defaults to [].
182
- image_keys (tuple, optional): Tuple containing keys for image data. Defaults to ("image", "image").
183
- stop (list[str] | None, optional): List of strings that indicate the end of the conversation. Defaults to ["USER:", "ASSISTANT:"].
184
- model (str, optional): The model to use for generating the response. Defaults to "default_model".
185
- stream (bool, optional): Whether to stream the response. Defaults to True.
186
- **kwargs: Additional keyword arguments to pass to the chatbot API.
187
-
188
- Returns:
189
- dict: The response from the chatbot.
181
+ """
182
+ 获取响应函数。构造并发送一个聊天请求,以获取模型的响应。
183
+
184
+ 参数:
185
+ - prompt: 用户的输入文本。
186
+ - sys_info: 系统信息,用于提供给模型作为上下文。
187
+ - assis_info: 助手信息,用于在用户输入后提供给模型。
188
+ - images: 图像列表,用于提供给模型作为输入的一部分。
189
+ - image_keys: 图像键的元组,用于指定图像的类型和位置。
190
+ - stop: 停止序列,用于指示模型在生成文本时应停止的序列。
191
+ - model: 使用的模型名称。
192
+ - stream: 是否以流式传输方式接收响应。
193
+ - **kwargs: 其他传递给模型的参数。
194
+
195
+ 返回:
196
+ - response: 模型的响应。
190
197
  """
191
198
 
199
+ # 初始化内容列表,至少包含用户的文本输入
192
200
  content = [
193
201
  {"type": "text", "text": prompt},
194
202
  ]
203
+
204
+ # 根据image_keys的长度,调整其为三元组形式
195
205
  if isinstance(image_keys, str):
196
206
  image_keys = (image_keys,) * 3
197
207
  elif len(image_keys) == 2:
198
208
  image_keys = (image_keys[0],) + tuple(image_keys)
199
209
  elif len(image_keys) == 1:
200
210
  image_keys = (image_keys[0],) * 3
211
+
212
+ # 如果提供了图像,将其添加到内容列表中
201
213
  if images:
202
214
  if isinstance(images, str):
203
215
  images = [images]
@@ -209,30 +221,39 @@ class OpenAI_M():
209
221
  }
210
222
  })
211
223
  else:
224
+ # 如果没有提供图像,内容仅包含提示文本
212
225
  content = prompt
213
226
 
227
+ # 初始化消息列表,首先添加系统信息(如果提供)
214
228
  messages = []
215
229
  if sys_info:
216
230
  messages.append({
217
231
  "role": "system",
218
232
  "content": sys_info
219
233
  })
234
+
235
+ # 添加用户输入作为消息
220
236
  messages.append({
221
237
  "role": "user",
222
238
  "content": content
223
239
  })
240
+
241
+ # 如果提供了助手信息,将其添加到消息列表中
224
242
  if assis_info:
225
243
  messages.append({
226
244
  "role": "assistant",
227
245
  "content": assis_info
228
246
  })
229
247
 
248
+ # 调用模型生成响应
230
249
  response = self.client.chat.completions.create(
231
250
  messages=messages,
232
251
  stream=stream,
233
252
  model=model,
234
253
  **kwargs
235
254
  )
255
+
256
+ # 返回模型的响应
236
257
  return response
237
258
 
238
259
  def invoke(
@@ -343,10 +364,12 @@ class OpenAI_M():
343
364
  """
344
365
  fn_template = kwargs.pop("fn_template", FN_TEMPLATE)
345
366
  prompt_final = fn_template
346
- for tool in self.tools:
347
- prompt_final += self.tool_desc.get(tool.__name__, "")
367
+ # for tool in self.tools:
368
+ # prompt_final += self.tool_desc.get(tool.__name__, "")
348
369
  # prompt_final += f"\n用户的问题:\n{prompt}"
349
370
 
371
+ prompt_final += self.tool_desc_str
372
+
350
373
  decision_dict_str = self.invoke(
351
374
  prompt=prompt,
352
375
  sys_info=prompt_final,
hdl/utils/llm/chatgr.py CHANGED
@@ -5,16 +5,12 @@ from .chat import OpenAI_M
5
5
  # 定义流式输出的生成函数
6
6
  def chat_with_llm(user_input, chat_history=[]):
7
7
  """
8
- Facilitates a chat interaction with a language model (LLM).
9
- This function takes user input and maintains a chat history. It streams the response from the LLM and updates the chat history in real-time.
8
+ Generates a response from the LLM based on the given user input and chat history.
10
9
  Args:
11
- user_input (str): The input message from the user.
12
- chat_history (list, optional): A list of tuples representing the chat history. Each tuple contains two strings: the user's message and the bot's response. Defaults to an empty list.
10
+ user_input (str): The user input message.
11
+ chat_history (list): A list of tuples representing the chat history, where each tuple contains the user's message and the bot's response.
13
12
  Yields:
14
- tuple: A tuple containing three elements:
15
- - An empty string (for compatibility with certain frameworks).
16
- - The updated chat history including the latest user message and the bot's response.
17
- - The same updated chat history.
13
+ tuple: A tuple containing the bot's response, the updated chat history, and the original chat history.
18
14
  """
19
15
 
20
16
  chat_history.append(("User: " + user_input, "Bot: ")) # 初始先追加用户消息
hdl/utils/llm/visrag.py CHANGED
@@ -15,21 +15,38 @@ from .chat import OpenAI_M
15
15
  from .vis import pilimg_to_base64
16
16
 
17
17
  def get_image_md5(img: Image.Image):
18
+ """
19
+ 计算给定图像的MD5哈希值。
20
+
21
+ 该函数接收一个PIL.Image对象作为输入,将其转换为字节流,并计算该字节流的MD5哈希值。
22
+ 这主要用于在不保存图像的情况下,快速识别或验证图像的内容。
23
+
24
+ Args:
25
+ img (Image.Image): 输入的图像,为PIL.Image对象。
26
+
27
+ Returns:
28
+ str: 图像的MD5哈希值的十六进制表示字符串。
29
+ """
30
+ # 将图像转换为字节流,以便进行哈希计算
18
31
  img_byte_array = img.tobytes()
32
+
33
+ # 创建一个MD5哈希对象
19
34
  hash_md5 = hashlib.md5()
35
+
36
+ # 使用图像的字节流更新哈希对象
20
37
  hash_md5.update(img_byte_array)
38
+
39
+ # 获取哈希值的十六进制表示字符串
21
40
  hex_digest = hash_md5.hexdigest()
41
+
42
+ # 返回计算出的MD5哈希值
22
43
  return hex_digest
23
44
 
24
45
  def calculate_md5_from_binary(binary_data):
25
- """
26
- 计算给定二进制数据的MD5哈希值。
27
-
28
- 参数:
29
- binary_data (bytes): 需要计算MD5的二进制数据。
30
-
31
- 返回:
32
- str: 二进制数据的MD5哈希值的十六进制表示。
46
+ """ 计算二进制数据的MD5哈希值。
47
+ 参数:
48
+ binary_data (bytes): 二进制数据
49
+ 返回值:计算出的MD5哈希值的十六进制表示
33
50
  """
34
51
  # 初始化MD5哈希对象
35
52
  hash_md5 = hashlib.md5()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hjxdl
3
- Version: 0.2.42
3
+ Version: 0.2.43
4
4
  Summary: A collection of functions for Jupyter notebooks
5
5
  Home-page: https://github.com/huluxiaohuowa/hdl
6
6
  Author: Jianxing Hu
@@ -1,5 +1,5 @@
1
1
  hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
2
- hdl/_version.py,sha256=ErCkM0K6ix4_v_PL5flvrXq2XQXCxsWfoy3q3rTAsOY,413
2
+ hdl/_version.py,sha256=hhhc3c0qXE4Jw2R0jmE_VD8PeLjsTMffMOC9gufYSAA,413
3
3
  hdl/args/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  hdl/args/loss_args.py,sha256=s7YzSdd7IjD24rZvvOrxLLFqMZQb9YylxKeyelSdrTk,70
5
5
  hdl/controllers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -122,24 +122,24 @@ hdl/utils/database_tools/connect.py,sha256=xCacGucKxlQUXs6AsNddpeECvdqT1180V1ZWq
122
122
  hdl/utils/database_tools/datetime.py,sha256=xqE2xNiOpADzX-R8_bM0bioJRF3Ay9Jp1CAG6dy6uVI,1202
123
123
  hdl/utils/database_tools/web.py,sha256=PJ7R1chUD-vz-Up0orfFqdrZq3CFOlwiO9gIjFSb-R8,5224
124
124
  hdl/utils/desc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
125
- hdl/utils/desc/func_desc.py,sha256=tDGEuUuqg6LnBVpmWQIxYZPvrcb0N70A9i20PbujCks,2660
126
- hdl/utils/desc/template.py,sha256=a0UAkkKctt_EHY9UECsIIAwVkGPcM1Hr01HSkRMeIuw,1272
125
+ hdl/utils/desc/func_desc.py,sha256=m5MfrZkI6IbYfyu0zD9JM_S_Q98CdG661nLybaRXhN0,3141
126
+ hdl/utils/desc/template.py,sha256=09wnkMgg4Kxtl7zbsvQe61DilRDRUb-In5Qbi3809DY,2216
127
127
  hdl/utils/general/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
128
128
  hdl/utils/general/glob.py,sha256=8-RCnt6L297wMIfn34ZAMCsGCZUjHG3MGglGZI1cX0g,491
129
- hdl/utils/general/runners.py,sha256=w9kgR9UgGug0s6NR27hpfPQbbFvRtmtqz5IfwyMTC1E,2351
129
+ hdl/utils/general/runners.py,sha256=aWvEXYeuIF_F0AwucHIrrOOeeTM89goEFwW1rM35x1U,2231
130
130
  hdl/utils/llm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
131
- hdl/utils/llm/chat.py,sha256=tSIg5FcMutcsmEnjOJI-ZP0OkQeB8csuUu2cwsapsAg,15658
132
- hdl/utils/llm/chatgr.py,sha256=lKaDeimYz-Bw32QXYtde51xjf7gbfS2CEYt5R0ODunM,4075
131
+ hdl/utils/llm/chat.py,sha256=GyHyQpFx5PbV9tjo3nJYhfB3tpx9Jl8m7IF4neQdLfQ,16313
132
+ hdl/utils/llm/chatgr.py,sha256=A9SCWAvQulgO2x0ArIqcmgd_iVWVbSi1Oj15Diwb3Ok,3743
133
133
  hdl/utils/llm/embs.py,sha256=Tf0FOYrOFZp7qQpEPiSCXzlgyHH0X9HVTUtsup74a9E,7174
134
134
  hdl/utils/llm/extract.py,sha256=2sK_WJzmYIc8iuWaM9DA6Nw3_6q1O4lJ5pKpcZo-bBA,6512
135
135
  hdl/utils/llm/llama_chat.py,sha256=watcHGOaz-bv3x-yDucYlGk5f8FiqfFhwWogrl334fk,4387
136
136
  hdl/utils/llm/vis.py,sha256=BsGAfy5X8sMFnX5A3vHpTPDRe_-IDdhs6YVQ-efvyQ0,21424
137
- hdl/utils/llm/visrag.py,sha256=ZH7Z63taj3hgGAVvDyrMIA_V0tqconCobcfYAAElYmU,9552
137
+ hdl/utils/llm/visrag.py,sha256=0i-VrxqgiV-J7R3VPshu9oc7-rKjFJOldYik3HDXj6M,10176
138
138
  hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
139
139
  hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
140
140
  hdl/utils/weather/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
141
141
  hdl/utils/weather/weather.py,sha256=k11o6wM15kF8b9NMlEfrg68ak-SfSYLN3nOOflFUv-I,4381
142
- hjxdl-0.2.42.dist-info/METADATA,sha256=6uQ_KhJW8cYaF0ufn0n4RpyZeYby0vKwTzvQ15JCIas,836
143
- hjxdl-0.2.42.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
144
- hjxdl-0.2.42.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
145
- hjxdl-0.2.42.dist-info/RECORD,,
142
+ hjxdl-0.2.43.dist-info/METADATA,sha256=CSRvUF_XdF7bXnpTnJYoml1WbsJwl5Txmfo5c51waBg,836
143
+ hjxdl-0.2.43.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
144
+ hjxdl-0.2.43.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
145
+ hjxdl-0.2.43.dist-info/RECORD,,
File without changes