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

Creating a Chat Application

00:00 In the last lesson, you sent a single message to the model and got back a response. Now let’s look at how to do that multiple times over and over to create a basic chat application. Again, from ollama import chat.

00:18 Then initialize your messages list. messages equals an empty list. Now you’re going to get the prompt from the user instead of having it hard-coded.

00:28 So prompt equals the input() function. Call that. And inside, you’re going to put the prompt that the user sees. So it could be a string.

00:40 "Send a message", colon, space. So the user will type something into the console, press Enter, and it will save that response as a string in prompt.

00:53 Then add on to messages a new message from the user. messages.append(), and inside, create a new dictionary that has a key of role and a value of user.

01:11 Then a key of content, and the value is going to be the prompt variable from earlier. Then you’re going to send the messages to the chat() function.

01:22 So response = chat, call the function, pass in model = llama3.2 again, and messages = messages.

01:38 Now we want to print the response. So print response.message.content. And you also want to save the response’s message to the list of messages.

01:53 So messages.append(response.message).

02:01 So now the response is being saved to the list of messages. And then you want to do everything over again from the prompt input onwards. So just above calling the input() function, you can add a while loop, while True, colon, and then indent everything else so that it all loops indefinitely.

02:28 And you could always check to see if the user quits by pressing Q or something, but for now, let’s just rely on them closing the application through Ctrl+C or Ctrl+D or closing their terminal.

02:42 All of those should work. So now you can run it. It prompts you to send a message,

02:50 Define list comprehensions in one sentence. And it will think a little bit,

03:01 return and print the message. And then in your next message, you can say something like, Provide a short practical example. And you don’t have to specify that you want the example from list comprehensions because that message is also being sent over and the model will read the previous messages and understand the context.

03:25 So the response does give a list comprehension example. And I can exit, Ctrl+D.

03:36 And this code is going to be inside of chat_with_history.py.

03:42 Finally, when you’re interacting with more traditional AI interfaces, it’s not thinking for a long time and then responding all at once. Often, you’ll see the message appear gradually as if the model is responding in real time and typing it out.

04:00 You can do that as well. If inside of calling chat(), you add stream = True. I’m not going to add it to this example, but there is some code in the additional course content called chat_stream.py.

04:17 And if you use stream = True when you’re calling the chat() function, that’s not going to return the full response. That will return a message with a content, and just a little bit of the content, just a chunk.

04:32 So in order to print it out, the stream is going to be an iterable and you can print out each chunk in the stream, add some things to the print() function here just so that it doesn’t wait for the full message or print newlines in between each chunk.

04:49 But you can see that it is going to print out to the console little bits at a time. So you can combine these if you want the stream = True with the chat application.

05:04 The trick there is going to be that you need to add the full message to the list of messages. So you kind of have to reconstitute it from the chunks.

05:16 And that was how to use the chat() function. Next, you’re going to look at how to use the generate() function.

Become a Member to join the conversation.