以编程方式使用文档

Ray Serve 是一个可扩展的模型服务库,用于构建在线推理 API。Serve 特别适合系统组合,使您能够使用纯 Python 代码构建包含多个链和业务逻辑的复杂推理服务。

本笔记本的目标

本笔记本展示了一个如何将 OpenAI 链部署到生产环境的简单示例。您可以将其扩展到部署您自己的自托管模型,在其中您可以轻松定义运行模型所需的硬件资源(GPU 和 CPU)数量以高效地进行生产。了解更多关于 Ray Serve 中可用选项的信息,包括自动扩展 文档.

设置 Ray Serve

使用以下方式安装 Ray pip install ray[serve].

通用框架

部署服务的一般框架如下:

# 0: Import ray serve and request from starlette
from ray import serve
from starlette.requests import Request


# 1: Define a Ray Serve deployment.
@serve.deployment
class LLMServe:
    def __init__(self) -> None:
        # All the initialization code goes here
        pass

    async def __call__(self, request: Request) -> str:
        # You can parse the request here
        # and return a response
        return "Hello World"


# 2: Bind the model to deployment
deployment = LLMServe.bind()

# 3: Run the deployment
serve.api.run(deployment)
# Shutdown the deployment
serve.api.shutdown()

使用自定义提示词部署 OpenAI 链的示例

从以下位置获取 OpenAI API 密钥 OpenAI API 密钥页面。运行以下代码时,系统会提示您提供 API 密钥。

from langchain_classic.chains import LLMChain
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from getpass import getpass

OPENAI_API_KEY = getpass()
@serve.deployment
class DeployLLM:
    def __init__(self):
        # We initialize the LLM, template and the chain here
        llm = OpenAI(openai_api_key=OPENAI_API_KEY)
        template = "Question: {question}\n\nAnswer: Let's think step by step."
        prompt = PromptTemplate.from_template(template)
        self.chain = LLMChain(llm=llm, prompt=prompt)

    def _run_chain(self, text: str):
        return self.chain(text)

    async def __call__(self, request: Request):
        # 1. Parse the request
        text = request.query_params["text"]
        # 2. Run the chain
        resp = self._run_chain(text)
        # 3. Return the response
        return resp["text"]

现在我们可以绑定部署。

# Bind the model to deployment
deployment = DeployLLM.bind()

我们可以指定运行部署时的端口号和主机。

# Example port number
PORT_NUMBER = 8282
# Run the deployment
serve.api.run(deployment, port=PORT_NUMBER)

现在服务已部署在端口 localhost:8282 我们可以发送 POST 请求来获取结果。

text = "What NFL team won the Super Bowl in the year Justin Beiber was born?"
response = requests.post(f"http://localhost:{PORT_NUMBER}/?text={text}")
print(response.content.decode())