You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4 KiB

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'")