forked from Robot-Learning-Library/VIMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcot_query.py
69 lines (53 loc) · 1.93 KB
/
cot_query.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
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
import os
import openai
import wandb
import numpy as np
from private import *
def load_prompt(folder=''):
prompts = np.load(folder)
return prompts
def query_in_batch(prompts, wandb_activate=False):
openai.api_key = f"{OPENAI_API_KEY}"
if wandb_activate:
run = wandb.init(project='GPT-3 in Python')
prediction_table = wandb.Table(columns=["prompt", "completion"])
results = []
for gpt_prompt in prompts:
response = openai.Completion.create(
engine="text-davinci-002",
prompt=gpt_prompt,
temperature=0.9, # https://beta.openai.com/docs/quickstart/adjust-your-settings
max_tokens=256,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
print(gpt_prompt, response['choices'][0]['text'])
if wandb_activate:
prediction_table.add_data(gpt_prompt,response['choices'][0]['text'])
results.append(response['choices'][0]['text'])
if wandb_activate:
wandb.log({'predictions': prediction_table})
wandb.finish()
return results
def query(gpt_prompt, wandb_activate=False, temperature=0.8):
openai.api_key = f"{OPENAI_API_KEY}"
if wandb_activate:
run = wandb.init(project='GPT-3 in Python')
prediction_table = wandb.Table(columns=["prompt", "completion"])
response = openai.Completion.create(
engine="text-davinci-002",
prompt=gpt_prompt,
temperature=temperature, # https://beta.openai.com/docs/quickstart/adjust-your-settings
max_tokens=256,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
print('Query: ', gpt_prompt, 'Response: ', response['choices'][0]['text'])
if wandb_activate:
prediction_table.add_data(gpt_prompt,response['choices'][0]['text'])
if wandb_activate:
wandb.log({'predictions': prediction_table})
wandb.finish()
return response['choices'][0]['text']