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

XFEstudio/gpt4free

Feat: add proxy settings in GUI and library.

70acea30
Civitasv <hscivitasv@gmail.com>
提交于

代码差异

6 个文件 +43 -20
Modified gpt4free/forefront/__init__.py +6 -0
@@ -98,12 +98,15 @@ class StreamingCompletion:
98 98 action_type='new',
99 99 default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
100 100 model='gpt-4',
101 proxy=None
101 102 ) -> Generator[ForeFrontResponse, None, None]:
102 103 if not token:
103 104 raise Exception('Token is required!')
104 105 if not chat_id:
105 106 chat_id = str(uuid4())
106 107
108 proxies = { 'http': 'http://' + proxy, 'https': 'http://' + proxy } if proxy else None
109
107 110 headers = {
108 111 'authority': 'chat-server.tenant-forefront-default.knative.chi.coreweave.com',
109 112 'accept': '*/*',
@@ -135,6 +138,7 @@ class StreamingCompletion:
135 138 for chunk in post(
136 139 'https://chat-server.tenant-forefront-default.knative.chi.coreweave.com/chat',
137 140 headers=headers,
141 proxies=proxies,
138 142 json=json_data,
139 143 stream=True,
140 144 ).iter_lines():
@@ -169,6 +173,7 @@ class Completion:
169 173 action_type='new',
170 174 default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
171 175 model='gpt-4',
176 proxy=None
172 177 ) -> ForeFrontResponse:
173 178 text = ''
174 179 final_response = None
@@ -179,6 +184,7 @@ class Completion:
179 184 action_type=action_type,
180 185 default_persona=default_persona,
181 186 model=model,
187 proxy=proxy
182 188 ):
183 189 if response:
184 190 final_response = response
Modified gpt4free/quora/__init__.py +7 -1
@@ -187,7 +187,7 @@ class Account:
187 187 enable_bot_creation: bool = False,
188 188 ):
189 189 client = TLS(client_identifier='chrome110')
190 client.proxies = {'http': f'http://{proxy}', 'https': f'http://{proxy}'} if proxy else None
190 client.proxies = {'http': f'http://{proxy}', 'https': f'http://{proxy}'} if proxy else {}
191 191
192 192 mail_client = Emailnator()
193 193 mail_address = mail_client.get_mail()
@@ -293,10 +293,13 @@ class StreamingCompletion:
293 293 custom_model: bool = None,
294 294 prompt: str = 'hello world',
295 295 token: str = '',
296 proxy: Optional[str] = None
296 297 ) -> Generator[PoeResponse, None, None]:
297 298 _model = MODELS[model] if not custom_model else custom_model
298 299
300 proxies = { 'http': 'http://' + proxy, 'https': 'http://' + proxy } if proxy else False
299 301 client = PoeClient(token)
302 client.proxy = proxies
300 303
301 304 for chunk in client.send_message(_model, prompt):
302 305 yield PoeResponse(
@@ -330,10 +333,13 @@ class Completion:
330 333 custom_model: str = None,
331 334 prompt: str = 'hello world',
332 335 token: str = '',
336 proxy: Optional[str] = None
333 337 ) -> PoeResponse:
334 338 _model = MODELS[model] if not custom_model else custom_model
335 339
340 proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False
336 341 client = PoeClient(token)
342 client.proxy = proxies
337 343
338 344 chunk = None
339 345 for response in client.send_message(_model, prompt):
Modified gpt4free/theb/__init__.py +7 -4
@@ -2,7 +2,7 @@ from json import loads
2 2 from queue import Queue, Empty
3 3 from re import findall
4 4 from threading import Thread
5 from typing import Generator
5 from typing import Generator, Optional
6 6
7 7 from curl_cffi import requests
8 8 from fake_useragent import UserAgent
@@ -19,7 +19,7 @@ class Completion:
19 19 stream_completed = False
20 20
21 21 @staticmethod
22 def request(prompt: str):
22 def request(prompt: str, proxy: Optional[str]=None):
23 23 headers = {
24 24 'authority': 'chatbot.theb.ai',
25 25 'content-type': 'application/json',
@@ -27,9 +27,12 @@ class Completion:
27 27 'user-agent': UserAgent().random,
28 28 }
29 29
30 proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None
31
30 32 requests.post(
31 33 'https://chatbot.theb.ai/api/chat-process',
32 34 headers=headers,
35 proxies=proxies,
33 36 content_callback=Completion.handle_stream_response,
34 37 json={'prompt': prompt, 'options': {}},
35 38 )
@@ -37,8 +40,8 @@ class Completion:
37 40 Completion.stream_completed = True
38 41
39 42 @staticmethod
40 def create(prompt: str) -> Generator[str, None, None]:
41 Thread(target=Completion.request, args=[prompt]).start()
43 def create(prompt: str, proxy: Optional[str]=None) -> Generator[str, None, None]:
44 Thread(target=Completion.request, args=[prompt, proxy]).start()
42 45
43 46 while not Completion.stream_completed or not Completion.message_queue.empty():
44 47 try:
Modified gpt4free/you/__init__.py +4 -0
@@ -30,12 +30,16 @@ class Completion:
30 30 include_links: bool = False,
31 31 detailed: bool = False,
32 32 debug: bool = False,
33 proxy: Optional[str] = None
33 34 ) -> PoeResponse:
34 35 if chat is None:
35 36 chat = []
36 37
38 proxies = { 'http': 'http://' + proxy, 'https': 'http://' + proxy } if proxy else {}
39
37 40 client = Session(client_identifier='chrome_108')
38 41 client.headers = Completion.__get_headers()
42 client.proxies = proxies
39 43
40 44 response = client.get(
41 45 f'https://you.com/api/streamingSearch',
Modified gui/query_methods.py +15 -14
@@ -1,5 +1,6 @@
1 1 import os
2 2 import sys
3 from typing import Optional
3 4
4 5 sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir))
5 6
@@ -7,14 +8,14 @@ from gpt4free import quora, forefront, theb, you
7 8 import random
8 9
9 10
10 def query_forefront(question: str) -> str:
11 def query_forefront(question: str, proxy: Optional[str] = None) -> str:
11 12 # create an account
12 token = forefront.Account.create(logging=False)
13 token = forefront.Account.create(logging=False, proxy=proxy)
13 14
14 15 response = ""
15 16 # get a response
16 17 try:
17 return forefront.Completion.create(token=token, prompt='hello world', model='gpt-4').text
18 return forefront.Completion.create(token=token, prompt='hello world', model='gpt-4', proxy=proxy).text
18 19 except Exception as e:
19 20 # Return error message if an exception occurs
20 21 return (
@@ -22,16 +23,16 @@ def query_forefront(question: str) -> str:
22 23 )
23 24
24 25
25 def query_quora(question: str) -> str:
26 token = quora.Account.create(logging=False, enable_bot_creation=True)
27 return quora.Completion.create(model='gpt-4', prompt=question, token=token).text
26 def query_quora(question: str, proxy: Optional[str] = None) -> str:
27 token = quora.Account.create(logging=False, enable_bot_creation=True, proxy=proxy)
28 return quora.Completion.create(model='gpt-4', prompt=question, token=token, proxy=proxy).text
28 29
29 30
30 def query_theb(question: str) -> str:
31 def query_theb(question: str, proxy: Optional[str] = None) -> str:
31 32 # Set cloudflare clearance cookie and get answer from GPT-4 model
32 33 response = ""
33 34 try:
34 return ''.join(theb.Completion.create(prompt=question))
35 return ''.join(theb.Completion.create(prompt=question, proxy=proxy))
35 36
36 37 except Exception as e:
37 38 # Return error message if an exception occurs
@@ -40,11 +41,11 @@ def query_theb(question: str) -> str:
40 41 )
41 42
42 43
43 def query_you(question: str) -> str:
44 def query_you(question: str, proxy: Optional[str] = None) -> str:
44 45 # Set cloudflare clearance cookie and get answer from GPT-4 model
45 46 try:
46 result = you.Completion.create(prompt=question)
47 return result["response"]
47 result = you.Completion.create(prompt=question, proxy=proxy)
48 return result.text
48 49
49 50 except Exception as e:
50 51 # Return error message if an exception occurs
@@ -66,11 +67,11 @@ avail_query_methods = {
66 67 }
67 68
68 69
69 def query(user_input: str, selected_method: str = "Random") -> str:
70 def query(user_input: str, selected_method: str = "Random", proxy: Optional[str] = None) -> str:
70 71 # If a specific query method is selected (not "Random") and the method is in the dictionary, try to call it
71 72 if selected_method != "Random" and selected_method in avail_query_methods:
72 73 try:
73 return avail_query_methods[selected_method](user_input)
74 return avail_query_methods[selected_method](user_input, proxy=proxy)
74 75 except Exception as e:
75 76 print(f"Error with {selected_method}: {e}")
76 77 return "😵 Sorry, some error occurred please try again."
@@ -89,7 +90,7 @@ def query(user_input: str, selected_method: str = "Random") -> str:
89 90 chosen_query_name = [k for k, v in avail_query_methods.items() if v == chosen_query][0]
90 91 try:
91 92 # Try to call the chosen method with the user input
92 result = chosen_query(user_input)
93 result = chosen_query(user_input, proxy=proxy)
93 94 success = True
94 95 except Exception as e:
95 96 print(f"Error with {chosen_query_name}: {e}")
Modified gui/streamlit_chat_app.py +4 -1
@@ -76,7 +76,7 @@ user_input = input_placeholder.text_input(
76 76 submit_button = st.button("Submit")
77 77
78 78 if user_input or submit_button:
79 output = query(user_input, st.session_state['query_method'])
79 output = query(user_input, st.session_state['query_method'], st.session_state['proxy'])
80 80 escaped_output = output.encode('utf-8').decode('unicode-escape')
81 81
82 82 st.session_state.current_conversation['user_inputs'].append(user_input)
@@ -94,6 +94,9 @@ if st.sidebar.button("New Conversation"):
94 94
95 95 st.session_state['query_method'] = st.sidebar.selectbox("Select API:", options=avail_query_methods, index=0)
96 96
97 # Proxy
98 st.session_state['proxy'] = st.sidebar.text_input("Proxy: ")
99
97 100 # Sidebar
98 101 st.sidebar.header("Conversation History")
99 102