Explore Code Generation Using DeepSeek Coder Models
A handy way to explore all of the models available on Workers AI is to use a Jupyter Notebook.
You can download the DeepSeek Coder notebook or view the embedded notebook below.
For more Cloudflare video content, check out our YouTube channel.
Exploring Code Generation Using DeepSeek Coder
AI Models being able to generate code unlocks all sorts of use cases. The
DeepSeek Coder models @hf/thebloke/deepseek-coder-6.7b-base-awq
and @hf/thebloke/deepseek-coder-6.7b-instruct-awq
are now available on Workers AI.
Let’s explore them using the API!
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)
Configuring your environment
To use the API you’ll need your Cloudflare Account ID (head to Workers & Pages > Overview > Account details > Account ID) and a Workers AI enabled API Token.
If you want to add these files to your environment, you can create a new file named .env
Generate code from a comment
A common use case is to complete the code for the user after they provide a descriptive comment.
Assist in debugging
We’ve all been there, bugs happen. Sometimes those stacktraces can be very intimidating, and a great use case of using Code Generation is to assist in explaining the problem.
The error in your code is that you are trying to use a variable name
which is not defined anywhere in your function. The correct variable to use is first_name
. So, you should change f"Hello, {name}!"
to f"Hello, {first_name}!"
.
Here is the corrected code:
Now, when you call hello_world()
, it will print “Hello, World” by default. If you call hello_world("John")
, it will print “Hello, John”.
Write tests!
Writing unit tests is a common best practice. With the enough context, it’s possible to write unit tests.
Here is a simple unittest test case for the User class:
In this test case, we have two tests:
test_full_name
tests thefull_name
method when the user has both a first name and a last name.test_default_last_name
tests thefull_name
method when the user only has a first name and the last name is set to “Mc” + first name.
If all these tests pass, it means that the full_name
method is working as expected. If any of these tests fail, it
Fill-in-the-middle Code Completion
A common use case in Developer Tools is to autocomplete based on context. DeepSeek Coder provides the ability to submit existing code with a placeholder, so that the model can complete in context.
Warning: The tokens are prefixed with <|
and suffixed with |>
make sure to copy and paste them.
Experimental: Extract data into JSON
No need to threaten the model or bring grandma into the prompt. Get back JSON in the format you want.