Repository navigation
Expand file tree
/
Copy pathchatgpt_api.py
More file actions
72 lines (34 loc) · 1.66 KB
/
Copy pathchatgpt_api.py
File metadata and controls
72 lines (34 loc) · 1.66 KB
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
62
63
64
65
66
67
68
69
70
71
72
from openai import OpenAI
from dotenv import load_dotenv
import os
#Copy the openAI API Key in a file named .env on your project root folder, in same folder where this Python file resides
#.env file follows a simple key=value format where you can provide all project configurations
#e.g. I have my .env file locally stored which has
#OPENAI_API_KEY = "sk-**** redacted for security ***************"
#In your setup the .env file you have should have your API key.
load_dotenv() # Load all the config variables set as key=value pairs in .env file locally stored
API_KEY = os.getenv('OPENAI_API_KEY') # Get the value of the variable OPENAI_API_KEY stored in .env file
MODEL_NAME="gpt-3.5-turbo" # Specify the openAI model you want to use
#It could be the latest gpt-4o , OpenAIs latest model GPT-4 omni
#or gpt-4-turbo or gpt-3.5-turbo
#Create the openAI client object
client = OpenAI(
# This is the default and can be omitted
api_key=API_KEY,
)
# # Invoke the gpt-3.5 turbo model, Pass the text query to it and get its response back
response = client.chat.completions.create(
messages=[
{"role": "system", "content": 'You are an expert in Machine learning, Data science.'},
{
"role": "user",
"content": "You are an expert in Machine learning, Data science. Give me a study map for learning data science , courses and websites to learn from ",
}
],
model=MODEL_NAME,
)
#Print the data from the model response
if response.choices:
print(response.choices[0].message.content)
else:
print("No choices received.")