gradio icon indicating copy to clipboard operation
gradio copied to clipboard

More documentation is needed on how to use `gradio_client` with multi-turn dialogue with history

Open lychees opened this issue 2 years ago • 2 comments
trafficstars

  • [x] I have searched to see if a similar issue already exists.

I am trying to use ChatGLM with gradio_client to do some research, but soon found that it doesn't support multi-turn dialogue like the original app. Below is my implementation:

def predict(input, history=None):
    if history is None:
        history = []
 
    client = Client('https://multimodalart-chatglm-6b.hf.space/')
    with open(client.predict(input, fn_index=0)) as f: 
        text = process_text(f.read())
        output = json.loads(text)[0]
        history += [output]
        return history, history

Since we remove state from the API recently, 4107, I thought it could be impossible to support multi-turn dialogue, is that true?

lychees avatar May 11 '23 22:05 lychees

Hi @lychees, yes actually the Client automatically preserves state so you don't have to do anything extra. If you'd like to reset the state, you can do so by doing:

client.reset_session()

Here's an example:

from gradio_client import Client
 
client = Client('https://multimodalart-chatglm-6b.hf.space/', serialize=False)

history = client.predict("Hello! How are you?", fn_index=0)
print("The chatbot responded: ", history[0][-1][-1])
history = client.predict("What is the capital of China?", fn_index=0)
print("The chatbot responded: ", history[0][-1][-1])
history = client.predict("And what is it's largest city?", fn_index=0)
print("The chatbot responded: ", history[0][-1][-1])
client.reset_session()
history = client.predict("And what is it's largest city?", fn_index=0)
print("The chatbot responded: ", history[0][-1][-1])

Here's the response:

The chatbot responded:  As an AI language model, I don’t have feelings, but I’m functioning well and ready to assist you with any questions or concerns you may have. How can I help you today?
The chatbot responded:  The capital of China is Beijing.
The chatbot responded:  China’s largest city is Shanghai, followed by Shenzhen, Guangzhou, Chengdu...
The chatbot responded:  India’s largest city is NewDelhi, followed by Mumbai...

abidlabs avatar May 12 '23 17:05 abidlabs

I think we can document this better, so I'll reframe this issue around that

abidlabs avatar May 12 '23 17:05 abidlabs