Spaces:
Running
Running
:gem: [Feature] Enable conversation with openai
Browse files- tests/openai.py +17 -12
tests/openai.py
CHANGED
@@ -56,15 +56,20 @@ class OpenaiAPI:
|
|
56 |
logger.note(f"> {method}:", end=" ")
|
57 |
logger.mesg(f"{url}", end=" ")
|
58 |
|
59 |
-
def log_response(self, res: requests.Response):
|
60 |
status_code = res.status_code
|
61 |
status_code_str = f"[{status_code}]"
|
|
|
62 |
if status_code == 200:
|
63 |
-
logger.success
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
65 |
else:
|
66 |
-
|
67 |
-
logger.warn(res.json())
|
68 |
|
69 |
def get_models(self):
|
70 |
self.log_request(self.api_models)
|
@@ -75,11 +80,10 @@ class OpenaiAPI:
|
|
75 |
timeout=10,
|
76 |
impersonate="chrome120",
|
77 |
)
|
78 |
-
|
79 |
self.log_response(res)
|
80 |
|
81 |
def auth(self):
|
82 |
-
self.log_request(self.
|
83 |
res = requests.post(
|
84 |
self.api_chat_requirements,
|
85 |
headers=self.requests_headers,
|
@@ -87,12 +91,13 @@ class OpenaiAPI:
|
|
87 |
timeout=10,
|
88 |
impersonate="chrome120",
|
89 |
)
|
90 |
-
|
91 |
self.log_response(res)
|
92 |
|
93 |
def chat_completions(self, prompt: str):
|
94 |
new_headers = {
|
95 |
"Accept": "text/event-stream",
|
|
|
96 |
}
|
97 |
requests_headers = copy.deepcopy(self.requests_headers)
|
98 |
requests_headers.update(new_headers)
|
@@ -106,7 +111,7 @@ class OpenaiAPI:
|
|
106 |
"metadata": {},
|
107 |
}
|
108 |
],
|
109 |
-
|
110 |
"model": "text-davinci-002-render-sha",
|
111 |
"timezone_offset_min": -480,
|
112 |
"suggestions": [],
|
@@ -116,7 +121,7 @@ class OpenaiAPI:
|
|
116 |
"force_paragen_model_slug": "",
|
117 |
"force_nulligen": False,
|
118 |
"force_rate_limit": False,
|
119 |
-
|
120 |
}
|
121 |
self.log_request(self.api_conversation, method="POST")
|
122 |
res = requests.post(
|
@@ -127,13 +132,13 @@ class OpenaiAPI:
|
|
127 |
timeout=10,
|
128 |
impersonate="chrome120",
|
129 |
)
|
130 |
-
self.log_response(res)
|
131 |
|
132 |
|
133 |
if __name__ == "__main__":
|
134 |
api = OpenaiAPI()
|
135 |
# api.get_models()
|
136 |
-
|
137 |
prompt = "who are you?"
|
138 |
api.chat_completions(prompt)
|
139 |
|
|
|
56 |
logger.note(f"> {method}:", end=" ")
|
57 |
logger.mesg(f"{url}", end=" ")
|
58 |
|
59 |
+
def log_response(self, res: requests.Response, stream=False):
|
60 |
status_code = res.status_code
|
61 |
status_code_str = f"[{status_code}]"
|
62 |
+
|
63 |
if status_code == 200:
|
64 |
+
logger_func = logger.success
|
65 |
+
else:
|
66 |
+
logger_func = logger.warn
|
67 |
+
logger_func(status_code_str)
|
68 |
+
|
69 |
+
if stream:
|
70 |
+
logger_func(res.text)
|
71 |
else:
|
72 |
+
logger_func(res.json())
|
|
|
73 |
|
74 |
def get_models(self):
|
75 |
self.log_request(self.api_models)
|
|
|
80 |
timeout=10,
|
81 |
impersonate="chrome120",
|
82 |
)
|
|
|
83 |
self.log_response(res)
|
84 |
|
85 |
def auth(self):
|
86 |
+
self.log_request(self.api_chat_requirements, method="POST")
|
87 |
res = requests.post(
|
88 |
self.api_chat_requirements,
|
89 |
headers=self.requests_headers,
|
|
|
91 |
timeout=10,
|
92 |
impersonate="chrome120",
|
93 |
)
|
94 |
+
self.chat_requirements_token = res.json()["token"]
|
95 |
self.log_response(res)
|
96 |
|
97 |
def chat_completions(self, prompt: str):
|
98 |
new_headers = {
|
99 |
"Accept": "text/event-stream",
|
100 |
+
"Openai-Sentinel-Chat-Requirements-Token": self.chat_requirements_token,
|
101 |
}
|
102 |
requests_headers = copy.deepcopy(self.requests_headers)
|
103 |
requests_headers.update(new_headers)
|
|
|
111 |
"metadata": {},
|
112 |
}
|
113 |
],
|
114 |
+
"parent_message_id": str(uuid.uuid4()),
|
115 |
"model": "text-davinci-002-render-sha",
|
116 |
"timezone_offset_min": -480,
|
117 |
"suggestions": [],
|
|
|
121 |
"force_paragen_model_slug": "",
|
122 |
"force_nulligen": False,
|
123 |
"force_rate_limit": False,
|
124 |
+
"websocket_request_id": str(uuid.uuid4()),
|
125 |
}
|
126 |
self.log_request(self.api_conversation, method="POST")
|
127 |
res = requests.post(
|
|
|
132 |
timeout=10,
|
133 |
impersonate="chrome120",
|
134 |
)
|
135 |
+
self.log_response(res, stream=True)
|
136 |
|
137 |
|
138 |
if __name__ == "__main__":
|
139 |
api = OpenaiAPI()
|
140 |
# api.get_models()
|
141 |
+
api.auth()
|
142 |
prompt = "who are you?"
|
143 |
api.chat_completions(prompt)
|
144 |
|