Resources mentioned in this lesson:
Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Using Ollama's Text Generation Interface
00:00
In the last two lessons, you used Ollama’s chat() function. But if you don’t need to have a conversation or understand context, then the generate() function is great for one-shot prompts.
00:13
Here’s how to use it. Start off by importing the generate() function. So from ollama import generate. Instead of having a list of messages, you can just have a single prompt string.
00:28
So prompt equals a string, "Describe Python in one sentence."
00:36
Next, you’ll call the generate() function, and it’s going to return a response. So response = generate, call it, and pass in a model.
00:47
You can use llama3.2, like last lesson, and prompt = prompt. So you’re just passing in a string instead of a list of messages. And now you can print response.response
01:05 and run it to make sure that it works.
01:10 So you can see that this is a simpler interface. You’re not dealing with messages or roles, and this function is convenient for performing one-off tasks like summarizing, rewriting, and rephrasing text.
01:25 In the next lesson, you’ll see how to use it to generate Python code.
Become a Member to join the conversation.