模态框
Modal 云平台 提供便捷的按需访问,可从本地计算机上的 Python 脚本使用无服务器云计算。 使用 modal 运行您自己的自定义大语言模型(LLM),而不必依赖 LLM API。
此示例介绍了如何使用 LangChain 与 modal HTTPS 网络端点 进行交互。
使用 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 集成类要求您部署一个带有符合以下 JSON 接口的 Web 端点的 Modal 应用程序:
- LLM 提示在键
"prompt"下作为str值被接受 - LLM 返回的响应在键
"prompt"下的值为str
示例请求 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 端点 指南,了解如何设置满足此接口的基本端点。
- 请参阅 Modal 的 '使用 AutoGPTQ 运行 Falcon-40B' 开源大语言模型示例,作为您自定义大语言模型的起点!
一旦部署了 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)