2023-04-28 19:33:51 +09:00
|
|
|
import os
|
|
|
|
import sys
|
2023-04-30 14:19:46 +09:00
|
|
|
from typing import Optional
|
2023-04-28 19:33:51 +09:00
|
|
|
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir))
|
|
|
|
|
2023-04-29 18:25:24 +09:00
|
|
|
from gpt4free import quora, forefront, theb, you
|
2023-04-28 08:15:50 +09:00
|
|
|
import random
|
|
|
|
|
|
|
|
|
2023-04-30 14:19:46 +09:00
|
|
|
def query_forefront(question: str, proxy: Optional[str] = None) -> str:
|
2023-04-28 08:15:50 +09:00
|
|
|
# create an account
|
2023-04-30 14:19:46 +09:00
|
|
|
token = forefront.Account.create(logging=False, proxy=proxy)
|
2023-04-28 19:33:51 +09:00
|
|
|
|
|
|
|
response = ""
|
2023-04-28 08:15:50 +09:00
|
|
|
# get a response
|
|
|
|
try:
|
2023-04-30 14:19:46 +09:00
|
|
|
return forefront.Completion.create(token=token, prompt='hello world', model='gpt-4', proxy=proxy).text
|
2023-04-28 08:15:50 +09:00
|
|
|
except Exception as e:
|
|
|
|
# Return error message if an exception occurs
|
2023-04-29 18:31:16 +09:00
|
|
|
return (
|
|
|
|
f'An error occurred: {e}. Please make sure you are using a valid cloudflare clearance token and user agent.'
|
|
|
|
)
|
2023-04-28 08:15:50 +09:00
|
|
|
|
|
|
|
|
2023-04-30 14:19:46 +09:00
|
|
|
def query_quora(question: str, proxy: Optional[str] = None) -> str:
|
|
|
|
token = quora.Account.create(logging=False, enable_bot_creation=True, proxy=proxy)
|
|
|
|
return quora.Completion.create(model='gpt-4', prompt=question, token=token, proxy=proxy).text
|
2023-04-28 08:15:50 +09:00
|
|
|
|
|
|
|
|
2023-04-30 14:19:46 +09:00
|
|
|
def query_theb(question: str, proxy: Optional[str] = None) -> str:
|
2023-04-28 08:15:50 +09:00
|
|
|
# Set cloudflare clearance cookie and get answer from GPT-4 model
|
2023-04-28 19:33:51 +09:00
|
|
|
response = ""
|
2023-04-28 08:15:50 +09:00
|
|
|
try:
|
2023-04-30 14:19:46 +09:00
|
|
|
return ''.join(theb.Completion.create(prompt=question, proxy=proxy))
|
2023-04-29 18:31:16 +09:00
|
|
|
|
2023-04-28 08:15:50 +09:00
|
|
|
except Exception as e:
|
|
|
|
# Return error message if an exception occurs
|
2023-04-29 18:31:16 +09:00
|
|
|
return (
|
|
|
|
f'An error occurred: {e}. Please make sure you are using a valid cloudflare clearance token and user agent.'
|
|
|
|
)
|
2023-04-28 08:15:50 +09:00
|
|
|
|
|
|
|
|
2023-04-30 14:19:46 +09:00
|
|
|
def query_you(question: str, proxy: Optional[str] = None) -> str:
|
2023-04-28 08:15:50 +09:00
|
|
|
# Set cloudflare clearance cookie and get answer from GPT-4 model
|
|
|
|
try:
|
2023-04-30 14:19:46 +09:00
|
|
|
result = you.Completion.create(prompt=question, proxy=proxy)
|
|
|
|
return result.text
|
2023-04-29 18:31:16 +09:00
|
|
|
|
2023-04-28 08:15:50 +09:00
|
|
|
except Exception as e:
|
|
|
|
# Return error message if an exception occurs
|
2023-04-29 18:31:16 +09:00
|
|
|
return (
|
|
|
|
f'An error occurred: {e}. Please make sure you are using a valid cloudflare clearance token and user agent.'
|
|
|
|
)
|
|
|
|
|
2023-04-28 08:15:50 +09:00
|
|
|
|
2023-04-28 19:33:51 +09:00
|
|
|
# Define a dictionary containing all query methods
|
|
|
|
avail_query_methods = {
|
2023-04-29 18:31:16 +09:00
|
|
|
"Forefront": query_forefront,
|
|
|
|
"Poe": query_quora,
|
|
|
|
"Theb": query_theb,
|
|
|
|
"You": query_you,
|
|
|
|
# "Writesonic": query_writesonic,
|
|
|
|
# "T3nsor": query_t3nsor,
|
|
|
|
# "Phind": query_phind,
|
|
|
|
# "Ora": query_ora,
|
|
|
|
}
|
2023-04-28 08:15:50 +09:00
|
|
|
|
2023-04-28 19:33:51 +09:00
|
|
|
|
2023-04-30 14:19:46 +09:00
|
|
|
def query(user_input: str, selected_method: str = "Random", proxy: Optional[str] = None) -> str:
|
2023-04-28 08:15:50 +09:00
|
|
|
# If a specific query method is selected (not "Random") and the method is in the dictionary, try to call it
|
|
|
|
if selected_method != "Random" and selected_method in avail_query_methods:
|
|
|
|
try:
|
2023-04-30 14:19:46 +09:00
|
|
|
return avail_query_methods[selected_method](user_input, proxy=proxy)
|
2023-04-28 08:15:50 +09:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error with {selected_method}: {e}")
|
|
|
|
return "😵 Sorry, some error occurred please try again."
|
|
|
|
|
|
|
|
# Initialize variables for determining success and storing the result
|
|
|
|
success = False
|
|
|
|
result = "😵 Sorry, some error occurred please try again."
|
|
|
|
# Create a list of available query methods
|
|
|
|
query_methods_list = list(avail_query_methods.values())
|
|
|
|
|
|
|
|
# Continue trying different methods until a successful result is obtained or all methods have been tried
|
|
|
|
while not success and query_methods_list:
|
|
|
|
# Choose a random method from the list
|
|
|
|
chosen_query = random.choice(query_methods_list)
|
|
|
|
# Find the name of the chosen method
|
|
|
|
chosen_query_name = [k for k, v in avail_query_methods.items() if v == chosen_query][0]
|
|
|
|
try:
|
|
|
|
# Try to call the chosen method with the user input
|
2023-04-30 14:19:46 +09:00
|
|
|
result = chosen_query(user_input, proxy=proxy)
|
2023-04-28 08:15:50 +09:00
|
|
|
success = True
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error with {chosen_query_name}: {e}")
|
|
|
|
# Remove the failed method from the list of available methods
|
|
|
|
query_methods_list.remove(chosen_query)
|
|
|
|
|
|
|
|
return result
|