From a1e8007ec693082c847790fe3a98ef8b4350495e Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 16 Nov 2024 17:30:17 +0800 Subject: [PATCH] fix: json decode errr #267 --- README.md | 5 ++--- setup.py | 2 +- src/hugchat/hugchat.py | 36 ++++++++++++++---------------------- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 9b55900..7908f05 100644 --- a/README.md +++ b/README.md @@ -88,9 +88,8 @@ info = chatbot.get_conversation_info() print(info.id, info.title, info.model, info.system_prompt, info.history) # Assistant -assistant = chatbot.search_assistant(assistant_name="ChatGpt") # assistant name list in https://huggingface.co/chat/assistants -assistant_list = chatbot.get_assistant_list_by_page(page=0) -chatbot.new_conversation(assistant=assistant, switch_to=True) # create a new conversation with assistant +ASSISTANT_ID = "66017fca58d60bd7d5c5c26c" # get the assistant id from https://huggingface.co/chat/assistants +chatbot.new_conversation(assistant=ASSISTANT_ID, switch_to=True) # create a new conversation with assistant # [DANGER] Delete all the conversations for the logged in user chatbot.delete_all_conversations() diff --git a/setup.py b/setup.py index 4e1e419..6ca3bda 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name="hugchat", - version="0.4.15", + version="0.4.16", description="A huggingchat python api.", long_description=open("README.md", "rt", encoding="utf-8").read(), long_description_content_type="text/markdown", diff --git a/src/hugchat/hugchat.py b/src/hugchat/hugchat.py index c033868..d179bb8 100644 --- a/src/hugchat/hugchat.py +++ b/src/hugchat/hugchat.py @@ -166,7 +166,7 @@ def new_conversation( Create a new conversation. Return a conversation object. modelIndex: int, get it from get_available_llm_models(). If None, use the default model. - assistant: str or Assistant, the assistant **id** or assistant object. Use search_assistant() to get the assistant object. + assistant: str or Assistant, the assistant **id**. Assistant id can be found in the assistant url, such as https://huggingface.co/chat/assistant/65bf2ddbf4017c8048ae43a3, the id is `65bf2ddbf4017c8048ae43a3`. - You should change the conversation by calling change_conversation() after calling this method. Or set param switch_to to True. - if you use assistant, the parameter `system_prompt` will be ignored. @@ -488,17 +488,20 @@ def get_remote_conversations(self, replace_conversation_list=True): raise Exception( f"Failed to get remote conversations with status code: {r.status_code}" ) + + # temporary workaround for #267 + line_ = r.text.splitlines()[1] + data = json.loads(line_) - data = r.json()["nodes"][0]["data"] - conversationIndices = data[data[0]["conversations"]] + conversationIndices = data['data'][0] conversations = [] for index in conversationIndices: - conversation_data = data[index] + conversation_data = data['data'][index] c = Conversation( - id=data[conversation_data["id"]], - title=data[conversation_data["title"]], - model=data[conversation_data["model"]], + id=data['data'][conversation_data["id"]], + title=data['data'][conversation_data["title"]], + model=data['data'][conversation_data["model"]], ) conversations.append(c) @@ -611,20 +614,17 @@ def get_assistant_list_by_page(self, page: int) -> List[Assistant]: get assistant list by page number. if page < 0 or page > max_page then return `None`. ''' - url_cache = f"https://api.soulter.top/hugchat/assistants/__data.json?p={page}" url = f"https://huggingface.co/chat/assistants/__data.json?p={page}&x-sveltekit-invalidated=01" - try: - res = requests.get(url_cache, timeout=5) - except BaseException: - res = self.session.get(url, timeout=10) + res = self.session.get(url, timeout=10) res = res.json() if res['nodes'][1]['type'] == 'error': return None # here we parse the result return self._parse_assistants(res['nodes'][1]['data']) - + def search_assistant(self, assistant_name: str = None, assistant_id: str = None) -> Assistant: ''' + [DEPRECATED] - If you created an assistant by your own, you should pass the assistant_id here but not the assistant_name. You can pass your assistant_id into the new_conversation() directly. - Search an available assistant by assistant name or assistant id. - Will search on api.soulter.top/hugchat because offifial api doesn't support search. @@ -817,12 +817,4 @@ def chat( web_search=web_search, conversation=conversation ) - return msg - - -if __name__ == "__main__": - bot = ChatBot() - message_content = bot.chat("Hello") - print(message_content) - sharelink = bot.share_conversation() - print(sharelink) + return msg \ No newline at end of file