现在测试报错,源码是按照视频写的,
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel,Field
from langchain_core.tools import tool
import os
model=ChatOpenAI(model=“moonshot-v1-8k”,
api_key=“sk-xp66oqrt7Ow5daBH68XqDKNlrf2F8IBzPnkFhnwLD7Ls0fil”,
base_url=“https//api.moonshot.cn/v1”,
temperature=0)
class mutiply(BaseModel):
""“Return product of two numbers”""
a:float=Field(…,description=“First number”)
b:float=Field(…,description=“Second number”)
@tool
def exponentiate(a:float,b:float)->float:
return a**b
def subtract(a:float,b:float)->float:
return a-b
add={
“name”:“add”,
“description”:“Add ‘x’ and ‘y’”,
“parameters”:{
“type”:“object”,
“properties”:{
“x”:{
“type”:“number”,
“description”:“First number to add”
},
“y”:{
“type”:“number”,
“description”:“Sencond number to add”
},
},
“required”:[“x”,“y”]
}
}
llm_with_tools = model.bind_tools([mutiply,exponentiate,add,subtract])
res = llm_with_tools.invoke(“What is 2 to the power of 3?”)
print(res)
大致差不多,运行后报错
ValueError Traceback (most recent call last)
Cell In[5], line 16
13 a:float=Field(…,description=“First number”)
14 b:float=Field(…,description=“Second number”)
—> 16 @tool
17 def exponentiate(a:float,b:float)->float:
18 return a**b
20 def subtract(a:float,b:float)->float:
File c:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\langchain_core\tools\convert.py:304, in tool(name_or_callable, runnable, return_direct, args_schema, infer_schema, response_format, parse_docstring, error_on_invalid_docstring, *args)
298 elif name_or_callable is not None:
299 if callable(name_or_callable) and hasattr(name_or_callable, “name”):
300 # Used as a decorator without parameters
301 # @tool
302 # def my_tool():
303 # pass
–> 304 return create_tool_factory(name_or_callable.name)(name_or_callable)
305 elif isinstance(name_or_callable, str):
306 # Used with a new name for the tool
307 # @tool(“search”)
(…)
314 # def my_tool():
315 # pass
316 return create_tool_factory(name_or_callable)
…
191 if description is None:
192 # Only apply if using the function’s docstring
193 description = textwrap.dedent(description).strip()
ValueError: Function must have a docstring if description not provided.
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings…
我对python不熟悉,不知道哪里错误了