Anthropic Claude 教程:如何用 Claude 总结 PDF 文件

Anthropic Claude 教程:如何用 Claude 总结 PDF 文件

什么是克劳德?

Claude 是一个大型语言模型,由 Anthropic 创建。Claude 可以作为聊天机器人、摘要工具、代码编写等为您提供帮助。最近 Anthropic 宣布 Claude 将其上下文大小增加到 100k 标记,即大约 75000 个单词!这是一个大容量,可以让许多人加快处理大型文档和书籍的速度。以前,仅阅读这么长的文本可能需要大约 5 小时。现在模型将能够在几分钟内阅读、总结、分析文本并回答问题!Anthropic 的 Claud 也主要关注安全性。用户还声称,与他们的 LLM 互动会带来更多人性化的感觉。也许新的领导者出现了,我们将在不久的将来都使用 Anthropic Apps?

也许吧,但首先测试我们的目的并检查一下

如何使用它?

要使用 Claude,您必须申请抢先体验!

今天我将使用 Anthropic Python SDK 来让我们更轻松地使用模型。您还可以使用 API 或 TypeScript/JavaScript SDK。

Claude账号注册方法

总结

文件

出于本教程的目的,我想使用书籍 – “小王子”和“老人与海”。这些书没有那么多的标记(24815 和 40394),但它们仍然很长。我们将看看克劳德如何处理它们。这些文件的格式是.pdf为了处理它们,他将使用 PDF 阅读器。

依赖关系

让我们从创建一个新目录和虚拟环境开始。

mkdir claude_tutorialcd claude_tutorialpython3 -m venv venv# Linux/MacOSsource venv/bin/activate# Windowsvenv\\Scripts\\activate.bat

出于本教程的目的,我们将使用 PyPDF2 和 Anthropic SDK。让我们安装它们!

pip install PyPDF2 pycryptodome # PyPDF2 and ycryptodome are used to read PDF filespip install anthropic # Anthropic SDK

现在是时候导入必要的库了。

import osfrom PyPDF2 import PdfReaderimport anthropic

此外,我还有一个 API 密钥,是我从早期访问中获得的。

API_KEY = "sk-ant-..."anthropic_client = anthropic.Client(API_KEY)

用法

为了有效地使用 Claude,我将创建一个函数来总结给定的 PDF 文件。我们会提供文件的路径,然后读取它,检查文本的长度,如果没问题,然后将它发送到 API 并进行汇总。所以让我们开始吧!

def summarize_pdf(path: str) -> str:    reader = PdfReader(path)    text = "\\n".join([page.extract_text() for page in reader.pages])    no_tokens = anthropic.count_tokens(text)    print(f"Number of tokens in text: {no_tokens}")    if no_tokens > 100000:        raise ValueError(f"Text is too long {no_tokens}.")    prompt = f"{anthropic.HUMAN_PROMPT}: Summarize the following text, should be readable:\\n\\n{text}\\n\\n{anthropic.AI_PROMPT}:\\n\\nSummary"    res = anthropic_client.completion(prompt=prompt, model="claude-v1.3-100k", max_tokens_to_sample=1000)    return res["completion"]

现在我们可以用它来总结我们的书了!

summary_little_prince = summarize_pdf(PATH_LITTLE_PRINCE)

结果

The story is narrated by an aviator whose plane crashes in the Sahara desert. There he meets a little boy who is a prince from a tiny asteroid. The little prince tells the aviator about his tiny planet, how he cared for his small rose and protected it from baobabs, and his adventures traveling the universe.The little prince visits six other asteroids, each inhabited by a single adult. He discovers the pompous and pointless life of each one, especially the businessman, geographer, and conceited man. He realizes how absurd and meaningless their lives are.He eventually lands on Earth, where he meets a snake, fox, flowers, and railway switchman. The fox teaches him that “One sees clearly only with the heart. What is essential is invisible to the eye.” The little prince realizes the fox is the only creature that truly understands him.A year later, the little prince returns to say goodbye to the fox before returning to his tiny planet. The little prince is worried that his beloved rose, left alone for so long, may have suffered in his absence. The aviator never learns whether the rose survived.The story explores themes of loneliness, friendship, love, and loss. Although a children's book, it has become popular with adults for its poetic style and profound insights into human nature.In the 2004 news article, it is revealed that wreckage of Saint-Exupery's plane that crashed in 1944 was found in the Mediterranean Sea. Although no body was recovered, authorities identified the plane based on its serial number. Saint-Exupery disappeared during a wartime spy mission, and the discovery of the wreckage provides some closure, although his mysterious end will still remain.
summary_old_man_and_the_sea = summarize_pdf(PATH_OLD_MAN_AND_THE_SEA)

结果

The story is set in 1940s Cuba. The main character is Santiago, an old fisherman who has been unable to catch any fish for 84 days. Despite being considered unlucky by the other fishermen, Santiago refuses to give up. He sails his skiff far out into the Gulf Stream where he hooks a giant marlin. After a prolonged struggle, he manages to harpoon the fish, but then has to fight off sharks in order to bring it home. By the time he does, all that remains is a stripped carcass of the once massive marlin tied to the side of his boat. Despite being ridiculed when he returns to land, the old man is able to retain his dignity and is left with a renewed sense of hope for the future.

结论

Anthropic Claude 教程:如何用 Claude 总结 PDF 文件

如您所见,这两个摘要大多是准确的。这意味着 Claude 能够处理大文本。我已经很期待它未来的样子了。

用 AI 塑造未来。

成为AIHubPro.cn的一部分

谢谢你!– AI未来百科 ; 探索AI的边界与未来! 懂您的AI未来站

声明:本站部分文章来源于网络,如有侵犯您的合法权益,请您即时与我们联系,我们将在第一时间处理。如需转载本站文章,请在转载时标明出处并保留原文链接,否则我们将保留追究法律责任的权利。

给TA打赏
共{{data.count}}人
人已打赏
AI教程

Chroma 教程:如何赋予 GPT-3.5 聊天机器人类似记忆的功能

2023-5-28 13:06:36

AI教程

AI 代理教程:如何使用和构建 AI 代理

2023-5-28 13:06:40

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧