forked from cognitivecomputations/SystemChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_intents.py
75 lines (66 loc) · 2.23 KB
/
filter_intents.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
69
70
71
72
73
74
75
import jsonlines
import json
from tqdm import tqdm
import json
import random
from openai import AzureOpenAI
from dotenv import load_dotenv
import os
from concurrent.futures import ThreadPoolExecutor
import threading
import time
from tenacity import retry, wait_exponential, stop_after_attempt
import queue
in_file = "user_intents.txt"
out_file = "user_intents_filtered.txt"
load_dotenv()
openai_api_version = os.getenv("OPENAI_API_VERSION")
deployment_name = os.getenv("DEPLOYMENT_NAME")
openai_api_key = os.getenv("OPENAI_API_KEY")
model_name = os.getenv("MODEL_NAME")
azure_endpoint = os.getenv("AZURE_ENDPOINT")
client = AzureOpenAI(
api_key=openai_api_key,
api_version=openai_api_version,
azure_endpoint=azure_endpoint
)
@retry(wait=wait_exponential(multiplier=1, min=4, max=60), stop=stop_after_attempt(10))
def generate_openai_response(messages, max_tokens=2000):
response = client.chat.completions.create(
model=deployment_name,
messages=messages,
max_tokens=max_tokens,
n=1,
stop=None,
temperature=0.7
)
return response
intents = open(in_file, "r").readlines()
q = queue.Queue()
for intent in intents:
q.put(intent)
lock = threading.Lock()
def do_check_intents():
while True:
intent = q.get().strip()
print("intent", intent)
if intent is None:
break
prompt = f"Regarding the following usecase, please determine whether it can be answered by a text-based chatbot (like ChatGPT) with no functions or external integrations. Reply true or false, with no other commentary.\nThe usecase is: {intent}"
response = generate_openai_response([{"role": "user", "content":prompt}], 5)
usable = "true" in response.choices[0].message.content.strip().lower()
if usable:
with lock:
with open(out_file, "a", encoding="utf-8") as f:
f.write(intent)
f.write("\n")
f.flush()
f.close()
q.task_done()
def main():
# do_check_intents()
with ThreadPoolExecutor(max_workers=1000) as executor:
for _ in range(1000):
executor.submit(do_check_intents)
if __name__ == "__main__":
main()