-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_completions_scrape.py
67 lines (56 loc) · 2.89 KB
/
chat_completions_scrape.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
import os
import openai
from dotenv import load_dotenv
load_dotenv(override=True, dotenv_path=".env")
openai.api_key = os.getenv("OPENAI_API_KEY")
def clean_training_data(user_message):
messages = []
messages.append({"role": "system", "content": "Given a text, rewrite it with proper grammar, punctuation, "
"spelling, and formatting. Make sure that emails and URLs are valid "
"and consistent. Remove any non-printable characters (except space) "
"and any unnecessary or redundant words. Do not change the meaning "
"or tone of the original text."})
messages.append({"role": "user", "content": user_message})
try:
response = openai.chat.completions.create(
model="gpt-4", messages=messages, temperature=0.5
)
return response.choices[0].message.content
except openai.APIError as e:
print(e)
print("Timeout")
return None
def rephrase_question(user_message):
messages = []
messages.append({"role": "system", "content": "Given a text, rewrite it as a question that can be used to "
"fine-tune a GPT model. The rewritten question should be "
"grammatically correct and should not change the meaning or tone of "
"the original text. Your answer should contain only the new text and "
"nothing more"})
messages.append({"role": "user", "content": user_message})
try:
response = openai.chat.completions.create(
model="gpt-4", messages=messages, temperature=0.6
)
return response.choices[0].message.content
except openai.APIError as e:
print(e)
print("Timeout")
return None
def rephrase_solution(user_message):
messages = []
messages.append({"role": "system", "content": "Given a text, rewrite it as a answer that can be used to "
"fine-tune a GPT model. The rewritten question should be "
"grammatically correct and should not change the meaning or tone of "
"the original text. Your answer should contain only the new text and "
"nothing more."})
messages.append({"role": "user", "content": user_message})
try:
response = openai.chat.completions.create(
model="gpt-4", messages=messages, temperature=0.6
)
return response.choices[0].message.content
except openai.APIError as e:
print(e)
print("Timeout")
return None