Louis-François Bouchard
commited on
Commit
·
592ba10
1
Parent(s):
9c8821d
basic ai tutor QA script
Browse files- scripts/basic_tutor.py +57 -0
scripts/basic_tutor.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import os
|
3 |
+
from openai import OpenAI
|
4 |
+
|
5 |
+
# Retrieve your OpenAI API key from the environment variables and activate the OpenAI client
|
6 |
+
openai_api_key = os.environ.get('OPENAI_API_KEY')
|
7 |
+
client = OpenAI(api_key=openai_api_key)
|
8 |
+
|
9 |
+
def ask_ai_tutor(question):
|
10 |
+
|
11 |
+
# Check if OpenAI key has been correctly added
|
12 |
+
if not openai_api_key:
|
13 |
+
return "OpenAI API key not found in environment variables."
|
14 |
+
|
15 |
+
try:
|
16 |
+
|
17 |
+
# Formulating the system prompt
|
18 |
+
system_prompt = (
|
19 |
+
"You are an AI tutor specialized in answering artificial intelligence-related questions. "
|
20 |
+
"Only answer AI-related question, else say that you cannot answer this question."
|
21 |
+
)
|
22 |
+
|
23 |
+
# Combining the system prompt with the user's question
|
24 |
+
prompt = f"Please provide an informative and accurate answer to the following question.\nQuestion: {question}\nAnswer:"
|
25 |
+
|
26 |
+
# Call the OpenAI API
|
27 |
+
response = client.chat.completions.create(
|
28 |
+
model='gpt-3.5-turbo-16k',
|
29 |
+
messages=[
|
30 |
+
{"role": "system", "content": system_prompt},
|
31 |
+
{"role": "user", "content": prompt}
|
32 |
+
]
|
33 |
+
)
|
34 |
+
|
35 |
+
# Return the AI's response
|
36 |
+
return response.choices[0].message.content.strip()
|
37 |
+
|
38 |
+
except Exception as e:
|
39 |
+
return f"An error occurred: {e}"
|
40 |
+
|
41 |
+
def main():
|
42 |
+
# Check if a question was provided as a command-line argument
|
43 |
+
if len(sys.argv) != 2:
|
44 |
+
print("Usage: python script_name.py 'Your AI-related question'")
|
45 |
+
sys.exit(1)
|
46 |
+
|
47 |
+
# The user's question is the first command-line argument
|
48 |
+
user_question = sys.argv[1]
|
49 |
+
|
50 |
+
# Get the AI's response
|
51 |
+
ai_response = ask_ai_tutor(user_question)
|
52 |
+
|
53 |
+
# Print the AI's response
|
54 |
+
print(f"AI Tutor says: {ai_response}")
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
main()
|