XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

refactored cocalc too

ac92b8a2
9fo <71867245+9fo@users.noreply.github.com>
提交于

代码差异

1 个文件 +57 -0
Added cocalc/__init__.py +57 -0
@@ -0,0 +1,57 @@
1 import requests
2
3 class Completion:
4 @staticmethod
5 def create(prompt:str, cookieInput:str) -> str:
6 # Initialize a session with custom headers
7 session = Completion._initialize_session(cookieInput)
8
9 # Set the data that will be submitted
10 payload = Completion._create_payload(prompt, ("ASSUME I HAVE FULL ACCESS TO COCALC. "))
11
12 # Submit the request and return the results
13 return Completion._submit_request(session, payload)
14
15 @classmethod
16 def _initialize_session(cls, conversationCookie) -> requests.Session:
17 """Initialize a session with custom headers for the request."""
18
19 session = requests.Session()
20 headers = {
21 'Accept': '*/*',
22 'Accept-Language': 'en-US,en;q=0.5',
23 'Origin': 'https://cocalc.com',
24 'Referer': 'https://cocalc.com/api/v2/openai/chatgpt',
25 'Cookie': conversationCookie,
26 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
27 }
28 session.headers.update(headers)
29
30 return session
31
32 @classmethod
33 def _create_payload(
34 cls,
35 prompt: str,
36 system_prompt: str
37 ) -> dict:
38
39 return {
40 "input": prompt,
41 "system": system_prompt,
42 "tag": "next:index"
43 }
44
45 @classmethod
46 def _submit_request(
47 cls,
48 session: requests.Session,
49 payload: dict
50 ) -> str:
51
52 response = session.post(
53 "https://cocalc.com/api/v2/openai/chatgpt", json=payload).json()
54 return {
55 "response":response["output"],
56 "success":response["success"]
57 }