A huggingchat python api.
Project description
hugging-chat-api
English | 简体中文
Unofficial HuggingChat Python API, extensible for chatbots etc.
Note
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
Recently new updates:
- Assistant(Image Generator, etc)
- Web search
- Memorize context
- Supports for changing LLMs
Installation
pip3 install hugchat
Usage
API
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 :)
from hugchat import hugchat
from hugchat.login import Login
# Log in to huggingface and grant authorization to huggingchat
EMAIL = "your email"
PASSWD = "your password"
cookie_path_dir = "./cookies/" # NOTE: trailing slash (/) is required to avoid errors
sign = Login(EMAIL, PASSWD)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
# Create your ChatBot
chatbot = hugchat.ChatBot(cookies=cookies.get_dict()) # or cookie_path="usercookies/<email>.json"
message_result = chatbot.chat("Hi!") # note: message_result is a generator, the method will return immediately.
# Non stream
message_str: str = message_result.wait_until_done() # you can also print(message_result) directly.
# get files(such as images)
file_list = message_result.get_files_created() # must call wait_until_done() first!
# tips: model "CohereForAI/c4ai-command-r-plus" can generate images :)
# Stream response
for resp in chatbot.chat(
"Hello",
stream=True
):
print(resp)
# Web search
query_result = chatbot.chat("Hi!", web_search=True)
print(query_result)
for source in query_result.web_search_sources:
print(source.link)
print(source.title)
# Create a new conversation
chatbot.new_conversation(switch_to = True) # switch to the new conversation
# Get conversations on the server that are not from the current session (all your conversations in huggingchat)
conversation_list = chatbot.get_remote_conversations(replace_conversation_list=True)
# Get conversation list(local)
conversation_list = chatbot.get_conversation_list()
# Get the available models (not hardcore)
models = chatbot.get_available_llm_models()
# Switch model with given index
chatbot.switch_llm(0) # Switch to the first model
chatbot.switch_llm(1) # Switch to the second model
# Get information about the current conversation
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
# [DANGER] Delete all the conversations for the logged in user
chatbot.delete_all_conversations()
CLI
Simply run the following command in your terminal to start the CLI mode
python -m hugchat.cli
CLI params:
-u <your huggingface email>
: Provide account email to login.-p
: Force request password to login, ignores saved cookies.-s
: Enable streaming mode output in CLI.-c
: Continue previous conversation in CLI ".
Commands in cli mode:
-
/new
: Create and switch to a new conversation. -
/ids
: Shows a list of all ID numbers and ID strings in current session. -
/switch
: Shows a list of all conversations' info in current session. Then you can choose one to switch to. -
/switch all
: Shows a list of all conversations' info in your account. Then you can choose one to switch to. (not recommended if your account has a lot of conversations) -
/del <index>
: Deletes the conversation linked with the index passed. Will not delete active session. -
/delete-all
: Deletes all the conversations for the logged in user. -
/clear
: Clear the terminal. -
/llm
: Get available models you can switch to. -
/llm <index>
: Switches model to given model index based on/llm
. -
/share
: Toggles settings for sharing data with model author. On by default. -
/exit
: Closes CLI environment. -
/stream
: Toggles streaming the response. -
/web
: Toggles web search. -
/web-hint
: Toggles display web search hint. -
AI is an area of active research with known problems such as biased generation and misinformation. Do not use this application for high-stakes decisions or advice.
-
Server resources are precious, it is not recommended to request this API in a high frequency. (
Hugging Face's CTO🤗
just liked the suggestion)
Disclaimers
This is not an official Hugging Face product. This is a personal project and is not affiliated with Hugging Face in any way. Don't sue us.
Star History
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hugchat-0.4.15.tar.gz
.
File metadata
- Download URL: hugchat-0.4.15.tar.gz
- Upload date:
- Size: 34.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06eafc4bf356633a28bf817f1071d77613d980b298d7e752d4808b69ab231e85 |
|
MD5 | 11998abc6dcd0d64144289ea6dcea3d1 |
|
BLAKE2b-256 | 7bb957c653aab509e40acdbeada1abe2aefb294254611b4e784c05ffe84c8428 |
File details
Details for the file hugchat-0.4.15-py3-none-any.whl
.
File metadata
- Download URL: hugchat-0.4.15-py3-none-any.whl
- Upload date:
- Size: 34.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 297c11554304debe524f870a002c68475f2278a7f47a29999724d4cef5f9c929 |
|
MD5 | ad6d2f616ae212b45ba3f7c814149702 |
|
BLAKE2b-256 | 2017251687173143d9b3a0d27e5420b7fa31bf2cffcb9372763bc5decc18d8a1 |