langchain-ai / langgraph

Build resilient language agents as graphs.
https://langchain-ai.github.io/langgraph/
MIT License
5.49k stars 869 forks source link

Use the firefunction-v2 model of ChatFireworks for multi-agent, and encountered an error: fireworks.client.error.InternalServerError: {'error': {'object': 'error', 'type': 'internal_server_error', 'message': 'server had an error while processing your request, please retry again after a brief wait'}} #1002

Closed SENVENHUHU closed 1 month ago

SENVENHUHU commented 1 month ago

Checked other resources

Example Code

import json
from langchain_core.messages import (
    AIMessage,
    BaseMessage,
    ChatMessage,
    FunctionMessage,
    HumanMessage,
)
from langchain.tools.render import format_tool_to_openai_function
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langgraph.graph import END, StateGraph
from langgraph.prebuilt.tool_executor import ToolExecutor, ToolInvocation
from langchain_core.utils.function_calling import convert_to_openai_function
from langchain_core.tools import tool
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_experimental.utilities import PythonREPL
from typing import Annotated
from typing import Dict
import subprocess

import os
from langchain_fireworks import ChatFireworks
# 设置和初始化
llm = ChatFireworks(
    api_key="UAbZbw6jUOa0PCKm6IXebmLlrpDUfqoMkBLr3hKMnV7aTACm",
    model="accounts/fireworks/models/firefunction-v2",
)

# nmap工具
@tool

def get_mojuan_price(name: str):
    """
    To get the price of the Morimaki brand burger.
    """
    return 66

# 示例调用

# 将工具导入一个列表中
tools = [get_mojuan_price]

# 创建状态
import operator
from typing import Annotated, List, Sequence, Tuple, TypedDict, Union

from langchain.agents import create_openai_functions_agent
from langchain.tools.render import format_tool_to_openai_function
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI
from typing_extensions import TypedDict

# 为每个代理和工具创建不同的节点
class AgentState(TypedDict):
    messages: Annotated[Sequence[BaseMessage], operator.add]
    sender: str

tool_executor = ToolExecutor(tools)

def tool_node(state):
    """This runs tools in the graph. It takes in an agent action and calls that tool and returns the result."""
    messages = state["messages"]
    last_message = messages[-1]
    tool_call = last_message.additional_kwargs["tool_calls"][0]
    tool_name = tool_call["function"]["name"]
    tool_input = json.loads(tool_call["function"]["arguments"])
    action = ToolInvocation(
        tool=tool_name,
        tool_input=tool_input,
    )
    response = tool_executor.invoke(action)
    function_message = FunctionMessage(
        content=f"{tool_name} response: {str(response)}", name=action.tool
    )
    return {"messages": [function_message]}

def router(state):
    messages = state["messages"]
    last_message = messages[-1]
    if "tool_calls" in last_message.additional_kwargs:
        if "function" in last_message.additional_kwargs["tool_calls"][0]:
            return "call_tool"
    if "FINAL ANSWER" in last_message.content:
        return "end"
    return "continue"

def create_agent(llm, tools, system_message: str):
    """Create an agent."""
    functions = [convert_to_openai_function(t) for t in tools]
    prompt = ChatPromptTemplate.from_messages(
        [
            (
                "system",
                "You are a helpful AI assistant, collaborating with other assistants."
            ),
            MessagesPlaceholder(variable_name="messages"),
        ]
    )
    prompt = prompt.partial(system_message=system_message)
    prompt = prompt.partial(tool_names=", ".join([tool.name for tool in tools]))
    return prompt | llm.bind_functions(functions)

def agent_node(state, agent, name):
    #state["messages"] = convert_tool_calls_to_function_calls(state["messages"])
    #print(f"这是agent_node:{state["messages"]}")

    result = agent.invoke(state)
    if isinstance(result, FunctionMessage):
        pass
    else:
        result = HumanMessage(**result.dict(exclude={"type", "name"}), name=name)
    return {
        "messages": [result],
        "sender": name,
    }

research_agent = create_agent(
    llm,
    [],
    system_message="You should provide the corresponding precise attack command to use for the attack",
)

nmap_agent = create_agent(
    llm,
    [get_mojuan_price],
    system_message="Users can see any results you scan.",
)

import functools

research_node = functools.partial(agent_node, agent=research_agent, name="Researcher")
chart_node = functools.partial(agent_node, agent=nmap_agent, name="Scan_Generator")

workflow = StateGraph(AgentState)

workflow.add_node("Researcher", research_node)
workflow.add_node("Scan_Generator", chart_node)
workflow.add_node("call_tool", tool_node)

workflow.add_conditional_edges(
    "Researcher",
    router,
    {"continue": "Scan_Generator", "call_tool": "call_tool", "end": END},
)
workflow.add_conditional_edges(
    "Scan_Generator",
    router,
    {"continue": "Researcher", "call_tool": "call_tool", "end": END},
)
workflow.add_conditional_edges(
    "call_tool",
    lambda x: x["sender"],
    {
        "Researcher": "Researcher",
        "Scan_Generator": "Scan_Generator",
    },
)

workflow.set_entry_point("Researcher")
graph = workflow.compile()

for s in graph.stream(
    {
        "messages": [
            HumanMessage(
                content="How much is the Morimaki Beef Burger? "
            )
        ],
    },
    {"recursion_limit": 50},
):
    print(s)
    print("----")

Error Message and Stack Trace (if applicable)

{'Researcher': {'messages': [HumanMessage(content='I need to know the current menu prices to answer this question. Can you please provide the current menu prices?', response_metadata={'token_usage': {'prompt_tokens': 280, 'total_tokens': 303, 'completion_tokens': 23}, 'model_name': 'accounts/fireworks/models/firefunction-v2', 'system_fingerprint': '', 'finish_reason': 'stop', 'logprobs': None}, name='Researcher', id='run-b4df2be4-131e-43eb-bf6a-ee53981f8501-0', tool_calls=[], usage_metadata={'input_tokens': 280, 'output_tokens': 23, 'total_tokens': 303}, invalid_tool_calls=[])], 'sender': 'Researcher'}}
----
{'Scan_Generator': {'messages': [HumanMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_w3VH2fJG1Q9LoZYLcr92VicZ', 'type': 'function', 'function': {'name': 'get_mojuan_price', 'arguments': '{"name": "Morimaki Beef Burger"}'}}]}, response_metadata={'token_usage': {'prompt_tokens': 385, 'total_tokens': 411, 'completion_tokens': 26}, 'model_name': 'accounts/fireworks/models/firefunction-v2', 'system_fingerprint': '', 'finish_reason': 'tool_calls', 'logprobs': None}, name='Scan_Generator', id='run-72e5902a-49ec-422d-86e5-855e7a14678a-0', tool_calls=[{'name': 'get_mojuan_price', 'args': {'name': 'Morimaki Beef Burger'}, 'id': 'call_w3VH2fJG1Q9LoZYLcr92VicZ'}], usage_metadata={'input_tokens': 385, 'output_tokens': 26, 'total_tokens': 411}, invalid_tool_calls=[])], 'sender': 'Scan_Generator'}}
----
{'call_tool': {'messages': [FunctionMessage(content='get_mojuan_price response: 66', name='get_mojuan_price')]}}
----
Traceback (most recent call last):
  File "E:\python-test\VulnMapAI-main\test6.py", line 166, in <module>
    for s in graph.stream(
  File "D:\python310\lib\site-packages\langgraph\pregel\__init__.py", line 1073, in stream
    _panic_or_proceed(done, inflight, step)
  File "D:\python310\lib\site-packages\langgraph\pregel\__init__.py", line 1643, in _panic_or_proceed
    raise exc
  File "D:\python310\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "D:\python310\lib\site-packages\langgraph\pregel\retry.py", line 72, in run_with_retry
    task.proc.invoke(task.input, task.config)
  File "D:\python310\lib\site-packages\langchain_core\runnables\base.py", line 2505, in invoke
    input = step.invoke(input, config, **kwargs)
  File "D:\python310\lib\site-packages\langgraph\utils.py", line 95, in invoke
    ret = context.run(self.func, input, **kwargs)
  File "E:\python-test\VulnMapAI-main\test6.py", line 111, in agent_node
    result = agent.invoke(state)
  File "D:\python310\lib\site-packages\langchain_core\runnables\base.py", line 2507, in invoke
    input = step.invoke(input, config)
  File "D:\python310\lib\site-packages\langchain_core\runnables\base.py", line 4588, in invoke
    return self.bound.invoke(
  File "D:\python310\lib\site-packages\langchain_core\language_models\chat_models.py", line 248, in invoke
    self.generate_prompt(
  File "D:\python310\lib\site-packages\langchain_core\language_models\chat_models.py", line 677, in generate_prompt
    return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)
  File "D:\python310\lib\site-packages\langchain_core\language_models\chat_models.py", line 534, in generate
    raise e
  File "D:\python310\lib\site-packages\langchain_core\language_models\chat_models.py", line 524, in generate
    self._generate_with_cache(
  File "D:\python310\lib\site-packages\langchain_core\language_models\chat_models.py", line 749, in _generate_with_cache
    result = self._generate(
  File "D:\python310\lib\site-packages\langchain_fireworks\chat_models.py", line 477, in _generate
    response = self.client.create(messages=message_dicts, **params)
  File "D:\python310\lib\site-packages\fireworks\client\chat_completion.py", line 40, in create
    return super().create(
  File "D:\python310\lib\site-packages\fireworks\client\base_completion.py", line 76, in create
    return cls._create_non_streaming(
  File "D:\python310\lib\site-packages\fireworks\client\base_completion.py", line 158, in _create_non_streaming
    response = client.post_request_non_streaming(
  File "D:\python310\lib\site-packages\fireworks\client\api_client.py", line 131, in post_request_non_streaming
    self._error_handling(response)
  File "D:\python310\lib\site-packages\fireworks\client\api_client.py", line 112, in _error_handling
    self._raise_for_status(resp)
  File "D:\python310\lib\site-packages\fireworks\client\api_client.py", line 94, in _raise_for_status
    raise InternalServerError(get_error_message(resp, "internal_server_error"))
fireworks.client.error.InternalServerError: {'error': {'object': 'error', 'type': 'internal_server_error', 'message': 'server had an error while processing your request, please retry again after a brief wait'}}

Process finished with exit code 1

Description

I want to know how to use other models, or open-source large models in a multi-agent system.

System Info

langchain-community 0.2.6 langchain-core 0.2.10 langchain-experimental 0.0.62 langchain-fireworks 0.1.4 langchain-groq 0.1.5 langchain-openai 0.1.10 langchain-text-splitters 0.2.0 langchainhub 0.1.20 langdetect 1.0.9 langgraph 0.1.5

vbarda commented 1 month ago

@SENVENHUHU could you please confirm that the tool calling is working for you with a minimal example? perhaps just use a simple from langgraph.prebuilt import create_react_agent to test it.

also, we recommend using prebuilt implementation of ToolNode (from langgraph.prebuilt import ToolNode), as well as bind_tools instead of bind_functions

SENVENHUHU commented 1 month ago

@vbarda Below is the result I got when I tested using from langgraph.prebuilt import create_react_agent. ================================ Human Message =================================

How much is the Morimaki Beef Burger? ================================== Ai Message ================================== Tool Calls: get_mojuan_price (call_J0Vq9xz6rBZD0MhExKmOh4yR) Call ID: call_J0Vq9xz6rBZD0MhExKmOh4yR Args: name: Morimaki Beef Burger ================================= Tool Message ================================= Name: get_mojuan_price

66 ================================== Ai Message ==================================

The Morimaki Beef Burger costs 66.

So I think the tool call was successful, however, the problem still hasn't been resolved. I would like to ask how to replicate the Multi-Agent Collaboration example using other models instead of the GPT model.

vbarda commented 1 month ago

@SENVENHUHU you would likely need to customize the prompts as well as use more powerful models (note that the notebook is using gpt4) -- not all tool calling models will work equally well out of box

going to close, since this is not a langgraph issue, but feel free to open a discussion on this

dangyuuki123 commented 1 month ago

@SENVENHUHU can you give me sample code with firefunciton-v2 working with react? i still can't implement it after reading the information