人工知能の急速に進化する風景では、大規模な言語モデル(LLM)を最適化することは、可能なことの境界を押し広げるだけでなく、効率と費用対効果を確保することでもあります。
プロンプト圧縮は、計算費用を最小限に抑えながら、これらのモデルのパフォーマンスを向上させるための重要な手法として浮上しています。ほぼ毎週新しい研究が出現しているため、維持は挑戦的ですが、基本を理解することが不可欠です。 この記事では、迅速な圧縮の基本をカバーし、使用すべきか、RAGパイプラインのコストを削減する上での重要性について説明し、OpenAIのAPIを使用してGPT-3.5-Turbo-0125モデルを使用して例を提供します。 詳細を知りたい場合は、このコースを迅速なエンジニアリングでご覧ください。
プロンプト圧縮とは?
プロンプト圧縮は、自然言語処理(NLP)で使用される手法であり、出力の品質と関連性を大幅に変えることなく、LLMSに与えられた入力を最適化します。この最適化は、クエリのトークンの数がLLMパフォーマンスに与える影響のために重要です。
トークンは、言語モデルのトークネザーに応じて単語またはサブワードを表すテキストLLMSの基本単位です。プロンプトでトークンの数を減らすことは有益であり、いくつかの理由で必要な場合があります:llmsには、入力の最大トークン制限があります。この制限を超えると、重要な情報が切り捨てられ、出力の明確さとモデルの有効性が減少します。
プロンプト圧縮プロンプトの有効性を維持しながらトークンカウントを減らすことにより、これらの問題を軽減します。
プロンプト圧縮は普遍的な解決策ではなく、賢明に使用する必要があることに注意することが重要です。たとえば、会話のコンテキスト用に設計されたChatGPTのようなアシスタントモデルは、積極的な迅速な圧縮の恩恵を受けることはできません。
プロンプト圧縮はどのように機能しますか?
迅速な圧縮技術は、知識の蒸留、エンコーディング、フィルタリングの3つの主要な方法に分類できます。各手法は、LLMSのプロンプトの長さと効率を最適化するためにさまざまな強度を活用しています。
これらの各手法について話しますが、このペーパーでは、より包括的なアプローチを見つけることができます。大規模な言語モデルの効率的なプロンプトメソッド:調査。この記事を通して、この論文を「調査論文」と呼びます。知識蒸留
この手法は、モデルのアンサンブルをトレーニングするという計算上の課題に対処するために最初に開発されました。プロンプトエンジニアリングのコンテキストでは、知識の蒸留を使用して、モデルの代わりにプロンプトを圧縮できます。
これは、ソフトプロンプトチューニングを介してLLMS内のハードプロンプトを圧縮する方法を学ぶことで実現されます。詳細な洞察については、調査論文のセクション3.1および付録A.1.1を参照してください。エンコード
興味深いことに、LLMはBase64のような他の言語に熟練しています。これは、プロンプトのトークンサイズを削減するためにエンコードで使用できます。たとえば、プロンプト「次のテキストをフランス語に翻訳します:こんにちは、お元気ですか?」 base64でエンコードされているのは、「vhjhbnnsyxrlihrozsbmb2xsb3dpbmcgdgv4dcb0bybgcmvuy2g6icdizwxsbywgag93igfyzsb5b3unpw ==」です。お気に入りのLLMにテストするように促すことができます!
驚くべきことに、いくつかのエンコーディング手法は、LLMを操作して安全メカニズムをバイパスすることを伴うモデルのジェイルブレイクにも使用されます。エンコーディング方法の詳細については、調査論文のセクション3.2および付録A.1.2を参照してください。フィルタリング
目標は、プロンプトの最も関連性の高い部分のみを保持することです。 Li et alによる紙の選択的文脈で。 (2023)、研究者は自己情報メトリックを使用して冗長な情報をフィルタリングします。 Paper Llmlingua:大規模な言語モデルの加速推論のプロンプトを圧縮すると、Microsoftの研究者はプロンプトを主要なコンポーネントに改良し、各部品の圧縮比を動的に調整します。詳細については、調査論文のセクション3.3および付録A.1.3を参照してください。
pythonにプロンプト圧縮を実装する方法 このセクションでは、人気があり、最先端と見なされる選択的コンテキストアルゴリズムを実装およびテストします。アルゴリズムのみをテストしたい場合は、何もインストールする必要はありません。既にHuggingfaceプラットフォームでホストされています。
llmlinguaファミリーのシンプル(KIS)、SCLR、およびアルゴリズムなど、他の主流の圧縮技術もありますが、この短い記事ではそれらをカバーすることはできません。
Selective Context Webアプリでは、圧縮するプロンプトの言語(英語または単純化された中国語)を選択できます。圧縮比を設定して、文、トークン、またはフェーズを除外するかどうかを選択することもできます。 OpenAI API
を使用して、選択的コンテキストの実装とテスト
次に、Pythonの実装に取り組みましょう。また、GPT-3.5-Turbo-0125モデルでいくつかの圧縮プロンプトをテストします。
最初に、必要なすべてのモジュールをインストールする必要があります。 PIPを使用して、選択的コンテキストライブラリをインストールする必要があります:
また、SPACYからEN_CORE_WEB_SMモデルをダウンロードする必要があります。これは、次のコマンドで実行できます。
pip install selective-context
ここで、selectiveContextオブジェクトを初期化する必要があります。モデルの場合はキュリーまたはGPT-2を選択し、言語ではENまたはZHを選択できます。この例にはGPT-2を使用します
python -m spacy download en_core_web_sm
次に、いくつかの例を挙げましょう。 GPT-3.5-Turbo-0125モデルに、この記事の「いつプロンプト圧縮を使用する必要があるか」を要約します。次に、3つの削減レベルすべてを使用して、セクションを0.5圧縮率で圧縮します:文、フレーズ、トークン。モデルに、圧縮バージョンを再度要約し、各プロンプトのトークンカウントとモデルの出力を比較するように依頼します。
sc = SelectiveContext(model_type = ‘gpt-2’, lang = ‘en’)
以下に、使用する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トークンの長さです。モデルの概要は次のとおりです。
次に、文レベルの削減を試してみましょう。削減されたプロンプトは129トークンの長さです:
[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. ]
要約はほとんど正しいもので一貫性があります。ただし、攻撃的なプロンプト圧縮の恩恵を受ける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 ]
圧縮レベル
| token-level
162 |
chatgptについての重要なニュアンスを失い、積極的な圧縮の恩恵を受けず、間違いを犯しました。 |
|
cente-level
|
129
|
は間違いを犯しませんでしたが、ぼろきれパイプラインについてのいくつかのコンテキストを逃しました。
|
|
| 183
| トークンレベルと同様に、ChatGptは積極的な圧縮の恩恵を受けると誤って述べました。
| 全体として、迅速な圧縮は、主なアイデアを維持しながら、トークンカウントを大幅に減らすことができます。ただし、重要なニュアンスやコンテキストを失わないようにバランスをとることが不可欠です。
以上がプロンプト圧縮:Pythonの例を備えたガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。