Skip to main content

Modal

Modal 云平台 提供方便的按需访问服务器无关的云计算,用户可以通过本地计算机上的 Python 脚本进行访问。 使用 modal 来运行您自己的自定义 LLM 模型,而不是依赖 LLM API。

此示例介绍了如何使用 LangChain 与 modal HTTPS 网络端点 进行交互。

使用 LangChain 进行问答 是另一个如何将 LangChain 与 Modal 一起使用的示例。在该示例中,Modal 端到端运行 LangChain 应用,并使用 OpenAI 作为其 LLM API。

%pip install --upgrade --quiet  modal
# 注册一个 Modal 账户并获取一个新的令牌。

!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 接口的网络端点的 Modal 应用:

  1. LLM 提示作为 str 值在键 "prompt" 下接受
  2. LLM 响应作为 str 值在键 "prompt" 下返回

示例请求 JSON:

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

示例响应 JSON:

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

一个满足此接口的“虚拟” Modal 网络端点函数示例为

...
...

class Request(BaseModel):
prompt: str

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

一旦您部署了 Modal 网络端点,您可以将其 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."""
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)

相关


此页面是否有帮助?


您还可以留下详细的反馈 在 GitHub 上