Code Llamaのプロンプト方法
Code Llamaのプロンプト構造、バリエーション、機能(指示、コード補完、中間補完)について解説するガイドです。
キーポイント
Code Llamaの3つのモデルバリエーション(instruct、code completion、infill)の具体的なプロンプト方法を解説
コード生成、コードレビュー、単体テスト作成、コード補完など多様な開発タスクへの応用例を提示
オープンソースのOllamaプロジェクトを活用した実践的な実行方法を提供
影響分析・編集コメントを表示
影響分析
この記事はMetaのCode Llamaの実用的な活用方法を具体的に示しており、開発者コミュニティへの普及促進に貢献する。特にコードレビューやテスト作成など、開発プロセスの効率化に焦点を当てている点が実務的価値が高い。
編集コメント
実践的なプロンプト例が豊富で、開発者がすぐに試せる内容。Code Llamaの多様な活用シーンを具体的に示すことで、AI支援開発の可能性を実感させてくれる。
メタ社が発表したコード生成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
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み