-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_ai.py
29 lines (21 loc) · 675 Bytes
/
open_ai.py
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
import os
import openai
import json
os.environ['SECRET_KEY'] = 'YOUR KEY' # get your own key at https://beta.openai.com
openai.api_key = os.getenv("SECRET_KEY")
prompt = 'how to parse a nested json in python'
response = openai.Completion.create(
model="text-davinci-003",
prompt= prompt,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
openai_object_json = json.dumps(response,indent=4)
openai_object_data = json.loads(openai_object_json)
formatted_text = openai_object_data['choices'][0]['text']
print(f'\nThe prompt was:\n {prompt}\n')
print("--" * 25)
print(f'\nThe formatted response is :\n {formatted_text}\n')