Soulter / hugging-chat-api

HuggingChat Python API🤗
GNU Affero General Public License v3.0
861 stars 123 forks source link

how do i make it remember previous messages like chatgpt #167

Closed YoussefBechara closed 5 months ago

YoussefBechara commented 9 months ago

helpppppppppp

Whitelisted1 commented 9 months ago

I'm sorry, but you didn't provide any information about your issue, and seem to have selected an incorrect label. Can you please provide more information about the problems you are having?

Soulter commented 9 months ago

Hello, it defaults to remember your previous message. Are you encountered any problem?

YoussefBechara commented 8 months ago

thanks guys i fixed it by putting the same conv id each timr

rezkanas commented 8 months ago

@YoussefBechara where do you add that conv ID, and how do you get it in the first place?

I have this function and when ever i call it, it starts a new conversation. I am trying now to control it with chatbot.new_conversation(switch_to=True)... @Soulter any idea how to make it stick to one conversation thread?

from hugchat import hugchat
from hugchat.login import Login
import os
from dotenv import load_dotenv, find_dotenv

# read local .env file
_ = load_dotenv(find_dotenv())

def query_LLM(prompt, new_conversation: bool = False):
    # Log in to huggingface and grant authorization to huggingchat
    huggingchat_user_email = os.environ['huggingchat_user_email']
    huggingchat_user_pwd = os.environ['huggingchat_user_pwd']

    sign = Login(email=huggingchat_user_email, passwd=huggingchat_user_pwd)
    cookies = sign.login()

    # Save cookies to the local directory
    cookie_path_dir = "./cookies_snapshot"
    sign.saveCookiesToDir(cookie_path_dir)

    # Create a ChatBot
    chatbot = hugchat.ChatBot(cookies=cookies.get_dict())  # or cookie_path="usercookies/<email>.json"
    if new_conversation:
        chatbot.new_conversation(switch_to=True)
    # non stream response
    return chatbot.chat(prompt)['text']

also as where are at it, what is the utlity of

 # Save cookies to the local directory
    cookie_path_dir = "./cookies_snapshot"
    sign.saveCookiesToDir(cookie_path_dir)
YoussefBechara commented 7 months ago

@rezkanas use these two lines: id = chatbot.new_conversation() print(id) chatbot.change_conversation(id) Here we are making the new conv with id printed then we copy it and change ttje conv to it and yea

rezkanas commented 7 months ago

Thank you @YoussefBechara. I update my code and continuing in the same conversation is working now. but a new requirements pops up now. I need to change system prompt for each round of conversation. I have a go at it but I see that it does not work when running the code. @Soulter can I change system prompt during the conversation? or is there one system prompt per conversation?

    @staticmethod
    def query_LLM(system_prompt: str, prompt: str, new_conversation: bool = False):
        # Log in to huggingface and grant authorization to huggingchat
        huggingchat_user_email = os.environ['huggingchat_user_email']
        huggingchat_user_pwd = os.environ['huggingchat_user_pwd']

        sign = Login(email=huggingchat_user_email, passwd=huggingchat_user_pwd)
        cookies = sign.login()

        # Save cookies to the local directory
        cookie_path_dir = "./cookies_snapshot"
        sign.saveCookiesToDir(cookie_path_dir)

        # Create a ChatBot
        if new_conversation:
            global conversation_id, chatbot
            chatbot = hugchat.ChatBot(system_prompt=system_prompt,
                                      cookies=cookies.get_dict())  # or cookie_path="usercookies/<email>.json"

            chatbot.new_conversation(switch_to=True)
            conversation_id = chatbot.new_conversation()
        else:
            #for continuing conversation, change system prompt
            chatbot.system_prompt=system_prompt

        chatbot.change_conversation(conversation_id)

        return chatbot.chat(prompt)['text']
rezkanas commented 7 months ago

@Soulter, @Whitelisted1 any update about this?

YoussefBechara commented 7 months ago

@rezkanas what are system prompts? because it doesnt ask me anything i just send normal promptsto the same conversation again and again

HydRen-fr commented 7 months ago

Something like this worked for me (no system_prompt) :

from hugchat import hugchat
from hugchat.login import Login

def query_LLM(prompt: str, new_conversation: bool = False):
        # Log in to huggingface and grant authorization to huggingchat
        huggingchat_user_email = "your_email"
        huggingchat_user_pwd = "your_password"

        sign = Login(email=huggingchat_user_email, passwd=huggingchat_user_pwd)
        cookies = sign.login()

        # Save cookies to the local directory
        cookie_path_dir = "./cookies"
        cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)

        # Create a ChatBot
        if new_conversation:
            global conversation_id, chatbot
            chatbot = hugchat.ChatBot(cookies=cookies.get_dict())  # or cookie_path="usercookies/<email>.json"

            chatbot.new_conversation(switch_to=True)
            conversation_id = chatbot.new_conversation()
        else:
            pass

        chatbot.change_conversation(conversation_id)

        return chatbot.chat(prompt)['text']

rep1 = query_LLM('''Hello.''', True)
print(rep1)

rep2 = query_LLM('''How are you ?''', False)
print(rep2)
rezkanas commented 7 months ago

I would like to pass different system prompts at each turn of the conversation along with passing user prompts.

However, I've noticed that when I attempt to change the chatbot.system_prompt, the language model (LLM) doesn't seem to adjust its responses accordingly. Could it be that the system prompt can't be changed mid-conversation? I need confirmation or clarification on this matter. I think changing the system prompt mid-conversation isn't directly supported.

Whitelisted1 commented 7 months ago

I would like to pass different system prompts at each turn of the conversation along with passing user prompts.

However, I've noticed that when I attempt to change the chatbot.system_prompt, the language model (LLM) doesn't seem to adjust its responses accordingly. Could it be that the system prompt can't be changed mid-conversation? I need confirmation or clarification on this matter. I think changing the system prompt mid-conversation isn't directly supported.

With the current way that HuggingFace manages system prompts you can only use one system prompt per conversation.

rezkanas commented 7 months ago

thank you @Whitelisted1.

github-actions[bot] commented 5 months ago

This issue was marked as stale because of inactivity.

github-actions[bot] commented 5 months ago

This issue was closed because of inactivity.