langchain-ai / langgraph

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

DeepSeek V3 API Call Does Not Stop Automatically in LangGraph with ChatOpenAI() #3097

Closed ksmooi closed 4 months ago

ksmooi commented 5 months ago

Checked other resources

Example Code

!pip install -qU langchain-openai
!pip install -qU langchain_community
!pip install -qU langchain_experimental
!pip install -qU langgraph
!pip install -qU duckduckgo-search

from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from langchain_community.tools import DuckDuckGoSearchRun
from kaggle_secrets import UserSecretsClient

# the code is run on the kaggle notebook
llm_api_key = UserSecretsClient().get_secret("api-key-deepseek")
model = ChatOpenAI(model="deepseek-chat", temperature=0, openai_api_key=llm_api_key, openai_api_base='https://api.deepseek.com')

# Initialize the search tool
search = DuckDuckGoSearchRun()

def search_web(query: str) -> str:
    '''Perform a web search using DuckDuckGo and return the results.'''
    return search.invoke(query)

tools = [search_web]
graph = create_react_agent(model, tools=tools)

inputs = {"messages": [("user", "Search for the latest news on AI advancements")]}
for s in graph.stream(inputs, stream_mode="values"):
    message = s["messages"][-1]
    if isinstance(message, tuple):
        print(message)
    else:
        message.pretty_print()

Error Message and Stack Trace (if applicable)

**Actual Behavior:**  

The API call continues indefinitely, making repeated tool calls and generating new search queries without stopping. This results in an infinite loop of search queries and responses.

**Example Output:**

================================ Human Message =================================

Search for the latest news on AI advancements
================================== Ai Message ==================================
Tool Calls:
  search_web (call_0_bbac5567-4a8c-4e13-8752-ad37a91f6f27)
 Call ID: call_0_bbac5567-4a8c-4e13-8752-ad37a91f6f27
  Args:
    query: latest news on AI advancements 2023
================================= Tool Message =================================
Name: search_web

With broad AI comes broad risks in everything from misinformation to AI-related privacy risks, failures, and mistakes. 2023 saw an increasing flow of regulation, from the United States AI Bill of ... 2022 was the year that generative artificial intelligence (AI) exploded into the public consciousness, and 2023 was the year it began to take root in the business world. 2024 thus stands to be a pivotal year for the future of AI, as researchers and enterprises seek to establish how this evolutionary leap in technology can be most practically integrated into our everyday lives. While overall AI private investment decreased in 2023, funding for generative AI sharply increased. The sector attracted $25.2 billion last year, nearly nine times the investment of 2022 and about 30 times the amount in 2019. Generative AI accounted for over a quarter of all AI-related private investment in 2023. Artificial intelligence. Download RSS feed: News Articles / In the Media / Audio. Displaying 1 - 15 of 1293 news articles related to this topic. Show: News Articles. In the Media. Audio. Explained: Generative AI's environmental impact. Rapid development and deployment of powerful generative AI models comes with environmental consequences ... GPU chips play a pivotal role in enhancing overall performance for genAI applications, underscoring their significance for future advancements. Defining AI trends for 2023. Amid the myriad of AI trends and themes that characterized 2023, four major themes emerged, shaping the narrative and capturing widespread attention:
================================== Ai Message ==================================
Tool Calls:
  search_web (call_0_744fb5ec-24f1-4e8d-b3d0-5704b72b60a7)
 Call ID: call_0_744fb5ec-24f1-4e8d-b3d0-5704b72b60a7
  Args:
    query: latest AI advancements 2023
================================= Tool Message =================================
Name: search_web

Uncover the transformative AI achievements of 2023, with a focus on NLP, computer vision, robotics, voice recognition, and HCI. This comprehensive overview highlights the latest advancements and their far-reaching impacts on technology, industry, and society. Top AI achievements of 2023. AI achievements in 2023 were quite phenomenal. March 30, 2023: GitHub presents AutoGTP based on ChatGPT-4.; May 12, 2023: OpenAI releases ChatGPT Plugins; May 25, 2023: Nvidia's stock price tripled in less than eight months, surging by 27% overnight and approaching a market cap of $1 trillion, driven by the skyrocketing demand for GPUs due to the growing interest in AI.; May 25, 2023: Google launches Google Search Generative Experience. The year 2023 marked a significant period in the evolution of artificial intelligence (AI), characterized by groundbreaking advancements across various domains. From the development of ... 2022 was the year that generative artificial intelligence (AI) exploded into the public consciousness, and 2023 was the year it began to take root in the business world. 2024 thus stands to be a pivotal year for the future of AI, as researchers and enterprises seek to establish how this evolutionary leap in technology can be most practically integrated into our everyday lives. AI Highlights from May 2023. Advancements in robotics took center stage in May 2023 as an AI system enabled robots to autonomously conduct scientific experiments. While Sanctuary AI's Phoenix Robot became the world's first commercially available general-purpose humanoid robot, Tesla's Optimus Bot also showed significant progress. OpenAI ...
================================== Ai Message ==================================
Tool Calls:
  search_web (call_0_ba4b87e9-2096-49f2-840c-f1790cacc0f4)
 Call ID: call_0_ba4b87e9-2096-49f2-840c-f1790cacc0f4
  Args:
    query: AI advancements 2023
================================= Tool Message =================================
Name: search_web

The report, which is in its seventh edition, covers trends such as technical advancements in AI, public perceptions of the technology and the geopolitical dynamics surrounding its development. Emerging Technologies ... In 2023, 25 AI-related regulations were enacted in the US, growing the total number by 56.3%. Compare that to 2016, when just ... 2023 AI Recap: Innovations in AI Technology. OpenAI's GPT-4: Revolutionized user interactions with AI through advanced natural language understanding, setting a new standard in generative models.; Multimodal Capabilities: GPT-4's ability to process various types of data, including text and images, marked a significant step towards more versatile AI systems. This year's AI Index — a 500-page report tracking 2023's worldwide trends in AI — is out.. The index is an independent initiative at the Stanford Institute for Human-Centered Artificial Intelligence (HAI), led by the AI Index Steering Committee, an interdisciplinary group of experts from across academia and industry. This year's report covers the rise of multimodal foundation models ... 2022 was the year that generative artificial intelligence (AI) exploded into the public consciousness, and 2023 was the year it began to take root in the business world. 2024 thus stands to be a pivotal year for the future of AI, as researchers and enterprises seek to establish how this evolutionary leap in technology can be most practically integrated into our everyday lives. GPU chips play a pivotal role in enhancing overall performance for genAI applications, underscoring their significance for future advancements. Defining AI trends for 2023. Amid the myriad of AI trends and themes that characterized 2023, four major themes emerged, shaping the narrative and capturing widespread attention:
================================== Ai Message ==================================
Tool Calls:
  search_web (call_0_9d944700-80fc-4092-a60c-d75e36d351d1)
 Call ID: call_0_9d944700-80fc-4092-a60c-d75e36d351d1
  Args:
    query: AI trends 2023
================================= Tool Message =================================
Name: search_web

This year's AI Index — a 500-page report tracking 2023's worldwide trends in AI — is out.. The index is an independent initiative at the Stanford Institute for Human-Centered Artificial Intelligence (HAI), led by the AI Index Steering Committee, an interdisciplinary group of experts from across academia and industry. This year's report covers the rise of multimodal foundation models ... Defining AI trends for 2023. Amid the myriad of AI trends and themes that characterized 2023, four major themes emerged, shaping the narrative and capturing widespread attention: Business adoption: Although consumer genAI applications grabbed headlines, 2023 also saw increased business experimentation. Enterprise adoption, startup activity and ... Get 90+ pages of charts and data detailing the latest venture trends in AI. First name. Last name. Email. ... In 2023, AI startups raised $42.5B across 2,500 equity rounds. Although down 10% year-over-year (YoY), AI funding fell far less than broader venture funding (-42% in 2023). AI deal volume decreased by 24% YoY — also less than the ... 2022 was the year that generative artificial intelligence (AI) exploded into the public consciousness, and 2023 was the year it began to take root in the business world. 2024 thus stands to be a pivotal year for the future of AI, as researchers and enterprises seek to establish how this evolutionary leap in technology can be most practically integrated into our everyday lives. March 30, 2023: GitHub presents AutoGTP based on ChatGPT-4.; May 12, 2023: OpenAI releases ChatGPT Plugins; May 25, 2023: Nvidia's stock price tripled in less than eight months, surging by 27% overnight and approaching a market cap of $1 trillion, driven by the skyrocketing demand for GPUs due to the growing interest in AI.; May 25, 2023: Google launches Google Search Generative Experience.
================================== Ai Message ==================================
Tool Calls:
  search_web (call_0_3a3d86af-8b05-482a-ac02-4fc1c4437cfc)
 Call ID: call_0_3a3d86af-8b05-482a-ac02-4fc1c4437cfc
  Args:
    query: AI regulations 2023
================================= Tool Message =================================
Name: search_web

New Framework Advances AI Innovation While Protecting U.S. National Security. Washington, D.C. — Today, the Department of Commerce's Bureau of Industry and Security (BIS) announced controls on advanced computing chips and certain closed artificial intelligence (AI) model weights, alongside new license exceptions and updates to the Data Center Validated End User (VEU) authorization. Section 1. Purpose. Artificial intelligence (AI) is a defining technology of our era. ... and Executive Order 14110 of November 1, 2023 (Safe, Secure, and Trustworthy Development and Use of ... In October 2023, President Joe Biden issued a broad executive order on AI (AI Order), which the administration touted as a vehicle to establish AI safety and security standards while protecting privacy, advancing civil rights and promoting innovation. However, most of the AI Order was a series of directives to various federal agencies to study ... In October 2023, President Joe Biden issued a broad executive order on AI ... we expect to see states take an even more active role in enacting state-specific AI regulations. Monitor Legal and Regulatory Developments: Stay informed about new state and federal AI regulations, along with the developing case law related to AI and ERISA litigation. Establish a process for ...
================================== Ai Message ==================================
Tool Calls:
  search_web (call_0_10269960-464f-4abd-a458-b863da1710b4)
 Call ID: call_0_10269960-464f-4abd-a458-b863da1710b4
  Args:
    query: AI investment trends 2023
  search_web (call_1_97eabda5-9144-4a28-93f9-e21ec851b5ba)
 Call ID: call_1_97eabda5-9144-4a28-93f9-e21ec851b5ba
  Args:
    query: AI private investment 2023
================================= Tool Message =================================
Name: search_web

Private equity and venture capital firms poured more than twice as much money into generative AI companies in 2023 compared with the prior year, and dealmaking got off to a fast start in 2024. ... Incera said. Private equity-backed investment peaked at $927.7 million in the third quarter of 2023, up from $121.5 million recorded in the prior ... The volume of private investment in artificial intelligence (AI) worldwide in 2023 had its largest share concentrated in the United States of America, with approximately 67.2 billion U.S. Generative AI accounted for over a quarter of all AI-related private investments in 2023. U.S. Wins $$ Race. And again, in 2023 the United States dominates in AI private investment. In 2023, the $67.2 billion invested in the U.S. was roughly 8.7 times greater than the amount invested in the next highest country, China, and 17.8 times the amount ... The graph below shows the yearly number of private equity platform and add-on investments in AI. Quick stats. 315 Private equity firms with current investments in the Artificial Intelligence and Machine Learning industry. 573 current private equity investments in AI/ML companies (2013 - 2023, 52 exits) Download our free list As from 2013 to 2023, the sum of private investments in artificial intelligence (AI) worldwide has had its largest share coming from the United States of America, accounting for over 336 billion U.S.
================================== Ai Message ==================================
Tool Calls:
  search_web (call_0_1667e946-5452-48ab-b5e2-d6e3fb9a526b)
 Call ID: call_0_1667e946-5452-48ab-b5e2-d6e3fb9a526b
  Args:
    query: AI investment trends 2023
  search_web (call_1_d7bdf09b-5892-4e60-91a8-63a4b898a209)
 Call ID: call_1_d7bdf09b-5892-4e60-91a8-63a4b898a209
  Args:
    query: AI private investment 2023
================================= Tool Message =================================
Name: search_web

Private equity and venture capital firms poured more than twice as much money into generative AI companies in 2023 compared with the prior year, and dealmaking got off to a fast start in 2024. ... Incera said. Private equity-backed investment peaked at $927.7 million in the third quarter of 2023, up from $121.5 million recorded in the prior ... The volume of private investment in artificial intelligence (AI) worldwide in 2023 had its largest share concentrated in the United States of America, with approximately 67.2 billion U.S. Generative AI accounted for over a quarter of all AI-related private investments in 2023. U.S. Wins $$ Race. And again, in 2023 the United States dominates in AI private investment. In 2023, the $67.2 billion invested in the U.S. was roughly 8.7 times greater than the amount invested in the next highest country, China, and 17.8 times the amount ... Figure 3 shows that no country, at least on a private investment basis, is spending anything close to what the U.S. is spending, which likely means it will be difficult to challenge U.S. leadership in this area. Figure 3: Private Investment in AI by Geographic Area, 2023. 2023 Was a Year of Building AI Applications As from 2013 to 2023, the sum of private investments in artificial intelligence (AI) worldwide has had its largest share coming from the United States of America, accounting for over 336 billion U.S.
================================== Ai Message ==================================
Tool Calls:
  search_web (call_0_cb9e01b0-7850-4038-a727-aa7086c97191)
 Call ID: call_0_cb9e01b0-7850-4038-a727-aa7086c97191
  Args:
    query: AI investment trends 2023
  search_web (call_1_d658f5fe-7db0-42e1-81a6-d003f231efe6)
 Call ID: call_1_d658f5fe-7db0-42e1-81a6-d003f231efe6
  Args:
    query: AI private investment 2023
  search_web (call_2_7ccf4fdf-3921-41b9-b805-9157d25998be)
 Call ID: call_2_7ccf4fdf-3921-41b9-b805-9157d25998be
  Args:
    query: AI regulations 2023
  search_web (call_3_df9e132a-1328-4db8-8984-3051c59dcee2)
 Call ID: call_3_df9e132a-1328-4db8-8984-3051c59dcee2
  Args:
    query: AI advancements 2023
  search_web (call_4_46a83a96-205c-47c0-8c67-94d07ac91894)
 Call ID: call_4_46a83a96-205c-47c0-8c67-94d07ac91894
  Args:
    query: latest AI advancements 2023
  search_web (call_5_d506554b-9025-407b-8cdc-9154d547ab88)
 Call ID: call_5_d506554b-9025-407b-8cdc-9154d547ab88
  Args:
    query: latest news on AI advancements 2023
================================= Tool Message =================================
Name: search_web

With broad AI comes broad risks in everything from misinformation to AI-related privacy risks, failures, and mistakes. 2023 saw an increasing flow of regulation, from the United States AI Bill of ... Artificial intelligence. Download RSS feed: News Articles / In the Media / Audio. Displaying 1 - 15 of 1293 news articles related to this topic. Show: News Articles. In the Media. Audio. Explained: Generative AI's environmental impact. Rapid development and deployment of powerful generative AI models comes with environmental consequences ... March 30, 2023: GitHub presents AutoGTP based on ChatGPT-4.; May 12, 2023: OpenAI releases ChatGPT Plugins; May 25, 2023: Nvidia's stock price tripled in less than eight months, surging by 27% overnight and approaching a market cap of $1 trillion, driven by the skyrocketing demand for GPUs due to the growing interest in AI.; May 25, 2023: Google launches Google Search Generative Experience. GPU chips play a pivotal role in enhancing overall performance for genAI applications, underscoring their significance for future advancements. Defining AI trends for 2023. Amid the myriad of AI trends and themes that characterized 2023, four major themes emerged, shaping the narrative and capturing widespread attention: AI Highlights from May 2023. Advancements in robotics took center stage in May 2023 as an AI system enabled robots to autonomously conduct scientific experiments. While Sanctuary AI's Phoenix Robot became the world's first commercially available general-purpose humanoid robot, Tesla's Optimus Bot also showed significant progress. OpenAI ...

Description

Describe the bug

When using ChatOpenAI() in LangChain with a custom tool (e.g., DuckDuckGoSearchRun), the API call does not terminate automatically after completing the task. Instead, it continues to make repeated tool calls indefinitely, leading to an infinite loop of search queries and responses.

Expected behavior

The API call should terminate after completing the task (e.g., retrieving search results for the query) and return the final response.

System Info

Environment:

crixue commented 4 months ago

Yes, I meet the same issue. Even I refine the system prompt like: You serve as an intelligent assistant. You must give your final answer in 1 rounds tool calls. And if you don't know the answer, just answer 'Sorry, I don't know'

It still repeat to call the tool until the input tokens exceed 64k context tokens limit. I leverage the langchain components to call agent to call tools. Qwen-max and Gpt-4o have a good understanding the various system prompt, but deepseek-v3 is not. It's so frustrating, even though the price is cheap.

jason571 commented 4 months ago

DeepSeek V3 API function have bug

vbarda commented 4 months ago

This is the underlying API issue as @jason571 pointed out, and is not related to LangGraph or LangChain. The condition for react agent termination is absence of tool calls in AI message, so if the API keeps returning tool calls, there is nothing we can do on LangGraph side.