跳转到内容
搜索文档

使用 DeepSeek Coder 模型探索代码生成

最后更新 查看 MarkdownAgent 设置

探索 Workers AI 上所有可用模型的便捷方式是使用 Jupyter Notebook

你可以下载 DeepSeek Coder notebook或在下方查看嵌入的 notebook。


使用 DeepSeek Coder 探索代码生成

AI 模型能够生成代码,解锁各种用例。DeepSeek Coder 模型 @hf/thebloke/deepseek-coder-6.7b-base-awq@hf/thebloke/deepseek-coder-6.7b-instruct-awq 现已在 Workers AI 上提供。

让我们使用 API 来探索它们!

import sys
!{sys.executable} -m pip install requests python-dotenv
Requirement already satisfied: requests in ./venv/lib/python3.12/site-packages (2.31.0)
Requirement already satisfied: python-dotenv in ./venv/lib/python3.12/site-packages (1.0.1)
Requirement already satisfied: charset-normalizer<4,>=2 in ./venv/lib/python3.12/site-packages (from requests) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in ./venv/lib/python3.12/site-packages (from requests) (3.6)
Requirement already satisfied: urllib3<3,>=1.21.1 in ./venv/lib/python3.12/site-packages (from requests) (2.1.0)
Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.12/site-packages (from requests) (2023.11.17)
import os
from getpass import getpass

from IPython.display import display, Image, Markdown, Audio

import requests
%load_ext dotenv
%dotenv

配置环境

要使用 API,你需要 Cloudflare Account ID(前往 Workers & Pages > Overview > Account details > Account ID)和已启用 Workers AI 的 API Token

如果希望将这些文件添加到环境,可以创建名为 .env 的新文件

CLOUDFLARE_API_TOKEN="YOUR-TOKEN"
CLOUDFLARE_ACCOUNT_ID="YOUR-ACCOUNT-ID"
if "CLOUDFLARE_API_TOKEN" in os.environ:
    api_token = os.environ["CLOUDFLARE_API_TOKEN"]
else:
    api_token = getpass("Enter you Cloudflare API Token")
if "CLOUDFLARE_ACCOUNT_ID" in os.environ:
    account_id = os.environ["CLOUDFLARE_ACCOUNT_ID"]
else:
    account_id = getpass("Enter your account id")

从注释生成代码

常见用例是在用户提供描述性注释后为其补全代码。

model = "@hf/thebloke/deepseek-coder-6.7b-base-awq"

prompt = "# A function that checks if a given word is a palindrome"

response = requests.post(
    f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}",
    headers={"Authorization": f"Bearer {api_token}"},
    json={"messages": [
        {"role": "user", "content": prompt}
    ]}
)
inference = response.json()
code = inference["result"]["response"]

display(Markdown(f"""
    ```python
    {prompt}
    {code.strip()}
    ```
"""))
# A function that checks if a given word is a palindrome
def is_palindrome(word):
    # Convert the word to lowercase
    word = word.lower()

    # Reverse the word
    reversed_word = word[::-1]

    # Check if the reversed word is the same as the original word
    if word == reversed_word:
        return True
    else:
        return False

# Test the function
print(is_palindrome("racecar"))  # Output: True
print(is_palindrome("hello"))    # Output: False

协助调试

我们都遇到过这种情况,Bug 总是在所难免。有时那些堆栈轨迹 (stacktraces) 可能会非常令人望而生畏,而使用代码生成的一个极佳用例就是辅助解释问题。

model = "@hf/thebloke/deepseek-coder-6.7b-instruct-awq"

system_message = "The user is going to give you code that isn't working. Explain to the user what might be wrong"

code = """# Welcomes our user
def hello_world(first_name="World"):
    print(f"Hello, {name}!")
"""

response = requests.post(
    f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}",
    headers={"Authorization": f"Bearer {api_token}"},
    json={"messages": [
        {"role": "system", "content": system_message},
        {"role": "user", "content": code},
    ]}
)
inference = response.json()
response = inference["result"]["response"]
display(Markdown(response))

代码中的错误在于您尝试使用一个在函数中任何地方都未定义的变量 name。应该使用的正确变量是 first_name。因此,您应该将 f"Hello, {name}!" 更改为 f"Hello, {first_name}!"

以下是更正后的代码:

# Welcomes our user
def hello_world(first_name="World"):
    print(f"Hello, {first_name}!")

现在,当您调用 hello_world() 时,它将默认打印 "Hello, World!"。如果您调用 hello_world("John"),它将打印 "Hello, John!"。

编写测试!

编写单元测试是常见的最佳实践。有足够的上下文时,可以编写单元测试。

model = "@hf/thebloke/deepseek-coder-6.7b-instruct-awq"

system_message = "The user is going to give you code and would like to have tests written in the Python unittest module."

code = """
class User:

    def __init__(self, first_name, last_name=None):
        self.first_name = first_name
        self.last_name = last_name
        if last_name is None:
            self.last_name = "Mc" + self.first_name

    def full_name(self):
        return self.first_name + " " + self.last_name
"""

response = requests.post(
    f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}",
    headers={"Authorization": f"Bearer {api_token}"},
    json={"messages": [
        {"role": "system", "content": system_message},
        {"role": "user", "content": code},
    ]}
)
inference = response.json()
response = inference["result"]["response"]
display(Markdown(response))

以下是 User 类的简单 unittest 测试用例:

import unittest

class TestUser(unittest.TestCase):

    def test_full_name(self):
        user = User("John", "Doe")
        self.assertEqual(user.full_name(), "John Doe")

    def test_default_last_name(self):
        user = User("Jane")
        self.assertEqual(user.full_name(), "Jane McJane")

if __name__ == '__main__':
    unittest.main()

在此测试用例中,我们有两个测试:

  • test_full_name 测试用户同时有 first name 和 last name 时的 full_name 方法。
  • test_default_last_name 测试当用户只有名字且姓氏被设置为 "Mc" + 名字时的 full_name 方法。

如果所有这些测试都通过,说明 full_name 方法是按预期工作的。

中间填充代码补全

开发工具中的一个常见用例是基于上下文进行自动补全。DeepSeek Coder 能够提交带有占位符的现有代码,以便模型可以在上下文中完成补全。

警告:这些 token 前缀为 <|,后缀为 |>,请确保进行复制和粘贴。

model = "@hf/thebloke/deepseek-coder-6.7b-base-awq"

code = """
<|fim▁begin|>import re

from jklol import email_service

def send_email(email_address, body):
    <|fim▁hole|>
    if not is_valid_email:
        raise InvalidEmailAddress(email_address)
    return email_service.send(email_address, body)<|fim▁end|>
"""

response = requests.post(
    f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}",
    headers={"Authorization": f"Bearer {api_token}"},
    json={"messages": [
        {"role": "user", "content": code}
    ]}
)
inference = response.json()
response = inference["result"]["response"]
display(Markdown(f"""
    ```python
    {response.strip()}
    ```
"""))
is_valid_email = re.match(r"[^@]+@[^@]+\.[^@]+", email_address)

实验性:将数据提取为 JSON

无需威胁模型或在提示词中搬出祖母。直接以您想要的格式获取 JSON 返回。

model = "@hf/thebloke/deepseek-coder-6.7b-instruct-awq"

# Learn more at https://json-schema.org/
json_schema = """
{
  "title": "User",
  "description": "A user from our example app",
  "type": "object",
  "properties": {
    "firstName": {
      "description": "The user's first name",
      "type": "string"
    },
    "lastName": {
      "description": "The user's last name",
      "type": "string"
    },
    "numKids": {
      "description": "Amount of children the user has currently",
      "type": "integer"
    },
    "interests": {
      "description": "A list of what the user has shown interest in",
      "type": "array",
      "items": {
        "type": "string"
      }
    },
  },
  "required": [ "firstName" ]
}
"""

system_prompt = f"""
The user is going to discuss themselves and you should create a JSON object from their description to match the json schema below.

<BEGIN JSON SCHEMA>
{json_schema}
<END JSON SCHEMA>

Return JSON only. Do not explain or provide usage examples.
"""

prompt = """Hey there, I'm Craig Dennis and I'm a Developer Educator at Cloudflare. My email is craig@cloudflare.com.
            I am very interested in AI. I've got two kids. I love tacos, burritos, and all things Cloudflare"""

response = requests.post(
    f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}",
    headers={"Authorization": f"Bearer {api_token}"},
    json={"messages": [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": prompt}
    ]}
)
inference = response.json()
response = inference["result"]["response"]
display(Markdown(f"""
    ```json
    {response.strip()}
    ```
"""))
{
  "firstName": "Craig",
  "lastName": "Dennis",
  "numKids": 2,
  "interests": ["AI", "Cloudflare", "Tacos", "Burritos"]
}

这篇文档对您有帮助吗?