模块
LLMS
Deepinfra Example

DeepInfra#

DeepInfra 提供了多种LLM several LLMs (opens in a new tab).

本文介绍如何使用Langchain与DeepInfra (opens in a new tab)进行交互。

Imports#

import os
from langchain.llms import DeepInfra
from langchain import PromptTemplate, LLMChain

设置环境变量的API Key#

请确保从DeepInfra获取API Key。您必须登录 (opens in a new tab)并获取新令牌。

您将获得1个小时的免费服务器级GPU计算时间,以测试不同的模型(请参见此处 (opens in a new tab))。

您可以使用 deepctl auth token 命令打印您的令牌。

# get a new token: https://deepinfra.com/login?from=%2Fdash

from getpass import getpass

DEEPINFRA_API_TOKEN = getpass()
os.environ["DEEPINFRA_API_TOKEN"] = DEEPINFRA_API_TOKEN

创建DeepInfra实例#

确保先通过deepctl deploy create -m google/flat-t5-xl部署模型(参见此处 (opens in a new tab)

llm = DeepInfra(model_id="DEPLOYED MODEL ID")

创建提示模板#

我们将为问题和答案创建提示模板。

template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])

启动LLMChain#

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

运行LLMChain#

提供一个问题并运行LLMChain。

question = "What NFL team won the Super Bowl in 2015?"

llm_chain.run(question)