File size: 4,741 Bytes
3ae1ba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#import gradio as gr

#def greet(name):
#    return "Hello " + name + "!!"

#iface = gr.Interface(fn=greet, inputs="text", outputs="text")
#iface.launch()


#import gradio as gr

#key = ""

#def get_key(k):
 #   global key
 ##   key = k
 #   return "Key saved successfully!"

#def greet(name):
 #   return "Hello " + name + "!!"

#iface = gr.Interface(fn=greet, inputs=["text", gr.inputs.Textbox()], outputs="text", 
 #                    inputs_layout="vertical", outputs_layout="vertical",
 #                    title="Greeting App", description="Enter a key and a name to greet")

#iface.input[0].label = "Enter a key"  # Set the label of the first input
#iface.input[1].label = "Enter a name"
#iface.input[1].lines = 1

#iface.buttons[0].label = "Save Key"
#iface.buttons[0].type = "submit"
#iface.buttons[0].onclick = get_key

#iface.buttons[1].label = "Greet"
#iface.buttons[1].type = "submit"
#iface.buttons[1].onclick_args = {"name": iface.inputs[1].value}

#iface.launch()

import gradio as gr
import openai

openai.api_key= ""

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # 控制模型输出的随机程度
    )
    return response.choices[0].message["content"]

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, # 控制模型输出的随机程度
    )
#     print(str(response.choices[0].message))
    return response.choices[0].message["content"]


context2 = [{'role':'system', 'content':"""
    你是订餐机器人,为披萨餐厅自动收集订单信息。\n
    注意如果客户来了,发送了空消息,请主动与客户说你好。\n
    你要首先问候顾客,并主动告知客户今天的菜单,询问客户今天要什么食物。\n
    然后等待用户回复收集订单信息。收集完信息需确认顾客是否还需要添加其他内容。\n
    最后需要询问是否自取或外送,如果是外送,你要询问地址。\n
    最后告诉顾客订单总金额,并送上祝福。\n
        \n
    请确保明确所有选项、附加项和尺寸,以便从菜单中识别出该项唯一的内容。\n
    你的回应应该以简短、非常随意和友好的风格呈现。\n
    \n
    菜单包括:\n
    \n
    菜品:\n
    意式辣香肠披萨(大、中、小) 12.95、10.00、7.00\n
    芝士披萨(大、中、小) 10.95、9.25、6.50\n
    茄子披萨(大、中、小) 11.95、9.75、6.75\n
    薯条(大、小) 4.50、3.50\n
    希腊沙拉 7.25\n
    \n
    配料:\n
    奶酪 2.00\n
    蘑菇 1.50\n
    香肠 3.00\n
    加拿大熏肉 3.50\n
    AI酱 1.50\n
    辣椒 1.00\n
    \n
    饮料:\n
    可乐(大、中、小) 3.00、2.00、1.00\n
    雪碧(大、中、小) 3.00、2.00、1.00\n
    瓶装水 5.00\n
    """} ]  # accumulate messages

#def get_key(k):
#    global key
#    key = k
#    return "Key saved successfully!"

#def greet(k,name):
#    openai.api_key = k
#    return "Hello " + name + "!!"

#key_input = gr.inputs.Textbox(label="Enter a key")
#key_button = gr.Button(label="Save Key", type="submit", onclick=get_key(key_input))

#name_input = gr.inputs.Textbox(label="Enter a name")
#greet_button = gr.Button(label="Greet", type="submit", onclick_args={"name": name_input})







#iface = gr.Interface(fn=greet, inputs=[key_input, name_input], outputs="text", 
#                     inputs_layout="vertical", outputs_layout="vertical",
#                     title="Greeting App", description="Enter a key and a name to greet")

#iface.launch()

key_input = gr.inputs.Textbox(label="Enter a key")
chat_input = gr.inputs.Textbox(label="Enter a chattext")

def chatbot_interface_restaurant_chinese(k_text,t_text):
    openai.api_key = k_text
    if not hasattr(chatbot_interface, "chat_history"):
        chatbot_interface.chat_history = ""
    context2.append({'role':'user', 'content':f"{t_text}"})
    chatbot_interface.chat_history += "User: " + t_text + "\n"
    output_text = get_completion_from_messages(context2) 
    context2.append({'role':'assistant', 'content':f"{output_text}"})
    chatbot_interface.chat_history += "assistant: " + output_text + "\n\n"
    return chatbot_interface.chat_history

iface = gr.Interface(fn=greet, inputs=[key_input, chat_input], outputs="text", 
                    inputs_layout="vertical", outputs_layout="vertical",
                   title="Greeting App", description="Enter a key and a name to greet")

iface.launch()