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

Reviewing Ollama's `chat` and `generate` Functions

Resources mentioned in this lesson:

00:00 Python’s ollama library has two main ways of interacting with it. One is the chat() function, which allows you to send messages back and forth with a model.

00:08 The other is the generate() function, which is for one-off prompts that return a single response. So what’s the difference between these and why would you use one over the other?

00:17 One main difference is that the chat() function understands context and can use details from previous messages when formulating the next response.

00:24 It also supports a feature called tool calling where you can pass a custom Python function in that the model can use when responding. So if you’re creating an application with multi-turn interactions that understand context like an AI assistant or chatbot, use the chat() function.

00:41 On the other hand, if you’re able to put all the necessary context into a single prompt, then the generate() function is more appropriate. You can use it for summarizing, analyzing, editing, or generating text, whether that text is a poem, cover letter, or code. So while the generate() function is pretty similar to the chat() function, if you only need to perform one-shot tasks, it has a simpler interface and is better suited for non-conversational applications.

01:09 In this course, you’ll use it to generate some Python code. A few useful resources for you to look at are in the GitHub repository and the REST API docs. The GitHub repo includes a folder called examples, as you can see here.

01:26 It provides small code snippets for different features, including chat.py, chat-with-history.py, chat-stream.py, generate.py, and much more. The GitHub repository, though, isn’t the best place to look at if you want thorough documentation.

01:41 That’s because the Python package is designed around the official Ollama REST API.

01:47 Therefore, you should use the REST API documentation for those details. So if you want to know the specific arguments you can pass into the functions, you can see generate and chat are here, and these are the arguments it takes in.

02:05 And then if you also want to know the fields on the response, you can look here.

02:13 Now that you’ve seen the difference between the functions, let’s look at interacting with Ollama’s chat interface.

Become a Member to join the conversation.