import gradio as gr import chromadb from datetime import datetime import requests # Initialize ChromaDB chroma_client = chromadb.Client() collection = chroma_client.get_or_create_collection("chat_memory") # Load initial messages trip_messages = [ {"id": "chat_001", "user": "Nithin", "text": "Hey Nithish, are we still on for the Goa trip?", "timestamp": "2024-04-20T09:00"}, {"id": "chat_002", "user": "Nithish", "text": "Yes bro, I was actually looking at places to stay near Baga beach.", "timestamp": "2024-04-20T09:05"}, {"id": "chat_003", "user": "Nithin", "text": "Awesome! Let’s book a beach shack or a villa.", "timestamp": "2024-04-20T09:10"}, {"id": "chat_004", "user": "Nithish", "text": "Villa sounds better. Should I check Airbnb or OYO?", "timestamp": "2024-04-20T09:15"}, {"id": "chat_005", "user": "Nithin", "text": "Airbnb. I’ve seen some great ones with pools and ocean views.", "timestamp": "2024-04-20T09:20"}, {"id": "chat_006", "user": "Nithish", "text": "Cool, let’s finalize the travel dates. Are you free on May 10th?", "timestamp": "2024-04-20T09:25"}, {"id": "chat_007", "user": "Nithin", "text": "Yes! Let’s plan from May 10 to May 14. 4 nights should be enough.", "timestamp": "2024-04-20T09:30"}, {"id": "chat_008", "user": "Nithish", "text": "Perfect. Let’s book the flights today and split the cost.", "timestamp": "2024-04-20T09:35"}, {"id": "chat_009", "user": "Nithin", "text": "Should we rent scooters there? It’ll be easier to move around.", "timestamp": "2024-04-20T09:40"}, {"id": "chat_010", "user": "Nithish", "text": "Yes! Let’s also hit up Thalassa and Curlies for sure!", "timestamp": "2024-04-20T09:45"}, ] # Add messages to ChromaDB collection.add( documents=[msg["text"] for msg in trip_messages], metadatas=[{"user": msg["user"], "timestamp": msg["timestamp"]} for msg in trip_messages], ids=[msg["id"] for msg in trip_messages] ) # Preload messages into chat history preloaded_history = [(msg["text"], None) if msg["user"] == "Nithin" else (None, msg["text"]) for msg in trip_messages] # OpenAI API Key api_key = "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" # Replace with your OpenAI API key message_counter = len(trip_messages) + 1 with gr.Blocks() as demo: gr.Markdown("## ✈️ Goa Trip Plan Chat") chatbot = gr.Chatbot(value=preloaded_history, label="Trip Chat") chroma_suggestions = gr.Markdown("**🔍 Similar Past Conversations will appear here...**") llm_suggestion = gr.Markdown("**🤖 Smart LLM Suggestion will appear here...**") with gr.Row(): msg1 = gr.Textbox(label="Nithin", placeholder="Type as Nithin...") send1 = gr.Button("Send as Nithin") msg2 = gr.Textbox(label="Nithish", placeholder="Type as Nithish...") send2 = gr.Button("Send as Nithish") state = gr.State(preloaded_history) counter = gr.State(message_counter) def send_message(user, message, history, count): # Query similar messages from Chroma try: results = collection.query(query_texts=[message], n_results=3) matches = results["documents"][0] chroma_md = "🔍 **Similar Conversations:**\n" + "\n".join([f"- {m}" for m in matches]) except Exception as e: chroma_md = f"_ChromaDB error: {e}_" matches = [] # Generate LLM response using matched context try: past_messages = "".join([ f"- [{metadata['timestamp']}] {metadata['user']}: {text}\n" for text, metadata in zip(matches, results['metadatas'][0]) ]) prompt = f"""You are replying as **{user}** in a group chat planning a trip. Here are some relevant past messages from the chat: {past_messages} Now, as {user}, respond naturally to the following new message: \"{message}\" Make sure your tone matches a casual, friendly group chat. """ response = requests.post( url="https://api.openai.com/v1/chat/completions", headers={ "Authorization": f"Bearer {api_key}", "Content-Type": "application/json" }, json={ "model": "gpt-4o", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt} ] } ) if response.status_code == 200: llm_reply = response.json()["choices"][0]["message"]["content"] else: llm_reply = "_LLM failed to respond._" except Exception as e: llm_reply = f"_LLM error: {e}_" # Add new message to history timestamp = datetime.now().isoformat(timespec="minutes") msg_id = f"chat_{count:03d}" collection.add( documents=[message], metadatas=[{"user": user, "timestamp": timestamp}], ids=[msg_id] ) history.append((message, None) if user == "Nithin" else (None, message)) return history, "", history, count + 1, chroma_md, f"🤖 **LLM Suggestion:**\n\n{llm_reply}" # Hook buttons send1.click( fn=lambda msg, hist, count: send_message("Nithin", msg, hist, count), inputs=[msg1, state, counter], outputs=[chatbot, msg1, state, counter, chroma_suggestions, llm_suggestion] ) send2.click( fn=lambda msg, hist, count: send_message("Nithish", msg, hist, count), inputs=[msg2, state, counter], outputs=[chatbot, msg2, state, counter, chroma_suggestions, llm_suggestion] ) demo.launch(share=True, debug=True)