In the rapidly evolving world of Artificial Intelligence (AI) and Machine Learning (ML), Large Language Models (LLMs) represent a significant breakthrough in the field.
They have the potential to interpret prompts in natural language and generate coherent responses. One such state-of-the-art LLM is Code Llama, a code-focused LLM built on Llama 2.
This guide explores the practice of prompt engineering with Code Llama, demonstrating how it can translate natural language into Structured Query Language (SQL) and numerous other programming tasks.
Understanding Code Llama
Code Llama, an advanced LLM, excels at generating and interpreting code from both code snippets and natural language prompts.
This suite of code-centric LLMs excels in completing code segments, managing extensive input contexts, and adhering to programming instructions without needing prior training.
Here is a snapshot of the various Code Llama models that you can access on the AI/ML API:
- Code Llama Instruct (13B)
- Code Llama Instruct (34B)
- Code Llama Instruct (70B)
- Code Llama Instruct (7B)
Advantages of Using Code Llama
Code Llama models provide numerous advantages that significantly boost coding efficiency. Some of the noteworthy advantages include:
- Enhanced Code Quality: Code Llama models can help improve the quality of your code by suggesting suitable code snippets and completing partially written code segments.
- Increased Productivity: These models can streamline the coding process, thereby boosting the productivity of programmers.
- Support for Complex Codebases: Code Llama models are capable of handling complex codebases, making them a valuable tool for developers working on large projects.
- Ease of Use: With clear and straightforward natural language prompts, the use of Code Llama models becomes extremely user-friendly, making them an excellent choice for both novice and experienced coders.
The Power of Prompts
Prompts are crucial for guiding LLMs in translating natural language into SQL queries. Providing clear instructions in the prompt is essential to control the model's output accurately. For instance, directing the model to articulate the query between ## can streamline verbose output.
Use Cases for Code Llama Models
The Code Llama models offer a plethora of use cases that elevate software development, including:
- Code Completion: Code Llama models can streamline the coding process by suggesting code snippets and completing partially written code segments, enhancing efficiency and accuracy.
- Infilling: Code Llama models can address gaps in a codebase quickly and efficiently, ensuring smooth execution of applications and minimizing development time.
- Instruction-Based Code Generation: Code Llama models can simplify the coding process by generating code directly from natural language instructions, reducing the barrier to entry for novice programmers and expediting development.
- Debugging: Code Llama models can identify and resolve bugs in code by analyzing error messages and suggesting potential fixes based on contextual information, improving code quality and reliability.
Prompting Techniques with Code Llama
Various techniques are available for effectively prompting Code Llama. These techniques range from simple phrases to detailed instructions, depending on the task requirements and the capabilities of the model. Here are some of the widely used prompting techniques:
- Basic Direct Prompting: This involves using simple, straightforward prompts to guide the model.
- Role Prompting: Role prompting involves assigning roles to the model, which can help guide its responses.
- Few-Shot Prompting: Few-shot prompting involves providing the model with a few examples of the desired output, which helps the model understand the task better.
- Chain-of-Thought (CoT) Prompting: This technique involves providing intermediate steps to the model, which helps guide the model's response.
- Self-Ask Prompting: This technique involves breaking down complex prompts into simpler tasks, which can help the model generate more accurate responses.
Configure Model Access
First, ensure you have the necessary libraries installed:
API_KEY which you you can obtain Here. We then set the base_url as https://api.aimlapi.com/ which will allow us to use the familiar OpenAI python client.
Import the libraries and configure your API access as follows:
Define a function for easy model querying:
Basic Code Completion
Test the model with a simple prompt, such as generating a Python function for the nth Fibonacci number:
Output:
This function calculates the nth Fibonacci number using recursive technique. The base cases are 1 and 2, and the recursive case is the sum of the previous two numbers.
This function is a classic example of recursion, a powerful concept in computer science where a function calls itself to solve a problem. The base cases for n == 1 and n == 2 ensure that the recursion has a clear stopping point, preventing infinite recursion. For any value of n greater than 2, the function calls itself with the arguments n-1 and n-2, summing these calls' results to compute the nth Fibonacci number.
The test loop at the bottom runs the fibonacci function for the first 10 numbers in the Fibonacci sequence, printing each result. This is a straightforward way to verify the function's correctness and observe the sequence it generates.
Debugging
Use Code Llama to identify and fix bugs in your code.
For example:
Output:
Here's a Python function for the nth Fibonacci number using the recursive formula:
This function calls itself with the `n-1` and `n-2` values until it reaches the base case of `n <= 1`, where it returns the result.Alternatively, you can use a loop to compute the Fibonacci sequence:
This function uses a loop to compute the Fibonacci sequence by keeping track of the previous two terms and adding them together to get the next term.You can also use a generator expression to compute the Fibonacci sequence:
This function uses a generator expression to compute the Fibonacci sequence by keeping track of the previous two terms and adding them together to get the next term.All three functions will give you the same result for the nth Fibonacci number.
Writing Unit Tests
Code Llama can also assist in creating unit tests for your code. Here's how you might ask for help writing tests for a function:
Output:
Here is an example of how you could write unit tests for a function that returns unique elements from a list:
This test case covers the following scenarios:
- An empty list
- A list with a single element
- A list with multiple elements
- A list with duplicate elements
The get_unique_elements function is tested by calling it with different inputs and comparing the result to the expected output. The assertEqual method is used to check that the result is correct.
You can run this test case by saving it to a file (e.g. test_get_unique_elements.py) and running it with the Python interpreter:
This will run the test case and print the results. If the test case passes, you should see a message indicating that all tests passed. If the test case fails, you will see a message indicating which test failed and why.
Text to SQL Conversion
Prompt engineering significantly aids LLMs in generating the most suitable response for a given purpose and context. It becomes especially crucial in enterprise business applications that require responses with a thorough understanding of the intent and context of the request. Some practiced prompting techniques include basic direct prompting, role prompting involving model role assignment strategies, few-shot prompting incorporating in-prompt demonstrations, chain-of-thought (CoT) Prompting with intermediate step guidance, and self-ask prompting involving input decomposition.
When generating SQL from natural language with LLMs, offering precise instructions in the prompt is vital for controlling the model's output. In our experimentation with Code Llama models, an effective approach is to prompt description with the following string within the prompt to annotate distinct components:
Output:
Here is an SQL query that selects the customer names and the total amount of the order for all orders made after January 1, 2022, sorted by the total amount of the order in descending order:
This query uses the SUM function to calculate the total amount of each order, and the GROUP BY clause to group the orders by customer name. The WHERE clause is used to filter the orders to only those made after January 1, 2022. The ORDER BY clause is used to sort the results by the total amount of the order in descending order.
Few-shot Prompting with Code Llama
Few-shot prompting involves providing the model with a few examples of the task at hand, followed by the actual task. This method can significantly improve the model's performance on complex tasks:
Create dataset includes a set of students with their names, nationalities, overall grades, ages, majors, and GPAs.
Now, we're ready to set up our few-shot examples and the corresponding prompt (FEW_SHOT_PROMPT_User) that includes the question from the user, for which we aim to have the model produce accurate pandas code.
Lastly, we present the concluding system prompt, the few-shot examples, and the ultimate question from the user:
Output:
The prompts and examples for the pandas dataframe were inspired by the recent research conducted by Ye et al. in 2024.
Function Calling
While the specific API configuration mentioned does not directly support function calling, you can simulate this by integrating external APIs or functions into your application logic and using Code Llama to guide the interactions.
This example will demonstrate how Code Llama could be used to guide interactions with a hypothetical currency conversion function.
In this scenario, the assistant is tasked with converting an amount of money (100 USD) into three different currencies (EUR, GBP, and JPY) using the convert_currency function. This example showcases how an application could integrate with external APIs or functions to provide dynamic responses based on user queries, even when the specific API configuration doesn't natively support such function calls.
Safety Guardrails
It's important to implement safety guardrails when interacting with AI models to prevent the generation of harmful or unethical content. Here's how you might approach setting up these guardrails:This example will demonstrate how to handle a request that could potentially lead to sharing sensitive or harmful information, by redirecting the conversation towards a safe and constructive response.
Output:
Now let's try to remove the system prompt:
Output:
Conclusion
In conclusion, the art of prompting LLMs offers unparalleled flexibility and ease of use, making advanced AI accessible to a wide audience. From direct and contextual prompting for straightforward queries to few-shot and chain-of-thought techniques for complex problem-solving, these methods unlock the vast potential of LLMs. They allow for rapid prototyping, efficient data processing, and customized responses across various contexts. Notably, contextual and chain-of-thought prompting enhances the models' problem-solving capabilities by simulating a step-by-step reasoning process. Furthermore, the adaptability of these models to different tasks through simple prompting reduces the need for extensive retraining, saving valuable time and resources.

.webp)
.webp)
.webp)


.webp)