Unofficial HuggingChat Python API, extensible for chatbots etc. PyPi SupportPlatform…
- User
Unofficial HuggingChat Python API, extensible for chatbots etc.
PyPi Support_Platform DownloadsPW Downloads
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/.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 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
Hello! It looks like you've shared the documentation for the Unofficial HuggingChat Python API. I'd be happy to help you with it. Here are a few ways I can assist:
-
Understanding the Documentation: If you have any questions about specific sections or need clarification on how certain features work, let me know!
-
Installation and Setup: Need guidance on installing the package or setting up your environment?
-
Usage Examples: I can provide additional code examples or walk you through building a chatbot using the API.
-
Troubleshooting: Encountering any issues or errors while using the API? Share the details, and I'll help you resolve them.
-
Feature Enhancements: Looking to extend the functionality or integrate it with other tools? I can offer suggestions or code snippets.
-
Best Practices: Advice on optimizing your chatbot, managing conversations, or handling web searches effectively.
Please let me know how you'd like to proceed or if there's something specific you're aiming to achieve!
-