Skip to main content
Gradient 允许通过简单的 Web API 对 LLM 进行微调并获取补全结果。 本 notebook 介绍如何将 LangChain 与 Gradient 配合使用。

导入

from langchain_classic.chains import LLMChain
from langchain_community.llms import GradientLLM
from langchain_core.prompts import PromptTemplate

设置环境 API 密钥

确保从 Gradient AI 获取您的 API 密钥。您将获得 $10 的免费额度用于测试和微调不同模型。
import os
from getpass import getpass

if not os.environ.get("GRADIENT_ACCESS_TOKEN", None):
    # Access token under https://auth.gradient.ai/select-workspace
    os.environ["GRADIENT_ACCESS_TOKEN"] = getpass("gradient.ai access token:")
if not os.environ.get("GRADIENT_WORKSPACE_ID", None):
    # `ID` listed in `$ gradient workspace list`
    # also displayed after login at at https://auth.gradient.ai/select-workspace
    os.environ["GRADIENT_WORKSPACE_ID"] = getpass("gradient.ai workspace id:")
可选:验证您的环境变量 GRADIENT_ACCESS_TOKENGRADIENT_WORKSPACE_ID,以获取当前已部署的模型。使用 gradientai Python 包。
pip install -qU  gradientai
Requirement already satisfied: gradientai in /home/michi/.venv/lib/python3.10/site-packages (1.0.0)
Requirement already satisfied: aenum>=3.1.11 in /home/michi/.venv/lib/python3.10/site-packages (from gradientai) (3.1.15)
Requirement already satisfied: pydantic<2.0.0,>=1.10.5 in /home/michi/.venv/lib/python3.10/site-packages (from gradientai) (1.10.12)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/michi/.venv/lib/python3.10/site-packages (from gradientai) (2.8.2)
Requirement already satisfied: urllib3>=1.25.3 in /home/michi/.venv/lib/python3.10/site-packages (from gradientai) (1.26.16)
Requirement already satisfied: typing-extensions>=4.2.0 in /home/michi/.venv/lib/python3.10/site-packages (from pydantic<2.0.0,>=1.10.5->gradientai) (4.5.0)
Requirement already satisfied: six>=1.5 in /home/michi/.venv/lib/python3.10/site-packages (from python-dateutil>=2.8.2->gradientai) (1.16.0)
import gradientai

client = gradientai.Gradient()

models = client.list_models(only_base=True)
for model in models:
    print(model.id)
99148c6d-c2a0-4fbe-a4a7-e7c05bdb8a09_base_ml_model
f0b97d96-51a8-4040-8b22-7940ee1fa24e_base_ml_model
cc2dafce-9e6e-4a23-a918-cad6ba89e42e_base_ml_model
new_model = models[-1].create_model_adapter(name="my_model_adapter")
new_model.id, new_model.name
('674119b5-f19e-4856-add2-767ae7f7d7ef_model_adapter', 'my_model_adapter')

创建 Gradient 实例

您可以指定不同的参数,如模型、最大生成 token 数、温度等。 由于我们稍后希望对模型进行微调,因此选择 id 为 674119b5-f19e-4856-add2-767ae7f7d7ef_model_adapter 的模型适配器,但您可以使用任何基础或可微调的模型。
llm = GradientLLM(
    # `ID` listed in `$ gradient model list`
    model="674119b5-f19e-4856-add2-767ae7f7d7ef_model_adapter",
    # # optional: set new credentials, they default to environment variables
    # gradient_workspace_id=os.environ["GRADIENT_WORKSPACE_ID"],
    # gradient_access_token=os.environ["GRADIENT_ACCESS_TOKEN"],
    model_kwargs=dict(max_generated_token_count=128),
)

创建 Prompt 模板

我们将创建一个问答 Prompt 模板。
template = """Question: {question}

Answer: """

prompt = PromptTemplate.from_template(template)

初始化 LLMChain

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

运行 LLMChain

提供问题并运行 LLMChain。
question = "What NFL team won the Super Bowl in 1994?"

llm_chain.run(question=question)
'\nThe San Francisco 49ers won the Super Bowl in 1994.'

通过微调改善结果(可选)

嗯——这个答案是错误的,旧金山 49 人队并没有获胜。 正确答案应该是 达拉斯牛仔队(The Dallas Cowboys)! 让我们通过使用 PromptTemplate 对正确答案进行微调,来提高正确答案的概率。
dataset = [
    {
        "inputs": template.format(question="What NFL team won the Super Bowl in 1994?")
        + " The Dallas Cowboys!"
    }
]
dataset
[{'inputs': 'Question: What NFL team won the Super Bowl in 1994?\n\nAnswer:  The Dallas Cowboys!'}]
new_model.fine_tune(samples=dataset)
FineTuneResponse(number_of_trainable_tokens=27, sum_loss=78.17996)
# we can keep the llm_chain, as the registered model just got refreshed on the gradient.ai servers.
llm_chain.run(question=question)
'The Dallas Cowboys'