Spaces:
Running
Running
:hammer: [WIP] Try to enable conversation api
Browse files- tests/openai.py +48 -4
tests/openai.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import uuid
|
2 |
|
3 |
from pathlib import Path
|
@@ -18,12 +19,14 @@ class OpenaiAPI:
|
|
18 |
self.api_me = f"{self.api_base}/me"
|
19 |
self.api_models = f"{self.api_base}/models"
|
20 |
self.api_chat_requirements = f"{self.api_base}/sentinel/chat-requirements"
|
|
|
21 |
self.uuid = str(uuid.uuid4())
|
22 |
self.requests_headers = {
|
23 |
# "Accept": "*/*",
|
24 |
"Accept-Encoding": "gzip, deflate, br, zstd",
|
25 |
"Accept-Language": "en-US,en;q=0.9",
|
26 |
"Cache-Control": "no-cache",
|
|
|
27 |
"Oai-Device-Id": self.uuid,
|
28 |
"Oai-Language": "en-US",
|
29 |
"Pragma": "no-cache",
|
@@ -58,11 +61,10 @@ class OpenaiAPI:
|
|
58 |
status_code_str = f"[{status_code}]"
|
59 |
if status_code == 200:
|
60 |
logger.success(status_code_str)
|
|
|
61 |
else:
|
62 |
logger.warn(status_code_str)
|
63 |
-
logger.warn(
|
64 |
-
|
65 |
-
logger.line(res.json())
|
66 |
|
67 |
def get_models(self):
|
68 |
self.log_request(self.api_models)
|
@@ -88,9 +90,51 @@ class OpenaiAPI:
|
|
88 |
|
89 |
self.log_response(res)
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
api = OpenaiAPI()
|
94 |
-
api.
|
|
|
|
|
|
|
95 |
|
96 |
# python -m tests.openai
|
|
|
1 |
+
import copy
|
2 |
import uuid
|
3 |
|
4 |
from pathlib import Path
|
|
|
19 |
self.api_me = f"{self.api_base}/me"
|
20 |
self.api_models = f"{self.api_base}/models"
|
21 |
self.api_chat_requirements = f"{self.api_base}/sentinel/chat-requirements"
|
22 |
+
self.api_conversation = f"{self.api_base}/conversation"
|
23 |
self.uuid = str(uuid.uuid4())
|
24 |
self.requests_headers = {
|
25 |
# "Accept": "*/*",
|
26 |
"Accept-Encoding": "gzip, deflate, br, zstd",
|
27 |
"Accept-Language": "en-US,en;q=0.9",
|
28 |
"Cache-Control": "no-cache",
|
29 |
+
"Content-Type": "application/json",
|
30 |
"Oai-Device-Id": self.uuid,
|
31 |
"Oai-Language": "en-US",
|
32 |
"Pragma": "no-cache",
|
|
|
61 |
status_code_str = f"[{status_code}]"
|
62 |
if status_code == 200:
|
63 |
logger.success(status_code_str)
|
64 |
+
logger.success(res.json())
|
65 |
else:
|
66 |
logger.warn(status_code_str)
|
67 |
+
logger.warn(res.json())
|
|
|
|
|
68 |
|
69 |
def get_models(self):
|
70 |
self.log_request(self.api_models)
|
|
|
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)
|
99 |
+
post_data = {
|
100 |
+
"action": "next",
|
101 |
+
"messages": [
|
102 |
+
{
|
103 |
+
"id": self.uuid,
|
104 |
+
"author": {"role": "user"},
|
105 |
+
"content": {"content_type": "text", "parts": [prompt]},
|
106 |
+
"metadata": {},
|
107 |
+
}
|
108 |
+
],
|
109 |
+
# "parent_message_id": "aaa1de6f-3d50-4d57-8591-ec7042deb594",
|
110 |
+
"model": "text-davinci-002-render-sha",
|
111 |
+
"timezone_offset_min": -480,
|
112 |
+
"suggestions": [],
|
113 |
+
"history_and_training_disabled": False,
|
114 |
+
"conversation_mode": {"kind": "primary_assistant"},
|
115 |
+
"force_paragen": False,
|
116 |
+
"force_paragen_model_slug": "",
|
117 |
+
"force_nulligen": False,
|
118 |
+
"force_rate_limit": False,
|
119 |
+
# "websocket_request_id": "f4bd44ac-64ad-4832-b6ca-3603ac6b38c5",
|
120 |
+
}
|
121 |
+
self.log_request(self.api_conversation, method="POST")
|
122 |
+
res = requests.post(
|
123 |
+
self.api_conversation,
|
124 |
+
headers=requests_headers,
|
125 |
+
json=post_data,
|
126 |
+
proxies=self.requests_proxies,
|
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 |
+
# api.auth()
|
137 |
+
prompt = "who are you?"
|
138 |
+
api.chat_completions(prompt)
|
139 |
|
140 |
# python -m tests.openai
|