Skip to content

Commit 8a4beb8

Browse files
committed
fix: json decode errr #267
1 parent b9202bc commit 8a4beb8

3 files changed

Lines changed: 17 additions & 26 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ info = chatbot.get_conversation_info()
8888
print(info.id, info.title, info.model, info.system_prompt, info.history)
8989

9090
# Assistant
91-
assistant = chatbot.search_assistant(assistant_name="ChatGpt") # assistant name list in https://huggingface.co/chat/assistants
92-
assistant_list = chatbot.get_assistant_list_by_page(page=0)
93-
chatbot.new_conversation(assistant=assistant, switch_to=True) # create a new conversation with assistant
91+
ASSISTANT_ID = "66017fca58d60bd7d5c5c26c" # get the assistant id from https://huggingface.co/chat/assistants
92+
chatbot.new_conversation(assistant=ASSISTANT_ID, switch_to=True) # create a new conversation with assistant
9493

9594
# [DANGER] Delete all the conversations for the logged in user
9695
chatbot.delete_all_conversations()

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name="hugchat",
6-
version="0.4.15",
6+
version="0.4.16",
77
description="A huggingchat python api.",
88
long_description=open("README.md", "rt", encoding="utf-8").read(),
99
long_description_content_type="text/markdown",

‎src/hugchat/hugchat.py‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def new_conversation(
166166
Create a new conversation. Return a conversation object.
167167
168168
modelIndex: int, get it from get_available_llm_models(). If None, use the default model.
169-
assistant: str or Assistant, the assistant **id** or assistant object. Use search_assistant() to get the assistant object.
169+
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`.
170170
171171
- You should change the conversation by calling change_conversation() after calling this method. Or set param switch_to to True.
172172
- if you use assistant, the parameter `system_prompt` will be ignored.
@@ -488,17 +488,20 @@ def get_remote_conversations(self, replace_conversation_list=True):
488488
raise Exception(
489489
f"Failed to get remote conversations with status code: {r.status_code}"
490490
)
491+
492+
# temporary workaround for #267
493+
line_ = r.text.splitlines()[1]
494+
data = json.loads(line_)
491495

492-
data = r.json()["nodes"][0]["data"]
493-
conversationIndices = data[data[0]["conversations"]]
496+
conversationIndices = data['data'][0]
494497
conversations = []
495498

496499
for index in conversationIndices:
497-
conversation_data = data[index]
500+
conversation_data = data['data'][index]
498501
c = Conversation(
499-
id=data[conversation_data["id"]],
500-
title=data[conversation_data["title"]],
501-
model=data[conversation_data["model"]],
502+
id=data['data'][conversation_data["id"]],
503+
title=data['data'][conversation_data["title"]],
504+
model=data['data'][conversation_data["model"]],
502505
)
503506

504507
conversations.append(c)
@@ -611,20 +614,17 @@ def get_assistant_list_by_page(self, page: int) -> List[Assistant]:
611614
get assistant list by page number.
612615
if page < 0 or page > max_page then return `None`.
613616
'''
614-
url_cache = f"https://api.soulter.top/hugchat/assistants/__data.json?p={page}"
615617
url = f"https://huggingface.co/chat/assistants/__data.json?p={page}&x-sveltekit-invalidated=01"
616-
try:
617-
res = requests.get(url_cache, timeout=5)
618-
except BaseException:
619-
res = self.session.get(url, timeout=10)
618+
res = self.session.get(url, timeout=10)
620619
res = res.json()
621620
if res['nodes'][1]['type'] == 'error':
622621
return None
623622
# here we parse the result
624623
return self._parse_assistants(res['nodes'][1]['data'])
625-
624+
626625
def search_assistant(self, assistant_name: str = None, assistant_id: str = None) -> Assistant:
627626
'''
627+
[DEPRECATED]
628628
- 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.
629629
- Search an available assistant by assistant name or assistant id.
630630
- Will search on api.soulter.top/hugchat because offifial api doesn't support search.
@@ -817,12 +817,4 @@ def chat(
817817
web_search=web_search,
818818
conversation=conversation
819819
)
820-
return msg
821-
822-
823-
if __name__ == "__main__":
824-
bot = ChatBot()
825-
message_content = bot.chat("Hello")
826-
print(message_content)
827-
sharelink = bot.share_conversation()
828-
print(sharelink)
820+
return msg

0 commit comments

Comments
 (0)