123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- from __future__ import annotations
- import json, base64, requests, execjs, random, uuid
- from ..typing import Messages, TypedDict, CreateResult, Any
- from .base_provider import BaseProvider
- from ..debug import logging
- class Vercel(BaseProvider):
- url = 'https://sdk.vercel.ai'
- working = False
- supports_message_history = True
- supports_gpt_35_turbo = True
- supports_stream = True
- @staticmethod
- def create_completion(
- model: str,
- messages: Messages,
- stream: bool,
- proxy: str = None,
- **kwargs
- ) -> CreateResult:
-
- if not model:
- model = "gpt-3.5-turbo"
- elif model not in model_info:
- raise ValueError(f"Vercel does not support {model}")
- headers = {
- 'authority': 'sdk.vercel.ai',
- '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',
- 'content-type': 'application/json',
- 'custom-encoding': get_anti_bot_token(),
- 'origin': 'https://sdk.vercel.ai',
- 'pragma': 'no-cache',
- 'referer': 'https://sdk.vercel.ai/',
- 'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
- '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': f'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.{random.randint(99, 999)}.{random.randint(99, 999)} Safari/537.36',
- }
- json_data = {
- 'model' : model_info[model]['id'],
- 'messages' : messages,
- 'playgroundId': str(uuid.uuid4()),
- 'chatIndex' : 0,
- **model_info[model]['default_params'],
- **kwargs
- }
- max_retries = kwargs.get('max_retries', 20)
- for _ in range(max_retries):
- response = requests.post('https://sdk.vercel.ai/api/generate',
- headers=headers, json=json_data, stream=True, proxies={"https": proxy})
- try:
- response.raise_for_status()
- except:
- continue
- for token in response.iter_content(chunk_size=None):
- yield token.decode()
- break
- def get_anti_bot_token() -> str:
- headers = {
- 'authority': 'sdk.vercel.ai',
- '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://sdk.vercel.ai/',
- 'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
- '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': f'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.{random.randint(99, 999)}.{random.randint(99, 999)} Safari/537.36',
- }
- response = requests.get('https://sdk.vercel.ai/openai.jpeg',
- headers=headers).text
- raw_data = json.loads(base64.b64decode(response,
- validate=True))
- js_script = '''const globalThis={marker:"mark"};String.prototype.fontcolor=function(){return `<font>${this}</font>`};
- return (%s)(%s)''' % (raw_data['c'], raw_data['a'])
- raw_token = json.dumps({'r': execjs.compile(js_script).call(''), 't': raw_data['t']},
- separators = (",", ":"))
- return base64.b64encode(raw_token.encode('utf-16le')).decode()
- class ModelInfo(TypedDict):
- id: str
- default_params: dict[str, Any]
- model_info: dict[str, ModelInfo] = {
- # 'claude-instant-v1': {
- # 'id': 'anthropic:claude-instant-v1',
- # 'default_params': {
- # 'temperature': 1,
- # 'maximumLength': 1024,
- # 'topP': 1,
- # 'topK': 1,
- # 'presencePenalty': 1,
- # 'frequencyPenalty': 1,
- # 'stopSequences': ['\n\nHuman:'],
- # },
- # },
- # 'claude-v1': {
- # 'id': 'anthropic:claude-v1',
- # 'default_params': {
- # 'temperature': 1,
- # 'maximumLength': 1024,
- # 'topP': 1,
- # 'topK': 1,
- # 'presencePenalty': 1,
- # 'frequencyPenalty': 1,
- # 'stopSequences': ['\n\nHuman:'],
- # },
- # },
- # 'claude-v2': {
- # 'id': 'anthropic:claude-v2',
- # 'default_params': {
- # 'temperature': 1,
- # 'maximumLength': 1024,
- # 'topP': 1,
- # 'topK': 1,
- # 'presencePenalty': 1,
- # 'frequencyPenalty': 1,
- # 'stopSequences': ['\n\nHuman:'],
- # },
- # },
- 'replicate/llama70b-v2-chat': {
- 'id': 'replicate:replicate/llama-2-70b-chat',
- 'default_params': {
- 'temperature': 0.75,
- 'maximumLength': 3000,
- 'topP': 1,
- 'repetitionPenalty': 1,
- },
- },
- 'a16z-infra/llama7b-v2-chat': {
- 'id': 'replicate:a16z-infra/llama7b-v2-chat',
- 'default_params': {
- 'temperature': 0.75,
- 'maximumLength': 3000,
- 'topP': 1,
- 'repetitionPenalty': 1,
- },
- },
- 'a16z-infra/llama13b-v2-chat': {
- 'id': 'replicate:a16z-infra/llama13b-v2-chat',
- 'default_params': {
- 'temperature': 0.75,
- 'maximumLength': 3000,
- 'topP': 1,
- 'repetitionPenalty': 1,
- },
- },
- 'replicate/llama-2-70b-chat': {
- 'id': 'replicate:replicate/llama-2-70b-chat',
- 'default_params': {
- 'temperature': 0.75,
- 'maximumLength': 3000,
- 'topP': 1,
- 'repetitionPenalty': 1,
- },
- },
- 'bigscience/bloom': {
- 'id': 'huggingface:bigscience/bloom',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 1024,
- 'topP': 0.95,
- 'topK': 4,
- 'repetitionPenalty': 1.03,
- },
- },
- 'google/flan-t5-xxl': {
- 'id': 'huggingface:google/flan-t5-xxl',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 1024,
- 'topP': 0.95,
- 'topK': 4,
- 'repetitionPenalty': 1.03,
- },
- },
- 'EleutherAI/gpt-neox-20b': {
- 'id': 'huggingface:EleutherAI/gpt-neox-20b',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 1024,
- 'topP': 0.95,
- 'topK': 4,
- 'repetitionPenalty': 1.03,
- 'stopSequences': [],
- },
- },
- 'OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5': {
- 'id': 'huggingface:OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5',
- 'default_params': {
- 'maximumLength': 1024,
- 'typicalP': 0.2,
- 'repetitionPenalty': 1,
- },
- },
- 'OpenAssistant/oasst-sft-1-pythia-12b': {
- 'id': 'huggingface:OpenAssistant/oasst-sft-1-pythia-12b',
- 'default_params': {
- 'maximumLength': 1024,
- 'typicalP': 0.2,
- 'repetitionPenalty': 1,
- },
- },
- 'bigcode/santacoder': {
- 'id': 'huggingface:bigcode/santacoder',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 1024,
- 'topP': 0.95,
- 'topK': 4,
- 'repetitionPenalty': 1.03,
- },
- },
- 'command-light-nightly': {
- 'id': 'cohere:command-light-nightly',
- 'default_params': {
- 'temperature': 0.9,
- 'maximumLength': 1024,
- 'topP': 1,
- 'topK': 0,
- 'presencePenalty': 0,
- 'frequencyPenalty': 0,
- 'stopSequences': [],
- },
- },
- 'command-nightly': {
- 'id': 'cohere:command-nightly',
- 'default_params': {
- 'temperature': 0.9,
- 'maximumLength': 1024,
- 'topP': 1,
- 'topK': 0,
- 'presencePenalty': 0,
- 'frequencyPenalty': 0,
- 'stopSequences': [],
- },
- },
- # 'gpt-4': {
- # 'id': 'openai:gpt-4',
- # 'default_params': {
- # 'temperature': 0.7,
- # 'maximumLength': 8192,
- # 'topP': 1,
- # 'presencePenalty': 0,
- # 'frequencyPenalty': 0,
- # 'stopSequences': [],
- # },
- # },
- # 'gpt-4-0613': {
- # 'id': 'openai:gpt-4-0613',
- # 'default_params': {
- # 'temperature': 0.7,
- # 'maximumLength': 8192,
- # 'topP': 1,
- # 'presencePenalty': 0,
- # 'frequencyPenalty': 0,
- # 'stopSequences': [],
- # },
- # },
- 'code-davinci-002': {
- 'id': 'openai:code-davinci-002',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 1024,
- 'topP': 1,
- 'presencePenalty': 0,
- 'frequencyPenalty': 0,
- 'stopSequences': [],
- },
- },
- 'gpt-3.5-turbo': {
- 'id': 'openai:gpt-3.5-turbo',
- 'default_params': {
- 'temperature': 0.7,
- 'maximumLength': 4096,
- 'topP': 1,
- 'topK': 1,
- 'presencePenalty': 1,
- 'frequencyPenalty': 1,
- 'stopSequences': [],
- },
- },
- 'gpt-3.5-turbo-16k': {
- 'id': 'openai:gpt-3.5-turbo-16k',
- 'default_params': {
- 'temperature': 0.7,
- 'maximumLength': 16280,
- 'topP': 1,
- 'topK': 1,
- 'presencePenalty': 1,
- 'frequencyPenalty': 1,
- 'stopSequences': [],
- },
- },
- 'gpt-3.5-turbo-16k-0613': {
- 'id': 'openai:gpt-3.5-turbo-16k-0613',
- 'default_params': {
- 'temperature': 0.7,
- 'maximumLength': 16280,
- 'topP': 1,
- 'topK': 1,
- 'presencePenalty': 1,
- 'frequencyPenalty': 1,
- 'stopSequences': [],
- },
- },
- 'text-ada-001': {
- 'id': 'openai:text-ada-001',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 1024,
- 'topP': 1,
- 'presencePenalty': 0,
- 'frequencyPenalty': 0,
- 'stopSequences': [],
- },
- },
- 'text-babbage-001': {
- 'id': 'openai:text-babbage-001',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 1024,
- 'topP': 1,
- 'presencePenalty': 0,
- 'frequencyPenalty': 0,
- 'stopSequences': [],
- },
- },
- 'text-curie-001': {
- 'id': 'openai:text-curie-001',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 1024,
- 'topP': 1,
- 'presencePenalty': 0,
- 'frequencyPenalty': 0,
- 'stopSequences': [],
- },
- },
- 'text-davinci-002': {
- 'id': 'openai:text-davinci-002',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 1024,
- 'topP': 1,
- 'presencePenalty': 0,
- 'frequencyPenalty': 0,
- 'stopSequences': [],
- },
- },
- 'text-davinci-003': {
- 'id': 'openai:text-davinci-003',
- 'default_params': {
- 'temperature': 0.5,
- 'maximumLength': 4097,
- 'topP': 1,
- 'presencePenalty': 0,
- 'frequencyPenalty': 0,
- 'stopSequences': [],
- },
- },
- }
|