How to prompt Code Llama

How to prompt Code Llama · Ollama Blog

如何提示Code Llama

2023年9月9日


两周前,Meta发布了Code Llama模型,并提供了三种变体:
- Instruct
- 代码补全(Code completion)
- Python

本指南介绍了针对Code Llama不同变体和特性的多种提示结构。下面的示例使用了具有4-bit量化的7亿参数模型,但也提供了130亿和340亿参数模型。本指南采用开源的[Ollama](https://github.com/jmorganca/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()

这个测试用例涵盖了输入为0到9的整数所有情况。

代码补全

标准的代码补全模型将根据提供的提示继续生成后续标记。如果您只想生成代码,例如在看到函数签名或注释后让模型编写函数,这种变体最适合:

提示

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>

要在现有代码中使用此格式,将代码在示例中前后分成两部分:前缀(prefix)和后缀(suffix)。例如,对我们的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>,这是一种特殊的标记,表示响应的结束,类似于<PRE><SUF><MID>。

Python

作为对社区和创建该模型的工具的感谢,Code Llama的作者们发布了一个Python变体,该变体在额外的1000亿个Python标记上进行了微调,使其在处理机器学习相关工具或任何其他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的工具

- Cody有一个使用infill支持的实验版本
- Continue支持将Code Llama作为GPT-4的替代品
- 来自Phind团队和WizardLM团队的微调版本。
- Open interpreter可以使用Code Llama生成并在本地终端运行的函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值