我如何将我的所需参数传递给langchain工具模块?

huangapple go评论47阅读模式
英文:

How can I pass my desired parameters to langchain tools module?

问题

"工具模块中的一些工具需要传递参数才能使用,但默认传递的参数并不是函数所需的。例如,如果我传递 {"name": "name", "dt": 2020, "id": 123},但只有 name 被传递给工具函数的 action_input。"

"我想要将我的整个输入内容传递给函数,这样我就可以自己解析它。也许可以使用 args schema?"

英文:

"Some of the tools in the tools module require parameters to be passed to use them, but the default parameters passed are not what the function needs. For example, if I pass in {"name": "name", "dt": 2020, "id": 123}, but only name is passed to the action_input of the tool function."

I want to pass the entire contents of my input to the function, so that I can parse it myself.
maybe use args schema?

答案1

得分: 2

我已找到将函数放入一个类中的解决方案,其中参数在调用类时传递,函数直接访问这些参数。

class A:
    def __init__(self, args):
        self.args = args
    
    def b(self, query):
        return self.args
英文:

I have found a solution to place the function inside a class, where the parameters are passed when calling the class, and the function directly accesses these parameters

class A:
    def __init__(self,args):
        self.args = args
    def b(query):
        return self.args

答案2

得分: 0

基于对原始问题的评论(“您是否想找出如何将多个参数传递给工具(即API函数)?”),以下可能是解决方案:

def set_x_y(string):
    x_val, y_val = string.split(",")
    x_val = int(x_val.strip())
    y_val = int(y_val.strip())
    self.x = x_val
    self.y = y_val

tools = [
    Tool(
        name="SetXY",
        func=set_x_y,
        description="设置X和Y的值。该工具的输入应为以逗号分隔的字符串列表,每个字符串长度为两个字符。第一个字符是X的值,第二个字符是Y的值。例如,`3,4`将是输入,如果您想将X的值设置为3和Y的值设置为4"
    ),
]
英文:

Based on the comment to the original question ("Do you want to find out how multiple parameters can be passed to a tool (i.e. API function?)"), following may be solution:

def set_x_y(string):
    x_val, y_val = string.split(",")
    x_val = int(x_val.strip())
    y_val = int(y_val.strip())
    self.x = x_val
    self.y = y_val


tools = [
    Tool(
        name="SetXY",
        func=set_x_y,
        description="Sets the value for X and Y. The input to this tool should be a comma separated list of "\
                    "strings of length two. The first one is the value of X and the second one is the value of Y. "\
                    "For example, `3,4` would be the input if you want to set value of X to 3 and value of Y to 4"
    ),
]

huangapple
  • 本文由 发表于 2023年6月1日 17:14:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定