from openai import OpenAI
from func_timeout import func_timeout
math_methods = {k: v for k, v in math.__dict__.items()
if not k.startswith('_')}
'math': addict.Addict(math_methods),
allowed_methods['__builtins__'] = None
return eval(expr, allowed_methods, allowed_methods)
def calculator(expression: str):
res = func_timeout(timeout=5, func=_safe_eval, args=[expression])
return f'The result of {expression} is {res}'
"description": ('A calculator tool. The input must be a single Python '
'expression and you cannot import packages. You can use functions '
'in the `math` package without import.'),
"description": "The expression for calculator",
"required": ["expression"],
client = OpenAI(api_key='YOUR_API_KEY', base_url='http://0.0.0.0:23333/v1')
model_name = client.models.list().data[0].id
messages = [{'role': 'user', 'content': '使用计算器,13.8 和 13.11 谁更大?'}]
response = client.chat.completions.create(
func1_name = response.choices[0].message.tool_calls[0].function.name
func1_args = response.choices[0].message.tool_calls[0].function.arguments
func1_out = eval(f'{func1_name}(**{func1_args})')
{'role': 'assistant', 'content': response.choices[0].message.content})
{'role': 'environment', 'content': func1_out, 'name': 'plugin'})
response = client.chat.completions.create(
{'role': 'assistant', 'content': response.choices[0].message.content})
for message in messages:
print(f'{message["role"]}: {message["content"]}\n')