import logging
# import os
from typing import Dict
import requests
YOUR_OPENAI_API_KEY = YOUR_KEY # os.environ.get("YOUR_OPENAI_API_KEY")
logging.basicConfig(level=logging.INFO)
def call_gpt4(query: str) -> Dict:
"""
Calls GPT-4 with a query and returns the response.
Args:
query (str): The input query for GPT-4.
Returns:
Dict: The response from GPT-4.
"""
endpoint = "https://api.openai.com/v1/engines/davinci/completions" # Update this if GPT-4 has a different endpoint
headers = {
"Authorization": f"Bearer {YOUR_OPENAI_API_KEY}",
"Content-Type": "application/json",
}
data = {
"prompt": query,
"max_tokens": 10, # Adjust this value as needed
"temperature": 0.2
}
try:
response = requests.post(endpoint, headers=headers, json=data, timeout=10)
response.raise_for_status() # This will raise an HTTPError if the request fails
except requests.RequestException as e:
logging.error(f"An error occurred: {e}")
raise # Re-throw the exception after logging it
return response.json()
if __name__ == "__main__":
query_str = "How much is 2 + 2?" # input("Enter your query: ")
response = call_gpt4(query_str)
# logging.info(response)
print(response['choices'][0]['text'])