Daniele Polizzi commited on
Commit
96eab8e
1 Parent(s): 8c2e8a2

Update __init__.py

Browse files

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()

Files changed (1) hide show
  1. unfinished/cocalc/__init__.py +16 -12
unfinished/cocalc/__init__.py CHANGED
@@ -1,27 +1,31 @@
1
- from requests import Session
2
- import json
3
 
4
  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:
8
 
9
- client = Session()
10
- client.headers = {
 
 
 
11
  'Accept': '*/*',
12
  '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",
16
  }
 
17
 
 
18
  payload = {
19
  "input": prompt,
20
  "system": system_prompt,
21
  "tag": "next:index"
22
  }
23
 
24
- response = client.post(f"https://cocalc.com/api/v2/openai/chatgpt", json=payload).json()
 
25
 
 
26
  return response
27
-
 
1
+ import requests
 
2
 
3
  class Completion:
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:
 
6
 
7
+ # Initialize a session
8
+ session = requests.Session()
9
+
10
+ # Set headers for the request
11
+ headers = {
12
  'Accept': '*/*',
13
  'Accept-Language': 'en-US,en;q=0.5',
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',
17
  }
18
+ session.headers.update(headers)
19
 
20
+ # Set the data that will be submitted
21
  payload = {
22
  "input": prompt,
23
  "system": system_prompt,
24
  "tag": "next:index"
25
  }
26
 
27
+ # Submit the request
28
+ response = session.post("https://cocalc.com/api/v2/openai/chatgpt", json=payload).json()
29
 
30
+ # Return the results
31
  return response