|
import sys |
|
import os |
|
from openai import OpenAI |
|
|
|
|
|
openai_api_key = os.environ.get("OPENAI_API_KEY") |
|
client = OpenAI(api_key=openai_api_key) |
|
|
|
|
|
def ask_ai_tutor(question): |
|
|
|
|
|
if not openai_api_key: |
|
return "OpenAI API key not found in environment variables." |
|
|
|
try: |
|
|
|
|
|
system_prompt = ( |
|
"You are an AI tutor specialized in answering artificial intelligence-related questions. " |
|
"Only answer AI-related question, else say that you cannot answer this question." |
|
) |
|
|
|
|
|
prompt = f"Please provide an informative and accurate answer to the following question.\nQuestion: {question}\nAnswer:" |
|
|
|
|
|
response = client.chat.completions.create( |
|
model="gpt-3.5-turbo", |
|
messages=[ |
|
{"role": "system", "content": system_prompt}, |
|
{"role": "user", "content": prompt}, |
|
], |
|
) |
|
|
|
|
|
return response.choices[0].message.content.strip() |
|
|
|
except Exception as e: |
|
return f"An error occurred: {e}" |
|
|
|
|
|
def main(): |
|
|
|
if len(sys.argv) != 2: |
|
print("Usage: python script_name.py 'Your AI-related question'") |
|
sys.exit(1) |
|
|
|
|
|
user_question = sys.argv[1] |
|
|
|
|
|
ai_response = ask_ai_tutor(user_question) |
|
|
|
|
|
print(f"AI Tutor says: {ai_response}") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|