commit 7a522d48f47dd6647e75e11f74556e8c3dfc5061 Author: jackfood Date: Sat Sep 16 16:11:32 2023 +0800 aiengine.py diff --git a/aiengine.py b/aiengine.py new file mode 100644 index 0000000..8c97c1c --- /dev/null +++ b/aiengine.py @@ -0,0 +1,42 @@ +import g4f +import os + +# Read user input from '~prompt.txt' +with open('~prompt.txt', 'r', encoding='utf-8') as file: + input_prompt = file.read() + +# Use the input prompt to generate a response from the GPT-3 model +response = g4f.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[{"role": "user", "content": input_prompt}], + stream=False, +) + +# Extract the AI's response content from the response object +ai_response = response + +# Check if 'aianswer.txt' exists, and overwrite or create accordingly +with open('aianswer.txt', 'w', encoding='utf-8') as file: + file.write(ai_response) + +# Read the AI's response from 'aianswer.txt' +with open('aianswer.txt', 'r', encoding='utf-8') as file: + ai_response = file.read() + +# Find and extract content between triple backticks (```python) and (```) +start_marker = "```python" +end_marker = "```" +start_index = ai_response.find(start_marker) +end_index = ai_response.find(end_marker, start_index + len(start_marker)) + +if start_index != -1 and end_index != -1: + python_content = ai_response[start_index + len(start_marker):end_index] + + # Save the extracted Python content to 'aipythonanswer.txt' as UTF-8 + with open('aipythonanswer.txt', 'w', encoding='utf-8') as python_file: + python_file.write(python_content) + + print("Python content extracted and saved to 'aipythonanswer.txt'") +else: + print("No Python code found between triple backticks in 'aianswer.txt'") +