Langchain-Chatchat icon indicating copy to clipboard operation
Langchain-Chatchat copied to clipboard

使用模型chatglm3-6b,如何在langchain-chatchat交互中调用到chatglm3-6b自定义的function,请提供实例

Open Andy1018 opened this issue 11 months ago • 1 comments

使用模型chatglm3-6b,如何在langchain-chatchat交互中调用到chatglm3-6b自定义的function,请提供实例

Andy1018 avatar Mar 08 '24 08:03 Andy1018

嘿,@Andy1018!很高兴再次见到你。希望一切都好!

根据您的需求,我在Langchain-Chatchat的代码库中找到了一些相关的信息。如果您想在ChatGLM3-6B模型中调用自定义函数,可以通过创建一个继承自BaseTool的自定义工具类来实现。以下是一个示例:

from langchain.tools.base import BaseTool
from langchain.schema.language_model import BaseLanguageModel

# 假设我们有一个自定义函数,我们想要在ChatGLM3-6B模型中调用它
def custom_function(param1, param2):
    # 这里是自定义函数的实现
    return f"处理了参数 {param1} 和 {param2}"

# 创建一个继承自BaseTool的自定义工具类
class CustomTool(BaseTool):
    name = "custom_tool"
    description = "这是一个自定义工具,用于演示如何在ChatGLM3-6B模型中调用自定义函数。"
    
    def run(self, param1, param2):
        # 调用自定义函数
        return custom_function(param1, param2)

# 假设我们已经有了一个BaseLanguageModel的实例
llm = BaseLanguageModel()

# 创建自定义工具的实例
custom_tool = CustomTool()

# 使用initialize_glm3_agent函数初始化ChatGLM3-6B模型的代理,将自定义工具作为参数传递
agent_executor = initialize_glm3_agent(
    tools=[custom_tool],  # 将自定义工具作为列表传递
    llm=llm,
    prompt="这里是你的提示信息"
)

# 现在,ChatGLM3-6B模型的代理已经可以使用自定义工具了
# 在交互过程中,可以通过特定的格式调用自定义工具,例如:"tool_call(custom_tool, param1='value1', param2='value2')"

这个示例中的initialize_glm3_agent函数的结构和参数如下:

def initialize_glm3_agent(
        tools: Sequence[BaseTool],
        llm: BaseLanguageModel,
        prompt: str = None,
        memory: Optional[ConversationBufferWindowMemory] = None,
        agent_kwargs: Optional[dict] = None,
        *,
        tags: Optional[Sequence[str]] = None,
        **kwargs: Any,
) -> AgentExecutor:

这些信息来自于server/agent/custom_agent/ChatGLM3Agent.py

请注意,这些都是基于Langchain-Chatchat代码库中的信息提供的可能的解决方案,实际的实现可能会有所不同。根据您的具体情况,可能需要进行一些调整。

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

dosubot[bot] avatar Mar 08 '24 08:03 dosubot[bot]

这个问题已经被标记为 stale ,因为它已经超过 30 天没有任何活动。

github-actions[bot] avatar Apr 07 '24 21:04 github-actions[bot]