Clarifai
Clarifai 是一个人工智能平台,提供完整的人工智能生命周期,包括数据探索、数据标注、模型训练、评估和推理。
本示例介绍如何使用 LangChain 与 Clarifai
模型 进行交互。
要使用 Clarifai,您必须拥有一个账户和一个个人访问令牌 (PAT) 密钥。 在这里查看 以获取或创建 PAT。
依赖项
# Install required dependencies
%pip install --upgrade --quiet clarifai
# Declare clarifai pat token as environment variable or you can pass it as argument in clarifai class.
import os
os.environ["CLARIFAI_PAT"] = "CLARIFAI_PAT_TOKEN"
导入
在这里我们将设置个人访问令牌。您可以在您的Clarifai帐户的设置/安全中找到您的PAT。
# Please login and get your API key from https://clarifai.com/settings/security
from getpass import getpass
CLARIFAI_PAT = getpass()
<!--IMPORTS:[{"imported": "LLMChain", "source": "langchain.chains", "docs": "https://python.langchain.com/api_reference/langchain/chains/langchain.chains.llm.LLMChain.html", "title": "Clarifai"}, {"imported": "Clarifai", "source": "langchain_community.llms", "docs": "https://python.langchain.com/api_reference/community/llms/langchain_community.llms.clarifai.Clarifai.html", "title": "Clarifai"}, {"imported": "PromptTemplate", "source": "langchain_core.prompts", "docs": "https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.prompt.PromptTemplate.html", "title": "Clarifai"}]-->
# Import the required modules
from langchain.chains import LLMChain
from langchain_community.llms import Clarifai
from langchain_core.prompts import PromptTemplate
输入
创建一个提示词模板以用于LLM链:
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
设置
设置模型所在的用户ID和应用ID。您可以在https://clarifai.com/explore/models上找到公共模型的列表。
您还需要初始化模型ID,如果需要,还要初始化模型版本ID。一些模型有多个版本,您可以选择适合您任务的版本。
或者,您可以使用model_url(例如:“https://clarifai.com/anthropic/completion/models/claude-v2”)进行初始化。
USER_ID = "openai"
APP_ID = "chat-completion"
MODEL_ID = "GPT-3_5-turbo"
# You can provide a specific model version as the model_version_id arg.
# MODEL_VERSION_ID = "MODEL_VERSION_ID"
# or
MODEL_URL = "https://clarifai.com/openai/chat-completion/models/GPT-4"
# Initialize a Clarifai LLM
clarifai_llm = Clarifai(user_id=USER_ID, app_id=APP_ID, model_id=MODEL_ID)
# or
# Initialize through Model URL
clarifai_llm = Clarifai(model_url=MODEL_URL)
# Create LLM chain
llm_chain = LLMChain(prompt=prompt, llm=clarifai_llm)
运行链
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"
llm_chain.run(question)
' Okay, here are the steps to figure this out:\n\n1. Justin Bieber was born on March 1, 1994.\n\n2. The Super Bowl that took place in the year of his birth was Super Bowl XXVIII. \n\n3. Super Bowl XXVIII was played on January 30, 1994.\n\n4. The two teams that played in Super Bowl XXVIII were the Dallas Cowboys and the Buffalo Bills. \n\n5. The Dallas Cowboys defeated the Buffalo Bills 30-13 to win Super Bowl XXVIII.\n\nTherefore, the NFL team that won the Super Bowl in the year Justin Bieber was born was the Dallas Cowboys.'