Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
OGIOS edited this page Oct 1, 2023 · 4 revisions

It should be noticed that Bot cannot be created with variable = Bot()
you have to get it through HuggingChat which can be imported:

from hugchat_api import HuggingChat
# or
from hugchat_api.core import HuggingChat

# and then
HUG = HuggingChat()
bot = HUG.getBot(<email>, <cookies>, <model?>)

Param model is a string represents which model to be used, and it defaults to ListBots.OPENASSISTENT_30B_XOR
You can find all available models under hugchat_api.core.ListBots.

Properties

  • self.conversations: Dict[str, str]
    store all conversations.
    structure: { id: title }
  • self.current_conversation: str this one is mainly for terminal_cli, normally it shouldn't be concerned.

Usage

Fetch Conversations

this method will be called with wait=True once the Bot is created

def fetchConversations(self, wait: bool = True) -> Future | None

Get conversation_id and title, save them inside self.conversations: Dict[str, str].
Method returns a Future (wait=False) or None since the conversations will be directly updated in Bot it self.

Chat

def chat(
    self,
    text: str,
    conversation_id=None,
    web_search=False,
    max_tries: int = 3,
    custom_data: RequestData | None = None,
) -> Message | None

customize parameters is no longer supprted by huggingface.co/chat, but the old request body can still get through it.

Create a chat workflow and submit it to the asyncio loop.
custom_data supports customized parameters such as max_new_tokens top_k etc...
No Future returns but Message which continuously updating web search process and stream message.

System Prompt

def setSystemPrompt(
    self, model: str, prompt: str, wait: bool = True
) -> Union[Future, None]:

def setSystemPrompts(
    self, prompts: dict[str, str], wait: bool = True
) -> Union[Future, None]:

Set system prompt for the given mdoel, it works globally.
For model: str, please see hugchat_api.core.ListBots

Update Title

def updateTitle(self, conversation_id: str, wait: bool = True) -> Future | str

Get conversation summary.
the conversation's title in self.conversations will be updated as well. Normally it will be called after the first chat of the new conversation.
Method returns a Future (wait=False) or the title string

Create Conversation

def createConversation(self, wait: bool = True) -> Future | str

Create a new conversation and save the id to self.conversations and change self.current_conversation to the new id.
Method returns a Future (wait=False) or id string

Remove Conversation

def removeConversation(
    self, conversation_id: str, wait: bool = True
) -> Future | None:

Removes a given conversation and delete it in self.conversations, if the given one is current conversation then set self.current_conversation to None.
Method returns a Future (wait=False) or None.

Conversation Histories

def getHistoriesByID(self, conversation_id=None, wait=True) -> Future | list

Fetch histories of the given conversation.
Method returns a Future (wait=False) or History list.
For the history data structure, please see hugchat_api.utils.formatHistory as an example.

Feel free to ask any questions or share me with your thoughts!

Clone this wiki locally