及時壓縮:帶有Python示例的指南
在人工智能的快速發展的景觀中,優化大型語言模型(LLMS)不僅在於突破可能的界限,而且還涉及確保效率和成本效益。
>
提示壓縮已成為一種重要技術,可以增強這些模型的性能,同時最大程度地減少計算費用。隨著新的研究幾乎每週都會出現,保持挑戰是具有挑戰性的,但是了解基本面是至關重要的。 >本文涵蓋了及時壓縮的基礎知識,討論了何時應該使用它,其在降低抹布管道中的成本中的重要性,並使用OpenAI的API。
如果您想了解更多信息,請在及時工程上查看本課程。什麼是提示壓縮?
提示壓縮是一種自然語言處理(NLP)中使用的技術,可通過減少其長度而不顯著改變輸出的質量和相關性來優化對LLM的輸入。由於查詢中令牌的數量對LLM性能的影響,因此這種優化至關重要。
令牌是文本LLMS使用的基本單元,根據語言模型的令牌代表單詞或子字。在提示中減少令牌數量是有益的,有時是由於幾個原因所必需的:令牌限制約束: 處理效率和降低成本:
提示壓縮通過減少令牌計數的同時保留提示的有效性來減輕這些問題。
>抹布管道將信息檢索與文本生成相結合,並且經常用於專門的聊天機器人和上下文理解至關重要的其他應用程序。這些管道通常需要廣泛的對話歷史或作為提示檢索文件,從而導致代幣計數和增加的費用。
在這種情況下,
>提示壓縮的適用性和局限性
>重要的是要注意,及時壓縮不是通用的解決方案,應明智地使用。例如,為會話上下文設計的助理模型,可能不會受益於積極的提示壓縮。
這些模型通常不會為每個令牌收費,並且具有集成的聊天摘要和內存功能來有效地管理對話歷史記錄,從而使壓縮冗餘。
>也必須注意的是,即使使用指控的模型,過度壓縮也可能導致細微差別或重要細節的損失。在減小大小和保持提示含義的完整性之間達到正確的平衡是關鍵。>提示壓縮如何工作?
>提示壓縮技術可以分為三種主要方法:知識蒸餾,編碼和過濾。每種技術都利用不同的優勢來優化LLMS提示的長度和效率。
>我們將討論這些技術中的每一種,但您可以在本文中找到一種更全面的方法:大型語言模型的有效提示方法:調查。在本文中,我將本文稱為“調查文件”。知識蒸餾
Hinton等人首先引入了機器學習領域的知識蒸餾。 (2015年),其中一個較小,更簡單的模型(學生)經過培訓以復制較大,更複雜的模型(老師)的行為。最初開發了該技術來解決訓練模型集合的計算挑戰。在迅速工程的背景下,知識蒸餾可用於壓縮提示而不是模型。
這是通過學習如何通過軟提示調整來壓縮LLM中的硬提示來實現的。有關詳細見解,請參閱調查文件的第3.1節和附錄A.1.1。
>編碼編碼方法將輸入文本轉換為向量,減少及時長度而不會丟失關鍵信息。這些向量捕獲了提示的基本含義,從而使LLM可以有效地處理較短的輸入。
>有趣的是,LLM精通其他語言,例如base64,可以在編碼中用於降低提示的令牌大小。例如,提示“將以下文本轉換為法語:你好,你好嗎?”在基本64中編碼的是“ vhjhbnnsyxrlihrozsbmb2xsb3dpbmcgdgv4dcb0b0bybgcmvuy2g6icdizwxsbywgag93ig93igfyzsb5b5b5b3unpw ==”。您可以嘗試提示您喜歡的LLM測試!
>令人驚訝的是,一些編碼技術也用於模型越獄,其中涉及操縱LLM以繞過其安全機制。有關編碼方法的更多詳細信息,請參見調查文件的第3.2節和附錄A.1.2。
過濾>前兩種方法試圖壓縮整個提示,而過濾技術的重點是消除不必要的零件以提高LLM的效率。
>過濾技術評估提示的不同部分的信息內容,並刪除冗餘信息,因為提示中的所有信息都對LLM有益。這可以在各個級別上完成,例如句子,短語或令牌。
的目標是僅保留提示中最相關的部分。在論文中,Li等人的選擇性背景。 (2023),研究人員使用自我信息指標來過濾冗餘信息。在論文llmlingua中:壓縮提示以加速大型語言模型的推理,Microsoft的研究人員將提示提示為關鍵組件,並動態調整每個部分的壓縮比。有關進一步閱讀,請參閱調查文件的第3.3節和附錄A.1.3。
如何在Python中實現提示壓縮
在本節中,我將實施並測試受歡迎並被認為是最先進的選擇性上下文算法。如果您只想測試該算法,則無需安裝任何內容,它已經在HuggingFace平台上託管。
>應用鏈接
在選擇性上下文Web應用程序中,您可以選擇要壓縮的提示(英語或簡化中文)的語言。您還可以設置壓縮率,然後選擇是否過濾句子,令牌或相位。
>>使用OpenAI API
實現和測試選擇性上下文現在,讓我們研究Python實施。我們還將使用GPT-3.5-Turbo-0125型號測試一些壓縮提示。 >
我們還需要從Spacy下載en_core_web_sm模型,可以使用以下命令來完成:
pip install selective-context
>現在我們需要初始化selectivecontext對象。我們可以為模型選擇Curie或GPT-2,然後為語言選擇EN或ZH。我將在此示例中使用gpt-2。
python -m spacy download en_core_web_sm
>
sc = SelectiveContext(model_type = ‘gpt-2’, lang = ‘en’)
原始段落和摘要
在下面,您可以看到我將使用的API調用 - 我們將在格式字符串中更改文本變量。首先,我們要求該模型總結原始的未壓縮段落:>通過檢查完成。我們可以看到原始提示為304代幣長,這是輸出:
如您所見,該模型很好地總結了未壓縮的段落。
context, reduced_content = sc(text, reduce_ratio = 0.5, reduce_level = ‘sent’)
>現在讓我們嘗試使用令牌還原版本。壓縮段落看起來像這樣:
from openai import OpenAI client = OpenAI(api_key = key) completion = client.chat.completions.create( model = “gpt-3.5-turbo-0125”, messages = [ {“role”: “user”, “content”: f”Summarize the following paragraph:\n {text}”} ] )
提示為162個令牌長,以下是模型摘要:
段未正確匯總該段落。關於changpt的細微差別不從積極的壓縮中受益。
>句子級減少
[Prompt compression is useful in scenarios such as advanced prompt engineering techniques and retrieval-augmented generation pipelines to reduce token counts and costs while preserving effectiveness and context. However, it is not a universal solution and should be used judiciously, as excessive compression can lead to a loss of important details. It is important to strike a balance between reducing size and maintaining the prompt's meaning.]
這是模型摘要:
[When Should Prompt CompProm compression particularly following Advanced prompt engineering Techniques like chainofthought prompting while highly lengthy prompts reach thousands tokensThis increases processing costs and may exceed token limits of certain modelsProm compression mit these token count while prompt effectivenessRetri-au generationRAG pipelines R combine information retrieval text specialized chat contextual understandingThese pipelines frequently involve providing extensive conversation histories retrieved as prompts leading high token counts increased expensesProm compression essential such essential context while costsApplic prompt compression It prompt universal judFor instance assistant models like ChatGPT designed convers contexts benefit aggressive prompt compressionThese models often do charge per token integrated chat summar memory features conversation history effectively compression redundantIt even working models charge per token excessive compression could nuance importantSt reducing size maintaining prompt’s meaning is]
此摘要不包含任何錯誤,但缺少抹布討論。
短語級還原
最後,讓我們測試短語級還原。提示為183個令牌長:
[Prompt compression is recommended following advanced prompt engineering techniques like chain of thought prompting, especially when dealing with lengthy prompts that reach thousands of tokens. This helps reduce processing costs and ensure that the prompt remains effective for models like RAG pipelines, which involve extensive conversation histories. Prompt compression is essential for maintaining context while keeping costs low, particularly for models like ChatGPT that charge per token and benefit from aggressive compression. However, excessive compression could lead to loss of important nuances, so it is important to strike a balance between reducing size and maintaining the prompt's meaning.]
[When Should We Use Prompt Compression? This increases processing times and costs and may exceed token limits of certain models. These pipelines frequently involve providing extensive conversation histories or retrieved documents as prompts, leading to high token counts and increased expenses. Prompt compression is essential in such cases to maintain essential context while minimizing costs. For instance, assistant models like ChatGPT, designed for conversational contexts, may not benefit from aggressive prompt compression. These models often do not charge per token and have integrated chat summarization and memory features to manage conversation history effectively, making compression redundant. ]
評估提示壓縮
通過比較不同壓縮級別的模型摘要的令牌計數和內容,我們可以看到迅速壓縮對模型輸出的影響:>
[Prompt compression is necessary when processing times, costs, and token limits may be exceeded in pipelines involving extensive conversation histories or retrieved documents as prompts. However, for certain models like ChatGPT, designed for conversational contexts, aggressive prompt compression may not be beneficial as they already have features to manage conversation history effectively without the need for compression.]
[When Should Prompt Compression Prompt compression particularly beneficial Advanced prompt engineering techniques Techniques like chainofthought prompting while highly lengthy prompts reach thousands tokens This increases processing costs and may exceed token limits of certain models Prompt compression these issues token count while the prompt's effectiveness Retrieval-augmented generation (RAG) pipelines RAG pipelines combine information retrieval text generation specialized chatbots contextual understanding These pipelines frequently involve providing extensive conversation histories or retrieved as prompts leading high token counts increased expenses Prompt compression essential such cases to maintain essential context while costs Applicability prompt compression It's For instance assistant models like ChatGPT designed conversational contexts may benefit aggressive prompt compression These models often do charge per token have integrated chat summarization memory features manage conversation history effectively making compression redundant It even working models charge per token excessive compression could nuance important details reducing size maintaining the prompt’s meaning is ]
壓縮級別
| 令牌級
162 |
>對Chatgpt的重要細微差別不從積極的壓縮中受益,並犯了錯誤。 |
|
>句子級
|
129
|
沒有犯任何錯誤,而是錯過了有關抹布管道的某些上下文。
|
|
| 183
| >與令牌級別類似,錯誤地指出,chatgpt受益於積極的壓縮。
| 總體而言,迅速壓縮可以顯著降低令牌計數,同時保留主要想法。但是,要避免失去重要的細微差別和背景是至關重要的。
以上是及時壓縮:帶有Python示例的指南的詳細內容。更多資訊請關注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)

Meta的Llama 3.2:多模式和移動AI的飛躍 Meta最近公佈了Llama 3.2,這是AI的重大進步,具有強大的視覺功能和針對移動設備優化的輕量級文本模型。 以成功為基礎

嘿,編碼忍者!您當天計劃哪些與編碼有關的任務?在您進一步研究此博客之前,我希望您考慮所有與編碼相關的困境,這是將其列出的。 完畢? - 讓&#8217

Shopify首席執行官TobiLütke最近的備忘錄大膽地宣布AI對每位員工的基本期望是公司內部的重大文化轉變。 這不是短暫的趨勢。這是整合到P中的新操作範式

本週的AI景觀:進步,道德考慮和監管辯論的旋風。 OpenAI,Google,Meta和Microsoft等主要參與者已經釋放了一系列更新,從開創性的新車型到LE的關鍵轉變

介紹 Openai已根據備受期待的“草莓”建築發布了其新模型。這種稱為O1的創新模型增強了推理能力,使其可以通過問題進行思考

介紹 想像一下,穿過美術館,周圍是生動的繪畫和雕塑。現在,如果您可以向每一部分提出一個問題並獲得有意義的答案,該怎麼辦?您可能會問:“您在講什麼故事?

SQL的Alter表語句:動態地將列添加到數據庫 在數據管理中,SQL的適應性至關重要。 需要即時調整數據庫結構嗎? Alter表語句是您的解決方案。本指南的詳細信息添加了Colu

斯坦福大學以人為本人工智能研究所發布的《2025年人工智能指數報告》對正在進行的人工智能革命進行了很好的概述。讓我們用四個簡單的概念來解讀它:認知(了解正在發生的事情)、欣賞(看到好處)、接納(面對挑戰)和責任(弄清我們的責任)。 認知:人工智能無處不在,並且發展迅速 我們需要敏銳地意識到人工智能發展和傳播的速度有多快。人工智能係統正在不斷改進,在數學和復雜思維測試中取得了優異的成績,而就在一年前,它們還在這些測試中慘敗。想像一下,人工智能解決複雜的編碼問題或研究生水平的科學問題——自2023年
