Skip to main content
Open In Colab在 GitHub 上打开

模 态

Modal 云平台提供从本地计算机上的 Python 脚本对无服务器云计算的便捷按需访问。 用modal运行您自己的自定义 LLM 模型,而不是依赖 LLM API。

此示例介绍了如何使用 LangChain 与modalHTTPS Web 终端节点

使用 LangChain 进行问答是如何使用 LangChain alonside 的另一个示例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 终端节点部署模态应用程序:

  1. LLM 提示被接受为str"prompt"
  2. LLM 响应作为str"prompt"

请求 JSON 示例:

{
"prompt": "Identify yourself, bot!",
"extra": "args are allowed",
}

响应 JSON 示例:

{
"prompt": "This is the LLM speaking",
}

实现此接口的“虚拟”模态 Web 端点函数示例为

...
...

class Request(BaseModel):
prompt: str

@stub.function()
@modal.web_endpoint(method="POST")
def web(request: Request):
_ = request # ignore input
return {"prompt": "hello world"}

部署模态 Web 端点后,您可以将其 URL 传递到langchain.llms.modal.ModalLLM 类。然后,此类可以用作链中的构建块。

from langchain.chains import LLMChain
from langchain_community.llms import Modal
from langchain_core.prompts import PromptTemplate
API 参考:LLMChain | 模态 | 提示模板
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)