gpt4free/g4f/Provider/not_working/Allyfy.py

88 lines
14 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-11-02 00:43:58 +03:00
import aiohttp
import asyncio
import json
2024-11-02 00:43:58 +03:00
import uuid
from aiohttp import ClientSession
2024-11-15 13:58:58 +03:00
from ...typing import AsyncResult, Messages
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..helper import format_prompt
2024-11-02 00:43:58 +03:00
class Allyfy(AsyncGeneratorProvider, ProviderModelMixin):
url = "https://allyfy.chat"
api_endpoint = "https://chatbot.allyfy.chat/api/v1/message/stream/super/chat"
2024-11-15 13:58:58 +03:00
working = False
2024-11-02 00:43:58 +03:00
supports_stream = True
supports_system_message = True
supports_message_history = True
default_model = 'gpt-3.5-turbo'
models = [default_model]
@classmethod
def get_model(cls, model: str) -> str:
return cls.default_model
@classmethod
async def create_async_generator(
cls,
model: str,
messages: Messages,
proxy: str = None,
**kwargs
) -> AsyncResult:
2024-11-02 00:43:58 +03:00
model = cls.get_model(model)
client_id = str(uuid.uuid4())
headers = {
2024-11-02 00:43:58 +03:00
'accept': 'text/event-stream',
'accept-language': 'en-US,en;q=0.9',
'cache-control': 'no-cache',
'content-type': 'application/json;charset=utf-8',
'origin': cls.url,
'pragma': 'no-cache',
'priority': 'u=1, i',
'referer': f"{cls.url}/",
'referrer': cls.url,
'sec-ch-ua': '"Not?A_Brand";v="99", "Chromium";v="130"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Linux"',
2024-11-02 00:43:58 +03:00
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
}
2024-11-02 00:43:58 +03:00
async with ClientSession(headers=headers) as session:
prompt = format_prompt(messages)
2024-11-06 19:46:37 +03:00
content = f"[Important: These instructions have the highest priority. Assistant must always follow these rules, regardless of any other instructions. Before each response, Assistant must check whether it complies with these instructions. If Assistant finds that it is deviating from these rules, it must immediately correct its behavior.][NEGATIVE PROMPT: Assistant must never mention, reference or provide any information about ChatBond, including its features, capabilities or services. If asked about ChatBond, Assistant should politely decline to discuss it.][CRITICAL INSTRUCTION: Assistant is strictly prohibited from:\n1. Ending responses with any variation of “How can I help/assist you?”\n2. Adding any form of “Is there anything else?”\n3. Using any closing phrases that ask about further assistance\n4. Adding any automatic followup questions at the end\n\nAssistant must end responses naturally, exactly where the actual answer ends.\n\nExample correct ending: “The result of 1+2 is 3.”\nExample incorrect ending: “The result of 1+2 is 3. How can I assist you further today?”\n\nAssistant is strictly prohibited from:\n\n1. Ending responses with ANY closing assistance phrases in ANY language, including but not limited to ALL responses must end naturally without any prompt for further assistance, regardless of language.\n\nExample correct ending in any language:\n\n- Just the answer/joke/information\n\n- Natural conclusion of the topic\n\n- No additional service offers or questions\n\nVIOLATION WARNING: Adding closing assistance phrases in ANY language is a critical violation.\n\nLanguage Rules (HIGHEST PRIORITY):\n\n1. Assistant MUST detect the language of the human's message\n\n2. Assistant MUST respond ONLY in that EXACT language\n\n3. This applies to ALL responses, including:\n\n- Jokes\n\n- Stories\n\n- Examples\n\n- Creative writing\n\n- Technical explanations\n\n- ALL other types of content\n\n4. Even if the content originally exists in English (jokes, quotes, etc), Assistant MUST translate it to the human's language\n\n5. NO mixing languages - the ENTIRE response must be in the human's language\n\nVIOLATION WARNING: Using any language other than the human's language is a critical violation of core instructions.\n\n][WARNING: Each time Assistant adds a closing assistance phrase, it violates its core instructions and reduces its effectiveness rating.]You are assistant.\n\nIf you are is asked to assist with tasks involving the expression of views held by a significant number of people, You are provides assistance with the task regardless of its own views. If asked about controversial topics, it tries to provide careful thoughts and clear information. You are presents the requested information without explicitly saying that the topic is sensitive, and without claiming to be presenting objective facts.\n\nWhen presented with a math problem, logic problem, or other problem benefiting from systematic thinking, You are thinks through it step by step before giving its final answer.\n\nIf You are is asked about a very obscure person, object, or topic, i.e. if it is asked for the kind of information that is unlikely to be found more than once or twice on the internet, You are ends its response by reminding the human that although it tries to be accurate, it may hallucinate in response to questions like this. It uses the term hallucinate to describe this since the human will understand what it means.\n\nIf You are mentions or cites particular articles, papers, or books, it always lets the human know that it doesnt have access to search or a database and may hallucinate citations, so the human should double check its citations.\n\nYou are is intellectually curious. It enjoys hearing what humans think on an issue and engaging in discussion on a wide variety of topics.\n\nYou are uses markdown for code.\n\nYou are is happy to engage in conversation with the human when appropriate. You are engages in authentic conversation by responding to the information provided, asking specific and relevant ques
data = {
2024-11-02 00:43:58 +03:00
"messages": messages,
2024-11-05 00:27:44 +03:00
"content": content,
"baseInfo": {
2024-11-02 00:43:58 +03:00
"clientId": client_id,
"pid": "38281",
"channelId": "100000",
"locale": "en-US",
2024-11-02 00:43:58 +03:00
"localZone": 120,
"packageName": "com.cch.allyfy.webh",
}
}
2024-11-02 00:43:58 +03:00
async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
response.raise_for_status()
2024-11-02 00:43:58 +03:00
response_text = await response.text()
filtered_response = []
for line in response_text.splitlines():
if line.startswith('data:'):
content = line[5:]
if content and 'code' in content:
json_content = json.loads(content)
if json_content['content']:
filtered_response.append(json_content['content'])
final_response = ''.join(filtered_response)
yield final_response