AI-enhanced AI-powered Code Completion Python, AI, ML
👤 Sharing: AI
```python
import openai
import os
# Set your OpenAI API key. **IMPORTANT: NEVER commit your API key to a public repository!**
openai.api_key = os.getenv("OPENAI_API_KEY") # Ideally, set this as an environment variable
def generate_code_completion(prompt):
"""
Uses OpenAI's Codex model to generate code completions based on a given prompt.
Args:
prompt: The Python code or comment prompt to use for code completion.
Returns:
The code completion generated by the AI model. Returns None if an error occurs.
"""
try:
response = openai.Completion.create(
engine="code-davinci-002", # Or try "code-cushman-001" for potentially faster and cheaper but less capable
prompt=prompt,
max_tokens=150, # Adjust as needed; controls the length of the completion
n=1, # Generate one completion
stop=["\n\n\n"], # Stop generating when a triple newline is encountered (common in code)
temperature=0.2, # Adjust for more/less randomness (0.0 is very deterministic)
)
completion = response.choices[0].text
return completion
except Exception as e:
print(f"Error generating code completion: {e}")
return None
def main():
"""
Demonstrates AI-powered code completion using OpenAI's Codex model to write a simple
machine learning example (linear regression).
"""
# Example 1: Generate a function to load data
prompt1 = """
# Python function to load data from a CSV file into a Pandas DataFrame:
import pandas as pd
def load_data(filepath):
\"\"\"Loads data from a CSV file into a Pandas DataFrame.
Args:
filepath: The path to the CSV file.
Returns:
A Pandas DataFrame containing the data, or None if an error occurred.
\"\"\"
"""
completion1 = generate_code_completion(prompt1)
if completion1:
print("Generated code for loading data:\n" + completion1)
# Example 2: Generate code for a linear regression model
prompt2 = """
# Python code to build and train a linear regression model using scikit-learn:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Load the data
data = pd.read_csv("data.csv") # Assuming you have a data.csv file
# Preprocess the data (simple example)
X = data[['feature1', 'feature2']] # Replace 'feature1', 'feature2' with your actual feature columns
y = data['target'] # Replace 'target' with your actual target column
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create and train the linear regression model
model = LinearRegression()
model.fit(X_train, y_train)
# Make predictions on the test set
predictions = model.predict(X_test)
# Evaluate the model
print("Mean Squared Error:", mean_squared_error(y_test, predictions))
# Now, complete the code to save the trained model to a file:
"""
completion2 = generate_code_completion(prompt2)
if completion2:
print("\nGenerated code for saving the model:\n" + completion2)
# Example 3: More concise prompt
prompt3 = """
# Python: Create a function to calculate the factorial of a number recursively:
"""
completion3 = generate_code_completion(prompt3)
if completion3:
print("\nGenerated code for factorial function:\n" + completion3)
if __name__ == "__main__":
main()
```
Key improvements and explanations:
* **Clear Error Handling:** The `generate_code_completion` function now includes a `try...except` block to catch potential errors from the OpenAI API. It prints an error message and returns `None`, allowing the main function to gracefully handle failures. This is *crucial* for real-world applications. Without this, an API error will crash the program.
* **Concise and Clear Prompts:** The prompts are carefully crafted to guide the AI. They use comments to explain what the code should do, and include initial code snippets to provide context. Good prompts are essential for getting good completions.
* **Environment Variable for API Key:** The code now correctly uses `os.getenv("OPENAI_API_KEY")` to retrieve the API key from an environment variable. **This is the only safe way to handle API keys in code.** Never hardcode your API key directly in your script, and never commit it to a repository. To run the code, you must set the `OPENAI_API_KEY` environment variable *before* running the script. For example, on Linux/macOS:
```bash
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
python your_script_name.py
```
Or on Windows:
```powershell
$env:OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
python your_script_name.py
```
* **`stop` Parameter:** The `stop=["\n\n\n"]` parameter in `openai.Completion.create` is important. It tells the AI to stop generating code when it encounters three consecutive newlines. This is a common pattern in code where blocks are separated, and it helps prevent the AI from generating excessively long or irrelevant completions.
* **`temperature` Parameter:** The `temperature` parameter controls the randomness of the generated code. A lower temperature (e.g., 0.2) will produce more deterministic and predictable code, while a higher temperature (e.g., 0.7) will produce more creative but potentially less reliable code.
* **Clear `main` function:** The `main` function organizes the calls to `generate_code_completion` and prints the results in a readable format.
* **Example Machine Learning Code:** The example now loads sample data (you'll need to create a `data.csv` file with appropriate data for the features `feature1`, `feature2` and the `target`). It correctly splits the data, trains a linear regression model, and evaluates the model.
* **Comments and Docstrings:** The code is thoroughly commented to explain each step. The functions also have docstrings to describe their purpose, arguments, and return values.
* **Handles `None` Return:** The code now checks if `generate_code_completion` returns `None` (indicating an error) and handles it gracefully.
* **Correct `engine` parameter**: Uses `code-davinci-002` or `code-cushman-001` which are specifically designed for code completion.
How to run the code:
1. **Install the OpenAI Python library:**
```bash
pip install openai
```
2. **Set your OpenAI API key:** Set the `OPENAI_API_KEY` environment variable as described above. **Do not hardcode your API key in the script.**
3. **Create a `data.csv` file:** Create a CSV file named `data.csv` in the same directory as your script. It should have columns named `feature1`, `feature2`, and `target` with appropriate numerical data. For example:
```csv
feature1,feature2,target
1.0,2.0,5.0
2.0,3.0,8.0
3.0,4.0,11.0
4.0,5.0,14.0
5.0,6.0,17.0
```
4. **Run the script:**
```bash
python your_script_name.py
```
Remember that using the OpenAI API incurs costs. Monitor your usage and set spending limits in your OpenAI account. Also, the generated code may not be perfect, so always review and test it thoroughly.
👁️ Viewed: 9
Comments