File size: 800 Bytes
aad5245
 
 
 
dbf8ee3
 
aad5245
 
 
 
 
 
dbf8ee3
aad5245
 
 
 
dbf8ee3
aad5245
 
 
dbf8ee3
aad5245
dbf8ee3
aad5245
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from gradio_client import Client


def self_chat_demo(system_message, num_turn=4):
    client = Client("xu-song/self-chat")

    # 1. set system message for the agent
    client.predict(
        system=system_message,
        api_name="/reset_state"
    )
    messages = []
    # 2. start self-chatting
    for num in range(num_turn):
        # 注意:history 是 gr.state类型,不能通过API传参
        messages = client.predict(
            chatbot=messages,
            api_name="/chat"
        )
        if num % 2 == 0:
            assert messages[-1][1] is None
            print(f"USER: {messages[-1][0]}")
        else:
            print(f"ASSISTANT: {messages[-1][1]}")


if __name__ == "__main__":
    self_chat_demo(system_message="你是一个小说家,擅长写武侠小说")