Code Llamaのプロンプト方法
本文の状態
日本語全文を表示中
詳細モードで約3分の本文を読めます。
同じ出来事の情報源
この情報源を基点に整理
Ollama Blog
Code Llamaのプロンプト構造、バリエーション、機能(指示、コード補完、中間補完)について解説するガイドです。
Source Article
元記事を日本語で読む
本文に関係しない購読案内、埋め込み通知、サイト内プロモーションは除いています。
メタ社が発表したコード生成AI「Code Llama」には、主に3つのバリエーションが存在し、それぞれ異なるプロンプト構造と用途を持っています。この要約では、各バリエーションの特徴と効果的な使用方法について解説します。
まず、7B、13B、34Bという3種類のパラメータ規模のモデルが提供されており、例示では7Bパラメータで4ビット量子化されたモデルが使用されています。実行環境としてはオープンソースのOllamaプロジェクトが用いられていますが、提示されるプロンプトは他のプラットフォームでも通用するように設計されています。
主要なバリエーションとその活用方法は以下の通りです。
第一に、「Instructモデル」は、人間のような回答を生成するように訓練されています。ChatGPTに近い挙動を示し、自然言語とコードの両方を含む質問に対応可能です。例えば、『あなたは簡潔なコードと説明を書くエキスパートプログラマです。n番目のフィボナッチ数を生成するPython関数を書いてください』といったプロンプトで、再帰を用いた関数とその説明を出力できます。
第二に、コードレビュー支援です。チームにとって時間のかかるコードレビューを、Code Llamaが第二の目としてサポートします。単純なバグの発見に役立ち、例えば特定のコード片におけるバグの位置を指摘するようプロンプトを投げることができます。例では、フィボナッチ関数が「n=1」の場合を適切に扱っていないというバグを検出しています。
第三に、単体テストの自動生成です。多くのボイラープレート(定型的なコード)を必要とする単体テストの作成を支援します。既存の関数ファイルを読み込み、その関数に対する単体テストを生成するよう指示できます。提示された例では、フィボナッチ関数に対して、0から9までの入力値に対するテストケースを含むユニットテストコードを生成しています。
第四に、標準的な「コード補完モデル」です。これは、与えられたプロンプトに基づいて後続のトークンを生成するもので、関数シグネチャやコメントを見せた上でコードを書かせるような、純粋なコード生成に最適です。例えば、文字列の空白を除去するPython関数のコメントをプロンプトとして与えると、対応する関数本体を完成させます。
最後に、「インフィル(FIM: Fill-in-the-Middle)」機能です。これはコード補完モデルがサポートする特殊なプロンプト形式で、既に記述された二つのコードブロックの間にあるコードを補完します。既存の関数内にコードを挿入する場合などに特に効果的です。
これらの機能を適切に使い分けることで、Code Llamaはプログラミングにおける質問応答、コードレビュー、テスト作成、コード生成・補完など、多岐にわたる作業を効率化する強力なツールとなります。
原文を表示
Two weeks ago the Code Llama model was released by Meta with three variations:
This guide walks through the different ways to structure prompts for Code Llama for its different variations and features.
Examples below use the 7 billion parameter model with 4-bit quantization, but 13 billion and 34 billion parameter models were made available as well. This guide uses the open-source Ollama project to download and prompt Code Llama, but these prompts will work in other model providers and runtimes too.
The instruct model was trained to output human-like answers to questions. This model variation is the easiest to use and will behave closest to ChatGPT, with answer questions including both natural language and code:
ollama run codellama:7b-instruct 'You are an expert programmer that writes simple, concise code and explanations. Write a python function to generate the nth fibonacci number.' Response
Here is a Python function that generates the nth Fibonacci number: def fib(n): if n return result With Code Llama, infill prompts require a special format that the model expects.
{prefix} {suffix} To use this with existing code, split the code before and after in the example above the into parts: the prefix, and the suffix. For example, for our LCM example above:
ollama run codellama:7b-code ' def compute_gcd(x, y): return result ' While results will vary, you should get something like this:
if x == y: return x if x > y: x = x - y else: y = y - x result = compute_gcd(x, y) Note: the model may return at the end of the result. This is a special token in the response that represents the end of the response similar to , and
As a thank you to the community and tooling that created the model, the authors of Code Llama included a Python variation which is fine-tuned on 100B additional Python tokens, making it a good model to use when working on machine learning-related tooling, or any other Python code:
ollama run codellama:7b-python ' # django view for rendering the current day and time without a template def current_datetime(request):' Response
now = datetime.now() html = "It is now %s." % now return HttpResponse(html) Tools built on Code Llama
Cody has an experimental version that uses Code Llama with infill support.
Continue supports Code Llama as a drop-in replacement for GPT-4
Fine-tuned versions of Code Llama from the Phind and WizardLM teams
Open interpreter can use Code Llama to generate functions that are then run locally in the terminal
関連記事
News to Guide
ニュースの次に確認する
発表内容を、現在の料金や仕様と照らし合わせられる関連ガイドです。
今日のまとめ
AIデイリーブリーフで今日の重要ニュースをまとめ読み