Soulter / hugging-chat-api

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

Conversation as prompt and response chain #5

Closed halilergul1 closed 1 year ago

halilergul1 commented 1 year ago

Hi There,

Thanks for this api contribution. But if I did not misunderstand, there is no way to have a continuous conversation with this API. What I have in my mind is a API request-response structure similar to what OpenAi provides: https://platform.openai.com/docs/api-reference/chat/create. Am I right?

Because every time I try to come with different way to do such thing (where I can maintain the conversation with AI as in the web UI) I realize that it fails.

PveOnly commented 1 year ago

Yeah same problem, the conversation id is the same for each chatbot.chat call but the results say that it doesn't remember the previous prompt. I think it may be because of javascript. My current solution is globally to use selenium.

Soulter commented 1 year ago

I didn't find context fields when crawling the network request of hugging chat before, so either the official server maintained the context of this dialogue, or there was no implementation of the context. I'll check it later.

halilergul1 commented 1 year ago

Yeah same problem, the conversation id is the same for each chatbot.chat call but the results say that it doesn't remember the previous prompt. I think it may be because of javascript. My current solution is globally to use selenium.

so the problem was solved on your part? I mean can you now maintain a dialogue?

halilergul1 commented 1 year ago

I didn't find context fields when crawling the network request of hugging chat before, so either the official server maintained the context of this dialogue, or there was no implementation of the context. I'll check it later.

That would be great, thanks

PveOnly commented 1 year ago

Yeah same problem, the conversation id is the same for each chatbot.chat call but the results say that it doesn't remember the previous prompt. I think it may be because of javascript. My current solution is globally to use selenium.

so the problem was solved on your part? I mean can you now maintain a dialogue?

Yeah but it's a trash work around imo because it's really not scalable nor clean since selenium is pretty much just running chrome and clicking on button. I'm gonna check about these context fields too. Since the chat-ui is open-source maybe searching directly there would be faster https://huggingface.co/spaces/huggingchat/chat-ui/tree/main

TonyGlezx commented 1 year ago

I didn't find context fields when crawling the network request of hugging chat before, so either the official server maintained the context of this dialogue, or there was no implementation of the context. I'll check it later.

Can I help with this?

Soulter commented 1 year ago

I didn't find context fields when crawling the network request of hugging chat before, so either the official server maintained the context of this dialogue, or there was no implementation of the context. I'll check it later.

Can I help with this?

Sure!

avijit15 commented 1 year ago

Hello Guys, If you want to save the context and want to use the same context in your next query you have to hit this two apis. I am sharing the code with you guys. Hope this will help you.

def addId(chatId, cookies):
      ## only provide the hf-chat cookie value as cookies
      ## You will get the hf_chat cookie value from the same cookies.json file you are using
      url = f"https://huggingface.co/chat/conversation/{chatId}/summarize"
      payload={}
      headers = {
        'Cookie': f'hf-chat={cookies}'
      }
      response = requests.request("POST", url, headers=headers, data=payload)
      if response.status_code == 200:
          return {'message': "Successfully Added", "status":200}
      else:
          return {'message': "Internal Error", "status": 500}
def preserveContext(chatId,cookies):
   ## only provide the hf-chat cookie value as cookies
   ## You will get the hf_chat cookie value from the same cookies.json file you are using
    url = f"https://huggingface.co/chat/conversation/{chatId}/__data.json?x-sveltekit-invalidated=1_"
    payload={}
    headers = {
      'Cookie': f'hf-chat={cookies}'
    }
    response = requests.request("GET", url, headers=headers, data=payload)
    if response.status_code == 200:
        return {'message': "Context Successfully Preserved", "status":200}
    else:
        return {'message': "Internal Error", "status": 500}

For the 1st time, after getting you query output you have to run both the functions. And from 2nd time on wards, whenever you will run the chatbot.chat() , run only the preserveContext(chatId,cookies) functions.

You can also see all your conversation in hugging chat ui also.

Soulter commented 1 year ago

Hello Guys, If you want to save the context and want to use the same context in your next query you have to hit this two apis. I am sharing the code with you guys. Hope this will help you.

def addId(chatId, cookies):
      ## cookies should be in 'hf-chat=****-****-....' in this format
      url = f"https://huggingface.co/chat/conversation/{chatId}/summarize"
      payload={}
      headers = {
        'Cookie': f'{cookies}'
      }
      response = requests.request("POST", url, headers=headers, data=payload)
      if response.status_code == 200:
          return {'message': "Successfully Added", "status":200}
      else:
          return {'message': "Internal Error", "status": 500}
def preserveContext(chatId,cookies):
   ## cookies should be in 'hf-chat=****-****-....' in this format
    url = f"https://huggingface.co/chat/conversation/{chatId}/__data.json?x-sveltekit-invalidated=1_"
    payload={}
    headers = {
      'Cookie': f'cookies'
    }
    response = requests.request("GET", url, headers=headers, data=payload)
    if response.status_code == 200:
        return {'message': "Context Successfully Preserved", "status":200}
    else:
        return {'message': "Internal Error", "status": 500}

For the 1st time, after getting you query output you have to run both the functions. And from 2nd time on wards, whenever you will run the chatbot.chat() , run only the preserveContext(chatId,cookies) functions.

You can also see all your conversation in hugging chat ui also.

Thanks! You can open a new Pull Request to this repo.

MalcolmHfx commented 1 year ago

Sorry I am newish to github, but does this mean it is fixed in the current code, or still an issue?

If I go: answer = chatbot.chat("question" then I look at the answer, and then do chatbot.chat(" again does it remember context??

MalcolmHfx commented 1 year ago

Can you give a full example showing how you chat once, save context, then chat again and have it remember?

RockChinQ commented 1 year ago

Sorry I am newish to github, but does this mean it is fixed in the current code, or still an issue?

We're still trying to solve this problem, you can just subscribe this issue and wait for it to be closed.

MalcolmHfx commented 1 year ago

Thanks you rock!

On Thu, Jun 8, 2023 at 9:36 PM Rock Chin @.***> wrote:

Sorry I am newish to github, but does this mean it is fixed in the current code, or still an issue?

We're still trying to solve this problem, you can just subscribe this issue and wait for it to be closed.

— Reply to this email directly, view it on GitHub https://github.com/Soulter/hugging-chat-api/issues/5#issuecomment-1583676494, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL6UAOFQMM3IJVDLKOCMISLXKJVYDANCNFSM6AAAAAAXR3A3GA . You are receiving this because you commented.Message ID: @.***>

Soulter commented 1 year ago

UPDATE: I have been investigating the reason why it cannot remember the context. Just now, I discovered a problem in my code that will finally result in the inability to remember the context. I will update a new version soon to fix this issue.