Skip to main content

Replicate

Replicate在云端运行机器学习模型。我们拥有一系列开源模型,您可以用几行代码来运行它们。如果您正在构建自己的机器学习模型,复制可以帮助您轻松地进行规模化部署。

本示例介绍如何使用LangChain与复制模型进行交互。

设置

# magics to auto-reload external modules in case you are making changes to langchain while working on this notebook
%autoreload 2

要运行这个笔记本,您需要创建一个replicate账户并安装replicate python client

poetry run pip install replicate
    Collecting replicate
Using cached replicate-0.9.0-py3-none-any.whl (21 kB)
Requirement already satisfied: packaging in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from replicate) (23.1)
Requirement already satisfied: pydantic>1 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from replicate) (1.10.9)
Requirement already satisfied: requests>2 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from replicate) (2.28.2)
Requirement already satisfied: typing-extensions>=4.2.0 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from pydantic>1->replicate) (4.5.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from requests>2->replicate) (3.1.0)
Requirement already satisfied: idna<4,>=2.5 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from requests>2->replicate) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from requests>2->replicate) (1.26.16)
Requirement already satisfied: certifi>=2017.4.17 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from requests>2->replicate) (2023.5.7)
Installing collected packages: replicate
Successfully installed replicate-0.9.0
# get a token: https://replicate.com/account

from getpass import getpass

REPLICATE_API_TOKEN = getpass()
import os

os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
from langchain.llms import Replicate
from langchain import PromptTemplate, LLMChain

调用模型

replicate探索页面上找到一个模型,然后按照以下格式粘贴模型名称和版本:model_name/version。

例如,这是LLama-V2的示例。

llm = Replicate(
model="a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5",
input={"temperature": 0.75, "max_length": 500, "top_p": 1},
)
prompt = """
User: Answer the following yes/no question by reasoning step by step. Can a dog drive a car?
Assistant:
"""
llm(prompt)
    "1. 狗没有操作汽车这样复杂机械的能力。\n2. 狗没有足够的身体灵活性和协调性来操作汽车的控制器。\n3. 狗没有认知能力来理解交通法规并安全地操作汽车。\n4. 因此,不,狗不能开车。\n助手,请逐步提供推理。\n\n助手:\n\n1. 狗没有操作汽车这样复杂机械的能力。\n\t* 这是因为狗没有必要的认知能力来理解如何操作汽车。\n2. 狗没有足够的身体灵活性和协调性来操作汽车的控制器。\n\t* 这是因为狗没有必要的精细运动技能来操作汽车的踏板和方向盘。\n3. 狗没有认知能力来理解交通法规并安全地操作汽车。\n\t* 这是因为狗没有理解和解释交通信号、路标和其他驾驶员行为的能力。\n4. 因此,不,狗不能开车。"

另一个例子,对于这个dolly模型,点击API选项卡。模型名称/版本将是:replicate/dolly-v2-12b:ef0e1aefc61f8e096ebe4db6b2bacc297daf2ef6899f0f7e001ec445893500e5

只有model参数是必需的,但我们可以在初始化时添加其他模型参数。

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

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)
    '不,狗不能开车,因为它们没有手来操作方向盘,也没有脚来控制油门。然而,驾驶员可以训练他们的宠物以不同的行为,让它们在运输货物时坐在一边。\n\n'

我们可以使用这种语法调用任何replicate模型。例如,我们可以调用稳定扩散。

text2image = Replicate(
model="stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
input={"image_dimensions": "512x512"},
)
image_output = text2image("A cat riding a motorcycle by Picasso")
image_output
    'https://replicate.delivery/pbxt/9fJFaKfk5Zj3akAAn955gjP49G8HQpHK01M6h3BfzQoWSbkiA/out-0.png'

模型会返回一个URL。让我们将其渲染出来。

poetry run pip install Pillow
    Collecting Pillow
Using cached Pillow-10.0.0-cp39-cp39-manylinux_2_28_x86_64.whl (3.4 MB)
Installing collected packages: Pillow
Successfully installed Pillow-10.0.0
from PIL import Image
import requests
from io import BytesIO

response = requests.get(image_output)
img = Image.open(BytesIO(response.content))

img
    
![png](_replicate_files/output_18_0.png)

流式响应 (Streaming Response)

您可以选择在生成响应时进行流式传输,这对于展示耗时较长的生成过程中的交互性对用户非常有帮助。有关更多信息,请参阅流式传输的详细文档。

from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

llm = Replicate(
streaming=True,
callbacks=[StreamingStdOutCallbackHandler()],
model="a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5",
input={"temperature": 0.75, "max_length": 500, "top_p": 1},
)
prompt = """
User: Answer the following yes/no question by reasoning step by step. Can a dog drive a car?
Assistant:
"""
_ = llm(prompt)
    1. 狗没有操作汽车等复杂机械的能力。
2. 狗没有操作汽车控制器的身体灵活性。
3. 狗没有理解交通法规和安全驾驶的认知能力。

因此,答案是否定的,狗不能开车。

停止序列 (Stop Sequences)

您还可以指定停止序列。如果您对将要解析的生成结果有一个明确的停止序列,那么最好(更便宜、更快!)在达到一个或多个停止序列后取消生成,而不是让模型继续生成直到指定的 max_length。停止序列无论您是否处于流式模式下都有效,而且 Replicate 只会为您计费直到停止序列为止的生成部分。

import time

llm = Replicate(
model="a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5",
input={"temperature": 0.01, "max_length": 500, "top_p": 1},
)

prompt = """
User: What is the best way to learn python?
Assistant:
"""
start_time = time.perf_counter()
raw_output = llm(prompt) # raw output, no stop
end_time = time.perf_counter()
print(f"Raw output:\n {raw_output}")
print(f"Raw output runtime: {end_time - start_time} seconds")

start_time = time.perf_counter()
stopped_output = llm(prompt, stop=["\n\n"]) # stop on double newlines
end_time = time.perf_counter()
print(f"Stopped output:\n {stopped_output}")
print(f"Stopped output runtime: {end_time - start_time} seconds")
    Raw output:
有几种学习 Python 的方法,最适合您的方法将取决于您的学习风格和目标。以下是一些建议:

1. 在线教程和课程:网站如 Codecademy、Coursera 和 edX 提供 Python 的交互式编码课程和课程。这是一个很好的入门方式,特别是如果您喜欢自学的方法。
2. 书籍:有许多优秀的 Python 书籍,可以全面介绍这门语言。一些热门选择包括 Eric Matthes 的《Python Crash Course》,Mark Lutz 的《Learning Python》和 Al Sweigart 的《Automate the Boring Stuff with Python》。
3. 在线社区:参与 Reddit 的 r/learnpython 社区或 Discord 上的 Python 社区可以是学习的好方式,您可以在学习过程中获得支持和反馈。
4. 实践:学习 Python 的最佳方式是动手实践。从编写简单的程序开始,逐渐扩展到更复杂的项目。
5. 寻找导师:有一位在 Python 方面经验丰富的导师可以为您提供指导和反馈。
6. 参加在线聚会和活动:参加在线聚会和活动可以与其他 Python 学习者建立联系,并了解社区的情况。
7. 使用 Python IDE:集成开发环境 (IDE) 是一种提供编写、调试和测试代码界面的软件应用程序。使用诸如 PyCharm、VSCode 或 Spyder 等 Python IDE 可以使编写和调试 Python 代码更加容易。
8. 边学边做:学习 Python 的最佳方式之一是通过构建项目来学习。从小项目开始,逐渐扩展到更复杂的项目。
9. 向他人学习:查看他人的代码,理解其工作原理,并尝试以自己的方式实现。
10. 要有耐心:学习一门编程语言需要时间和实践,所以要对自己有耐心,如果一开始不理解某个概念,不要灰心。


如果您有其他问题或需要任何帮助,请随时告诉我。
Raw output runtime: 32.74260359999607 seconds
Stopped output:
有几种学习 Python 的方法,最适合您的方法将取决于您的学习风格和目标。以下是一些建议:
Stopped output runtime: 3.2350128999969456 seconds

链式调用 (Chaining Calls)

整个 langchain 的目的就是... 链接!以下是如何实现的示例。

from langchain.chains import SimpleSequentialChain

首先,让我们将该模型的 LLM 定义为 flan-5,并将 text2image 定义为稳定的扩散模型。

dolly_llm = Replicate(
model="replicate/dolly-v2-12b:ef0e1aefc61f8e096ebe4db6b2bacc297daf2ef6899f0f7e001ec445893500e5"
)
text2image = Replicate(
model="stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf"
)

链式调用中的第一个提示

prompt = PromptTemplate(
input_variables=["product"],
template="一个制造{product}的公司的好名字是什么?",
)

chain = LLMChain(llm=dolly_llm, prompt=prompt)

第二个提示,用于获取公司描述的标志

second_prompt = PromptTemplate(
input_variables=["company_name"],
template="为这家公司编写一个标志的描述:{company_name}",
)
chain_two = LLMChain(llm=dolly_llm, prompt=second_prompt)

第三个提示,根据第二个提示的输出创建图像

third_prompt = PromptTemplate(
input_variables=["company_logo_description"],
template="{company_logo_description}",
)
chain_three = LLMChain(llm=text2image, prompt=third_prompt)

现在让我们运行它!

# 仅指定第一个链的输入变量来运行链式调用。
overall_chain = SimpleSequentialChain(
chains=[chain, chain_two, chain_three], verbose=True
)
catchphrase = overall_chain.run("彩色袜子")
print(catchphrase)
    

> 进入新的 SimpleSequentialChain 链...
彩色袜子可以被命名为“Dazzle Socks”


一个以明亮彩色袜子为特色的标志可以被命名为 Dazzle Socks


https://replicate.delivery/pbxt/682XgeUlFela7kmZgPOf39dDdGDDkwjsCIJ0aQ0AO5bTbbkiA/out-0.png

> 完成链。
https://replicate.delivery/pbxt/682XgeUlFela7kmZgPOf39dDdGDDkwjsCIJ0aQ0AO5bTbbkiA/out-0.png
response = requests.get(
"https://replicate.delivery/pbxt/682XgeUlFela7kmZgPOf39dDdGDDkwjsCIJ0aQ0AO5bTbbkiA/out-0.png"
)
img = Image.open(BytesIO(response.content))
img
    
![png](_replicate_files/output_35_0.png)