Mastering Generative AI with OpenAI
Implementing Code Completion
Implementing Code Completion
Welcome to our comprehensive guide on leveraging GPT-3.5 Turbo for automated code completion. In this tutorial, you’ll learn how to:
- Build interactive HTML examples
- Generate well-documented Python scripts
- Produce ANSI-compliant SQL commands for table creation and queries
By following these steps, you can streamline development workflows and improve code quality across multiple languages.
1. Interactive HTML Example
To create a more dynamic HTML demo, we’ll inject JavaScript powered by GPT-3.5 Turbo responses. Here’s an example HTML file that requests code snippets and displays them in a live editor:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>GPT-3.5 Turbo Code Preview</title>
<style>
body { font-family: Arial, sans-serif; margin: 2rem; }
#editor { width: 100%; height: 300px; border: 1px solid #ccc; padding: 1rem; }
</style>
</head>
<body>
<h1>Auto-Generated Code Snippet</h1>
<div id="editor">Loading...</div>
<script>
async function fetchSnippet() {
const response = await fetch('/api/generate-code', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt: 'Write a JavaScript function to reverse a string.' })
});
const data = await response.json();
document.getElementById('editor').textContent = data.choices[0].text;
}
fetchSnippet();
</script>
</body>
</html>
2. Generating Python Code with Explanations
Below is a Python script that uses GPT-3.5 Turbo to generate functions with inline comments. This approach ensures your code is both functional and self-documented.
import openai
openai.api_key = 'YOUR_API_KEY'
def generate_python_function(prompt: str) -> str:
"""
Generate a Python function based on the provided prompt.
Returns the generated code as a string.
"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0.2,
max_tokens=250
)
return response.choices[0].message.content
if __name__ == '__main__':
prompt_text = "Create a Python function that checks if a number is prime, with comments."
code = generate_python_function(prompt_text)
print("Generated Python Code:\n", code)
Note
Be sure to install the OpenAI Python client with pip install openai
. Keep your API key secure and never commit it to a public repository.
3. Producing ANSI SQL Commands
For dataset management, GPT-3.5 Turbo can auto-generate ANSI-compliant SQL to create tables, insert data, and run queries. Below is an example prompt and the expected output:
-- Prompt: "Generate SQL to create a users table with id, name, email; insert three records; select all users."
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE
);
INSERT INTO users (id, name, email) VALUES
(1, 'Alice Smith', '[email protected]'),
(2, 'Bob Johnson', '[email protected]'),
(3, 'Carol White', '[email protected]');
SELECT * FROM users;
Warning
Always review generated SQL before executing it against production databases to avoid unintended data loss.
References
Watch Video
Watch video content