LLMのエンジニアリング・プロンプトの初心者向けガイド (2024/04/01)

LLMのエンジニアリング・プロンプトの初心者向けガイド (2024/04/01)

https://blogs.oracle.com/ai-and-datascience/post/beginners-guide-engineering-prompts-llm

投稿者: Shailender Singh | Principal Applied Scientist

Sid Padgaonkar | Sr. Director - Product Management (Gen AI) - Strategic Customers


Generative AIの導入はトレンドであり、Fortune 500のリーダーは自社の採用計画を策定したいと考えています。この投稿は、Generative AIの潜在能力を効果的に活用するための5つの部分からなるシリーズの3番目です。このブログ記事では、アプリケーションの大規模言語モデル(LLM)のための迅速なエンジニアリングの基本について説明します。


エンジニアは、大規模言語モデル(LLM)を効果的に使用して、人間のようなテキストを生成し、自然言語を理解することができます。効果的な迅速なエンジニアリングは、その可能性を活用するために不可欠です。このブログ投稿では、明確な目標の確立、コンテキストと特異性の提供、迅速な長さの検討、ユーザー指示の使用、出力に基づく反復など、迅速なエンジニアリングにおけるベストプラクティスを特定するためのガイドを提供します。これらの基本を理解することで、LLMとの最適な相互作用が保



基本の理解


迅速なエンジニアリングの基本に潜む前に、基礎を把握することが不可欠です。CohereなどのLLMは、入力に基づいてテキストを生成するように設計されています。ユーザーから提供されるこれらの入力は、プロンプトと呼ばれます。これらのプロンプトは指示として機能し、一貫性のあるコンテキストに関連する出力を生成するようにモデルをガイドします。重要なのは、LLMとやり取りするユーザーの目的を効果的に伝えるプロンプトの策定です。



明確で簡潔な目標の確立


エンジニアリングの効果的なプロンプトの最初のステップは、プロンプトから達成する目標を明確に定義することです。コンテンツの生成や質問への回答のいずれであっても、簡潔なスコープを持つ明確に定義された目標によって、効果的なプロンプトの作成の基盤が設定されます。


たとえば、LLMに創造的な短編小説を書いてもらいたい場合、主題、長さ、および品詞に関する目的が明確になるほど、出力の性質が正確になります。



コンテキストと特異性


コンテキストは、LLMを操作する場合に重要です。プロンプトにコンテキストを提供すればするほど、背景情報や制約など、特定の詳細を含む関連出力をモデルで理解して生成しやすくなり、LLMを必要な方向に導きます。たとえば、モデルで製品の説明を記述する場合は、製品、その機能およびターゲット・オーディエンスの詳細を含めます。この特異性は、LLMが指定されたコンテキストへの応答を調整するのに役立ちます。



プロンプトの長さと書式


プロンプトの長さと形式は、出力に大きく影響する可能性があります。簡潔なプロンプトが必要なタスクもありますが、より詳細な指示によるメリットもあります。詳細な応答を要求するタスクの場合、より長く、より構造化されたプロンプトを提供できます。逆に、迅速かつ簡単なタスクの場合は、簡潔なプロンプトで十分です。重要なのは、モデルのレスポンスに基づいて反復および調整することです。



システムおよびユーザーの指示を効果的に使用


LLMの文脈では、ユーザーの指示とシステム指示が重要な役割を果たします。ユーザー・インストラクションは、ユーザーのインテントに基づいてモデルをガイドし、システム・インストラクションは、モデルの動作に関するハイレベルのガイドラインを提供します。


たとえば、「面白いストーリーを書く」などのユーザー・インストラクションを使用し、「マーク・トウェインのスタイル」などのシステム・インストラクションを使用できます。この組合せは、LLMをガイドして特定のテーマとスタイルで詩を生成します。



出力に基づいて反復および調整


プロンプト・エンジニアリングは反復的なプロセスです。LLMから出力を受け取った後、それらを分析して、モデルが目標とどの程度一致しているかを理解します。モデルの出力に基づいてプロンプトを絞り込み、パフォーマンスを微調整し、必要に応じて出力を調整できます。これは、モデルのレスポンスから学習する動的で適応性の高いプロセスです。


これらの指針を使用して、いくつかの例を順を追って説明します。



ユースケースの例


プロンプトと温度値を取得する関数を定義し、OCI Generative AIサービス・エンドポイントをコールしてCohereコマンド・モデルにアクセスします。  OCI Generative AIは、APIを介して使用でき、様々な大規模言語モデルと統合できます。


JupyterノートブックまたはPythonファイルで、次の関数を追加します。この関数は、LLMモデルによって生成されたテキスト・レスポンスを返します。


import oci

# Create a default config using DEFAULT profile in default location

config = oci.config.from_file()

# Initialize service client with default config file

generative_ai_inference_client = oci.generative_ai_inference.GenerativeAiInferenceClient(config=config, service_endpoint=<endpoint>)

def generate_text(prompt, temp=0):

    generate_text_detail = oci.generative_ai_inference.models.GenerateTextDetails()

    generate_text_detail.compartment_id = "ocid1.test.oc1..<unique_ID>EXAMPLE-compartmentId-Value"

   generate_text_detail.serving_mode = oci.generative_ai_inference.models.OnDemandServingMode(model_id="cohere.command")    

    cohere_generate_text_request =   oci.generative_ai_inference.models.CohereLlmInferenceRequest()

    cohere_generate_text_request.temperature = temp

    cohere_generate_text_request.prompt = prompt

    cohere_generate_text_request.max_tokens = 200

    generate_text_detail.inference_request = cohere_generate_text_request

    response = generative_ai_inference_client.generate_text(generate_text_detail)

    return response.data.inference_response.generated_texts[0].text


明確な指示を提供


At its core, prompting a LLM model is about sending a clear instruction to a text generation model and getting a response back. This input can be a complete prompt in a short line of instruction to the model.
user_input = "a pair of sneaker product named SK-20"

prompt = f"""Write a creative product description for {user_input}"""

response = generate_text(prompt, temp=0)

print(response)


モデルは次の応答を返します。


Looking for a stylish and comfortable pair of sneakers? Look no further than the SK-20! With its sleek design and high-quality materials, this sneaker is the perfect addition to any fashion-forward individual's wardrobe. Crafted with premium leather and breathable mesh panels, the SK-20 is designed to provide ultimate comfort and durability. The rubber outsole provides excellent traction and stability, making them ideal for everyday wear and any kind of terrain.

The sleek and modern design of the SK-20 features a low-top profile, making them versatile and easy to style. Whether you're running errands, hitting the gym, or attending a casual event, these sneakers will complement any outfit.

Plus, with its affordable price point, the SK-20 is an excellent value for anyone looking for a stylish and versatile pair of sneakers. Upgrade your footwear game with the SK-20 today!

Remember, when you wear the SK-20, you're not just wearing any ordinary



具体性とコンテキストを追加して指示を与える


前のテキスト・プロンプトの問題の1つは、生成される出力が非常に一般的であるということです。


プロンプトは、機能やターゲット・オーディエンスなどの製品の詳細について説明されませんでした。人間が製品の説明を書くためにより多くの情報を必要とするのと同じように、モデルにその応答を導くための適切な詳細セットを与えなければなりません。


今度は、同じプロンプトで、機能、ターゲット・オーディエンス、主なメリットなどの特定の情報を含めてみましょう。


user_input_product = "a pair of sneaker product named SK-20"

user_input_keywords = "stylish", “durable”, “flexible”

user_input_customer = "athletes who are daily runner or jogger"

user_input_describe = "benefits of this product"

prompt = f"""Write a creative product description for {user_input_product}, with the keywords {user_input_keywords} for { user_input_customer} and describe {user_input_describe}"""

response = generate_text(prompt, temp=0.5)

print(response)

Experience unparalleled comfort and style with the SK-20 sneakers, the perfect addition to any athlete's wardrobe. Designed with durability, flexibility, and style in mind, these sneakers are perfect for daily runners and joggers who want to take their performance to the next level.

The sleek design of the SK-20 sneakers is complemented by their durability, making them ideal for intense workouts and long runs. The flexible material ensures a comfortable fit while providing the necessary support for your feet, reducing the risk of injury during strenuous activities.

Whether you're hitting the tracks or making a fashion statement, the SK-20 sneakers check all the boxes. Their stylish design will make you stand out from the crowd, while the practical features will keep your feet happy throughout the day.

Don't settle for anything less than the best. Upgrade your footwear with the SK-20 sneakers today.


プロンプトには、次の例を含め、様々な方法で特異性を追加できます。


  • ブランド・マネージャなど、タスクの個人またはジョブ・ロールをロールプレイングするためのプロンプトを追加します。例えば、「あなたはブランドマネージャーです。製品の説明を書く...
  • 楽しく、真面目で、カジュアルで、またはインスピレーションを与えるなど、テキストのトーンが何であるべきかを記述します。たとえば、「Tone: Inspiring」です。
  • 特定の長さまたは段落のテキストを生成するプロンプトを追加します。この追加は、モデルを一貫性のある簡潔なものにするために役立ちます。たとえば、「2つの段落で記述します。」などです。



コンテキストの追加


LLMは設計済入力用のテキストを生成できますが、コンテキスト対応のシナリオではまだ適切ではありません。実際のアプリケーションでは、コンテキストが重要であり、プロンプトにコンテキストを追加できることで、ドメインまたは会社の生成AIがパーソナライズされます。次の例では、プロンプトはコンテキストなしで製品の一般的な機能を提供します。(すべての応答が構成されます。)


user_input = "What are the key features of sneaker SK-20"

prompt = user_input

response = generate_text(prompt, temp=0)

The SK-20 sneaker is a popular shoe model known for its stylish design and comfortable wear. Here are some of its key features:

1. Lightweight Construction: The SK-20 is designed to be lightweight, making it ideal for everyday wear and sports activities. Its breathable mesh upper and lightweight materials help keep your feet comfortable during long wear times.

2. Durable Outsole: The sneaker features a durable outsole made of rubber or synthetic material, providing excellent traction and grip on various surfaces. This makes it suitable for both indoor and outdoor use.

3. Flexible Midsole: The shoe's midsole is designed to provide cushioning and support for your feet. This helps to reduce fatigue during long walks or strenuous activities, providing comfort with every step.


これで、関連するコンテキストが製品に提供されます。この例では、コンテキストに追加するために情報がすでに使用可能であると想定しています。


context = "SK-20 sneakers showcase a sleek and modern aesthetic that effortlessly complements any outfit. The water-resistant feature of these sneakers is achieved through a special coating that repels water, preventing it from seeping into the shoe. This means you can confidently step through puddles and navigate rainy streets without worrying about soggy feet. Comfort is not compromised with our water-resistant sneakers. The breathable and moisture-wicking interior lining keeps your feet fresh and dry, while the cushioned insole provides excellent support and shock absorption. Whether you're walking, running, or engaging in other activities, these sneakers offer the perfect blend of comfort and functionality."

user_input = "What are the key features of sneaker SK-20"

prompt = f"""{context} Given the information above, answer this question {user_input}"""  

response = generate_text(prompt, temp=0)


The key features of the sneaker SK-20 are:

1. A sleek and modern design

2. Water-resistant coating

3. Complementary look for any outfit

4. Breathable and moisture-wicking interior lining

5. Cushioned insole for support and shock absorption.


書式の指定


LLMモデルは、任意の詳細または長さでレスポンスを返すことができるため、これらのモデルは、JSONやYAMLなどの複数の形式でレスポンスを返すことができます。たとえば、次の製品および製品説明のリストでは、JSON形式でリストを生成するようモデルに要求します。


prompt ="""Turn the following information into a JSON string with the following keys: Product Name, Product Id, Product Description.

Product ID: #0890 NAME SK-20 DESC “Water proof shoes”

Product ID: #0891 NAME LX-33 DESC “Tall Leather Boots”

Product ID: #0892 NAME OX-29 DESC “Classic Lace-up shoes”

Product ID: #0811 NAME SD-42 DESC “Open toed sandals”

"""

response = generate_text(prompt, temp=0)

```json

[

  {

    "product_name": "SK-20",

    "product_id": "#0890",

    "product_description": "Water proof shoes"

  },

  {

    "product_name": "LX-33",

    "product_id": "#0891",

    "product_description": "Tall Leather Boots"

  },

  {

    "product_name": "OX-29",

    "product_id": "#0892",

    "product_description": "Classic Lace-up shoes"

  },

  {

    "product_name": "SD-42",

    "product_id": "#0811",

    "product_description": "Open toed sandals"

  }

]

```


まとめ


LLMのプロンプトを効果的にエンジニアリングする能力は、さまざまな業界にまたがる無数のアプリケーションへの扉を開く貴重なスキルです。このジャーニーに着手する際には、次の目標を考慮してください。


  • 明確な目標から始める。
  • 指示に従ってください。
  • 指示を使用して、出力をニーズに応えます。
  • これは反復的なプロセスであることに注意してください。


迅速なエンジニアリングに関するインサイトが得られたので、今こそ知識を行動に移す時です。次のリソースが、この移行の開始に役立ちます。Oracle Cloud Infrastructureを使い始めたばかりの方は、300USドルのクレジットが付いたOracle Cloudの無料トライアルを30日間お試しください。


この5部構成のブログ・シリーズの一部を『Navigating the Frontier』でお読みください。企業向けのGenerative AI統合戦略を開発するための主な考慮事項、およびアプリケーションの大規模言語モデルを最適化するための包括的戦術におけるパート2。第4部「Beyond Prompt Engineering: Deep dive into Fine Tuning(プロンプト・エンジニアリングを超える: ファインチューニングを深く掘り下げる)」


詳細は、次のリソースを参照してください。

コメント

このブログの人気の投稿

Oracle RACによるメンテナンスのためのドレインとアプリケーション・コンティニュイティの仕組み (2023/11/01)

Oracle Cloud Infrastructure Secure Desktopsを発表: デスクトップ仮想化のためのOracleのクラウドネイティブ・サービス (2023/06/28)

Oracle Cloudのデータベースをオブジェクト・ストレージにバックアップする3つの方法 (2021/12/13)