学习目标 #
1.了解流媒体的工作原理
2.使用流事件
让我们首先导入 anthropic SDK 并设置我们的客户端:
from dotenv import load_dotenv
from anthropic import Anthropic
#load environment variable
load_dotenv()
#automatically looks for an "ANTHROPIC_API_KEY" environment variable
client = Anthropic()
到目前为止,我们已经使用以下语法向 Claude 发送了消息:
response = client.messages.create(
messages=[
{
"role": "user",
"content": "Write me an essay about macaws and clay licks in the Amazon",
}
],
model="claude-3-haiku-20240307",
max_tokens=800,
temperature=0,
)
print("We have a response back!")
print("========================")
print(response.content[0].text)
We have a response back!
========================
Here is an essay about macaws and clay licks in the Amazon:
Macaws and Clay Licks in the Amazon
Deep within the lush, verdant rainforests of the Amazon basin, a remarkable natural phenomenon takes place. Flashes of vibrant color dart through the canopy, as large, magnificent parrots known as macaws congregate at special sites called clay licks. These clay licks, or "collpas" as they are known locally, are essential to the survival and well-being of macaws and other Amazonian wildlife.
Macaws are some of the most striking and iconic birds of the Amazon. With their vividly-hued plumage, hooked beaks, and long, tapered tails, these large parrots are a sight to behold as they soar through the treetops. The most well-known species include the scarlet macaw, the blue-and-gold macaw, and the green-winged macaw, each adorned in a stunning array of reds, blues, greens, and golds.
These magnificent birds play a crucial role in the Amazon ecosystem, acting as important seed dispersers as they feed on a variety of fruits and nuts. However, macaws face a unique dietary challenge - they require essential minerals and nutrients that are often lacking in the fruits and seeds that make up their primary diet. This is where the clay licks come into play.
Clay licks are exposed deposits of mineral-rich clay that attract hundreds, sometimes thousands, of macaws and other parrots, as well as other Amazonian wildlife such as tapirs, peccaries, and monkeys. The clay contains high concentrations of sodium, calcium, and other vital minerals that help macaws and other animals to regulate their digestive systems, neutralize toxins, and maintain overall health.
Visiting a clay lick is a truly awe-inspiring experience. As the first rays of dawn break through the forest canopy, the air fills with the raucous squawks and screeches of macaws as they begin to gather at the lick. Slowly, the birds will descend from the treetops, cautiously approaching the clay bank and taking turns nibbling at the mineral-rich soil. The sheer number of these vibrant birds, their colors contrasting against the earthy tones of the clay, creates a mesmerizing natural spectacle.
The importance of clay licks to the survival of macaws and other Amazonian wildlife cannot be overstated. These unique geological formations are essential feeding grounds that help sustain the delicate balance of the rainforest ecosystem. As deforestation and habitat loss continue to threaten the Amazon, the protection and preservation of these clay licks has become increasingly crucial to the long-term conservation of macaws and the broader biodiversity of this remarkable region.
这种方法很好用,但需要记住的是,使用这种方法时,我们只能在生成所有内容后才能从 API 中获取内容。尝试再次运行上述单元格,您会发现在一次性打印出整个响应之前不会打印出任何内容。这在许多情况下都很好,但如果您构建的应用程序迫使用户等待生成整个响应,则可能导致糟糕的用户体验。
进入流式传输!
流式传输使我们能够编写在模型生成内容时接收内容的应用程序,而不必等待生成整个响应。这就是 claude.ai 等应用程序的工作方式。当模型生成响应时,该内容将流式传输到用户的浏览器并显示:
使用流 #
要从 API 获取流式响应,只需将 stream=True 传递给 client.messages.create 即可。这是最简单的部分。事情变得有点棘手的是,我们随后如何处理流式响应并处理传入数据。
此内容仅限注册用户查看,请先登录
加微信:tianming608,加入大模型学习交流群,获取最新技术和创业经验。