Loading video player…

Talking to an LLM From Python

00:00 Let me start by talking to an LLM directly, and I’m going to be using OpenAI. Let’s create, let’s call it a client. This is how we’re going to talk to the LLM at OpenAI.

00:12 And I’m going to then go for chat.completions, and I’m going to create a chat completion. This is the magic formula to talk to the LLM. I’m going to say what model I want to use.

00:23 In this case, I’m going to use gpt-6-luna, which is a good model that doesn’t cost too much. And then we need to pass our message or messages, because typically we’re going to have more than one.

00:34 And we need to say what role this message is. I am the user. Now, I’ve got a folder here called habits. You can actually access this. You can download this and the code we’re going to write in this session.

00:48 But I’m not going to show you what’s in habits for two reasons. First of all, it doesn’t matter. And secondly, we’re going to actually ask our agent to figure that out.

00:57 So, what’s in the habits folder? That’s the message I’m going to send to my agent. Before I carry on, since we’re using OpenAI, you’re going to need an OpenAI key, which you can place in your .env.

01:12 And then if you go to your terminal, if you’re using uv, you can simply export UV_ENV_FILE=.env. Once you’re done, we can now run this code.

01:21 Let’s call this our response. So whatever comes back from this call is our response. And let’s print our response, which gives us .choices.

01:34 It’s plural because you can have more than one response, but typically we’re going to have only one. So we take the first one. And let’s print out its .message.content.

01:43 Let’s run this. So I’m using uv. So uv run ai_agent.py. And we’ll wait a bit because we’re sending information. The AI is sending us something.

01:51 So this is what we got back. I can’t access your file system from this chat. Okay, well, a few things, first of all. I’ve managed to send a message to an LLM.

02:01 In this case, it’s gpt-6-luna. It gave me back a response. And the response is, I cannot help you because I cannot actually read what’s in that folder.

02:09 So we’ll start chipping at this and improving how much this bit of code can do.

Become a Member to join the conversation.