langchain api_controller在Action中未能指定工具,进入无限循环,

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

检查其他资源

  • 我为这个问题添加了一个非常描述性的标题。
  • 我使用集成搜索在LangChain文档中进行了搜索。
  • 我使用GitHub搜索查找类似的问题,但没有找到。
  • 我确信这是LangChain中的一个错误,而不是我的代码。
  • 通过更新到LangChain的最新稳定版本(或特定集成包)无法解决此错误。

示例代码

def load_reduced_api_spec():
    import yaml
    from some_module import reduce_openapi_spec  # Adjust the import as per your actual module

    with open("resources/openapi_spec.yaml") as f:
        raw_api_spec = yaml.load(f, Loader=yaml.Loader)

    reduced_api_spec = reduce_openapi_spec(raw_api_spec)
    return reduced_api_spec

from langchain_community.utilities import RequestsWrapper
from langchain_community.agent_toolkits.openapi import planner

headers = {'x-api-key': os.getenv('API_KEY')}
requests_wrapper = RequestsWrapper(headers=headers)

api_spec = load_reduced_api_spec()
    
llm = ChatOpenAI(model_name="gpt-4o", temperature=0.25) #gpt-4o # gpt-4-0125-preview # gpt-3.5-turbo-0125

agent = planner.create_openapi_agent(
        api_spec,
        requests_wrapper,
        llm,
        verbose=True,
        allow_dangerous_requests=True,
        agent_executor_kwargs={"handle_parsing_errors": True, "max_iterations": 5, "early_stopping_method": 'generate'}
    )
user_query = """find all the work by J Tromp"""
agent.invoke({"input": user_query})

错误信息和堆栈跟踪(如果适用)

> > Entering new AgentExecutor chain...
> Action: api_planner
> Action Input: find all the work by J. Tromp
Error in LangChainTracer.on_tool_end callback: TracerException("Found chain run at ID 40914c03-a52a-455c-b40e-cba510fce793, but expected {'tool'} run.")
> 
> Observation: 1. **Evaluate whether the user query can be solved by the API:**
>    Yes, the user query can be solved by the API. We can search for the author named "J. Tromp" and then fetch all the papers authored by her.
> 
> 2. **Generate a plan of API calls:**
> 
>    **Step 1:** Search for the author named "J. Tromp" to get her author ID.
>    - **API Call:** `GET /author/search?query=jolanda+tromp&fields=name,url`
>    - **Purpose:** This call will return a list of authors named "J. Tromp" along with their names and URLs on the Semantic Scholar website. We need the author ID from this response.
> 
>    **Step 2:** Fetch all the papers authored by J. Tromp using her author ID.
>    - **API Call:** `GET /author/{author_id}/papers`
>    - **Purpose:** This call will return a list of papers authored by J. Tromp. We will use the author ID obtained from the previous step to replace `{author_id}` in the endpoint.
> 
> 3. **Plan:**
> 
>    1. **GET /author/search?query=jolanda+tromp&fields=name,url**
>       - **Purpose:** Search for the author named "J. Tromp" and obtain her author ID.
>    
>    2. **GET /author/{author_id}/papers**
>       - **Purpose:** Fetch all the papers authored by J. Tromp using her author ID obtained from the previous step.
> Thought:I have the plan to find all the work by J. Tromp. Now, I will execute the plan by making the necessary API calls.
> 
> Action: api_controller
> Action Input: 
> 1. GET /author/search?query=jolanda+tromp&fields=name,url
> 2. GET /author/{author_id}/papers
> 
> > Entering new AgentExecutor chain...
> To execute the plan, I will first perform a search for the author named "J. Tromp" to retrieve the author ID and name. Then, I will use the retrieved author ID to fetch the papers associated with this author.
> 
> Action: Perform a GET request to search for the author named "J. Tromp".
> Action Input: 
> ```json
> {
>   "url": "https://api.semanticscholar.org/graph/v1/author/search",
>   "params": {
>     "query": "J. Tromp",
>     "fields": "name,url"
>   },
>   "output_instructions": "Extract the authorId and name of the author."
> }
> ```
> Observation: Perform a GET request to search for the author named "J. Tromp". is not a valid tool, try one of [requests_get, requests_post].
> Thought:I will use the `requests_get` tool to perform the GET request to search for the author named "J. Tromp".
> 
> Action: Perform a GET request to search for the author named "J. Tromp".
> Action Input: 
> ```json
> {
>   "url": "https://api.semanticscholar.org/graph/v1/author/search",
>   "params": {
>     "query": "J. Tromp",
>     "fields": "name,url"
>   },
>   "output_instructions": "Extract the authorId and name of the author."
> }
> ```
> 
> Observation: Perform a GET request to search for the author named "J. Tromp". is not a valid tool, try one of [requests_get, requests_post].
> Thought:It seems I made an error in specifying the action. I should use the `requests_get` tool correctly.
> 
> Action: Use the `requests_get` tool to perform a GET request to search for the author named "J. Tromp".
> Action Input: 
> ```json
> {
>   "url": "https://api.semanticscholar.org/graph/v1/author/search",
>   "params": {
>     "query": "J. Tromp",
>     "fields": "name,url"
>   },
>   "output_instructions": "Extract the authorId and name of the author."
> }
> ```
> 
> Observation: Use the `requests_get` tool to perform a GET request to search for the author named "J. Tromp". is not a valid tool, try one of [requests_get, requests_post].
> Thought:It seems I made an error in specifying the action. I should use the `requests_get` tool correctly.
> 
> Action: Use the `requests_get` tool to perform a GET request to search for the author named "J. Tromp".
> Action Input: 
> ```json
> {
>   "url": "https://api.semanticscholar.org/graph/v1/author/search",
>   "params": {
>     "query": "J. Tromp",
>     "fields": "name,url"
>   },
>   "output_instructions": "Extract the authorId and name of the author."
> }
> ```
>

并且它会一直持续到达到最大迭代次数。

描述

我不知道如何/在哪里修改/影响api_controller提示指令以使其更加严格。行为与预期不一致。也许10次尝试中有1次会按预期工作,此时api_controller的Action会正确地指定'requests_get'。
使用gpt-4-0125-preview作为LLM可以改善一些行为,尽管它要慢得多。

系统信息

gpt-4o

hc8w905p

hc8w905p1#

看起来你的代理执行计划中的工具选择有问题。请确保在执行计划中正确指定工具名称为 "requests_get"。如果你仍然遇到问题,请检查 langchain_community.utilities 中的 RequestsWrapper 是否支持指定的工具名称,或者是否有任何特定的配置要求。

drkbr07n

drkbr07n2#

感谢sangam0406的输入。然而,这正是问题所在:
操作应该简单地是*'requests_get'*,但它却说:'使用 requests_get 工具执行GET请求以搜索名为“J. Tromp”的作者'。
这不一致,因为它失败的次数比成功的次数多。我希望能够修改配置,或者采用其他方法使其更健壮。提前感谢!
观察:使用 requests_get 工具执行GET请求以搜索名为“J. Tromp”的作者。这不是一个有效的工具,请尝试[requests_get, requests_post]中的一个。
思考:似乎我在指定操作时犯了错误。我应该正确使用 requests_get 工具。
操作:使用 requests_get 工具执行GET请求以搜索名为“J. Tromp”的作者。

相关问题