CoolFace
Apppublic

ksharma9719/AI_Chatbot_using_MCP

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py37 linesDownload Raw Back to root
1import os2from dotenv import load_dotenv3from langchain_groq import ChatGroq4from mcp_use import MCPAgent, MCPClient5import gradio as gr6 7# Load environment variables8load_dotenv()9os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")10config_file = "browser_mcp.json"11 12client = MCPClient.from_config_file(config_file)13llm = ChatGroq(model="llama3-8b-8192")14agent = MCPAgent(llm=llm, client=client, max_steps=15, memory_enabled=True)15 16def chat(user_input, history=[]):17    if user_input.lower() == "clear":18        agent.clear_conversation_history()19        return "", []20    response = agent.run(user_input)21    history = history + [[user_input, response]]22    return "", history23 24with gr.Blocks() as demo:25    gr.Markdown("# MCP Chatbot")26    chatbot = gr.Chatbot()27    msg = gr.Textbox(label="Type your message")28    clear = gr.Button("Clear Conversation")29 30    def respond(message, chat_history):31        _, updated_history = chat(message, chat_history)32        return "", updated_history33 34    msg.submit(respond, [msg, chatbot], [msg, chatbot])35    clear.click(lambda: ("", []), None, [msg, chatbot])36 37demo.launch()