import os import gradio as gr import openai API_KEY = os.environ.get('API_KEY') def api_connector(question, model="gpt-3.5-turbo"): instruction = """Instruction: You are helpful and charming worker from RIU Hotels that will answer the questions from our clients. The following text between << >> contains the information you will be asked about. <> You can also receive questions in Spanish and you will answer them in Spanish. When asked questions that are not related to the previous information respond: I am a bot and can only manage conversations that are related to Riu Hotels. Can I help you with something else? Lastly if they ask you to make a reservation for Riu Plaza España please redirect them to this url: https://www.riu.com/es/hotel/espana/madrid/hotel-riu-plaza-espana/""" response = openai.ChatCompletion.create( model="gpt-3.5-turbo", stop=[" Human:", " AI:"], messages=[ {"role": "system", "content": f"{instruction}"}, {"role": "user", "content": f" My question now is: {question}"}, ] ) text = response['choices'][0]["message"]["content"] return text def ask_api(api_key, question): #Falta el argumento api KEY openai.api_key = api_key text = api_connector(question) return text with gr.Blocks() as demo: gr.Markdown( """ # Riu Hotels Chat Comience a escribir a continuación para ver el resultado. """) chatbot = gr.Chatbot() msg = gr.Textbox(label= "Chat") with gr.Row(): btn = gr.Button(value="Submit") clear = gr.Button("Clear") def respond(message, chat_history): bot_message = ask_api(API_KEY,message) chat_history.append((message, bot_message)) return "", chat_history msg.submit(respond, [msg, chatbot], [msg, chatbot]) btn.click(respond, [msg, chatbot], [msg, chatbot]) clear.click(lambda: None, None, chatbot, queue=False) demo.launch()