Skip to main content

模态

本页面介绍如何使用模态生态系统运行LangChain自定义大型语言模型。 它分为两个部分:

  1. 模态安装和网络端点部署
  2. 使用部署的网络端点与LLM包装类。

安装和设置

  • 使用pip install modal安装
  • 运行modal token new

定义你的模态函数和网络钩子

你必须包含一个提示。响应结构是固定的:

class Item(BaseModel):
prompt: str

@stub.function()
@modal.web_endpoint(method="POST")
def get_text(item: Item):
return {"prompt": run_gpt2.call(item.prompt)}

以下是使用GPT2模型的示例:

from pydantic import BaseModel

import modal

CACHE_PATH = "/root/model_cache"

class Item(BaseModel):
prompt: str

stub = modal.Stub(name="example-get-started-with-langchain")

def download_model():
from transformers import GPT2Tokenizer, GPT2LMHeadModel
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')
tokenizer.save_pretrained(CACHE_PATH)
model.save_pretrained(CACHE_PATH)

# Define a container image for the LLM function below, which
# downloads and stores the GPT-2 model.
image = modal.Image.debian_slim().pip_install(
"tokenizers", "transformers", "torch", "accelerate"
).run_function(download_model)

@stub.function(
gpu="any",
image=image,
retries=3,
)
def run_gpt2(text: str):
from transformers import GPT2Tokenizer, GPT2LMHeadModel
tokenizer = GPT2Tokenizer.from_pretrained(CACHE_PATH)
model = GPT2LMHeadModel.from_pretrained(CACHE_PATH)
encoded_input = tokenizer(text, return_tensors='pt').input_ids
output = model.generate(encoded_input, max_length=50, do_sample=True)
return tokenizer.decode(output[0], skip_special_tokens=True)

@stub.function()
@modal.web_endpoint(method="POST")
def get_text(item: Item):
return {"prompt": run_gpt2.call(item.prompt)}

部署网络端点

使用modal deploy CLI命令将网络端点部署到Modal云。 您的网络端点将在modal.run域下获取一个持久的URL。

Modal网络端点的LLM包装器

接受您已部署的网络端点URL的Modal LLM包装器类。

from langchain_community.llms import Modal

endpoint_url = "https://ecorp--custom-llm-endpoint.modal.run" # REPLACE ME with your deployed Modal web endpoint's URL

llm = Modal(endpoint_url=endpoint_url)
llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"

llm_chain.run(question)

Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书