import gradio as gr import requests import json BOT_URL = "http://localhost:1212/api/v1/chat" def llm_response(message, history): # Last Message is : message # History is : [{'role': 'user', 'metadata': {'title': None}, 'content': 'hi', 'options': None}, {'role': 'assistant', 'metadata': {'title': None}, 'content': 'I am a bot', 'options': None}] # Send the message to the bot and get the response # Create user history payload_msg = [] if len(history) > 0: for hist in history: hist.pop("metadata", None) hist.pop("options", None) payload_msg.append(hist) # Add the user message user_message = {"role": "user", "content": message} payload_msg.append(user_message) # Send the message to the bot payload = {"messages": payload_msg} resp = requests.post(BOT_URL, json=payload) resp_json = resp.json() try: assistant_message = resp_json["choices"][0]['message']['content'] except KeyError: print(f"Error in response: {resp_json}") assistant_message = "Sorry, I did not understand that." return assistant_message with gr.Blocks(fill_height=True) as bot: # placeholder = "
Ask Me Anything About Remote Work!" # chatbot = gr.Chatbot(placeholder=placeholder) gr.ChatInterface(fn=llm_response, type="messages")#, chatbot=chatbot) if __name__ == "__main__": bot.launch(share=False)