资讯动态

LangChain--5--langchain+langserve+fastapi接口示例

发布时间:2026/9/4 12:41:15 来源:尧图企业网站定制
本地使用ollama部署的deepseek-r1:1.5b模型fromfastapiimportFastAPIfromfastapi.responsesimportResponsefromlangserveimportadd_routesfromlangchain.promptsimportChatPromptTemplatefromlangchain_community.chat_modelsimportChatOllamafrompydanticimportBaseModel appFastAPI(titleMy LangServer,version0.1.0,description暴露 LangChain 链为 REST API,debugTrue)classJokeInput(BaseModel):topic:strchat_modelChatOllama(modeldeepseek-r1:1.5b,base_urlhttp://localhost:11434,temperature0)promptChatPromptTemplate.from_template(讲一个关于{topic}的笑话)joke_chainprompt|chat_model add_routes(app,joke_chain,path/joke,input_typeJokeInput)app.get(/hello)asyncdefhello():returnResponse(hello, world)if__name____main__:importuvicorn uvicorn.run(app,host0.0.0.0,port8000)

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价