如何提示 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

中间填充 (FIM),或更简洁地说,infill 是代码补全模型支持的一种特殊提示格式,可以完成两个已编写代码块之间的代码。这对于在现有函数中插入代码尤其有效,如下所示

def compute_gcd(x, y):

    <FILL>

    return result

对于 Code Llama,infill 提示需要模型期望的特殊格式。

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

要将其与现有代码一起使用,请在之前和之后拆分代码在上面的示例中,将其分为几个部分:前缀和后缀。例如,对于我们上面的 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 变体,该变体在额外的 1000 亿个 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 构建的工具