File size: 1,811 Bytes
592ba10
 
 
 
 
a393007
592ba10
 
a393007
592ba10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4ba306
a393007
 
 
 
 
 
592ba10
 
 
 
 
 
a393007
592ba10
 
 
 
 
 
 
 
a393007
592ba10
 
a393007
592ba10
 
 
a393007
592ba10
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
import os
from openai import OpenAI

# Retrieve your OpenAI API key from the environment variables and activate the OpenAI client
openai_api_key = os.environ.get("OPENAI_API_KEY")
client = OpenAI(api_key=openai_api_key)


def ask_ai_tutor(question):

    # Check if OpenAI key has been correctly added
    if not openai_api_key:
        return "OpenAI API key not found in environment variables."

    try:

        # Formulating the system prompt
        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."
        )

        # Combining the system prompt with the user's question
        prompt = f"Please provide an informative and accurate answer to the following question.\nQuestion: {question}\nAnswer:"

        # Call the OpenAI API
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt},
            ],
        )

        # Return the AI's response
        return response.choices[0].message.content.strip()

    except Exception as e:
        return f"An error occurred: {e}"


def main():
    # Check if a question was provided as a command-line argument
    if len(sys.argv) != 2:
        print("Usage: python script_name.py 'Your AI-related question'")
        sys.exit(1)

    # The user's question is the first command-line argument
    user_question = sys.argv[1]

    # Get the AI's response
    ai_response = ask_ai_tutor(user_question)

    # Print the AI's response
    print(f"AI Tutor says: {ai_response}")


if __name__ == "__main__":
    main()