llama3-groq-tool-use:70b 在 ollama 0.2.7 中不会调用工具,

jc3wubiy  于 2个月前  发布在  其他
关注(0)|答案(4)|浏览(39)

问题是什么?

大家好!
我正在尝试使用requests库中的工具访问llama3-groq-tool-use:70b。以下是使用langchain==0.2.9的简单Python代码:

from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain.prompts import (
    ChatPromptTemplate,
    MessagesPlaceholder,
)
from langchain.agents import AgentExecutor, create_openai_tools_agent

@tool
def function_1(a: int, b: int) -> int:
    """uses function function_1 for arguments a and b."""
    return a % b + 2

@tool
def function_2(a: int, b: int) -> int:
    """uses function function_2 for arguments a and b."""
    return a * b + 1

tools = [function_1, function_2]

llm = ChatOpenAI(
    model="llama3-groq-tool-use:70b",
    temperature=0,
)

default_prompt = ChatPromptTemplate.from_messages(
        [
            ("system", "You are a helpful AI assistant."),
            MessagesPlaceholder("chat_history", optional=True),
            ("human", "{input}"),
            MessagesPlaceholder("agent_scratchpad"),
        ]
    )

input_message = "What is function_1(10, 11)? Also what is function_2(10, 11)?"

agent = create_openai_tools_agent(
    llm=llm,
    tools=tools,
    prompt=default_prompt
)

agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    verbose=True,
    return_intermediate_steps=False,
)

res = agent_executor.invoke({"input": input_message})

print(res)

结果如下:

> Entering new AgentExecutor chain...
<tool_call>
{"id": 0, "name": "function_1", "arguments": {"a": 10, "b": 11}}
</tool_call>
<tool_call>
{"id": 1, "name": "function_2", "arguments": {"a": 10, "b": 11}}
</tool_call>

> Finished chain.
{
    'input': 'What is function_1(10, 11)? Also what is function_2(10, 11)?',
    'output': '<tool_call>\n{"id": 0, "name": "function_1", "arguments": {"a": 10, "b": 11}}\n</tool_call>\n<tool_call>\n{"id": 1, "name": "function_2", "arguments": {"a": 10, "b": 11}}\n</tool_call>'
}

如果我使用langchain_community.chat_models.ollama.ChatOllama,输出也是一样的。
但是,如果我使用相同的模型(llama3-groq-70b-8192-tool-use-preview)并调用groq OpenAI兼容API,它会使用工具并调用函数,输出如下:

> Entering new AgentExecutor chain...

Invoking: `function_1` with `{'a': 10, 'b': 11}`

12
Invoking: `function_2` with `{'a': 10, 'b': 11}`

111The result of function_1(10, 11) is 12, and the result of function_2(10, 11) is 111.

> Finished chain.
{
    'input': 'What is function_1(10, 11)? Also what is function_2(10, 11)?',
    'output': 'The result of function_1(10, 11) is 12, and the result of function_2(10, 11) is 111.'
}

这是预期的行为还是这个问题仍在进行中?
非常感谢

操作系统

Linux

GPU

Nvidia

CPU

Intel

Ollama版本

0.2.7

mfuanj7w

mfuanj7w1#

专用工具处理是最近添加到ollama的,所以可能需要一些调整。查看您的日志,似乎ollama返回的内容与langchain所期望的不符,因此需要在双方的代码中进行一些挖掘以使它们匹配。

avkwfej4

avkwfej42#

我发现Ollama发送的是"stop",而不是"finish_reason": "tool_calls",就像我测试过的groq api一样。
我当时正在使用elixir的langchain。
删除Pattern匹配中的"finish_reason" => "tool_calls"后,问题得到了解决。
Ollama OpenAi api也应该以"tool_calls"作为结束原因来回答。

busg9geu

busg9geu3#

好的!希望这个问题能尽快解决。

uubf1zoe

uubf1zoe4#

很好!希望这个问题能尽快解决。
我也是!

相关问题