In this lesson, you'll build a circle around. You'll then observe situations where the model hallucinates. Let's go take a look. So let's first take a look at the SQL workflow with mama three. The user asks a question. The model takes in that question and produces SQL that SQL is run against a database and then the user sees the response. So an example is asking who's the highest paid NBA player. And the answer from lemma three is to answer this question. And we can use the following SQL light query and produces a SQL query here. And you always want to start with something easiest to iterate on to improve the performance of the model. So let's start with prompt engineering. Iteration is key for this step, and probably every single step you'll be working with today. So for SQL, a one pro tip is to include the schema and include its formatting. What I mean here is let's take a look at this prompt. So who is the highest NBA player? The user's question. This is some prompt says you're an NBA analyst with 15 years of experience running complex SQL queries. So is this telling the model what kind of setting they're in, who they are, and then adding an appropriate schema? So you see the schema here. It's showing, you know the different columns the types. And then it provides an example. And why is this important. Well let's just take a look at you know here it says team name jersey age height weight. Let's take a look at height here. If the height text actually didn't have the space for six foot seven inches, then that's a completely different format, right. So you would want the model to be producing pretty different SQL so that it actually executes correctly. So that's an example of why adding the formatting here or an examples is actually pretty critical. And you can see it just iterating. We're removing those examples and the model will do worse. So start with prompt engineering. Iterate here on on what you want to put in this system prompt. So out of the box the line as you can tell, produces a lot more than just SQL. It produces. You know, to answer this question we can use the following SQL like query produces the query and then explains the query a little bit. It produces way more than that SQL statement. And that makes it hard for you to then go take that SQL statement and executed against your database. You need to go, you know, maybe do another model call, maybe do something to extract and parse out that SQL query that you ultimately want. So instead, you can apply structured output to ensure the model only outputs SQL and instead outputs it, for example in a dictionary format or Json format. So you have a SQL query. Here is the key and only the SQL query coming out. That way you can programmatically then use that SQL query to execute against your database. And in the lab you'll explore exactly how to enforce a structured output with 100% format accuracy. Next is diagnosing these nasty little hallucinations. So again the SQL query can either be invalid or malformed. For example, the salary format can be wrong. So here again the salary is casted as a string which is malformed. It is a valid SQL query, but when it runs it gets the wrong output. It executes to exotic Bay, who makes about 4.5 million in this database. At least I think he is earning a bit more now, but instead it should actually be Steph Curry. So when you cast it here instead as an integer, the salary, you know, with a dollar sign, removing that as an integer, you're able to then sort an order by that number much more correctly than if you were ordering a string. So diagnosing hallucinations is a very iterative process, just like everything you'll see here. as you improve the model, you'll continue to just dig for what those hallucinations are and what the boundary of correct and incorrect is for the model. And this is a very important, skill because it will actually help you point the line in the right direction. Give the lamb a north star to climb to, to tell it, you know, hey, you need to improve here and it will improve for you. It's very good at optimizing. It just needs your help on knowing what to optimize for. So get good at this stuff. This is an important one. It'll help you actually be able to create the best models, because you have now tools to remove those hallucinations. Great. So now let's move on to the lab. So now you explore running all of this that you just saw in code. And specifically we're going to load some environment variables here for an API key. So we're going to load that up. You're going to use this library called alumni. So alumni makes it easy to run these open models as well as fine tune them and memory to them to reduce hallucinations, which you'll go over quite soon. So import that library next. I'm just going to put some, you know, imports here. to connect to the SQLite database. See here. Cool. So now time to instantiate your loom here llama three. Actually the model name here can be any, any open model from hugging face. Next llama three uses these as you explore previously a prompt template. So let's add that here function to handle that okay. Next we'll explore how to put the schema in first. You can actually just you know put the schema in like this. This is probably just all the columns. No examples as you can see. And we'll explore with that how that changes things. But in the user's question who's the highest paid NBA player. and then let's add in that system prompts. So that system prompt, you're an NBA analyst with 15 years of experience writing complex SQL queries. So giving the model an identity, consider the NBA roster table with the following schema. And then you would just put the schema here and write a SQL like query for the following. And I'm actually just going to print that out so you can see it fully. See. So it looks like that perfect. Let's make the prompts. And then let's have the LM generate from that prompt. Print out the output, see what it says. Okay so that's interesting. But it actually doesn't really follow the formatting in our SQL table. So let me add some formatting into the updated schema. So update schema. All I'm doing is adding these examples in So let's do that get updated schema. And all we have to update now is that system prompt. So get updated schema and be able to have this updated prompt. Here. You can just print this out so you can see it. Cool. There it is with the updated schema. And then let's ask the model to output updated response. Great. Fantastic. Now it's filtering out where the salary is. Essentially you know what this database is storing as null right. So no like this perfect okay. So there's a lot of information kind of being produced by the model in addition to the SQL query. What should we now do for structured output. So instead of just saying lm dot generate and you know max tokens, then we're going to actually specify a certain output type. And here I'm specifying, you know output type SQL like query as the key and the type string. So it should be a dictionary that comes back with SQL query and some string from it. And that's it. So let's run this and then look at the results. Great. So go like query and just the SQL here. And this is included into the prompt of the model. And output type is restricting the model to only I'll put a string here. So that's 100% enforced. So now we can actually take this SQL query and be able to run it against our database directly. Okay. So let's read that SQL in using the ID database engine. let's take a look at that data frame okay. Cool city pay great. However that is actually incorrect. So that's a hallucination. And let's just take a look at what the right query looks like. So first the wrong query just formatted well it looks like this. It is casting the salary to a real. So that is in a string in the database, which you know, that's totally understandable. If you look at the schema above, you'll see that it is in fact a string. however, if you want the highest paid person you know salary, then you have to realize that when you order a string this dollar sign and it's not actually ordering an integer, right? So it's ordering at a very differently. So that's why the response is wrong. Even though it was able to execute against the database correctly. So that's the wrong query. But the correct query looks something like this. So casting this into an integer and then being able to do that order by removing that dollar sign. Cool. So let's take a look at what that query does. So we just run that query against the database. We're going to get Steph Curry instead. So that's a hallucination. And you'll explore how to exactly remove these hallucinations when you find them. But it's really important to dig deep in and be able to see when the model is actually producing the wrong output. Something I encourage you to do is ask even more questions, right? This one was asking about who is the highest paid NBA player. Ask more questions and discover more. Hallucinate and understand what the boundary of what this alum can do. Or not. On top of this schema, you can continue to iterate on the schema, improve exactly what's in this entire prompt as you give it to the model. and finally, in structured output, feel free to play with this key. This key actually does go into the prompt of the model. It's basically enforced as the initial output says force as this. so it's enforced there. So it does actually impact what kind of tokens come after it. So these are fun different parameters to play with as you continue to diagnose and and find hallucinations. In the next lesson, you'll learn about how to evaluate the model much more rigorously to understand quantitatively what's going on with these hallucinations.