LangChain--5--langchain+langserve+fastapi接口示例
2026/9/8 13:27:48 网站建设 项目流程

本地使用ollama部署的deepseek-r1:1.5b模型

fromfastapiimportFastAPIfromfastapi.responsesimportResponsefromlangserveimportadd_routesfromlangchain.promptsimportChatPromptTemplatefromlangchain_community.chat_modelsimportChatOllamafrompydanticimportBaseModel app=FastAPI(title="My LangServer",version="0.1.0",description="暴露 LangChain 链为 REST API",debug=True)classJokeInput(BaseModel):topic:strchat_model=ChatOllama(model="deepseek-r1:1.5b",base_url="http://localhost:11434",temperature=0)prompt=ChatPromptTemplate.from_template("讲一个关于{topic}的笑话")joke_chain=prompt|chat_model add_routes(app,joke_chain,path="/joke",input_type=JokeInput)@app.get("/hello")asyncdefhello():returnResponse("hello, world")if__name__=="__main__":importuvicorn uvicorn.run(app,host="0.0.0.0",port=8000)

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询