如何提示 Code Llama

2023 年 9 月 9 日

兩週前,Meta 發布了 Code Llama 模型,共有三種變體

  • Instruct(指令)
  • 程式碼自動完成
  • Python

本指南將逐步說明如何針對 Code Llama 的不同變體和功能,建構提示詞。

以下範例使用 70 億參數模型和 4 位元量化,但也提供了 130 億和 340 億參數模型。本指南使用開源 Ollama 專案來下載和提示 Code Llama,但這些提示詞也適用於其他模型供應商和執行環境。

Instruct(指令)

instruct 模型經過訓練,可以輸出類似人類回答問題的內容。此模型變體最容易使用,行為也最接近 ChatGPT,回答問題時會包含自然語言和程式碼

提示

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.'

回應

Here is a Python function that generates the nth Fibonacci number:

def fib(n):
    if n <= 1:
        return n
    else:
        return fib(n-1) + fib(n-2)

This function uses the recursive formula for the Fibonacci sequence, which is:

fib(n) = fib(n-1) + fib(n-2)

程式碼審查

對於團隊來說,程式碼審查通常很耗時。Code Llama 可以協助針對簡單的錯誤,提供第二雙眼睛來檢查提交或提取請求

提示

ollama run codellama '
Where is the bug in this code?

def fib(n):
    if n <= 0:
        return n
    else:
        return fib(n-1) + fib(n-2)
'

回應

The bug in this code is that it does not handle the case where `n` is equal to 1.

單元測試

編寫單元測試通常需要相當多的樣板程式碼。Code Llama 可以提供協助

提示

ollama run codellama "write a unit test for this function: $(cat fib.py)"

回應

Here's an example of how you could write a unit test for the `fib` function:

```
import unittest

class TestFib(unittest.TestCase):
    def test_fib(self):
        self.assertEqual(fib(0), 0)
        self.assertEqual(fib(1), 1)
        self.assertEqual(fib(2), 1)
        self.assertEqual(fib(3), 2)
        self.assertEqual(fib(4), 3)
        self.assertEqual(fib(5), 5)
        self.assertEqual(fib(6), 8)
        self.assertEqual(fib(7), 13)
        self.assertEqual(fib(8), 21)
        self.assertEqual(fib(9), 34)
if __name__ == '__main__':
    unittest.main()
```

This test case covers all the cases where the input is an integer between 0 and 9, inclusive.

程式碼自動完成

標準程式碼自動完成模型只會根據提供的提示詞,繼續產生後續的 token。如果您只想產生程式碼,例如在看到函數簽名或註解後,讓模型編寫函數,則此變體效果最佳

提示

ollama run codellama:7b-code '# A simple python function to remove whitespace from a string:'

回應

def remove_whitespace(s):
    return ''.join(s.split())

Infill(填充)

Fill-in-the-middle (FIM),或更簡潔地說,infill(填充)是程式碼自動完成模型支援的一種特殊提示格式,可以完成兩個已編寫程式碼區塊之間的程式碼。這對於在現有函數中插入程式碼尤其有效,如下所示

def compute_gcd(x, y):

    <FILL>

    return result

使用 Code Llama 時,infill 提示詞需要模型期望的特殊格式。

<PRE> {prefix} <SUF>{suffix} <MID>

若要將其與現有程式碼搭配使用,請將程式碼在前後分割以上範例將 LCM 分割成:前綴和後綴。例如,對於我們的 LCM 範例

提示

ollama run codellama:7b-code '<PRE> def compute_gcd(x, y): <SUF>return result <MID>'

雖然結果會有所不同,但您應該會得到類似這樣的結果

回應

  if x == y:
        return x

    if x > y:
        x = x - y
    else:
        y = y - x

    result = compute_gcd(x, y)

注意:模型可能會在結果末尾傳回 <EOT>。這是回應中的特殊 token,表示回應結束,類似於 <PRE><SUF><MID>

Python

為了感謝社群和建立此模型的工具,Code Llama 的作者包含了一個 Python 變體,該變體在額外的 100B Python token 上進行了微調,使其成為處理機器學習相關工具或任何其他 Python 程式碼時的良好模型

提示

ollama run codellama:7b-python '
# django view for rendering the current day and time without a template
def current_datetime(request):'

回應

    now = datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

基於 Code Llama 建構的工具