diff --git a/free_one_api/impls/adapter/hugchat.py b/free_one_api/impls/adapter/hugchat.py index 589166f..71c2c38 100644 --- a/free_one_api/impls/adapter/hugchat.py +++ b/free_one_api/impls/adapter/hugchat.py @@ -17,11 +17,11 @@ @adapter.llm_adapter class HuggingChatAdapter(llm.LLMLibAdapter): - + @classmethod def name(cls) -> str: return "Soulter/hugging-chat-api" - + @classmethod def description(self) -> str: return "Use Soulter/hugging-chat-api to access reverse engineering huggingchat." @@ -36,30 +36,30 @@ def function_call_supported(self) -> bool: return False def stream_mode_supported(self) -> bool: - return True + return True def multi_round_supported(self) -> bool: return True - + @classmethod def config_comment(cls) -> str: return \ -"""Please provide email and passwd to sign up for HuggingChat: - -{ - "email": "your email", - "passwd": "your password" -} + """Please provide email and passwd to sign up for HuggingChat: + + { + "email": "your email", + "passwd": "your password" + } + + Please refer to https://github.com/Soulter/hugging-chat-api + """ -Please refer to https://github.com/Soulter/hugging-chat-api -""" - @classmethod def supported_path(self) -> str: return "/v1/chat/completions" - + _chatbot: hugchat.ChatBot = None - + @property def chatbot(self) -> hugchat.ChatBot: if self._chatbot is None: @@ -70,51 +70,46 @@ def chatbot(self) -> hugchat.ChatBot: except: cookie = sign.login() sign.saveCookiesToDir("data/hugchatCookies") - + self._chatbot = hugchat.ChatBot(cookies=cookie.get_dict()) return self._chatbot - + def __init__(self, config: dict, eval: evaluation.AbsChannelEvaluation): self.config = config self.eval = eval - + async def test(self) -> typing.Union[bool, str]: try: self.chatbot.change_conversation(self.chatbot.new_conversation()) - for data in self.chatbot.query( - "Hi, respond 'Hello, world!' please.", - stream=True - ): + for data in self.chatbot.chat("Hi, respond 'Hello, world!' please."): pass - + self.chatbot.delete_conversation(self.chatbot.current_conversation) - + return True, "" except Exception as e: traceback.print_exc() return False, str(e) - - async def query(self, req: request.Request) -> typing.AsyncGenerator[response.Response, None]: + + async def query(self, req: request.Request) -> typing.AsyncGenerator[response.Response, None]: prompt = "" - + for msg in req.messages: prompt += f"{msg['role']}: {msg['content']}\n" - + prompt += "assistant: " - + random_int = random.randint(0, 1000000000) self.chatbot.change_conversation(self.chatbot.new_conversation()) - - for resp in self.chatbot.query( - text=prompt, - stream=True - ): - yield response.Response( - id=random_int, - finish_reason=response.FinishReason.NULL, - normal_message=resp['token'], - function_call=None - ) + + for resp in self.chatbot.chat(prompt): + if resp is not None: + yield response.Response( + id=random_int, + finish_reason=response.FinishReason.NULL, + normal_message=resp['token'], + function_call=None + ) self.chatbot.delete_conversation(self.chatbot.current_conversation) yield response.Response( diff --git a/requirements.txt b/requirements.txt index 85d0b2b..2ebd5ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,5 @@ hugchat g4f revTongYi colorlog +re_gpt git+https://github.com/Zai-Kun/reverse-engineered-chatgpt \ No newline at end of file