Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty string result and other improvement #228

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/hugchat/hugchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,18 @@ def get_remote_conversations(self, replace_conversation_list=True):

return conversations

def get_conversation_info(self, conversation: Conversation = None) -> Conversation:
def get_conversation_info(self, conversation: Union[Conversation, str] = None) -> Conversation:
"""
Fetches information related to the specified conversation. Returns the conversation object.
conversation: Conversation object that has the conversation id Or None to use the current conversation.
"""

if conversation is None:
conversation = self.current_conversation

if isinstance(conversation, str):
conversation = Conversation(id=conversation)

r = self.session.get(
self.hf_base_url +
f"/chat/conversation/{conversation.id}/__data.json?x-sveltekit-invalidated=01",
Expand Down Expand Up @@ -608,6 +612,7 @@ def get_assistant_list_by_page(self, page: int) -> List[Assistant]:

def search_assistant(self, assistant_name: str = None, assistant_id: str = None) -> Assistant:
'''
- 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.
- Return the `Assistant` object if found, return None if not found.
Expand Down
17 changes: 12 additions & 5 deletions src/hugchat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ class Message(Generator):
g: Generator
_stream_yield_all: bool = False
web_search: bool = False

web_search_sources: list = []
# For backward compatibility, we have to reserve the `text` field.
text: str = ""
_result_text: str = ""
web_search_done: bool = not web_search
msg_status: int = MSGSTATUS_PENDING
error: Union[Exception, None] = None
Expand All @@ -76,6 +74,15 @@ def __init__(
self._stream_yield_all = _stream_yield_all
self.web_search = web_search

@property
def text(self) -> str:
self._result_text = self.wait_until_done()
return self._result_text

@text.setter
def text(self, v: str) -> None:
self._result_text = v

def _filterResponse(self, obj: dict):
if not obj.__contains__("type"):
if obj.__contains__("message"):
Expand All @@ -99,7 +106,7 @@ def __next__(self) -> dict:
t: str = a["type"]
message_type: str = ""
if t == RESPONSE_TYPE_FINAL:
self.text = a["text"]
self._result_text = a["text"]
self.msg_status = MSGSTATUS_RESOLVED
elif t == RESPONSE_TYPE_WEB:
# gracefully pass unparseable webpages
Expand Down Expand Up @@ -195,7 +202,7 @@ def wait_until_done(self) -> str:
while not self.is_done():
self.__next__()
if self.is_done() == MSGSTATUS_RESOLVED:
return self.text
return self._result_text
elif self.error is not None:
raise self.error
else:
Expand Down
Loading