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

Calling Ollama's Chat Interface

Resources mentioned in this lesson:

00:00 Now let’s start coding and interacting with Ollama’s chat interface. First, at the top, you’re going to import the chat() function from ollama.

00:09 So, from ollama import chat. Next, you can initialize a messages variable.

00:17 messages equals a new empty list. And you’re going to fill the content in later. Then you’re going to call the chat() function, and it’s going to return a response.

00:27 So, response = chat(),

00:32 and pass in model = llama3.2. That’s the model you installed in a previous lesson.

00:42 messages = messages. Then you can print the response. print (response). It’s going to have a message. And then that message is going to have some content. And that’s what the model is responding with.

00:59 Now, to actually send a message, you’re going to, inside of the messages list, add a dictionary. It needs a key of role. And to specify the user that’s interacting with it, so you, the role is going to be user.

01:17 And then you’re going to have a content key, and it’s going to contain the prompt that you want to send to the model. For example,

01:28 "Describe Python in one sentence".

01:32 Now you can save it and run it.

01:40 And it should print out to the console a description of Python in one sentence. So this is just using a single message. It’s not looking at previous messages and understanding context.

01:52 There’s no history. If you were to add history to it, you would just add additional messages to the messages list, where the earlier messages are at the beginning of the list and later messages are at the end. Now, without creating a full chat application yet, we can add another message at the top.

02:12 So at the beginning of the list, add a new dictionary. Make sure to add a comma afterwards. And add another message with role of system.

02:24 The system role will tell the model how you want it to respond. So the content here could be something like, "You are a pirate".

02:39 And it will talk to you like a pirate. And if you’re curious about what the role of the model is when it responds, you can print at the very end of the file response.message.role to see how the model is responding. So save that and run it.

03:00 And now it describes Python, but in pirate speak, and it has a role of assistant. So this code is going to be in the file in the accompanying course code content as chat_no_history.py. Next, we’ll look at using this code and turning it into a simple chat application.

Become a Member to join the conversation.