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

XFEstudio/gpt4free

Update __init__.py

I changed the import statement from request import Session to import requests because in the original the Session class is the only class imported from the request library. In this case, it is more efficient to import the whole library with import requests and use requests.Session() instead of Session() and added various code comments Instead of having a separate dictionary, I created a headers dictionary and used the update() method to update the session header. The create() function returns the results of a request made to a website as a JSON dictionary using the .json() method. However, you don't need to import the json library explicitly because the json() function is a method of the Response object returned by session.post(). In other words, the .json() method does the json decoding for us, so we don't need to use the json module's json()

96eab8ed
Daniele Polizzi <30258633+Daniele-Polizzi@users.noreply.github.com>
提交于

代码差异

1 个文件 +16 -12
Modified unfinished/cocalc/__init__.py +16 -12
@@ -1,27 +1,31 @@
1 from requests import Session
2 import json
1 import requests
3 2
4 3 class Completion:
5 def create(
6 prompt: str = "What is the square root of pi",
7 system_prompt: str = "ASSUME I HAVE FULL ACCESS TO COCALC. ENCLOSE MATH IN $. INCLUDE THE LANGUAGE DIRECTLY AFTER THE TRIPLE BACKTICKS IN ALL MARKDOWN CODE BLOCKS. How can I do the following using CoCalc? ") -> str:
4 def create(prompt="What is the square root of pi",
5 system_prompt="ASSUME I HAVE FULL ACCESS TO COCALC. ENCLOSE MATH IN $. INCLUDE THE LANGUAGE DIRECTLY AFTER THE TRIPLE BACKTICKS IN ALL MARKDOWN CODE BLOCKS. How can I do the following using CoCalc?") -> str:
8 6
9 client = Session()
10 client.headers = {
7 # Initialize a session
8 session = requests.Session()
9
10 # Set headers for the request
11 headers = {
11 12 'Accept': '*/*',
12 13 'Accept-Language': 'en-US,en;q=0.5',
13 "origin" : "https://cocalc.com",
14 "referer" : "https://cocalc.com/api/v2/openai/chatgpt",
15 "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",
14 'Origin': 'https://cocalc.com',
15 'Referer': 'https://cocalc.com/api/v2/openai/chatgpt',
16 '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',
16 17 }
18 session.headers.update(headers)
17 19
20 # Set the data that will be submitted
18 21 payload = {
19 22 "input": prompt,
20 23 "system": system_prompt,
21 24 "tag": "next:index"
22 25 }
23 26
24 response = client.post(f"https://cocalc.com/api/v2/openai/chatgpt", json=payload).json()
27 # Submit the request
28 response = session.post("https://cocalc.com/api/v2/openai/chatgpt", json=payload).json()
25 29
30 # Return the results
26 31 return response
27