LangServe服务端
安装 LangServe
···python
pip install “langserve[all]”
#只安装一端
#pip install “langserve[client]”
#pip install “langserve[server]”
···
首先要创建自己的env文件
OPENAI_API_KEY="sk-aggybFjhorevlj4XXXXXXXXXXXXXXXXXXX"
OPENAI_BASE_URL="https://api.fe8.cn/v1" #这是个国内代理地址
#!/usr/bin/env python
from fastapi import FastAPI
from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langserve import add_routes
import uvicorn
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
app = FastAPI(
title="LangChain Server",
version="1.0",
description="A simple api server using Langchain's Runnable interfaces",
)
model = ChatOpenAI()
prompt = ChatPromptTemplate.from_template("讲一个关于{topic}的笑话")
add_routes(
app,
prompt | model,
path="/joke",
)
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=9999)
LangSeve 客户端
import requests
response = requests.post(
"http://localhost:9999/joke/invoke",
json={'input': {'topic': '小明'}}
)
print(response.json())