import gradio as gr messages = [ChatMessage.from_system(chat_template)] chat_generator = get_chat_generator() def chatbot_with_fc(message, messages): messages.append(ChatMessage.from_user(message)) response = chat_generator.run(messages=messages) while True: if response and "" in response["replies"][0].content: function_calls = extract_tool_calls(response["replies"][0].content) for function_call in function_calls: # Parse function calling information function_name = function_call["name"] function_args = function_call["arguments"] # Find the corresponding function and call it with the given arguments function_to_call = available_functions[function_name] function_response = function_to_call(**function_args) # Append function response to the messages list using `ChatMessage.from_function` messages.append(ChatMessage.from_function(content=json.dumps(function_response), name=function_name)) response = chat_generator.run(messages=messages) # Regular Conversation else: messages.append(response["replies"][0]) break return response["replies"][0].content def chatbot_interface(user_input, state): response_content = chatbot_with_fc(user_input, state) return response_content, state with gr.Blocks() as demo: gr.Markdown("# AI Purchase Assistant") gr.Markdown("Ask me about products you want to buy!") state = gr.State(value=messages) with gr.Row(): user_input = gr.Textbox(label="Your message:") response_output = gr.Markdown(label="Response:") user_input.submit(chatbot_interface, [user_input, state], [response_output, state]) gr.Button("Send").click(chatbot_interface, [user_input, state], [response_output, state]) demo.launch()