ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

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

LangChain--5--langchain+langserve+fastapi接口示例 本地使用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)
返回列表