返回提交历史
Added
experimental/bard/__init__.py
+115
-0
Added
experimental/bard/typings.py
+15
-0
Added
experimental/bing/__ini__.py
+151
-0
Renamed
experimental/openai/__ini__.py
+0
-0
XFEstudio/gpt4free
opena, bard & bing [experimental]
0fed3059
代码差异
4 个文件
+281
-0
@@ -0,0 +1,115 @@
1
from requests import Session
2
from re import search
3
from random import randint
4
from json import dumps, loads
5
from random import randint
6
from urllib.parse import urlencode
7
from dotenv import load_dotenv; load_dotenv()
8
from os import getenv
9
10
from bard.typings import BardResponse
11
12
token = getenv('1psid')
13
proxy = getenv('proxy')
14
15
temperatures = {
16
0 : "Generate text strictly following known patterns, with no creativity.",
17
0.1: "Produce text adhering closely to established patterns, allowing minimal creativity.",
18
0.2: "Create text with modest deviations from familiar patterns, injecting a slight creative touch.",
19
0.3: "Craft text with a mild level of creativity, deviating somewhat from common patterns.",
20
0.4: "Formulate text balancing creativity and recognizable patterns for coherent results.",
21
0.5: "Generate text with a moderate level of creativity, allowing for a mix of familiarity and novelty.",
22
0.6: "Compose text with an increased emphasis on creativity, while partially maintaining familiar patterns.",
23
0.7: "Produce text favoring creativity over typical patterns for more original results.",
24
0.8: "Create text heavily focused on creativity, with limited concern for familiar patterns.",
25
0.9: "Craft text with a strong emphasis on unique and inventive ideas, largely ignoring established patterns.",
26
1 : "Generate text with maximum creativity, disregarding any constraints of known patterns or structures."
27
}
28
29
class Completion:
30
# def __init__(self, _token, proxy: str or None = None) -> None:
31
# self.client = Session()
32
# self.client.proxies = {
33
# 'http': f'http://{proxy}',
34
# 'https': f'http://{proxy}' } if proxy else None
35
36
# self.client.headers = {
37
# 'authority' : 'bard.google.com',
38
# 'content-type' : 'application/x-www-form-urlencoded;charset=UTF-8',
39
# 'origin' : 'https://bard.google.com',
40
# 'referer' : 'https://bard.google.com/',
41
# 'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
42
# 'x-same-domain' : '1',
43
# 'cookie' : f'__Secure-1PSID={_token}'
44
# }
45
46
# self.snlm0e = self.__init_client()
47
# self.conversation_id = ''
48
# self.response_id = ''
49
# self.choice_id = ''
50
# self.reqid = randint(1111, 9999)
51
52
def create(
53
prompt : str = 'hello world',
54
temperature : int = None,
55
conversation_id : str = '',
56
response_id : str = '',
57
choice_id : str = '') -> BardResponse:
58
59
if temperature:
60
prompt = f'''settings: follow these settings for your response: [temperature: {temperature} - {temperatures[temperature]}] | prompt : {prompt}'''
61
62
client = Session()
63
client.proxies = {
64
'http': f'http://{proxy}',
65
'https': f'http://{proxy}' } if proxy else None
66
67
client.headers = {
68
'authority' : 'bard.google.com',
69
'content-type' : 'application/x-www-form-urlencoded;charset=UTF-8',
70
'origin' : 'https://bard.google.com',
71
'referer' : 'https://bard.google.com/',
72
'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
73
'x-same-domain' : '1',
74
'cookie' : f'__Secure-1PSID={token}'
75
}
76
77
snlm0e = search(r'SNlM0e\":\"(.*?)\"', client.get('https://bard.google.com/').text).group(1)
78
79
params = urlencode({
80
'bl' : 'boq_assistant-bard-web-server_20230326.21_p0',
81
'_reqid' : randint(1111, 9999),
82
'rt' : 'c',
83
})
84
85
response = client.post(f'https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate?{params}',
86
data = {
87
'at': snlm0e,
88
'f.req': dumps([None, dumps([
89
[prompt],
90
None,
91
[conversation_id, response_id, choice_id],
92
])
93
])
94
}
95
)
96
97
chat_data = loads(response.content.splitlines()[3])[0][2]
98
if not chat_data: print('error, retrying'); Completion.create(prompt, temperature, conversation_id, response_id, choice_id)
99
100
json_chat_data = loads(chat_data)
101
results = {
102
'content' : json_chat_data[0][0],
103
'conversation_id' : json_chat_data[1][0],
104
'response_id' : json_chat_data[1][1],
105
'factualityQueries' : json_chat_data[3],
106
'textQuery' : json_chat_data[2][0] if json_chat_data[2] is not None else '',
107
'choices' : [{'id': i[0], 'content': i[1]} for i in json_chat_data[4]],
108
}
109
110
# self.conversation_id = results['conversation_id']
111
# self.response_id = results['response_id']
112
# self.choice_id = results['choices'][0]['id']
113
# self.reqid += 100000
114
115
return BardResponse(results)
@@ -0,0 +1,15 @@
1
class BardResponse:
2
def __init__(self, json_dict):
3
self.json = json_dict
4
5
self.content = json_dict.get('content')
6
self.conversation_id = json_dict.get('conversation_id')
7
self.response_id = json_dict.get('response_id')
8
self.factuality_queries = json_dict.get('factualityQueries', [])
9
self.text_query = json_dict.get('textQuery', [])
10
self.choices = [self.BardChoice(choice) for choice in json_dict.get('choices', [])]
11
12
class BardChoice:
13
def __init__(self, choice_dict):
14
self.id = choice_dict.get('id')
15
self.content = choice_dict.get('content')[0]
@@ -0,0 +1,151 @@
1
from requests import get
2
from browser_cookie3 import edge, chrome
3
from ssl import create_default_context
4
from certifi import where
5
from uuid import uuid4
6
from random import randint
7
from json import dumps, loads
8
9
import asyncio
10
import websockets
11
12
ssl_context = create_default_context()
13
ssl_context.load_verify_locations(where())
14
15
def format(msg: dict) -> str:
16
return dumps(msg) + '\x1e'
17
18
def get_token():
19
20
cookies = {c.name: c.value for c in edge(domain_name='bing.com')}
21
return cookies['_U']
22
23
24
25
class AsyncCompletion:
26
async def create(
27
prompt : str = 'hello world',
28
optionSets : list = [
29
'deepleo',
30
'enable_debug_commands',
31
'disable_emoji_spoken_text',
32
'enablemm',
33
'h3relaxedimg'
34
],
35
token : str = get_token()):
36
37
create = get('https://edgeservices.bing.com/edgesvc/turing/conversation/create',
38
headers = {
39
'host' : 'edgeservices.bing.com',
40
'authority' : 'edgeservices.bing.com',
41
'cookie' : f'_U={token}',
42
'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69',
43
}
44
)
45
46
conversationId = create.json()['conversationId']
47
clientId = create.json()['clientId']
48
conversationSignature = create.json()['conversationSignature']
49
50
wss: websockets.WebSocketClientProtocol or None = None
51
52
wss = await websockets.connect('wss://sydney.bing.com/sydney/ChatHub', max_size = None, ssl = ssl_context,
53
extra_headers = {
54
'accept': 'application/json',
55
'accept-language': 'en-US,en;q=0.9',
56
'content-type': 'application/json',
57
'sec-ch-ua': '"Not_A Brand";v="99", Microsoft Edge";v="110", "Chromium";v="110"',
58
'sec-ch-ua-arch': '"x86"',
59
'sec-ch-ua-bitness': '"64"',
60
'sec-ch-ua-full-version': '"109.0.1518.78"',
61
'sec-ch-ua-full-version-list': '"Chromium";v="110.0.5481.192", "Not A(Brand";v="24.0.0.0", "Microsoft Edge";v="110.0.1587.69"',
62
'sec-ch-ua-mobile': '?0',
63
'sec-ch-ua-model': "",
64
'sec-ch-ua-platform': '"Windows"',
65
'sec-ch-ua-platform-version': '"15.0.0"',
66
'sec-fetch-dest': 'empty',
67
'sec-fetch-mode': 'cors',
68
'sec-fetch-site': 'same-origin',
69
'x-ms-client-request-id': str(uuid4()),
70
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
71
'Referer': 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx',
72
'Referrer-Policy': 'origin-when-cross-origin',
73
'x-forwarded-for': f'13.{randint(104, 107)}.{randint(0, 255)}.{randint(0, 255)}'
74
}
75
)
76
77
await wss.send(format({'protocol': 'json', 'version': 1}))
78
await wss.recv()
79
80
struct = {
81
'arguments': [
82
{
83
'source': 'cib',
84
'optionsSets': optionSets,
85
'isStartOfSession': True,
86
'message': {
87
'author': 'user',
88
'inputMethod': 'Keyboard',
89
'text': prompt,
90
'messageType': 'Chat'
91
},
92
'conversationSignature': conversationSignature,
93
'participant': {
94
'id': clientId
95
},
96
'conversationId': conversationId
97
}
98
],
99
'invocationId': '0',
100
'target': 'chat',
101
'type': 4
102
}
103
104
await wss.send(format(struct))
105
106
base_string = ''
107
108
final = False
109
while not final:
110
objects = str(await wss.recv()).split('\x1e')
111
for obj in objects:
112
if obj is None or obj == '':
113
continue
114
115
response = loads(obj)
116
if response.get('type') == 1 and response['arguments'][0].get('messages',):
117
response_text = response['arguments'][0]['messages'][0]['adaptiveCards'][0]['body'][0].get('text')
118
119
yield (response_text.replace(base_string, ''))
120
base_string = response_text
121
122
elif response.get('type') == 2:
123
final = True
124
125
await wss.close()
126
127
async def run():
128
async for value in AsyncCompletion.create(
129
prompt = 'summarize cinderella with each word beginning with a consecutive letter of the alphabet, a-z',
130
# optionSets = [
131
# "deepleo",
132
# "enable_debug_commands",
133
# "disable_emoji_spoken_text",
134
# "enablemm"
135
# ]
136
optionSets = [
137
#"nlu_direct_response_filter",
138
#"deepleo",
139
#"disable_emoji_spoken_text",
140
# "responsible_ai_policy_235",
141
#"enablemm",
142
"galileo",
143
#"dtappid",
144
# "cricinfo",
145
# "cricinfov2",
146
# "dv3sugg",
147
]
148
):
149
print(value, end = '', flush=True)
150
151
asyncio.run(run())
此文件没有可显示的逐行差异。