Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id' #1784

Open
touful opened this issue Jan 14, 2025 · 10 comments

Comments

@touful
Copy link

touful commented Jan 14, 2025

I have encountered this error, but it is intermittent. How should I seek help to resolve it
openai.BadRequestError: Error code: 400 - {'error': {'message': "An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. (insufficient tool messages following tool_calls message)", 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}

@touful
Copy link
Author

touful commented Jan 14, 2025

同样的代码运行有时会出现这个错误,有时却不会,求助

@ysolanky
Copy link
Contributor

Hello @touful ! Please share your Agent configuration so that I can help debug.

@touful
Copy link
Author

touful commented Jan 14, 2025

this is my main.py ,but as I said before ,the same codes run return error or return normally.And now ,it works .I don't no why,please and thanks . @ysolanky

from aiagent.xmlagent import xml_search
from aiagent.virusagent import virus_detect
from aiagent.llmres import get_llm_responce
from aiagent.fridaagent import run_frida_hook
from phi.model.deepseek import DeepSeekChat
from phi.tools.python import PythonTools
from phi.knowledge.json import JSONKnowledgeBase
from phi.agent import Agent
from phi.knowledge.agent import AgentKnowledge
from aiagent.searchagent import lancedb_agent
from phi.embedder.ollama import OllamaEmbedder
from phi.vectordb.lancedb import LanceDb, SearchType

def knowledge_add(pathslist,uri):
    knowledge_base = JSONKnowledgeBase(
        path=pathslist,
        vector_db=LanceDb(
            table_name="javacode",
            uri=uri,
            search_type=SearchType.vector,
            embedder=OllamaEmbedder(model="nomic-embed-text:latest"),
        ),
    )
    knowledge_base.load()

basic_agent = Agent(
    model=DeepSeekChat(id="deepseek-chat"),
    tools=[xml_search,virus_detect],
    instructions=["Always include sources as more as you can,使用中文进行回答"],
    show_tool_calls=True,
    markdown=True,
    debug_mode=config.agent_debug,
    tool_call_limit=config.tools_calls_limit
)
print("[*]当前版本为:",config.version)
if config.version=="free":
    codetools = [PythonTools()]
elif config.version=="pro":
    codetools=[PythonTools(),run_frida_hook]
#knowledge_add(Path(r"C:\Users\ASUS\Desktop\apktest\classdir\javacode.json"),"tmp/lancedb")
# knowledge_base=AgentKnowledge(
#     vector_db=LanceDb(
#             table_name="javacode",
#             uri=r"C:\Users\ASUS\Desktop\app_forensic\tmp\lancedb",
#             search_type=SearchType.vector,
#             embedder=OllamaEmbedder(model="nomic-embed-text:latest"),
#     ),
#     num_documents=config.RAG_document_num,
# )
review_agent =Agent(
    model=DeepSeekChat(),
    tools=[lancedb_agent,get_llm_responce],
    #knowledge=knowledge_base,
    instructions=["Always include sources as more as you can,使用中文进行回答"],
    show_tool_calls=True,
    #add_references=True,
    markdown=True,
    debug_mode=config.agent_debug,
    tool_call_limit=config.tools_calls_limit
)
code_agent = Agent(
    model=DeepSeekChat(id="deepseek-chat"),
    tools=codetools,
    #knowledge=knowledge_base,
    instructions=["Always include sources as more as you can,使用中文进行回答"],
    show_tool_calls=True,
    #add_references=True,
    markdown=True,
    debug_mode=config.agent_debug,
    tool_call_limit=config.tools_calls_limit
)

main_agent = Agent(
    model=DeepSeekChat(id="deepseek-chat"),
    team=[basic_agent,code_agent,review_agent],
    #knowledge=knowledge_base,
    instructions=["Always include sources as more as you can,使用中文进行回答"],
    show_tool_calls=True,
    #add_references=True,
    markdown=True,
    debug_mode=config.agent_debug,
    tool_call_limit=config.tools_calls_limit,
    structured_outputs=True
)



if __name__ == "__main__":
    #apk2java()
    want_rag=["ahmyth.mine.king.ahmyth","Base64"]
    qus=config.meiya_questions[10]
    prompt = f'''这是1个app取证的题目:{qus},你需要充分使用工具,其中题目中要求的返回格式需要你最后整理给我,每个agent可能不会给你希望的返回格式,注意,假如你需要查询RAG数据库,请使用传统的java类格式,即A.B.C.D,只有这样的格式才能正确查询,并且,你应该多次执行不同的查询,每次查询调用需要确保提问方式的多样化,此外,RAG希望查询的关键词列表有{want_rag},你需要对这些关键词每个单独查询一次,你最多只能查询{config.tools_calls_limit}次。结合你的分析,告诉我答案,请仔细思考后一步一步回答'''
    main_agent.print_response(prompt, stream=True, show_full_reasoning=True)```

@ysolanky ysolanky mentioned this issue Jan 14, 2025
12 tasks
@ysolanky
Copy link
Contributor

@touful it looks like it is an issue with DeepSeekChat(id="deepseek-chat"), not returning the tool call ids properly.

Can you please:

  • Share debug logs. I would like to review the tool calls made by the Agent.
  • Test the same Agents with OpenAIChat and see if you can replicate the error. This would help us isolate the problem.

@touful
Copy link
Author

touful commented Jan 15, 2025

here is my stack traceback, as you said that I will try OpenAIChat after I get opanai_api_key(in my area ,I can't pay for it directly):

  File "C:\Users\ASUS\Desktop\app_forensic\agent.py", line 94, in <module>
    main_agent.print_response(prompt, stream=True, show_full_reasoning=True)
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\agent\agent.py", line 2815, in print_response
    for resp in self.run(message=message, messages=messages, stream=True, **kwargs):
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\agent\agent.py", line 1806, in _run
    for model_response_chunk in self.model.response_stream(messages=messages_for_model):
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\model\openai\chat.py", line 922, in response_stream
    yield from self.handle_stream_tool_calls(
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\model\openai\chat.py", line 845, in handle_stream_tool_calls
    for function_call_response in self.run_function_calls(
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\model\base.py", line 244, in run_function_calls
    for item in function_call.result:
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\agent\agent.py", line 391, in _transfer_task_to_agent
    for member_agent_run_response_chunk in member_agent_run_response_stream:
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\agent\agent.py", line 1806, in _run
    for model_response_chunk in self.model.response_stream(messages=messages_for_model):
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\model\openai\chat.py", line 925, in response_stream
    yield from self.handle_post_tool_call_messages_stream(messages=messages)
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\model\base.py", line 372, in handle_post_tool_call_messages_stream
    yield from self.response_stream(messages=messages)
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\model\openai\chat.py", line 870, in response_stream
    for response in self.invoke_stream(messages=messages):
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\phi\model\openai\chat.py", line 394, in invoke_stream
    yield from self.get_client().chat.completions.create(
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\openai\_utils\_utils.py", line 279, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\openai\resources\chat\completions.py", line 859, in create
    return self._post(
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\openai\_base_client.py", line 1283, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\openai\_base_client.py", line 960, in request
    return self._request(
  File "C:\Users\ASUS\.conda\envs\forensics\lib\site-packages\openai\_base_client.py", line 1064, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. (insufficient tool messages following tool_calls message)", 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}```

@touful
Copy link
Author

touful commented Jan 15, 2025

And more,I want to gain that whether I can use proxy for LanceDb.Because if I want use OpenAIChat,I must use proxy,but with proxy ,my LanceDb knowledge_base returns with code 500 like that. By reading LanceDb Class or its parent Class ,I don't find proxy args. Sincere thanks for you! @ysolanky

         <head>                                                                
          <title>500 Internal Privoxy Error</title>                            
          <link rel="shortcut icon"                                            
         href="http://config.privoxy.org/error-favicon.ico"                    
         type="image/x-icon"></head>                                           
         <body>                                                                
         <h1>500 Internal Privoxy Error</h1>                                   
         <p>Privoxy encountered an error while processing your request:</p>    
         <p><b>Could not load template file <code>no-server-data</code> or one 
         of its included components.</b></p>                                   
         <p>Please contact your proxy administrator.</p>                       
         <p>If you are the proxy administrator, please put the required        
         file(s)in the <code><i>(confdir)</i>/templates</code> directory.  The 
         location of the <code><i>(confdir)</i></code> directory is specified  
         in the main Privoxy <code>config</code> file.  (It's typically the    
         Privoxy install directory).</p>                                       
         </body>                                                               
         </html>                                                               
                                                                               
ERROR    Error searching for documents: list index out of range                

@ysolanky
Copy link
Contributor

@touful can you please share the debug logs when you are using the model: DeepSeekChat(id="deepseek-chat"). I want to review the tool calls that the model is making. I am not able to replicate openai.BadRequestError: Error code: 400 - {'error': {'message': "An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. error on my end.

With lancedb, can you please try reading a local file? For example, in this example: https://github.com/phidatahq/phidata/blob/main/cookbook/vectordb/lance_db.py, use
PDFKnowledgeBase instead of PDFUrlKnowledgeBase. As lanceDb is local, you do not require a proxy for insertion of any documents.

@touful
Copy link
Author

touful commented Jan 24, 2025

mydeepseekdebug.txt
This is all my debugging information when occured the error.
@ysolanky Yes, I use the lancedb locally. Codes are that:

knowledge_base = AgentKnowledge(
        vector_db=LanceDb(
            table_name="javacode",
            uri=r"C:\Users\ASUS\Desktop\app_forensic\tmp\lancedb",
            search_type=SearchType.vector,
            embedder=OllamaEmbedder(model="snowflake-arctic-embed2:latest"),
        ),
        num_documents=config.RAG_document_num,
    )

By using this lancedb code with my system proxy,it will query with return 500 as I said above.All data are in my local machine.

@touful
Copy link
Author

touful commented Jan 25, 2025

@ysolanky At this time every day, in my UTC+8 region, around 10 o'clock, my code will encounter the above error. Today, I carefully reviewed the official documentation of DeepSeek, which states:
The DeepSeek API does not limit user concurrency, and we will do our best to ensure the quality of service for all your requests.
However, please note that when our server is under high traffic pressure, it may take some time to receive a response from the server after your request is sent. During this period, your HTTP request will remain connected and continue to receive return content in the following format:
Non streaming request: Continuously returning empty lines
Streaming request: Continuously return SSE keep alive comments (: keep alive)
These contents do not affect the parsing of the JSON body of the response by OpenAI SDK. If you are parsing HTTP responses yourself, please pay attention to handling these blank lines or comments.
If the request is not completed after 30 minutes, the server will close the connection.
I think it's possible that a request timeout has been set in the Phidata framework? If that's the case, can you do an exception handling for this situation.

@touful
Copy link
Author

touful commented Jan 25, 2025

@ysolanky I don't know if I understand correctly, thank you again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants