Skip to main content

ForefrontAI

Forefront 平台使您能够微调和使用开源大型语言模型。

本笔记本介绍如何在 ForefrontAI 中使用 Langchain。

导入​

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

设置环境 API 密钥​

请确保从 ForefrontAI 获取您的 API 密钥。您将获得 5 天的免费试用期来测试不同的模型。

# 获取新的令牌:https://docs.forefront.ai/forefront/api-reference/authentication

from getpass import getpass

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

创建 ForefrontAI 实例​

您可以指定不同的参数,如模型端点 URL、长度、温度等。您必须提供一个端点 URL。

llm = ForefrontAI(endpoint_url="在此处填入您的端点 URL")

创建 Prompt 模板​

我们将为问题和答案创建一个 Prompt 模板。

template = """问题:{question}

答案:让我们一步一步地思考。"""

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

初始化 LLMChain​

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

运行 LLMChain​

提供一个问题并运行 LLMChain。

question = "贾斯汀·比伯出生的那一年,哪个 NFL 球队赢得了超级碗?"

llm_chain.run(question)