Skip to content

Commit

Permalink
update readme & add general exec function
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Jun 17, 2024
1 parent a55ed64 commit 22f1557
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 9 deletions.
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 协议开源。
Expand Down
58 changes: 58 additions & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion chattool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = """Rex Wang"""
__email__ = '[email protected]'
__version__ = '3.2.1'
__version__ = '3.3.0'

import os, sys, requests, json
from .chattype import Chat, Resp
Expand Down
26 changes: 25 additions & 1 deletion chattool/functioncall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 22f1557

Please sign in to comment.