audio audioduration (s) 20.1 30 | text stringlengths 264 581 |
|---|---|
If you're using large language models in production, you'll want a way to log the prompts and the responses. This is useful to evaluate the performance and check for quality in the responses. And it's also useful to gather data that can later be used for fine tuning. This is true, whether you're using private model... | |
a hosted offering and also offer a way for you to do it on a self-hosted basis. But today I'm going to show you a very simple approach that allows you to take any OpenAI style endpoint and ensure that all of the data is being logged to a Postgres database. You can run that Postgres database locally, or you can run ... | |
It's also a package that has been pushed to PyPy. So you can easily install this with pip install Trellis OpenAI Logger. I'll start by showing you a very quick example of how to use this logger to log the results or the response that comes back from a quick chat completion. And then I'll show you how to set up a lo... | |
And I want to show you how to quickly start and use the Trellis OpenAI Logger. It's very much the same as if you're normally importing the OpenAI library. So instead of importing OpenAI from the original library, you import it from Trellis OpenAI Logger. And you use it in very much the same way. You set up a client... | |
You pass in optionally your API key and your base URL if you're going to be using your own custom URL or API key. If you're just using OpenAI, you don't need to specify these. But there's one extra parameter you pass in when initializing your client, which is the Postgres URL here. And here I've just put in a local ... | |
I'm going to set up a temporary folder. I'll just make a directory here called temp. And I'll move into that temporary directory. Then I'll create a virtual environment to UVVenv. And I'll install Trellis OpenAI Logger, which is just like installing OpenAI, because this is a wrapper that will send and log your dat... | |
And get a response from OpenAI using GPT 4.1 Mini and print the response. So I'm going to copy all of this. Now before I do that, there's one more step I need to do, which is export my OpenAI API key. So I need to paste it in there. I'm just going to do that off screen now. Okay, I've entered my API key. And now... | |
I'll paste in that little snippet. And press Enter. And you can see I get back a response. And it's got the answer 1 and 2 added is 3. So that's good. And I'll exit now my interpreter with Control-D. So at this point, I've just sent a query to OpenAI. And now it should be logged. We can take a look at what the ... | |
we can connect to the Postgres database locally. And I'll go through this more slowly in a moment. And we can then make a query to inspect the last conversation. Just like this. And here you can see the latest record. So you can see the time, the model, the input messages, of which there's just one. The system me... | |
And now I'm going to show you how you can set up that local database. Or how you can point Trellis OpenAI logger to a database that's running on a digital ocean droplet. So first I'll show you that local setup. Just go to the database section. And you're going to have to set up some environment variables. Already ... | |
And also you should set a secure password. I recommend not using your secure password. You should put in something else. So you want to create a .env file with these variables. You can just copy paste this code and run it if you wish. Next, we'll actually set up the local Postgres database. If you're on Mac, you ... | |
Now, it'll probably say it'll fail because I've already created it. That's fine. And once it's created, we're going to run this local setup here. Now, I actually should be back here in the logger folder. And I'll run the setup. And just to show you what's happening with the local setup here, we're installing dbMat... | |
We're then setting a database URL for dbMate, which will help us with the migrations. And then we're running the migrations. And the migrations are set here in this dbMigrations folder. And it's setting up the way that we store the data. So we want to store the model name, the endpoint, the input messages, which is... | |
And then we can run a quick test just to check that the local database is working. And it looks like it is because we successfully connected. That's a simple connection test. And if you do want to delete that database, you can do drop db with LLM logs. So this is how we create the local database. If you want to do... | |
So to run that, you first want to source the environment variables, which basically will pass in the database password. And then run example.py. And when we check this here, this is not working because in my environment variables, I have got an old database URL. | |
And this is actually the correct approach to use if we're going to run from a droplet because we want to point to a remote URL. But we can just set the database URL for the local database like this here at the same time as running. So if we just take this command, I should have used the local command here because we ... | |
And the request you can see here in example.py. It's getting the connection info for the database from a URL. It's testing the connection. And then it's going to load up the database URL and create a client using that URL. It's creating a trial. So open AI logger client. And then it's getting a response to a simp... | |
And then it's going to query the log data to make sure that it is actually present and telling us that it has successfully logged and found logs within the database. So that's just a simple script you can run to test your database is working. If you want to more manually query the database, you can do exactly what we... | |
And it should show us. Let's just run that again. It should show us the last request. Indeed, it says hello. And here's the response and the latency and then the total tokens. So that's how you set up a local database. It's pretty fast. I'll now show you ways to set up a remote database. This is what you would ... | |
And I'll talk much more about retrieving the logs and how to use them for doing evals and doing fine tuning in a later video. So let's look at how we set up a remote database this time, a remote Postgres database. And for that, we go up to option two. Now, there are basically two ways you can do this with DigitalOce... | |
The first way is you set up a Ubuntu server and you'd install Postgres and you use that as your database. The second approach is DigitalOcean and other providers will directly provide databases. And so you can directly set up a database using DigitalOcean. That's called a managed database. Now, if you set up a drop... | |
Now, you can enable backups on a standard droplet too. But the managed database probably has got more tailored functionality for running databases robustly. So maybe use this if you're doing a more serious production application. I'll also note that to run Postgres comfortably, you probably want to have two gigabyte... | |
And therefore, probably it's going to be about $12 per month if you go with the specs that are like this here in my default. So I'll show you how to run both. First, we're going to make sure that the DigitalOcean client is installed with brewinstall.tl or D-O-C-T-L. That's DigitalOcean Client. And we're going to th... | |
So we're going to create an SSH key. And then we're going to add it to DigitalOcean so it can be used to access the droplet. So here's how you would generate an SSH key. I've already generated this one. And here's how you would add it to DigitalOcean. Okay. And with the SSH key generated, we can now proceed to th... | |
It will delete any droplets of the same name if you've created any in advance. So that's what it's doing right there because I had created one for testing earlier. And then it should move to starting up the next droplet. I'll show you now what that startup script looks like. It's the create droplet script here. It... | |
If there is none, it's going to then use this default, not very secure password here. So here is a quick validation and a warning if it's using the default password. Then it's going to create an SSH key if it doesn't exist already and you haven't created it. And once that is created, it'll move on to delete any drop... | |
So these migrations match the migrations in the migration file here. I know this isn't ideal really from a coding standpoint. You should have one source of truth. So if you do need to update the migrations, you do need to do it in two places right now. And there's a warning on that in the readme. But this format h... | |
Then there's going to be a symlink here for dbmate's expected structure. So this is allowing dbmate to run the migrations. And once we've run the migrations, then we have to define the run command, which is going to involve creating this database here. First of all, starting Postgres, creating the database, then upd... | |
creating the cryptography extension, and then restarting Postgres here. And then we're going to run the setup db script, which should allow us to have the database in the right format with the correct tables. So with that script defined, this is a script that will run after initialization, after the droplet is create... | |
We're going to specify a 2GB droplet with this droplet name. And once the droplet is ready, we're then going to, well, the cloud init should start. Once the droplet is ready, we'll get the IP. We'll then create the droplet, and we'll print out the droplet IP and the connection string once that's all been done. Then... | |
and then run the example on that remote droplet. So you can see here, this matches what you can see in the script. We've got the droplet being created. We've got this database URL. I'm just going to copy this over into my .env file now. And with that done, I can try sourcing my environment variables and running th... | |
And again, we can connect manually to see the logs if we like. This time, rather than connecting to the local database, we can source the environment variables, which will set the database URL and connect in this way. So now we're connected to the remote database. And again, we can use this query to check what is l... | |
I'll show you now the last way to set up the Postgres database, which is by directly creating a database on DigitalOcean. Just before then, if you want to clean up your droplet, you can run this cleanup droplet script. First, you need to exit from the Postgres, which I can do here, just with Control-D or slash Q. So... | |
And I'm going to say, no, I won't delete the SSH key from DigitalOcean. So for a managed database, I'll take you through the steps here. First thing is, we're literally going to create a database using the creation command from DigitalOcean. We're going to call it LLM loggerDB. And once we've got that created, we'r... | |
So if you want to get it, again, you could run this command, but we can actually find the details here. And you want to put this into your .env file. And I recommend putting it in between double quotes. So the way you want to paste it in is something like this here. So I'm going to take that and put it into my .env... | |
And all this does is it runs the migrations that are in the DBMigrations folder. So this is setting up the structure again that we want the logs to be stored in. Now, I can't run this just yet because I need to wait until the database has started up. I can maybe try it and we'll see. So, yeah, it's saying no such h... | |
Once it is started up, though, it will install DBMate, run the migrations, and then we should be ready to test the connection. And I've just given it a moment so I can run this script again. And I think that is run successfully, so we're in a position to run the example file. It's connecting to the remote database. ... | |
And we can query those logs, first by connecting to the database, by sourcing environment variables, and then by running our command here. And we should be able to now see, again, let me just run that. And you can see here the log of the command that we've run through the example. So that rounds up the three differe... | |
And you now are able to very easily log all of the traces, including the response times and the token usage of any queries you make to an OpenAI-style API. I've just shown you how to use actual OpenAI's API, but you can also use any other APIs that provide you with an OpenAI-style endpoint. In the next video, or in a... | |
They're also very useful if you need to do some fine-tuning, because you can take these responses from customers, you can filter out the private data, potentially, and then you can improve on the answers and use those to improve the performance of your LLMs through fine-tuning. That's it for this video, folks. Hope y... |
README.md exists but content is empty.
- Downloads last month
- 5