-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample.py
65 lines (56 loc) · 2.53 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''
Author: MasterYip 2205929492@qq.com
Date: 2023-08-06 16:31:07
LastEditors: MasterYip
LastEditTime: 2023-08-12 20:11:25
FilePath: \ChatGPT_API_NoKey\example.py
Description: file content
'''
import time
import openai
from threading import Thread
from chatgpt_api_nokey.fake_api import FakeAPI
from chatgpt_api_nokey.singlethread_server import SingleThreadServer
# CONFIG (default values are set in chatgpt_api_nokey/config.py)
HEADLESS = False # Whether to run Chrome in headless mode
"""
Note: There is a bug in undetected-chromedriver when setting headless=True.
For short term, you can modify the source code of undetected-chromedriver in `__init__.py`
385 if headless or options.headless:
387 # FIXME: uc version_main is > 108
388 if not self.patcher.version_main:
389 self.patcher.version_main = 110
390 if self.patcher.version_main < 108:
391 options.add_argument("--headless=chrome")
392 elif self.patcher.version_main >= 108:
393 options.add_argument("--headless=new")
"""
HEADER = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) \
AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1"
SERVER_ADDRESS = ('', 5000) # Server address (i.e. openai.api_base="http://localhost:5000")
PROXY = None # Proxy used to access ChatGPT
# PROXY = "http://127.0.0.1:7890" # If global proxy is set, you can leave it as None
def FakeAPIExample():
api = FakeAPI(HEADLESS, PROXY, HEADER)
print(api.request("Hello!"))
def SingleThreadServerExample():
Thread(target=SingleThreadServer, args=(HEADLESS, PROXY, HEADER, SERVER_ADDRESS)).start()
time.sleep(20) # Wait for the server to start (It's better to start the server in another thread)
openai.api_base = "http://localhost:5000"
openai.api_key = "whatever"
completions = openai.ChatCompletion.create(
model="whatever",
messages="Hello!",
)
print(completions['choices'][0]['message']['content'])
completions = openai.ChatCompletion.create(
model="whatever",
messages="How to write a program that can get rid of the APIKEY?",
)
print(completions['choices'][0]['message']['content'])
if __name__ == "__main__":
# Example
FakeAPIExample()
# SingleThreadServerExample()
# Launch Server
# SingleThreadServer(HEADLESS, PROXY, HEADER, SERVER_ADDRESS)