diff --git a/README.md b/README.md index c7c112b..818e7d5 100644 --- a/README.md +++ b/README.md @@ -113,10 +113,11 @@ chats = load_chats("async_chat.jsonl") await async_chat_completion(langs, chkpoint="async_chat.jsonl", nproc=2, data2chat=data2chat, wait=True) ``` -示例4,使用工具(自定义函数): +### 工具调用 + +定义函数: ```python -# 定义函数 def add(a: int, b: int) -> int: """ This function adds two numbers. @@ -142,12 +143,33 @@ def mult(a:int, b:int) -> int: int: The product of the two numbers. """ return a * b -# 传输函数 -chat = Chat("find the value of (23723 * 1322312 ) + 12312") # 传入函数列表,可以是多个函数 -# 自动调用工具,默认使用 tool_choice -chat.autoresponse(display=True, tool_choice='tool_choice') # 或者用 function_call ``` +添加函数到 `Chat` 对象: + +```py +from chattool import Chat +chat = Chat("find the value of (23723 * 1322312 ) + 12312") +chat.settools([add, mult]) +``` + +自动执行工具,根据返回信息判断是否结束,`maxturns` 默认为 3: + +```py +chat.autoresponse(display=True, tool_type='tool_choice', maxturns=3) +``` + +使用通用函数 `python` + +```py +from chattool.functioncall import python +chat = Chat("find the value of (23723 * 1322312 ) + 12312") +chat.settools([python]) +chat.autoresponse(display=True, tool_type='tool_choice', maxturns=3) +``` + +注意,执行模型生成的任意代码有潜在风险。 + ## 开源协议 使用 MIT 协议开源。 diff --git a/README_en.md b/README_en.md index 824115c..68b5e8d 100644 --- a/README_en.md +++ b/README_en.md @@ -118,6 +118,64 @@ when using `async_chat_completion` in Jupyter notebook, you should use the `awai await async_chat_completion(langs, chkpoint="async_chat.jsonl", nproc=2, data2chat=data2chat, wait=True) ``` + +### Tool Call + +Define functions: + +```python +def add(a: int, b: int) -> int: + """ + This function adds two numbers. + + Parameters: + a (int): The first number. + b (int): The second number. + + Returns: + int: The sum of the two numbers. + """ + return a + b + +def mult(a: int, b: int) -> int: + """This function multiplies two numbers. + It is a useful calculator! + + Args: + a (int): The first number. + b (int): The second number. + + Returns: + int: The product of the two numbers. + """ + return a * b +``` + +Add functions to the `Chat` object: + +```python +from chattool import Chat +chat = Chat("find the value of (23723 * 1322312 ) + 12312") +chat.settools([add, mult]) +``` + +Automatically execute the tool based on the return information. The default value for `maxturns` is 3: + +```python +chat.autoresponse(display=True, tool_type='tool_choice', maxturns=3) +``` + +Use the general function `python`: + +```python +from chattool.functioncall import python +chat = Chat("find the value of (23723 * 1322312 ) + 12312") +chat.settools([python]) +chat.autoresponse(display=True, tool_type='tool_choice', maxturns=3) +``` + +Note that executing any code generated by the model has potential risks. + ## License This package is licensed under the MIT license. See the LICENSE file for more details. diff --git a/chattool/__init__.py b/chattool/__init__.py index bb784f0..0b89eee 100644 --- a/chattool/__init__.py +++ b/chattool/__init__.py @@ -2,7 +2,7 @@ __author__ = """Rex Wang""" __email__ = '1073853456@qq.com' -__version__ = '3.2.1' +__version__ = '3.3.0' import os, sys, requests, json from .chattype import Chat, Resp diff --git a/chattool/functioncall.py b/chattool/functioncall.py index 309a1e1..80573c8 100644 --- a/chattool/functioncall.py +++ b/chattool/functioncall.py @@ -92,4 +92,28 @@ def exec_python_code(code:str)->dict: newspace[key] = str(val) return newspace except Exception as e: - return {'error': str(e)} \ No newline at end of file + return {'error': str(e)} + +def python(code: str): + """Execute the code and return the result or error message + + Args: + code (str): code to execute + + Returns: + result or error message + """ + local_vars = {} + # Split the code into lines + lines = code.strip().split('\n') + + try: + # Execute all lines except the last one + exec('\n'.join(lines[:-1]), {}, local_vars) + + # Evaluate the last expression + result = eval(lines[-1], {}, local_vars) + + return result + except Exception as e: + return str(e) \ No newline at end of file diff --git a/setup.py b/setup.py index 170c0b3..e316e18 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open('README.md') as readme_file: readme = readme_file.read() -VERSION = '3.2.1' +VERSION = '3.3.0' requirements = [ 'Click>=7.0', 'requests>=2.20', "responses>=0.23", 'aiohttp>=3.8',