フォーク元 g4f/gpt4free
コミットを比較
11 コミット
c7b9a02780
...
65239d1a79
作成者 | SHA1 | 日付 |
---|---|---|
![]() |
65239d1a79 | |
![]() |
f10d74eb6a | |
![]() |
abaed3da53 | |
![]() |
61d14252fd | |
![]() |
e4871265e8 | |
![]() |
61bfcb8a19 | |
![]() |
b2631af65e | |
|
6ecaa00b34 | |
|
283a1670ee | |
![]() |
172f660989 | |
![]() |
46c087a892 |
21
README.md
21
README.md
|
@ -26,6 +26,20 @@ for token in chat_completion:
|
|||
```
|
||||
|
||||
### simple usage:
|
||||
|
||||
providers:
|
||||
```py
|
||||
g4f.Providers.Openai # need to be logged in in browser
|
||||
g4f.Providers.Bing # need to be logged in in browser
|
||||
g4f.Providers.You
|
||||
g4f.Providers.Ails
|
||||
g4f.Providers.Phind
|
||||
g4f.Providers.Yqcloud
|
||||
|
||||
# usage:
|
||||
response = g4f.ChatCompletion.create(..., provider=g4f.Providers.ProviderName)
|
||||
```
|
||||
|
||||
```py
|
||||
import g4f
|
||||
|
||||
|
@ -42,13 +56,14 @@ for message in response:
|
|||
print(message)
|
||||
|
||||
# normal response
|
||||
response = g4f.ChatCompletion.create(model=g4f.Models.gpt_4, prompt='hi') # alterative model setting
|
||||
response = g4f.ChatCompletion.create(model=g4f.Models.gpt_4, messages=[
|
||||
{"role": "user", "content": "hi"}]) # alterative model setting
|
||||
|
||||
print(response)
|
||||
|
||||
|
||||
# Set with provider
|
||||
response = g4f.ChatCompletion.create(model='gpt-3.5-turbo', provider=g4f.Providers.Phind, messages=[
|
||||
response = g4f.ChatCompletion.create(model='gpt-3.5-turbo', provider=g4f.Providers.Openai, messages=[
|
||||
{"role": "user", "content": "Hello world"}], stream=True)
|
||||
|
||||
for message in response:
|
||||
|
@ -78,4 +93,4 @@ def _create_completion(prompt: str, args...):
|
|||
|
||||
params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
|
||||
', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
|
||||
```
|
||||
```
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
import ar, ar.utils
|
||||
import bundlr
|
||||
|
||||
import json
|
||||
import threading
|
||||
import urllib
|
||||
|
||||
wallet = None # assigned at end of file
|
||||
bundlr_url = bundlr.node.DEFAULT_API_URL # usually provides free upload for <100KB dataitems
|
||||
gateway = ar.PUBLIC_GATEWAYS[0]
|
||||
|
||||
bundlr_node = bundlr.Node(bundlr_url)
|
||||
peer = ar.Peer(gateway)
|
||||
current_block = peer.current_block()
|
||||
|
||||
def _log_completion(input: list, output: str, metadata: dict):
|
||||
# wrap the data in a convenient folder.
|
||||
# making the input and output their own entries lets them get longer without triggering a fee
|
||||
entries = {
|
||||
'input': __ditem(input),
|
||||
'output': __ditem(output, 'text/plain'),
|
||||
'metadata': __ditem(metadata),
|
||||
}
|
||||
tags = { k: v for k,v in metadata.items() if type(v) is str }
|
||||
|
||||
# upload it
|
||||
ditemid = __manifest(entries, tags)
|
||||
|
||||
# return the url to the folder on the gateway
|
||||
# include information in the url on finding the data
|
||||
urlpart = (f'{ditemid}' +
|
||||
f'#minblockheight={current_block["height"]}' +
|
||||
f'&minblockhash={current_block["indep_hash"]}')
|
||||
return urllib.parse.urljoin(gateway, urlpart)
|
||||
|
||||
def __ditem(data, content_type = None, **tags):
|
||||
# convert data into a signed dataitem object
|
||||
if content_type is not None:
|
||||
tags['Content-Type'] = content_type
|
||||
if type(data) in (dict, list):
|
||||
data = json.dumps(data)
|
||||
tags.setdefault('Content-Type', 'application/json')
|
||||
data = ar.DataItem(data = data.encode())
|
||||
data.header.tags = [ar.utils.create_tag(k, v, True) for k, v in tags.items()]
|
||||
data.sign(wallet.rsa)
|
||||
return data
|
||||
|
||||
def __manifest(ditems, tags):
|
||||
# add index.html
|
||||
ditems['index.html'] = __ditem('\n'.join([
|
||||
f'<p><a href="{name}">{name}</a></p>'
|
||||
for name in ditems
|
||||
]), 'text/html')
|
||||
|
||||
# add directory manifest
|
||||
ditems['manifest'] = __ditem(dict(
|
||||
manifest = 'arweave/paths',
|
||||
version = '0.1.0',
|
||||
index = dict(
|
||||
path = 'index.html',
|
||||
),
|
||||
paths = {
|
||||
name: dict(id = ditem.header.id)
|
||||
for name, ditem in ditems.items()
|
||||
}
|
||||
), 'application/x.arweave-manifest+json', **tags)
|
||||
|
||||
# store data
|
||||
threads = [threading.Thread(target=__send, args=(ditem,)) for ditem in ditems.values()]
|
||||
[thread.start() for thread in threads]
|
||||
[thread.join() for thread in threads]
|
||||
|
||||
# return id of manifest as locator
|
||||
return ditems['manifest'].header.id
|
||||
|
||||
def __send(ditem):
|
||||
# send a dataitem to the bundlr node
|
||||
result = bundlr_node.send_tx(ditem.tobytes())
|
||||
assert result['id'] == ditem.header.id
|
||||
|
||||
DEFAULT_WALLET_JWK = {
|
||||
'alg': 'RS256',
|
||||
'kty': 'RSA',
|
||||
'n': '4FtI5e6UCJcT1f58HKGihz988DzoO2n3lyk1NzX86wRpDAiwQGEWDI_mZXK9sHA4o6n8f80yzKXOaSX0tx6hybw1NgrlrUoaQ-3DwDtoCERG12gNttN4w9EU-LJL092tqVAyPvGhjj_I_L1-_IqpBMoOS2d600si-TDWDolJKi93VBmDCsRVLgmGeLPe7cjIDrrDLNesbP2HE3Zhdup4UWv7HP_8tvctOwCDUtf6n4QJBmx8_tmhXWO-4Y2Tuu-6Ujg9aTNTNiaFqIPzh0FzMhryA9V6-DjDjJa-A1KMnInjoV0KD-e-SBOwIxulQi7InaAIyyKNZ8iytkqk69VTQlHMAaTC2IgwcFFvm-Uag2dgM9Y_2RmUgbxWmqMpglB33atrHfixCY3iXsLRjYcC6zPXPEWvrCdRW6xxWojS7vH8YWhkmzzaMBsfORvO0pRlB0CvC0qYON_I3wiEW2YAAmq9GAXPV3SL1ZiebPwG1BKUa3NHdW9U-Dm4W1VLqp3IiCumZAqp7CWDZTRbm7XmTqAHb2HAbkdzFZ9Bee09N1VqBvxRESCETVx1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6nsbE6d3cUdOJ2OBij0Ta8rBksGQeIHqPtrT7sEkuJYlNvqXi70hSsfOtsHL_Wd-5T6ZITrJNYslLB916KJ3T-LU0O2LNh1k2b-Q',
|
||||
'qi': 'khIRVObEMDDJkfKfzojUpbSxRMNIRXKM8KoxOkykw_z5EdkeF5jSa8BBcEPGmyXPdFBganJVmtyX_D1yp9BwM4QkWImuwvUo1sJwfAPD_s2B6nk7sR0XAQp14jd5syIZHtt5rgW-yTxOg7uE611kjpS1xcuScdbzvfcCLEZiSiR6MIZgw6ifS4bmiDK5I4n95VMdgtPOMStrRK3s-INSrTYd3ty7KM1aiLGF1KvktyY-1YyoZS2rfctj0RaqWzPoQfZur3Yrla01uNOwOiVgPhTWGNxEFlw90Pumzsi373VeP1GcpCGykMYLsHbGAsbhI9N2svuTaScOybfZq3Sblg',
|
||||
'p2s': ''
|
||||
}
|
||||
wallet = ar.Wallet.from_data(DEFAULT_WALLET_JWK)
|
|
@ -0,0 +1,2 @@
|
|||
def _log_completion(input: list, output: str, metadata: dict):
|
||||
return
|
|
@ -0,0 +1 @@
|
|||
from . import Arweave, Logger
|
|
@ -21,10 +21,10 @@ class Utils:
|
|||
base_string: str = '%s:%s:%s:%s' % (
|
||||
json_data['t'],
|
||||
json_data['m'],
|
||||
secretKey.decode(),
|
||||
'OVbi[TPN{S#)c{36%9?g;usl)CL',
|
||||
len(json_data['m'])
|
||||
)
|
||||
|
||||
|
||||
return hashlib.sha256(base_string.encode()).hexdigest()
|
||||
|
||||
def format_timestamp(timestamp: int) -> str:
|
||||
|
@ -38,11 +38,20 @@ class Utils:
|
|||
def _create_completion(model: str,messages: list, temperature: float = 0.6, stream: bool = False):
|
||||
headers = {
|
||||
'authority': 'api.caipacity.com',
|
||||
'authorization': f'Bearer free',
|
||||
'accept': '*/*',
|
||||
'authorization': 'Bearer free',
|
||||
'client-id': str(uuid.uuid4()),
|
||||
'client-v': '0.1.26',
|
||||
'content-type': 'application/json',
|
||||
'origin': 'https://ai.ls',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
|
||||
'referer': 'https://ai.ls/',
|
||||
'sec-ch-ua': '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
|
||||
'sec-ch-ua-mobile': '?0',
|
||||
'sec-ch-ua-platform': '"macOS"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'cross-site',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
|
||||
}
|
||||
|
||||
timestamp = Utils.format_timestamp(int(time.time() * 1000))
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
import os
|
||||
import json
|
||||
import time
|
||||
import subprocess
|
||||
|
||||
from ..typing import sha256, Dict, get_type_hints
|
||||
|
||||
url = 'https://bing.com/chat'
|
||||
model = ['gpt-3.5-turbo', 'gpt-4']
|
||||
|
||||
def _create_completion(model: str, messages: list, **kwargs):
|
||||
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
config = json.dumps({
|
||||
'messages': messages,
|
||||
'model': model}, separators=(',', ':'))
|
||||
|
||||
cmd = ['python3', f'{path}/helpers/bing.py', config]
|
||||
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
|
||||
for line in iter(p.stdout.readline, b''):
|
||||
yield line.decode('utf-8')[:-1]
|
||||
|
||||
|
||||
params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
|
||||
'(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
|
|
@ -0,0 +1,27 @@
|
|||
import os
|
||||
import json
|
||||
import time
|
||||
import subprocess
|
||||
|
||||
from ..typing import sha256, Dict, get_type_hints
|
||||
|
||||
url = 'https://chat.openai.com/chat'
|
||||
model = ['gpt-3.5-turbo']
|
||||
|
||||
def _create_completion(model: str, messages: list, **kwargs):
|
||||
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
config = json.dumps({
|
||||
'messages': messages,
|
||||
'model': model}, separators=(',', ':'))
|
||||
|
||||
cmd = ['python3', f'{path}/helpers/openai.py', config]
|
||||
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
|
||||
for line in iter(p.stdout.readline, b''):
|
||||
yield line.decode('utf-8')[:-1]
|
||||
|
||||
|
||||
params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
|
||||
'(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
|
|
@ -3,13 +3,16 @@ import json
|
|||
import time
|
||||
import subprocess
|
||||
|
||||
url = None
|
||||
model = None
|
||||
from ..typing import sha256, Dict, get_type_hints
|
||||
|
||||
url = 'https://phind.com'
|
||||
model = ['gpt-3.5-turbo', 'gpt-4']
|
||||
|
||||
def _create_completion(model: str, messages: list, **kwargs):
|
||||
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
config = json.dumps({
|
||||
'model': model,
|
||||
'messages': messages}, separators=(',', ':'))
|
||||
|
||||
cmd = ['python3', f'{path}/helpers/phind.py', config]
|
||||
|
@ -26,4 +29,7 @@ def _create_completion(model: str, messages: list, **kwargs):
|
|||
if b'ping - 2023-' in line:
|
||||
continue
|
||||
|
||||
yield line.decode('utf-8')[:-1]
|
||||
yield line.decode('utf-8')[:-1]
|
||||
|
||||
params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
|
||||
'(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
|
|
@ -1,2 +0,0 @@
|
|||
def _create_completion(prompt: str, **kwargs):
|
||||
return 'helloooo', prompt
|
|
@ -0,0 +1,22 @@
|
|||
import os
|
||||
import json
|
||||
import time
|
||||
import subprocess
|
||||
|
||||
from ..typing import sha256, Dict, get_type_hints
|
||||
|
||||
url = 'https://you.com'
|
||||
model = 'gpt-3.5-turbo'
|
||||
|
||||
def _create_completion(model: str, messages: list, **kwargs):
|
||||
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
config = json.dumps({
|
||||
'messages': messages}, separators=(',', ':'))
|
||||
|
||||
cmd = ['python3', f'{path}/helpers/you.py', config]
|
||||
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
|
||||
for line in iter(p.stdout.readline, b''):
|
||||
yield line.decode('utf-8')[:-1]
|
|
@ -0,0 +1,32 @@
|
|||
import os
|
||||
import time
|
||||
import requests
|
||||
|
||||
from ..typing import sha256, Dict, get_type_hints
|
||||
|
||||
|
||||
def _create_completion(model: str, messages: list, **kwargs):
|
||||
|
||||
headers = {
|
||||
'authority': 'api.aichatos.cloud',
|
||||
'origin': 'https://chat9.yqcloud.top',
|
||||
'referer': 'https://chat9.yqcloud.top/',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
|
||||
}
|
||||
|
||||
json_data = {
|
||||
'prompt': 'always respond in english | %s' % messages[-1]['content'],
|
||||
'userId': f'#/chat/{int(time.time() * 1000)}',
|
||||
'network': True,
|
||||
'apikey': '',
|
||||
'system': '',
|
||||
'withoutContext': False,
|
||||
}
|
||||
|
||||
response = requests.post('https://api.aichatos.cloud/api/generateStream', headers=headers, json=json_data, stream=True)
|
||||
for token in response.iter_content(chunk_size=2046):
|
||||
if not b'always respond in english' in token:
|
||||
yield (token.decode('utf-8'))
|
||||
|
||||
params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
|
||||
'(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
|
|
@ -1 +1 @@
|
|||
from . import Ails, Phind, Test, Provider
|
||||
from . import Provider, Ails, Phind, You, Bing, Openai, Yqcloud
|
|
@ -0,0 +1,185 @@
|
|||
import sys
|
||||
import ssl
|
||||
import uuid
|
||||
import json
|
||||
import random
|
||||
import asyncio
|
||||
import certifi
|
||||
import requests
|
||||
import websockets
|
||||
import browser_cookie3
|
||||
|
||||
config = json.loads(sys.argv[1])
|
||||
|
||||
ssl_context = ssl.create_default_context()
|
||||
ssl_context.load_verify_locations(certifi.where())
|
||||
|
||||
conversationstyles = {
|
||||
'gpt-4': [ #'precise'
|
||||
"nlu_direct_response_filter",
|
||||
"deepleo",
|
||||
"disable_emoji_spoken_text",
|
||||
"responsible_ai_policy_235",
|
||||
"enablemm",
|
||||
"h3imaginative",
|
||||
"travelansgnd",
|
||||
"dv3sugg",
|
||||
"clgalileo",
|
||||
"gencontentv3",
|
||||
"dv3sugg",
|
||||
"responseos",
|
||||
"e2ecachewrite",
|
||||
"cachewriteext",
|
||||
"nodlcpcwrite",
|
||||
"travelansgnd",
|
||||
"nojbfedge",
|
||||
],
|
||||
'balanced': [
|
||||
"nlu_direct_response_filter",
|
||||
"deepleo",
|
||||
"disable_emoji_spoken_text",
|
||||
"responsible_ai_policy_235",
|
||||
"enablemm",
|
||||
"galileo",
|
||||
"dv3sugg",
|
||||
"responseos",
|
||||
"e2ecachewrite",
|
||||
"cachewriteext",
|
||||
"nodlcpcwrite",
|
||||
"travelansgnd",
|
||||
"nojbfedge",
|
||||
],
|
||||
'gpt-3.5-turbo': [ #'precise'
|
||||
"nlu_direct_response_filter",
|
||||
"deepleo",
|
||||
"disable_emoji_spoken_text",
|
||||
"responsible_ai_policy_235",
|
||||
"enablemm",
|
||||
"galileo",
|
||||
"dv3sugg",
|
||||
"responseos",
|
||||
"e2ecachewrite",
|
||||
"cachewriteext",
|
||||
"nodlcpcwrite",
|
||||
"travelansgnd",
|
||||
"h3precise",
|
||||
"clgalileo",
|
||||
"nojbfedge",
|
||||
]
|
||||
}
|
||||
|
||||
def format(msg: dict) -> str:
|
||||
return json.dumps(msg) + '\x1e'
|
||||
|
||||
def get_token():
|
||||
try:
|
||||
cookies = {c.name: c.value for c in browser_cookie3.edge(domain_name='bing.com')}
|
||||
return cookies['_U']
|
||||
except:
|
||||
print('Error: could not find bing _U cookie in edge browser.')
|
||||
exit(1)
|
||||
|
||||
class AsyncCompletion:
|
||||
async def create(
|
||||
prompt : str = None,
|
||||
optionSets : list = None,
|
||||
token : str = get_token()):
|
||||
|
||||
create = requests.get('https://edgeservices.bing.com/edgesvc/turing/conversation/create',
|
||||
headers = {
|
||||
'host' : 'edgeservices.bing.com',
|
||||
'authority' : 'edgeservices.bing.com',
|
||||
'cookie' : f'_U={token}',
|
||||
'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',
|
||||
}
|
||||
)
|
||||
|
||||
conversationId = create.json()['conversationId']
|
||||
clientId = create.json()['clientId']
|
||||
conversationSignature = create.json()['conversationSignature']
|
||||
|
||||
wss: websockets.WebSocketClientProtocol or None = None
|
||||
|
||||
wss = await websockets.connect('wss://sydney.bing.com/sydney/ChatHub', max_size = None, ssl = ssl_context,
|
||||
extra_headers = {
|
||||
'accept': 'application/json',
|
||||
'accept-language': 'en-US,en;q=0.9',
|
||||
'content-type': 'application/json',
|
||||
'sec-ch-ua': '"Not_A Brand";v="99", Microsoft Edge";v="110", "Chromium";v="110"',
|
||||
'sec-ch-ua-arch': '"x86"',
|
||||
'sec-ch-ua-bitness': '"64"',
|
||||
'sec-ch-ua-full-version': '"109.0.1518.78"',
|
||||
'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"',
|
||||
'sec-ch-ua-mobile': '?0',
|
||||
'sec-ch-ua-model': "",
|
||||
'sec-ch-ua-platform': '"Windows"',
|
||||
'sec-ch-ua-platform-version': '"15.0.0"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'same-origin',
|
||||
'x-ms-client-request-id': str(uuid.uuid4()),
|
||||
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
|
||||
'Referer': 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx',
|
||||
'Referrer-Policy': 'origin-when-cross-origin',
|
||||
'x-forwarded-for': f'13.{random.randint(104, 107)}.{random.randint(0, 255)}.{random.randint(0, 255)}'
|
||||
}
|
||||
)
|
||||
|
||||
await wss.send(format({'protocol': 'json', 'version': 1}))
|
||||
await wss.recv()
|
||||
|
||||
struct = {
|
||||
'arguments': [
|
||||
{
|
||||
'source': 'cib',
|
||||
'optionsSets': optionSets,
|
||||
'isStartOfSession': True,
|
||||
'message': {
|
||||
'author': 'user',
|
||||
'inputMethod': 'Keyboard',
|
||||
'text': prompt,
|
||||
'messageType': 'Chat'
|
||||
},
|
||||
'conversationSignature': conversationSignature,
|
||||
'participant': {
|
||||
'id': clientId
|
||||
},
|
||||
'conversationId': conversationId
|
||||
}
|
||||
],
|
||||
'invocationId': '0',
|
||||
'target': 'chat',
|
||||
'type': 4
|
||||
}
|
||||
|
||||
await wss.send(format(struct))
|
||||
|
||||
base_string = ''
|
||||
|
||||
final = False
|
||||
while not final:
|
||||
objects = str(await wss.recv()).split('\x1e')
|
||||
for obj in objects:
|
||||
if obj is None or obj == '':
|
||||
continue
|
||||
|
||||
response = json.loads(obj)
|
||||
if response.get('type') == 1 and response['arguments'][0].get('messages',):
|
||||
response_text = response['arguments'][0]['messages'][0]['adaptiveCards'][0]['body'][0].get('text')
|
||||
|
||||
yield (response_text.replace(base_string, ''))
|
||||
base_string = response_text
|
||||
|
||||
elif response.get('type') == 2:
|
||||
final = True
|
||||
|
||||
await wss.close()
|
||||
|
||||
async def run(optionSets, messages):
|
||||
async for value in AsyncCompletion.create(prompt=messages[-1]['content'],
|
||||
optionSets=optionSets):
|
||||
|
||||
print(value, flush=True)
|
||||
|
||||
optionSet = conversationstyles[config['model']]
|
||||
asyncio.run(run(optionSet, config['messages']))
|
|
@ -0,0 +1,97 @@
|
|||
import sys
|
||||
import uuid
|
||||
import json
|
||||
import browser_cookie3
|
||||
|
||||
from curl_cffi import requests
|
||||
|
||||
config = json.loads(sys.argv[1])
|
||||
|
||||
def session_auth(cookies):
|
||||
headers = {
|
||||
'authority': 'chat.openai.com',
|
||||
'accept': '*/*',
|
||||
'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
|
||||
'cache-control': 'no-cache',
|
||||
'pragma': 'no-cache',
|
||||
'referer': 'https://chat.openai.com/chat',
|
||||
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
|
||||
'sec-ch-ua-mobile': '?0',
|
||||
'sec-ch-ua-platform': '"macOS"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'same-origin',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
|
||||
}
|
||||
|
||||
return requests.get('https://chat.openai.com/api/auth/session',
|
||||
cookies=cookies, headers=headers, impersonate='chrome110').json()
|
||||
|
||||
all_cookies = {cookie.name: cookie.value for cookie in browser_cookie3.chrome(
|
||||
domain_name='chat.openai.com')}
|
||||
|
||||
try:
|
||||
cookies = {
|
||||
'__Secure-next-auth.session-token': all_cookies['__Secure-next-auth.session-token'],
|
||||
}
|
||||
except Exception:
|
||||
print('Failed to get "__Secure-next-auth.session-token" in chrome, please make sure you are authenticated on openai.com')
|
||||
exit(0)
|
||||
|
||||
headers = {
|
||||
'authority': 'chat.openai.com',
|
||||
'accept': 'text/event-stream',
|
||||
'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
|
||||
'authorization': 'Bearer ' + session_auth(cookies)['accessToken'],
|
||||
'cache-control': 'no-cache',
|
||||
'content-type': 'application/json',
|
||||
'origin': 'https://chat.openai.com',
|
||||
'pragma': 'no-cache',
|
||||
'referer': 'https://chat.openai.com/chat',
|
||||
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
|
||||
'sec-ch-ua-mobile': '?0',
|
||||
'sec-ch-ua-platform': '"macOS"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'same-origin',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
|
||||
}
|
||||
|
||||
payload = {
|
||||
'action': 'next',
|
||||
'history_and_training_disabled': False,
|
||||
'messages': [
|
||||
{
|
||||
'id': str(uuid.uuid4()),
|
||||
'author': {
|
||||
'role': 'user',
|
||||
},
|
||||
'content': {
|
||||
'content_type': 'text',
|
||||
'parts': [
|
||||
config['messages'][-1]['content']
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
'model': 'text-davinci-002-render-sha',
|
||||
'parent_message_id': str(uuid.uuid4()),
|
||||
'supports_modapi': True,
|
||||
'timezone_offset_min': -60
|
||||
}
|
||||
|
||||
completion = ''
|
||||
|
||||
def format(chunk):
|
||||
global completion
|
||||
|
||||
if b'parts' in chunk:
|
||||
json_data = json.loads(chunk.decode('utf-8').split('data: ')[1])
|
||||
token = json_data['message']['content']['parts'][0]
|
||||
token = token.replace(completion, '')
|
||||
completion += token
|
||||
|
||||
print(token, flush=True)
|
||||
|
||||
response = requests.post('https://chat.openai.com/backend-api/conversation',
|
||||
json=payload, headers=headers, content_callback=format, impersonate='chrome110')
|
|
@ -8,10 +8,12 @@ from curl_cffi import requests
|
|||
config = json.loads(sys.argv[1])
|
||||
prompt = config['messages'][-1]['content']
|
||||
|
||||
skill = 'expert' if config['model'] == 'gpt-4' else 'intermediate'
|
||||
|
||||
json_data = json.dumps({
|
||||
'question': prompt,
|
||||
'options': {
|
||||
'skill': 'expert',
|
||||
'skill': skill,
|
||||
'date': datetime.datetime.now().strftime('%d/%m/%Y'),
|
||||
'language': 'en',
|
||||
'detailed': False,
|
||||
|
@ -49,5 +51,5 @@ def output(chunk):
|
|||
print(chunk, flush=True)
|
||||
|
||||
|
||||
response = requests.post('https://www.phind.com/api/infer/creative',
|
||||
response = requests.post('https://www.phind.com/api/infer/answer',
|
||||
headers=headers, data=json_data, content_callback=output, timeout=999999, impersonate='safari15_5')
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
import sys
|
||||
import json
|
||||
import urllib.parse
|
||||
|
||||
from curl_cffi import requests
|
||||
|
||||
config = json.loads(sys.argv[1])
|
||||
messages = config['messages']
|
||||
prompt = ''
|
||||
|
||||
|
||||
def transform(messages: list) -> list:
|
||||
result = []
|
||||
i = 0
|
||||
|
||||
while i < len(messages):
|
||||
if messages[i]['role'] == 'user':
|
||||
question = messages[i]['content']
|
||||
i += 1
|
||||
|
||||
if i < len(messages) and messages[i]['role'] == 'assistant':
|
||||
answer = messages[i]['content']
|
||||
i += 1
|
||||
else:
|
||||
answer = ''
|
||||
|
||||
result.append({'question': question, 'answer': answer})
|
||||
|
||||
elif messages[i]['role'] == 'assistant':
|
||||
result.append({'question': '', 'answer': messages[i]['content']})
|
||||
i += 1
|
||||
|
||||
elif messages[i]['role'] == 'system':
|
||||
result.append({'question': messages[i]['content'], 'answer': ''})
|
||||
i += 1
|
||||
|
||||
return result
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
'Accept-Language': 'en-GB,en;q=0.9',
|
||||
'Sec-Fetch-Mode': 'navigate',
|
||||
'Host': 'you.com',
|
||||
'Origin': 'https://you.com',
|
||||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15',
|
||||
'Referer': 'https://you.com/api/streamingSearch?q=nice&safeSearch=Moderate&onShoppingPage=false&mkt=&responseFilter=WebPages,Translations,TimeZone,Computation,RelatedSearches&domain=youchat&queryTraceId=7a6671f8-5881-404d-8ea3-c3f8301f85ba&chat=%5B%7B%22question%22%3A%22hi%22%2C%22answer%22%3A%22Hello!%20How%20can%20I%20assist%20you%20today%3F%22%7D%5D&chatId=7a6671f8-5881-404d-8ea3-c3f8301f85ba&__cf_chl_tk=ex2bw6vn5vbLsUm8J5rDYUC0Bjzc1XZqka6vUl6765A-1684108495-0-gaNycGzNDtA',
|
||||
'Connection': 'keep-alive',
|
||||
'Sec-Fetch-Dest': 'document',
|
||||
'Priority': 'u=0, i',
|
||||
}
|
||||
|
||||
if messages[-1]['role'] == 'user':
|
||||
prompt = messages[-1]['content']
|
||||
messages = messages[:-1]
|
||||
|
||||
params = urllib.parse.urlencode({
|
||||
'q': prompt,
|
||||
'domain': 'youchat',
|
||||
'chat': transform(messages)
|
||||
})
|
||||
|
||||
def output(chunk):
|
||||
if b'"youChatToken"' in chunk:
|
||||
chunk_json = json.loads(chunk.decode().split('data: ')[1])
|
||||
|
||||
print(chunk_json['youChatToken'], flush=True)
|
||||
|
||||
response = requests.get(f'https://you.com/api/streamingSearch?{params}',
|
||||
headers=headers, content_callback=output, impersonate='safari15_5')
|
|
@ -1,7 +1,9 @@
|
|||
import datetime
|
||||
import sys
|
||||
|
||||
from .typing import MetaModels, Union
|
||||
from . import Providers
|
||||
from . import Loggers
|
||||
|
||||
|
||||
class Models(metaclass=MetaModels):
|
||||
|
@ -29,15 +31,18 @@ class Utils:
|
|||
|
||||
class ChatCompletion:
|
||||
@staticmethod
|
||||
def create(model: Models.model or str, messages: list, provider: Providers.Provider = None, stream: bool = False, **kwargs):
|
||||
def create(model: Models.model or str, messages: list, provider: Providers.Provider = None, log: Loggers.Logger = Loggers.Arweave, stream: bool = False, **kwargs):
|
||||
try:
|
||||
if isinstance(model, str):
|
||||
model = Utils.convert[model]
|
||||
|
||||
engine = model.best_site if not provider else provider
|
||||
|
||||
return (engine._create_completion(messages, **kwargs)
|
||||
if stream else ''.join(engine._create_completion(messages, **kwargs)))
|
||||
log_completion = _LogCompletion(log, messages, metadata=dict(model=model.name, engine=engine.__name__, kwargs=kwargs))
|
||||
output = engine._create_completion(model.name, messages, **kwargs)
|
||||
|
||||
return (log_completion.stream(output)
|
||||
if stream else log_completion.complete(output))
|
||||
|
||||
except TypeError as e:
|
||||
print(e)
|
||||
|
@ -45,3 +50,26 @@ class ChatCompletion:
|
|||
print(
|
||||
f"ValueError: {engine.__name__} does not support '{arg}' argument", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
class _LogCompletion:
|
||||
def __init__(self, logger, input, metadata):
|
||||
self.logger = logger
|
||||
self.input = input
|
||||
self.metadata = metadata
|
||||
self.metadata['timestamp'] = datetime.datetime.now().isoformat()
|
||||
def stream(self, output):
|
||||
with self:
|
||||
for token in output:
|
||||
self.output += token
|
||||
yield token
|
||||
def complete(self, output):
|
||||
with self:
|
||||
self.output = ''.join(output)
|
||||
return self.output
|
||||
def __enter__(self):
|
||||
self.output = ''
|
||||
return self
|
||||
def __exit__(self, *exc):
|
||||
if self.logger is not None:
|
||||
locator = self.logger._log_completion(self.input, self.output, self.metadata)
|
||||
print(f"Logged {locator}", file=sys.stderr)
|
||||
|
|
|
@ -2,6 +2,7 @@ from typing import Dict, NewType, Union, Optional, List, get_type_hints
|
|||
|
||||
sha256 = NewType('sha_256_hash', str)
|
||||
|
||||
|
||||
class MetaModels(type):
|
||||
def __str__(cls):
|
||||
output: List = [
|
||||
|
@ -12,5 +13,4 @@ class MetaModels(type):
|
|||
' ...'
|
||||
]
|
||||
|
||||
return '\n'.join(output)
|
||||
|
||||
return '\n'.join(output)
|
|
@ -4,100 +4,87 @@ import json
|
|||
import random
|
||||
|
||||
from g4f import Models, ChatCompletion, Providers
|
||||
from flask import Flask, request
|
||||
from flask import Flask, request, Response
|
||||
from flask_cors import CORS
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
|
||||
CORS(app)
|
||||
|
||||
class website:
|
||||
def __init__(self) -> None:
|
||||
self.routes = {
|
||||
'/chat/completions': {
|
||||
'function': self.chat_completions,
|
||||
'methods': ['POST', 'GET']
|
||||
}
|
||||
@app.route("/chat/completions", methods=['POST'])
|
||||
def chat_completions():
|
||||
streaming = request.json.get('stream', False)
|
||||
model = request.json.get('model', 'gpt-3.5-turbo')
|
||||
messages = request.json.get('messages')
|
||||
|
||||
models = {
|
||||
'gpt-3.5-turbo': 'gpt-3.5-turbo-0301'
|
||||
}
|
||||
|
||||
response = ChatCompletion.create(model=Models.gpt_35_turbo, provider=Providers.Openai, stream=streaming,
|
||||
messages=messages)
|
||||
|
||||
if not streaming:
|
||||
while 'curl_cffi.requests.errors.RequestsError' in response:
|
||||
response = ChatCompletion.create(model=Models.gpt_35_turbo, provider=Providers.Openai, stream=streaming,
|
||||
messages=messages)
|
||||
|
||||
completion_timestamp = int(time.time())
|
||||
completion_id = ''.join(random.choices(
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28))
|
||||
|
||||
return {
|
||||
'id': 'chatcmpl-%s' % completion_id,
|
||||
'object': 'chat.completion',
|
||||
'created': completion_timestamp,
|
||||
'model': models[model],
|
||||
'usage': {
|
||||
'prompt_tokens': None,
|
||||
'completion_tokens': None,
|
||||
'total_tokens': None
|
||||
},
|
||||
'choices': [{
|
||||
'message': {
|
||||
'role': 'assistant',
|
||||
'content': response
|
||||
},
|
||||
'finish_reason': 'stop',
|
||||
'index': 0
|
||||
}]
|
||||
}
|
||||
|
||||
self.config = {
|
||||
'host': '0.0.0.0',
|
||||
'port': 1337,
|
||||
'debug': True
|
||||
}
|
||||
|
||||
def chat_completions(self):
|
||||
streaming = request.json.get('stream', False)
|
||||
model = request.json.get('model', 'gpt-3.5-turbo')
|
||||
messages = request.json.get('messages')
|
||||
|
||||
models = {
|
||||
'gpt-3.5-turbo': 'gpt-3.5-turbo-0301'
|
||||
}
|
||||
|
||||
response = ChatCompletion.create(model=Models.gpt_35_turbo, stream=streaming,
|
||||
messages=messages)
|
||||
|
||||
if not streaming:
|
||||
|
||||
def stream():
|
||||
for token in response:
|
||||
completion_timestamp = int(time.time())
|
||||
completion_id = ''.join(random.choices(
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28))
|
||||
|
||||
return {
|
||||
'id': 'chatcmpl-%s' % completion_id,
|
||||
'object': 'chat.completion',
|
||||
completion_data = {
|
||||
'id': f'chatcmpl-{completion_id}',
|
||||
'object': 'chat.completion.chunk',
|
||||
'created': completion_timestamp,
|
||||
'model': models[model],
|
||||
'usage': {
|
||||
'prompt_tokens': None,
|
||||
'completion_tokens': None,
|
||||
'total_tokens': None
|
||||
},
|
||||
'choices': [{
|
||||
'message': {
|
||||
'role': 'assistant',
|
||||
'content': response
|
||||
},
|
||||
'finish_reason': 'stop',
|
||||
'index': 0
|
||||
}]
|
||||
'model': 'gpt-3.5-turbo-0301',
|
||||
'choices': [
|
||||
{
|
||||
'delta': {
|
||||
'content': token
|
||||
},
|
||||
'index': 0,
|
||||
'finish_reason': None
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def stream():
|
||||
for token in response:
|
||||
completion_timestamp = int(time.time())
|
||||
completion_id = ''.join(random.choices(
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28))
|
||||
yield 'data: %s\n\n' % json.dumps(completion_data, separators=(',' ':'))
|
||||
time.sleep(0.1)
|
||||
|
||||
completion_data = {
|
||||
'id': f'chatcmpl-{completion_id}',
|
||||
'object': 'chat.completion.chunk',
|
||||
'created': completion_timestamp,
|
||||
'model': 'gpt-3.5-turbo-0301',
|
||||
'choices': [
|
||||
{
|
||||
'delta': {
|
||||
'content': token
|
||||
},
|
||||
'index': 0,
|
||||
'finish_reason': None
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
yield 'data: %s\n\n' % json.dumps(completion_data, separators=(',' ':'))
|
||||
time.sleep(0.1)
|
||||
|
||||
return app.response_class(stream(), mimetype='text/event-stream')
|
||||
return app.response_class(stream(), mimetype='text/event-stream')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
website = website()
|
||||
config = {
|
||||
'host': '0.0.0.0',
|
||||
'port': 1337,
|
||||
'debug': True
|
||||
}
|
||||
|
||||
for route in website.routes:
|
||||
app.add_url_rule(
|
||||
route,
|
||||
view_func=website.routes[route]['function'],
|
||||
methods=website.routes[route]['methods']
|
||||
)
|
||||
|
||||
app.run(**website.config)
|
||||
app.run(**config)
|
||||
|
|
読み込み中…
新しいイシューから参照