Created
March 19, 2023 06:01
-
-
Save Kuzyashin/fa4f2bd273c970a2800c923faf268c66 to your computer and use it in GitHub Desktop.
chatgpt.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import requests | |
API_KEY = "YOUR_API_KEY" | |
ENDPOINT = "https://api.openai.com/v1/engines/davinci-codex/completions" | |
HEADERS = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {API_KEY}" | |
} | |
SESSION_ID = "your_session_id" | |
def chat_gpt_eval(expression): | |
# Вывод выражения в консоль и ожидание подтверждения пользователя | |
print(f"Получено выражение для выполнения: {expression}") | |
confirmation = input("Введите 'yes' для подтверждения выполнения или 'no' для отклонения: ") | |
if confirmation.lower() == 'yes': | |
try: | |
result = eval(expression) | |
print(f"Результат выполнения выражения: {result}") | |
send_result_to_gpt(result) | |
except Exception as e: | |
print(f"Ошибка при выполнении выражения: {e}") | |
else: | |
print("Выражение отклонено.") | |
def send_result_to_gpt(result): | |
data = { | |
"prompt": f"Результат выполнения выражения: {result}", | |
"max_tokens": 50, | |
"n": 1, | |
"stop": None, | |
"temperature": 0.7, | |
"session_id": SESSION_ID | |
} | |
response = requests.post(ENDPOINT, json=data, headers=HEADERS) | |
if response.status_code == 200: | |
print("Результат успешно отправлен.") | |
response_text = response.json()["choices"][0]["text"].strip() | |
print("Ответ от GPT: ", response_text) | |
else: | |
print("Ошибка при отправке результата:", response.status_code, response.text) | |
def get_chat_gpt_response(prompt): | |
data = { | |
"prompt": prompt, | |
"max_tokens": 50, | |
"n": 1, | |
"stop": None, | |
"temperature": 0.7, | |
"session_id": SESSION_ID | |
} | |
response = requests.post(ENDPOINT, json=data, headers=HEADERS) | |
if response.status_code == 200: | |
response_text = response.json()["choices"][0]["text"].strip() | |
return response_text | |
else: | |
print("Ошибка при получении ответа от GPT:", response.status_code, response.text) | |
return None | |
if __name__ == "__main__": | |
while True: | |
user_input = input("Введите ваше сообщение или выражение для выполнения: ") | |
if user_input.lower() == "exit": | |
break | |
gpt_response = get_chat_gpt_response(user_input) | |
if "eval:" in gpt_response: | |
expression = gpt_response.replace("eval:", "").strip() | |
chat_gpt_eval(expression) | |
else: | |
print("Ответ от GPT: ", gpt_response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment