返回提交历史
Modified
gpt4free/cocalc/__init__.py
+35
-15
XFEstudio/gpt4free
refactor(gpt4free/cocalc/__init__.py): Refactored init file, makes it cleaner and more robust.
b82e787d
代码差异
1 个文件
+35
-15
@@ -9,29 +9,52 @@ class CoCalcResponse(BaseModel):
9
9
10
10
11
11
class Completion:
12
"""A class for generating text completions using CoCalc's GPT-based chatbot."""
13
14
API_ENDPOINT = "https://cocalc.com/api/v2/openai/chatgpt"
15
DEFAULT_SYSTEM_PROMPT = "ASSUME I HAVE FULL ACCESS TO COCALC. "
16
12
17
@staticmethod
13
18
def create(prompt: str, cookie_input: str) -> CoCalcResponse:
19
"""
20
Generate a text completion for the given prompt using CoCalc's GPT-based chatbot.
21
22
Args:
23
prompt: The text prompt to complete.
24
cookie_input: The cookie required to authenticate the chatbot API request.
25
26
Returns:
27
A CoCalcResponse object containing the text completion and a boolean indicating
28
whether the request was successful.
29
"""
30
14
31
# Initialize a session with custom headers
15
32
session = Completion._initialize_session(cookie_input)
16
33
17
34
# Set the data that will be submitted
18
payload = Completion._create_payload(prompt, 'ASSUME I HAVE FULL ACCESS TO COCALC. ')
35
payload = Completion._create_payload(prompt, Completion.DEFAULT_SYSTEM_PROMPT)
19
36
20
# Submit the request and return the results
21
return Completion._submit_request(session, payload)
37
try:
38
# Submit the request and return the results
39
response = session.post(Completion.API_ENDPOINT, json=payload).json()
40
return CoCalcResponse(text=response['output'], status=response['success'])
41
except requests.exceptions.RequestException as e:
42
# Handle exceptions that may occur during the request
43
print(f"Error: {e}")
44
return CoCalcResponse(text="", status=False)
22
45
23
46
@classmethod
24
def _initialize_session(cls, conversation_cookie) -> requests.Session:
47
def _initialize_session(cls, conversation_cookie: str) -> requests.Session:
25
48
"""Initialize a session with custom headers for the request."""
26
49
27
50
session = requests.Session()
28
51
headers = {
29
'Accept': '*/*',
30
'Accept-Language': 'en-US,en;q=0.5',
31
'Origin': 'https://cocalc.com',
32
'Referer': 'https://cocalc.com/api/v2/openai/chatgpt',
33
'Cookie': conversation_cookie,
34
'User-Agent': UserAgent().random,
52
"Accept": "*/*",
53
"Accept-Language": "en-US,en;q=0.5",
54
"Origin": "https://cocalc.com",
55
"Referer": "https://cocalc.com/api/v2/openai/chatgpt",
56
"Cookie": conversation_cookie,
57
"User-Agent": UserAgent().random,
35
58
}
36
59
session.headers.update(headers)
37
60
@@ -39,9 +62,6 @@ class Completion:
39
62
40
63
@staticmethod
41
64
def _create_payload(prompt: str, system_prompt: str) -> dict:
42
return {'input': prompt, 'system': system_prompt, 'tag': 'next:index'}
65
"""Create the payload for the API request."""
43
66
44
@staticmethod
45
def _submit_request(session: requests.Session, payload: dict) -> CoCalcResponse:
46
response = session.post('https://cocalc.com/api/v2/openai/chatgpt', json=payload).json()
47
return CoCalcResponse(text=response['output'], status=response['success'])
67
return {"input": prompt, "system": system_prompt, "tag": "next:index"}