mirror of
https://github.com/RYDE-WORK/Langchain-Chatchat.git
synced 2026-02-07 15:38:27 +08:00
重写 calculate 工具
This commit is contained in:
parent
1dc069fa9c
commit
e06d56b8bc
@ -1,23 +1,15 @@
|
|||||||
from server.pydantic_types import BaseModel, Field
|
from langchain.agents import tool
|
||||||
|
|
||||||
def calculate(a: float, b: float, operator: str) -> float:
|
|
||||||
if operator == "+":
|
|
||||||
return a + b
|
|
||||||
elif operator == "-":
|
|
||||||
return a - b
|
|
||||||
elif operator == "*":
|
|
||||||
return a * b
|
|
||||||
elif operator == "/":
|
|
||||||
if b != 0:
|
|
||||||
return a / b
|
|
||||||
else:
|
|
||||||
return float('inf')
|
|
||||||
elif operator == "^":
|
|
||||||
return a ** b
|
|
||||||
else:
|
|
||||||
raise ValueError("Unsupported operator")
|
|
||||||
|
|
||||||
class CalculatorInput(BaseModel):
|
@tool
|
||||||
a: float = Field(description="first number")
|
def calculate(text: str) -> float:
|
||||||
b: float = Field(description="second number")
|
'''
|
||||||
operator: str = Field(description="operator to use (e.g., +, -, *, /, ^)")
|
Useful to answer questions about simple calculations.
|
||||||
|
translate user question to a math expression that can be evaluated by numexpr.
|
||||||
|
'''
|
||||||
|
import numexpr
|
||||||
|
|
||||||
|
try:
|
||||||
|
return str(numexpr.evaluate(text))
|
||||||
|
except Exception as e:
|
||||||
|
return f"wrong: {e}"
|
||||||
|
|||||||
@ -7,12 +7,7 @@ KB_info_str = '\n'.join([f"{key}: {value}" for key, value in KB_INFO.items()])
|
|||||||
template_knowledge = template.format(KB_info=KB_info_str, key="samples")
|
template_knowledge = template.format(KB_info=KB_info_str, key="samples")
|
||||||
|
|
||||||
all_tools = [
|
all_tools = [
|
||||||
StructuredTool.from_function(
|
calculate,
|
||||||
func=calculate,
|
|
||||||
name="calculate",
|
|
||||||
description="Useful for when you need to answer questions about simple calculations",
|
|
||||||
args_schema=CalculatorInput,
|
|
||||||
),
|
|
||||||
StructuredTool.from_function(
|
StructuredTool.from_function(
|
||||||
func=arxiv,
|
func=arxiv,
|
||||||
name="arxiv",
|
name="arxiv",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user