Skip to content

Commit

Permalink
fix: json decode errr #267
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulter committed Nov 16, 2024
1 parent 4c68af7 commit a1e8007
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 14 additions & 22 deletions src/hugchat/hugchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

0 comments on commit a1e8007

Please sign in to comment.