跳到主要内容
Forefront 平台使您能够微调和使用开源大型语言模型 本笔记本将介绍如何将 LangChain 与 ForefrontAI 结合使用。

导入

import os

from langchain.chains import LLMChain
from langchain_community.llms import ForefrontAI
from langchain_core.prompts import PromptTemplate

设置环境变量 API 密钥

请务必从 ForefrontAI 获取您的 API 密钥。您将获得 5 天的免费试用期来测试不同的模型。
# get a new token: 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="YOUR ENDPOINT URL HERE")

创建提示模板

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

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

prompt = PromptTemplate.from_template(template)

初始化 LLMChain

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

运行 LLMChain

提供一个问题并运行 LLMChain。
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"

llm_chain.run(question)

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.