Code Llama

Code Llama 模型,共有三种变体:

指导
代码补全
Python
本指南介绍了针对 Code Llama 的不同变体和功能构建提示的不同方法。

以下示例使用 4 位量化的 70 亿参数模型,但也提供了 130 亿和 340 亿参数模型。本指南使用开源Ollama项目下载并提示 Code Llama,但这些提示也适用于其他模型提供商和运行时。

指导
该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.
代码补全
标准代码补全模型将根据提供的提示继续生成后续标记。如果您只想生成代码,例如让模型在看到函数签名或注释后编写函数,则此变体效果最佳:

迅速的

ollama run codellama:7b-code ‘# A simple python function to remove whitespace from a string:’
回复

def remove_whitespace(s):
return ‘’.join(s.split())
填充
中间填充(FIM),或更简单地说,填充是代码完成模型支持的一种特殊提示格式,可以在两个已经编写的代码块之间完成代码。这对于在现有函数中插入代码特别有效,例如:

def compute_gcd(x, y):

<FILL>

return result

使用 Code Llama,填充提示需要模型所需的特殊格式。

 {prefix} {suffix} 
要将其与现有代码一起使用,请拆分前后代码在上面的例子中,分为两个部分:前缀和后缀。例如,对于上面的 LCM 示例:

迅速的

ollama run codellama:7b-code '
 def compute_gcd(x, y): return result '
尽管结果会有所不同,但你应该得到如下结果:

回复

  if x == y:
        return x

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

    result = compute_gcd(x, y)
注意:模型可能会在结果结束时返回。这是响应中的特殊标记,表示响应的结束,类似于


Python
为了感谢创建该模型的社区和工具,Code Llama 的作者提供了一个 Python 变体,该变体在 100B 个额外的 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 = "It is now %s." % now
    return HttpResponse(html)
基于 Code Llama 构建的工具
Cody有一个实验版本,它使用带有填充支持的 Code Llama。
Continue支持 Code Llama 作为GPT-4 的替代品
Phind和WizardLM团队对 Code Llama 进行了微调
开放解释器可以使用 Code Llama 生成函数,然后在终端本地运行

### 参考资料

- **How to prompt Code Llama**:https://ollama.com/blog/how-to-prompt-code-llama
- **Run Code Llama locally**:https://ollama.com/blog/run-code-llama-locally
- **Run Llama 2 uncensored locally**: https://ollama.com/blog/run-llama2-uncensored-locally
作者:Jeebiz  创建时间:2024-08-04 01:38
最后编辑:Jeebiz  更新时间:2024-08-29 20:41