Skip to content

Commit a42a061

Browse files
committed
fix: json decode error when fetching remote conversations
1 parent 8a4beb8 commit a42a061

2 files changed

Lines changed: 47 additions & 27 deletions

File tree

‎README.md‎

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
# hugging-chat-api
22

33
English | [简体中文](README_cn.md)
4+
5+
Unofficial HuggingChat Python API, extensible for chatbots etc. It supports:
46

5-
Unofficial HuggingChat Python API, extensible for chatbots etc.
7+
- Basic Chat
8+
- Assistant(Image Generator, [etc.](https://huggingface.co/chat/assistants))
9+
- Web search
10+
- Memorize context
11+
- [Switch LLMs](https://huggingface.co/chat/models)
12+
- ...
613

714
[![PyPi](https://img.shields.io/pypi/v/hugchat.svg?logo=pypi&logoColor=white)](https://pypi.python.org/pypi/hugchat)
815
[![Support_Platform](https://img.shields.io/badge/3.6+-%234ea94b.svg?logo=python&logoColor=white)](https://pypi.python.org/pypi/hugchat)
@@ -11,14 +18,7 @@ Unofficial HuggingChat Python API, extensible for chatbots etc.
1118

1219
> **Note**
1320
>
14-
> For the personal reasons, the update of this repo will become slow, and we will ensure that the most basic features can be used normally
15-
>
16-
>
17-
> Recently new updates:
18-
> - Assistant(Image Generator, etc)
19-
> - Web search
20-
> - Memorize context
21-
> - Supports for changing LLMs
21+
> For the personal reasons, the update of this repo will become slow, and we will ensure that the most basic features can be used normally. Welcome Any PR!
2222
2323
## Installation
2424
```bash
@@ -29,8 +29,29 @@ pip3 install hugchat
2929

3030
### API
3131

32+
#### Minimal Example
33+
34+
```py
35+
from hugchat import hugchat
36+
from hugchat.login import Login
37+
38+
# Log in to huggingface and grant authorization to huggingchat
39+
# DO NOT EXPOSE YOUR EMAIL AND PASSWORD IN CODES, USE ENVIRONMENT VARIABLES OR CONFIG FILES
40+
EMAIL = "your email"
41+
PASSWD = "your password"
42+
cookie_path_dir = "./cookies/" # NOTE: trailing slash (/) is required to avoid errors
43+
sign = Login(EMAIL, PASSWD)
44+
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
45+
46+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
47+
48+
print(chatbot.chat("Hi!").wait_until_done())
49+
```
50+
51+
3252
The following are all common usages of this repo, You may not necessarily use all of them, You can add or delete some as needed :)
3353

54+
3455
```py
3556
from hugchat import hugchat
3657
from hugchat.login import Login
@@ -47,26 +68,24 @@ chatbot = hugchat.ChatBot(cookies=cookies.get_dict()) # or cookie_path="usercoo
4768

4869
message_result = chatbot.chat("Hi!") # note: message_result is a generator, the method will return immediately.
4970

50-
# Non stream
51-
message_str: str = message_result.wait_until_done() # you can also print(message_result) directly.
71+
# Option 1: Non stream
72+
message_str: str = message_result.wait_until_done()
5273
# get files(such as images)
5374
file_list = message_result.get_files_created() # must call wait_until_done() first!
5475

55-
# tips: model "CohereForAI/c4ai-command-r-plus" can generate images :)
56-
57-
# Stream response
76+
# Option 2: Stream response
5877
for resp in chatbot.chat(
5978
"Hello",
6079
stream=True
6180
):
6281
print(resp)
6382

6483
# Web search
65-
query_result = chatbot.chat("Hi!", web_search=True)
84+
query_result = chatbot.chat("How many models stored in huggingface?", web_search=True).wait_until_done()
6685
print(query_result)
67-
for source in query_result.web_search_sources:
68-
print(source.link)
69-
print(source.title)
86+
# You can get the web search results from the query result
87+
for source in query_result.get_search_sources():
88+
print(source.link, source.title)
7089

7190
# Create a new conversation
7291
chatbot.new_conversation(switch_to = True) # switch to the new conversation
@@ -76,9 +95,8 @@ conversation_list = chatbot.get_remote_conversations(replace_conversation_list=T
7695
# Get conversation list(local)
7796
conversation_list = chatbot.get_conversation_list()
7897

79-
# Get the available models (not hardcore)
98+
# Get the available models
8099
models = chatbot.get_available_llm_models()
81-
82100
# Switch model with given index
83101
chatbot.switch_llm(0) # Switch to the first model
84102
chatbot.switch_llm(1) # Switch to the second model

‎src/hugchat/hugchat.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,18 +490,20 @@ def get_remote_conversations(self, replace_conversation_list=True):
490490
)
491491

492492
# temporary workaround for #267
493-
line_ = r.text.splitlines()[1]
494-
data = json.loads(line_)
493+
line_ = r.text.splitlines()[0]
494+
data = json.loads(line_)["nodes"][0]["data"]
495+
496+
conversationIndices = data[data[0]["conversations"]]
495497

496-
conversationIndices = data['data'][0]
497498
conversations = []
498499

499500
for index in conversationIndices:
500-
conversation_data = data['data'][index]
501+
conversation_data = data[index]
502+
501503
c = Conversation(
502-
id=data['data'][conversation_data["id"]],
503-
title=data['data'][conversation_data["title"]],
504-
model=data['data'][conversation_data["model"]],
504+
id=data[conversation_data["id"]],
505+
title=data[conversation_data["title"]],
506+
model=data[conversation_data["model"]],
505507
)
506508

507509
conversations.append(c)

0 commit comments

Comments
 (0)