模块
LLMS
Forefrontai Example

ForefrontAI#

Forefront 平台可让您微调和使用开源大型语言模型 (opens in a new tab)

本笔记本将介绍如何使用 Langchain 和ForefrontAI (opens in a new tab)

导入#

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

设置环境 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(template=template, input_variables=["question"])

启动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)