askrobots / askrobots_commitgen
Public
AI-Powered Git Commit Message Generator
danq9
Create commit message adjusting OpenAI initialization and error handling.
c0d2e25
2 years, 6 months ago
History
ai_commit.py · 1.1 KB · 25 lines
| 1 | from settings import OPENAI_API_KEY, OPENAI_MODEL, MAX_COMMIT_MESSAGE_LENGTH |
| 2 | from openai import OpenAI |
| 3 | import os |
| 4 | |
| 5 | # Assuming your API key is set in the environment or you set it somewhere in your settings |
| 6 | api_key = os.getenv('OPENAI_API_KEY') |
| 7 | client = OpenAI(api_key=api_key) |
| 8 | |
| 9 | def generate_commit_message(diff: str) -> str: |
| 10 | try: |
| 11 | chat_completion = client.chat.completions.create( |
| 12 | messages=[ |
| 13 | {"role": "system", "content": "You are a helpful assistant."}, |
| 14 | {"role": "user", "content": f"Generate a commit message for the following diff:\n{diff}"} |
| 15 | ], |
| 16 | model="gpt-3.5-turbo" # Ensure you are using an appropriate model that you have access to |
| 17 | ) |
| 18 | # Correctly accessing the first message content from the response |
| 19 | if chat_completion and chat_completion.choices: |
| 20 | return chat_completion.choices[0].message.content.strip() |
| 21 | else: |
| 22 | return "No valid response received from the API." |
| 23 | except Exception as e: |
| 24 | print(f"Error generating commit message: {e}") |
| 25 | return "" |