import os import gradio as gr from bagoodex_client import BagoodexClient from r_types import ChatMessage from prompts import ( SYSTEM_PROMPT_FOLLOWUP, SYSTEM_PROMPT_MAP, SYSTEM_PROMPT_BASE, SYSTEM_PROMPT_KNOWLEDGE_BASE ) from helpers import ( embed_video, format_links, embed_google_map, format_knowledge, format_followup_questions ) client = BagoodexClient() # ---------------------------- # Chat & Follow-up Functions # ---------------------------- def chat_function(message, history, followup_state, chat_history_state): """ Process a new user message. Appends the message and response to the conversation, and retrieves follow-up questions. """ # complete_chat returns a new followup id and answer followup_id_new, answer = client.complete_chat(message) # Update conversation history (if history is None, use an empty list) if history is None: history = [] updated_history = history + [ChatMessage({"role": "user", "content": message}), ChatMessage({"role": "assistant", "content": answer})] # Retrieve follow-up questions using the updated conversation followup_questions_raw = client.base_qna( messages=updated_history, system_prompt=SYSTEM_PROMPT_FOLLOWUP ) # Format them using the helper followup_md = format_followup_questions(followup_questions_raw) return answer, followup_id_new, updated_history, followup_md def handle_followup_click(question, followup_state, chat_history_state): """ When a follow-up question is clicked, send it as a new message. """ if not question: return chat_history_state, followup_state, "" # Process the follow-up question via complete_chat followup_id_new, answer = client.complete_chat(question) updated_history = chat_history_state + [ChatMessage({"role": "user", "content": question}), ChatMessage({"role": "assistant", "content": answer})] # Get new follow-up questions followup_questions_raw = client.base_qna( messages=updated_history, system_prompt=SYSTEM_PROMPT_FOLLOWUP ) followup_md = format_followup_questions(followup_questions_raw) return updated_history, followup_id_new, followup_md def handle_local_map_click(followup_state, chat_history_state): """ On local map click, try to get a local map. If issues occur, fall back to using the SYSTEM_PROMPT_MAP. """ if not followup_state: return chat_history_state try: result = client.get_local_map(followup_state) if result: map_url = result.get('link', '') # Use helper to produce an embedded map iframe html = embed_google_map(map_url) # Fall back: use the base_qna call with SYSTEM_PROMPT_MAP result = client.base_qna( messages=chat_history_state, system_prompt=SYSTEM_PROMPT_MAP ) # Assume result contains a 'link' field html = embed_google_map(result.get('link', '')) new_message = ChatMessage({"role": "assistant", "content": html}) return chat_history_state + [new_message] except Exception: return chat_history_state def handle_knowledge_click(followup_state, chat_history_state): """ On knowledge base click, fetch and format knowledge content. """ if not followup_state: return chat_history_state try: print('trying to get knowledge') result = client.get_knowledge(followup_state) knowledge_md = format_knowledge(result) if knowledge_md == 0000: print('falling back to base_qna') # Fall back: use the base_qna call with SYSTEM_PROMPT_KNOWLEDGE_BASE result = client.base_qna( messages=chat_history_state, system_prompt=SYSTEM_PROMPT_KNOWLEDGE_BASE ) knowledge_md = format_knowledge(result) new_message = ChatMessage({"role": "assistant", "content": knowledge_md}) return chat_history_state + [new_message] except Exception: return chat_history_state # ---------------------------- # Advanced Search Functions # ---------------------------- def perform_image_search(followup_state): if not followup_state: return [] result = client.get_images(followup_state) # For images we simply return a list of original URLs return [item.get("original", "") for item in result] def perform_video_search(followup_state): if not followup_state: return "
No followup ID available.
" result = client.get_videos(followup_state) # Use the helper to produce the embed iframes (supports multiple videos) return embed_video(result) def perform_links_search(followup_state): if not followup_state: return gr.Markdown("No followup ID available.") result = client.get_links(followup_state) return format_links(result) # ---------------------------- # UI Build # ---------------------------- css = """ #chatbot { height: 100%; } h1, h2, h3, h4, h5, h6 { text-align: center; display: block; } """ # defautl query: how to make slingshot? # who created light (e.g., electricity) Tesla or Edison in quick short? with gr.Blocks() as demo: gr.HTML("""