使用 Anthropic 的 Claude Sonnet 產生報告
利用Anthropic的Claude 3.5 Sonnet產生報表:兩種方法的比較
大家好!我是Raphael,巴西房地產公司Pilar的共同創辦人兼CTO。 Pilar為房地產經紀人和經紀公司提供軟體和服務,採用低成功費模式。我們不收取高昂的前期費用,而是從每次成功的交易中收取少量佣金,使我們的成功直接與客戶的成功掛鉤。我們由20位技術人員組成的團隊不斷創新,最新產品是Pilar Homes,一個全新的房地產入口網站,旨在為購屋者和房地產經紀人提供最佳體驗。
在這篇文章中,我將分享我們使用人工智慧產生報告的經驗,特別是Anthropic的Claude 3.5 Sonnet,並比較兩種不同的方法。
我們處理任務的理念將在未來的文章中詳細介紹(敬請關注!),但簡而言之,這些任務最終以Jira工單的形式出現在「技術服務台」看板上。產生報告就是這樣一項任務,大多數任務需要工程師花費大約30分鐘來解決,複雜報告很少超過幾個小時。但情況正在改變。我們最初只與一兩個合作夥伴合作的精品品牌正在擴張,成為更大的經紀公司,我們也與業內老牌公司簽訂了更多合約。雖然增加工程師的工作時間可以解決日益增長的報告需求,但我看到了探索人工智慧代理並在現實環境中學習架構模式的機會。
方法一:讓AI全權處理並達到max_tokens限制
在我們的初始方法中,我們將工具暴露給Claude的3.5 Sonnet模型,使其能夠執行資料庫查詢、將檢索到的文檔轉換為CSV並將其結果寫入.csv檔案。
以下是我們的結構,很大程度上受到了上面部落格文章的啟發:
<code># 每个collection对象描述一个MongoDB集合及其字段 # 这有助于Claude理解我们的数据模式 COLLECTIONS = [ { 'name': 'companies', 'description': 'Companies are the real estate brokerages. If the user provides a code to filter the data, it will be a company code. The _id may be retrieved by querying the company with the given code. Company codes are not used to join data.', 'fields': { '_id': 'The ObjectId is the MongoDB id that uniquely identifies a company document. Its JSON representation is \"{"$oid": "the id"}\"', 'code': 'The company code is a short and human friendly string that uniquely identifies the company. Never use it for joining data.', 'name': 'A string representing the company name', } }, # 此处之后描述了更多集合,但思路相同... ] # 这是client.messages.create的“system”参数 ROLE_PROMPT = "You are an engineer responsible for generating reports in CSV based on a user's description of the report content" # 这是“user”消息 task_prompt = f"{report_description}.\nAvailable collections: {COLLECTIONS}\nCompany codes: {company_codes}\n.Always demand a company code from the user to filter the data -- the user may use the terms imobiliária, marca, brand or company to reference a company. If the user wants a field that does not exist in a collection, don't add it to the report and don't ask the user for the field." </code>
report_description只是一個透過argparse讀取的命令列參數,company_codes是從資料庫中檢索到的,並將其暴露給模型,以便它知道哪些公司存在以及用戶輸入中什麼是公司代碼。範例:(MO - Mosaic Homes,NV - Nova Real Estate,等等)。
模型可用的工具包括:find和docs2csv。
<code>def find(collection: str, query: str, fields: list[str]) -> Cursor: """Find documents in a collection filtering by "query" and retrieving fields via projection""" return db.get_collection(collection).find(query, projection={field: 1 for field in fields}) def docs2csv(documents: list[dict]) -> list[str]: """ Convert a dictionary to a CSV string. """ print(f"Converting {len(documents)} documents to CSV") with open('report.csv', mode='w', encoding='utf-8') as file: writer = csv.DictWriter(file, fieldnames=documents[0].keys()) writer.writeheader() writer.writerows(documents) return "report.csv"</code>
Claude能夠呼叫find函數對我們的資料庫執行結構良好的查詢和投影,並使用docs2csv工具產生小型CSV報告(少於500行)。但是,較大的報告會觸發max_tokens錯誤。
在分析了我們的令牌使用模式後,我們意識到大部分令牌消耗都來自透過模型處理單一記錄。這促使我們探索另一種方法:讓Claude產生處理程式碼,而不是直接處理資料。
方法二:Python程式碼產生作為解決方法
雖然解決max_tokens限制在技術上並不困難,但它需要我們重新思考解決問題的方法。
解決方案?讓Claude產生將在我們的CPU上運行的Python程式碼,而不是透過AI處理每個文件。
我必須修改角色和任務提示並刪除工具。
以下是報告產生程式碼的要點。
產生報告的命令是:
<code># 每个collection对象描述一个MongoDB集合及其字段 # 这有助于Claude理解我们的数据模式 COLLECTIONS = [ { 'name': 'companies', 'description': 'Companies are the real estate brokerages. If the user provides a code to filter the data, it will be a company code. The _id may be retrieved by querying the company with the given code. Company codes are not used to join data.', 'fields': { '_id': 'The ObjectId is the MongoDB id that uniquely identifies a company document. Its JSON representation is \"{"$oid": "the id"}\"', 'code': 'The company code is a short and human friendly string that uniquely identifies the company. Never use it for joining data.', 'name': 'A string representing the company name', } }, # 此处之后描述了更多集合,但思路相同... ] # 这是client.messages.create的“system”参数 ROLE_PROMPT = "You are an engineer responsible for generating reports in CSV based on a user's description of the report content" # 这是“user”消息 task_prompt = f"{report_description}.\nAvailable collections: {COLLECTIONS}\nCompany codes: {company_codes}\n.Always demand a company code from the user to filter the data -- the user may use the terms imobiliária, marca, brand or company to reference a company. If the user wants a field that does not exist in a collection, don't add it to the report and don't ask the user for the field." </code>
Claude產生的Python內容(運作良好):
<code>def find(collection: str, query: str, fields: list[str]) -> Cursor: """Find documents in a collection filtering by "query" and retrieving fields via projection""" return db.get_collection(collection).find(query, projection={field: 1 for field in fields}) def docs2csv(documents: list[dict]) -> list[str]: """ Convert a dictionary to a CSV string. """ print(f"Converting {len(documents)} documents to CSV") with open('report.csv', mode='w', encoding='utf-8') as file: writer = csv.DictWriter(file, fieldnames=documents[0].keys()) writer.writeheader() writer.writerows(documents) return "report.csv"</code>
結論
我們與Claude 3.5 Sonnet的歷程表明,人工智慧可以顯著提高營運效率,但成功的關鍵在於選擇正確的架構。程式碼產生方法被證明比直接的AI處理更強大,同時保持了自動化的優勢。
除了正確建立報告外,程式碼產生方法還允許工程師審查AI的工作,這是一件非常好的事情。
為了完全自動化流程,消除人工參與並處理更大數量的報告,跨多個代理實例分配工作——每個實例處理更少的令牌——將是該系統的自然演變。對於這類分散式AI系統中的架構挑戰,我強烈推薦Phil Calçado關於建構AI產品的最新文章。
此實現的主要經驗教訓:
- 直接AI處理適用於較小的資料集
- 程式碼產生提供更好的可擴充性和可維護性
- 人工審查增加了可靠性
參考文獻
- Anthropic 文件
- Thomas Taylor 使用 Python SDK 的帶工具的 Anthropic Claude
- Phil Calçado 所寫的建構 AI 產品-第一部分:後端架構
以上是使用 Anthropic 的 Claude Sonnet 產生報告的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。
