Skip to main content
Open on GitHub

Replicate

本页面介绍如何在 LangChain 中运行 Replicate 上的模型。

安装与设置

  • 创建一个Replicate账号。获取您的API密钥并将其设置为环境变量(REPLICATE_API_TOKEN
  • 安装 Replicate Python客户端,使用 pip install replicate

调用一个模型

Replicate探索页面上找到一个模型,然后以以下格式粘贴模型名称和版本: owner-name/model-name:version

例如,对于这个dolly模型,点击API标签。模型名称/版本为:"replicate/dolly-v2-12b:ef0e1aefc61f8e096ebe4db6b2bacc297daf2ef6899f0f7e001ec445893500e5"

只需传递model参数,但也可以使用格式input={model_param: value, ...}传递其他任何模型参数

例如,如果我们正在运行稳定扩散,并希望更改图像尺寸:

Replicate(model="stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", input={'image_dimensions': '512x512'})

请注意,仅返回模型的第一个输出。 接下来,我们可以初始化我们的模型:

llm = Replicate(model="replicate/dolly-v2-12b:ef0e1aefc61f8e096ebe4db6b2bacc297daf2ef6899f0f7e001ec445893500e5")

运行它:

prompt = """
Answer the following yes/no question by reasoning step by step.
Can a dog drive a car?
"""
llm(prompt)

我们可以通过以下语法调用任何 Replicate 模型(不仅仅是语言模型)。例如,我们可以调用 Stable Diffusion:

text2image = Replicate(model="stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", input={'image_dimensions':'512x512'})

image_output = text2image("A cat riding a motorcycle by Picasso")