You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LangChain/FastAPI应用访问/agent路径出现404错误求助

LangChain LangServe 404错误问题解决

问题描述

完全按照LangChain官方教程编写代码,未做任何修改,启动应用后访问/agent路径时出现404错误,完整代码及控制台输出如下:

完整代码

#!/usr/bin/env python
from typing import List

from fastapi import FastAPI
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_community.document_loaders import WebBaseLoader
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.tools.retriever import create_retriever_tool
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain import hub
from langchain.agents import create_openai_functions_agent
from langchain.agents import AgentExecutor
from langchain.pydantic_v1 import BaseModel, Field
from langchain_core.messages import BaseMessage
from langserve import add_routes

# 1. 加载检索器
loader = WebBaseLoader("https://docs.smith.langchain.com/user_guide")
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)
embeddings = OpenAIEmbeddings()
vector = FAISS.from_documents(documents, embeddings)
retriever = vector.as_retriever()

# 2. 创建工具
retriever_tool = create_retriever_tool(
    retriever,
    "langsmith_search",
    "搜索LangSmith相关信息。所有关于LangSmith的问题必须使用这个工具!",
)
search = TavilySearchResults()
tools = [retriever_tool, search]


# 3. 创建Agent
prompt = hub.pull("hwchase17/openai-functions-agent")
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)


# 4. 定义应用
app = FastAPI(
  title="LangChain Server",
  version="1.0",
  description="使用LangChain Runnable接口搭建的简单API服务器",
)

# 5. 添加Chain路由

# 当前AgentExecutor缺少输入输出 schema,所以需要手动定义

class Input(BaseModel):
    input: str
    chat_history: List[BaseMessage] = Field(
        ...,
        extra={"widget": {"type": "chat", "input": "location"}},
    )


class Output(BaseModel):
    output: str

add_routes(
    app,
    agent_executor.with_types(input_type=Input, output_type=Output),
    path="/agent",
)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="localhost", port=8000)

控制台输出

INFO:     Started server process [55250]
INFO:     Waiting for application startup.

 __          ___      .__   __.   _______      _______. _______ .______     ____    ____  _______
|  |        /   \     |  \ |  |  /  _____|    /       ||   ____||   _  \    \   \  /   / |   ____|
|  |       /  ^  \    |   \|  | |  |  __     |   (----`|  |__   |  |_)  |    \   \/   /  |  |__
|  |      /  /_\  \   |  . `  | |  | |_ |     \   \    |   __|  |      /      \      /   |   __|
|  `----./  _____  \  |  |\   | |  |__| | .----)   |   |  |____ |  |\  \----.  \    /    |  |____
|_______/__/     \__\ |__| \__|  \______| |_______/    |_______|| _| `._____|   \__/     |_______|

LANGSERVE: Playground for chain "/agent/" is live at:
LANGSERVE:  │
LANGSERVE:  └──> /agent/playground/
LANGSERVE:
LANGSERVE: See all available routes at /docs/

LANGSERVE: ⚠️ Using pydantic 2.6.4. OpenAPI docs for invoke, batch, stream, stream_log endpoints will not be generated. API endpoints and playground should work as expected. If you need to see the docs, you can downgrade to pydantic 1. For example, `pip install pydantic==1.10.13`. See https://github.com/tiangolo/fastapi/issues/10360 for details.

INFO:     Application startup complete.
INFO:     Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)
INFO:     ::1:58755 - "GET /agent HTTP/1.1" 404 Not Found
INFO:     ::1:58756 - "GET /agent HTTP/1.1" 404 Not Found

解决方案

  • LangServe为/agent添加的不是直接可访问的GET路由,而是一组功能端点,比如/agent/invoke(POST请求)、/agent/stream等,直接访问/agent会返回404。
  • 快速测试方法:访问控制台提示的http://localhost:8000/agent/playground,这是交互式测试页面,可以直接输入问题进行测试。
  • 如果要通过API调用,使用POST请求访问http://localhost:8000/agent/invoke,请求体需要符合定义的Input模型,示例:
    {
      "input": "LangSmith是什么?",
      "chat_history": []
    }
    
  • 可访问http://localhost:8000/docs查看所有可用的API端点及详细调用说明。

内容的提问来源于stack exchange,提问作者steamfood

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 07:05:02