在人工智能的快速發展的景觀中,優化大型語言模型(LLMS)不僅在於突破可能的界限,而且還涉及確保效率和成本效益。
>
提示壓縮已成為一種重要技術,可以增強這些模型的性能,同時最大程度地減少計算費用。隨著新的研究幾乎每週都會出現,保持挑戰是具有挑戰性的,但是了解基本面是至關重要的。 >本文涵蓋了及時壓縮的基礎知識,討論了何時應該使用它,其在降低抹布管道中的成本中的重要性,並使用OpenAI的API。
如果您想了解更多信息,請在及時工程上查看本課程。什麼是提示壓縮?
提示壓縮是一種自然語言處理(NLP)中使用的技術,可通過減少其長度而不顯著改變輸出的質量和相關性來優化對LLM的輸入。由於查詢中令牌的數量對LLM性能的影響,因此這種優化至關重要。
令牌是文本LLMS使用的基本單元,根據語言模型的令牌代表單詞或子字。在提示中減少令牌數量是有益的,有時是由於幾個原因所必需的:令牌限制約束: 處理效率和降低成本:
提示壓縮通過減少令牌計數的同時保留提示的有效性來減輕這些問題。
>抹布管道將信息檢索與文本生成相結合,並且經常用於專門的聊天機器人和上下文理解至關重要的其他應用程序。這些管道通常需要廣泛的對話歷史或作為提示檢索文件,從而導致代幣計數和增加的費用。
在這種情況下,
>提示壓縮的適用性和局限性
這些模型通常不會為每個令牌收費,並且具有集成的聊天摘要和內存功能來有效地管理對話歷史記錄,從而使壓縮冗餘。
>也必須注意的是,即使使用指控的模型,過度壓縮也可能導致細微差別或重要細節的損失。在減小大小和保持提示含義的完整性之間達到正確的平衡是關鍵。>提示壓縮如何工作?
>提示壓縮技術可以分為三種主要方法:知識蒸餾,編碼和過濾。每種技術都利用不同的優勢來優化LLMS提示的長度和效率。
>我們將討論這些技術中的每一種,但您可以在本文中找到一種更全面的方法:大型語言模型的有效提示方法:調查。在本文中,我將本文稱為“調查文件”。最初開發了該技術來解決訓練模型集合的計算挑戰。在迅速工程的背景下,知識蒸餾可用於壓縮提示而不是模型。
這是通過學習如何通過軟提示調整來壓縮LLM中的硬提示來實現的。有關詳細見解,請參閱調查文件的第3.1節和附錄A.1.1。
>編碼>有趣的是,LLM精通其他語言,例如base64,可以在編碼中用於降低提示的令牌大小。例如,提示“將以下文本轉換為法語:你好,你好嗎?”在基本64中編碼的是“ vhjhbnnsyxrlihrozsbmb2xsb3dpbmcgdgv4dcb0b0bybgcmvuy2g6icdizwxsbywgag93ig93igfyzsb5b5b5b3unpw ==”。您可以嘗試提示您喜歡的LLM測試!
>令人驚訝的是,一些編碼技術也用於模型越獄,其中涉及操縱LLM以繞過其安全機制。有關編碼方法的更多詳細信息,請參見調查文件的第3.2節和附錄A.1.2。
過濾>前兩種方法試圖壓縮整個提示,而過濾技術的重點是消除不必要的零件以提高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接下來,我們可以在要壓縮的文本字符串上調用我們的selectIveContext對象。我們可以設置redaim_ratio和redus_level參數。 REDAL_LEVEL需要是以下內容之一:“發送”,“短語”或“令牌”。對象調用返回(上下文,降低了)元組,其中上下文是壓縮提示和降低的_content是刪除的短語,句子或代幣的列表。
>
sc = SelectiveContext(model_type = ‘gpt-2’, lang = ‘en’)現在讓我們做一些例子。我將要求GPT-3.5-Turbo-0125模型總結本文中的“何時應該使用及時的壓縮”部分。然後,我們將使用所有三個降低水平:句子,短語和令牌以0.5的壓縮率來壓縮該部分。我們將要求該模型再次總結壓縮版本,並比較每個提示的令牌計數和模型的輸出。
原始段落和摘要
在下面,您可以看到我將使用的API調用 - 我們將在格式字符串中更改文本變量。首先,我們要求該模型總結原始的未壓縮段落:如您所見,該模型很好地總結了未壓縮的段落。
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個令牌長,以下是模型摘要:
>句子級減少
[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.]接下來,讓我們嘗試減少句子級別。減少的提示為129個令牌長:
這是模型摘要:
[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]
此摘要不包含任何錯誤,但缺少抹布討論。
[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. ]摘要大多是正確且連貫的。但是,它錯誤地理解了有關Chatgpt的部分,從而受益於積極的提示壓縮。
評估提示壓縮
[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中文網其他相關文章!