-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAIQandA.py
More file actions
56 lines (48 loc) · 1.9 KB
/
Copy pathOpenAIQandA.py
File metadata and controls
56 lines (48 loc) · 1.9 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
# API call to Open AI
# OPENAI_API_KEY is obtained from https://platform.openai.com/playground, click on account and select "View API Keys"
# Set as an environment variable under Windows Control Panel
#
# sample response:
# {
# "choices": [
# {
# "finish_reason": "stop",
# "index": 0,
# "logprobs": null,
# "text": "\nMacbeth is a story about a brave warrior named Macbeth. He is a brave and loyal soldier who is very proud of his accomplishments. One day he meets three witches who make a prophecy that he will become the King of Scotland. Macbeth is tempted by his ambition and decides to take matters into his own hands by killing the current king and taking the throne for himself. He does this with the help of his wife, Lady Macbeth. After becoming king, Macbeth begins to have a guilty conscience and is haunted by nightmares. Eventually, Macbeth is defeated in battle and killed by the rightful king of Scotland."
# }
# ],
# "created": 1684519595,
# "id": "cmpl-7HympJNahOb4y4hz7VI0Rv7PI4Lz8",
# "model": "text-davinci-003",
# "object": "text_completion",
# "usage": {
# "completion_tokens": 131,
# "prompt_tokens": 15,
# "total_tokens": 146
# }
# }
#
# how to set an environment variable from the command line:
# export ENVVARIABLE="VALUE"
import os
import openai # install via python -m pip install openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Completion.create(
model="text-davinci-003",
prompt="summarize shakespeare's Macbeth for a second grader\n",
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
print("OpenAI Response Data Type:")
print(type(response))
print("\nOpenAI Response:")
print(response)
print("\nOpenAI Answer:")
print(response["choices"][0]["text"])
# try to print out response list components using For loop
for iter_var in response:
print("Response list info during traversal is: ", iter_var)