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_modify.py · 1.2 KB · 38 lines
| 1 | import sys |
| 2 | from settings import OPENAI_API_KEY, OPENAI_MODEL |
| 3 | import openai |
| 4 | |
| 5 | def modify_file(file_path: str, change_description: str) -> None: |
| 6 | try: |
| 7 | with open(file_path, 'r') as file: |
| 8 | file_contents = file.read() |
| 9 | |
| 10 | openai.api_key = OPENAI_API_KEY |
| 11 | response = openai.ChatCompletion.create( |
| 12 | model=OPENAI_MODEL, |
| 13 | messages=[ |
| 14 | {"role": "system", "content": "You are a helpful assistant."}, |
| 15 | {"role": "user", "content": f"Modify the following file based on this description: '{change_description}'.\n\nFile content:\n{file_contents}\n\nModified file:"} |
| 16 | ] |
| 17 | ) |
| 18 | modified_contents = response['choices'][0]['message']['content'].strip() |
| 19 | |
| 20 | with open(file_path, 'w') as file: |
| 21 | file.write(modified_contents) |
| 22 | |
| 23 | print(f"File '{file_path}' modified based on description: '{change_description}'") |
| 24 | except Exception as e: |
| 25 | print(f"Error modifying file: {e}") |
| 26 | |
| 27 | def main(): |
| 28 | if len(sys.argv) < 3: |
| 29 | print("Usage: python ai_modify.py <file_path> <change_description>") |
| 30 | return |
| 31 | |
| 32 | file_path = sys.argv[1] |
| 33 | change_description = sys.argv[2] |
| 34 | |
| 35 | modify_file(file_path, change_description) |
| 36 | |
| 37 | if __name__ == "__main__": |
| 38 | main() |