Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Generating Python Code With Ollama

00:00 The generate() function might not seem as powerful as the chat() function, but when you’re not creating chatbots, it’s often the preferred choice for interacting with language models in code.

00:10 Tasks like summarizing, translating, or analyzing inputs are perfect for the generate() function. Now let’s get meta and use Python to generate Python code.

00:20 In this example, you’ll generate code for a classic interview question called the FizzBuzz challenge. First, you’re going to want to use a model that’s more geared towards generating code.

00:33 The one we installed earlier was codellama. And then replace your prompt and use a multiline string. Now you’re going to provide the spec for what you want the model to return.

00:47 It can be detailed or concise. It would be something like this. """Write a Python function and we’re going to call it fizzbuzz().

00:58 You can provide this exact signature here, including type hints. So it’s going to take an n, which is an integer, and return a list of strings.

01:13 Then you can define what you want it to do. Returns a list of strings for the numbers 1..n. Use "Fizz" for multiples of 3, and use "Buzz" for multiples of 5.

01:42 Use "FizzBuzz" for multiples of 3 and 5. Use the number as a string

01:58 otherwise. And raise a ValueError if n is less than 1. Then you can add additional prompts or suggestions for your prompt, like Use type hints compatible with Python 3.8, since the ollama package is compatible with Python 3.8. And then to narrow it down further, sometimes when I run this, it will give me lots of description or add a docstring or use different methods for generating the code. I want to limit it to, say, Don't include a docstring or explanation, just so that it’s nice and short for this example.

02:52 Okay, so you can run it now

02:55 and copy and paste the results. Notice that it still gave me a docstring.

03:04 You can try it out in a new file

03:11 called fizzbuzz.py. And when you test it, you should also print out the results of calling fizzbuzz() with some number, like 16.

03:27 So run it, and you can see that it does in fact work. Because this is AI, make sure that you’re always reviewing, running, and testing the code that it creates, and edit the prompt and run it again if you’re not happy with your original result, or just edit the final code into something that works. I didn’t want a big long docstring, and thankfully the docstring it gave was pretty short, so we can keep that in.

03:55 The thing that it’s missing is that the type hints are not compatible with Python 3.8, because the built-in list doesn’t work with Python 3.8 unless you add a line to the top that says from __future__ import annotations.

04:13 So that’s it for getting started with Ollama and Python. In the summary lesson, you’ll get a review of what was covered, some additional resources, and suggestions for features to look into next.

Become a Member to join the conversation.