Modal
Modal 云平台 提供从本地计算机上的 Python 脚本便捷、按需地访问无服务器云计算。使用 modal
运行您自己的自定义 LLM 模型,而不是依赖 LLM API。
此示例介绍了如何使用 LangChain 与 modal
HTTPS web 端点 交互。
使用 LangChain 进行问答 是另一个关于如何结合使用 LangChain 和 Modal
的示例。在该示例中,Modal 端到端运行 LangChain 应用程序,并使用 OpenAI 作为其 LLM API。
%pip install --upgrade --quiet modal
# Register an account with Modal and get a new token.
!modal token new
Launching login page in your browser window...
If this is not showing up, please copy this URL into your web browser manually:
https://modal.com/token-flow/tf-Dzm3Y01234mqmm1234Vcu3
langchain.llms.modal.Modal
集成类要求您部署一个 Modal 应用程序,该应用程序具有符合以下 JSON 接口的 web 端点
- LLM 提示符以
str
值形式在键"prompt"
下接受 - LLM 响应以
str
值形式在键"prompt"
下返回
请求 JSON 示例
{
"prompt": "Identify yourself, bot!",
"extra": "args are allowed",
}
响应 JSON 示例
{
"prompt": "This is the LLM speaking",
}
一个满足此接口的“虚拟”Modal web 端点函数示例是
...
...
class Request(BaseModel):
prompt: str
@stub.function()
@modal.web_endpoint(method="POST")
def web(request: Request):
_ = request # ignore input
return {"prompt": "hello world"}
- 有关设置满足此接口的端点的基础知识,请参阅 Modal 的 web 端点 指南。
- 有关自定义 LLM 的起点,请参阅 Modal 的 “使用 AutoGPTQ 运行 Falcon-40B” 开源 LLM 示例!
一旦您部署了 Modal web 端点,您可以将其 URL 传递到 langchain.llms.modal.Modal
LLM 类中。然后,此类可以用作链中的构建块。
from langchain.chains import LLMChain
from langchain_community.llms import Modal
from langchain_core.prompts import PromptTemplate
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
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)