| import os |
| import gradio as gr |
| from huggingface_hub import InferenceClient |
|
|
| |
| HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
| |
| client = InferenceClient(model="meta-llama/Meta-Llama-3-8B-Instruct", token=HF_TOKEN) |
|
|
| |
| def respond_stateless(message): |
| |
| messages = [ |
| {"role": "system", "content": "You are a stateless AI. Answer only the current prompt."}, |
| {"role": "user", "content": message} |
| ] |
| |
| response = "" |
| for message_obj in client.chat_completion(messages, max_tokens=512, stream=True): |
| if hasattr(message_obj, 'choices') and len(message_obj.choices) > 0: |
| content = message_obj.choices[0].delta.content |
| if content: |
| response += content |
| return response |
|
|
| |
| def respond_stateful(message, history): |
| |
| messages = [{"role": "system", "content": "You are a Context-Aware Assistant. Use the history to remember details."}] |
| |
| |
| for user_msg, bot_msg in history: |
| messages.append({"role": "user", "content": user_msg}) |
| messages.append({"role": "assistant", "content": bot_msg}) |
| |
| messages.append({"role": "user", "content": message}) |
| |
| response = "" |
| for message_obj in client.chat_completion(messages, max_tokens=512, stream=True): |
| if hasattr(message_obj, 'choices') and len(message_obj.choices) > 0: |
| content = message_obj.choices[0].delta.content |
| if content: |
| response += content |
| |
| |
| history.append((message, response)) |
| return response, history |
|
|
| |
| def chat_coordinator(user_message, chat1_ui_data, state_history): |
| |
| res1 = respond_stateless(user_message) |
| |
| chat1_ui_data.append({"role": "user", "content": user_message}) |
| chat1_ui_data.append({"role": "assistant", "content": res1}) |
| |
| |
| res2, updated_state = respond_stateful(user_message, state_history) |
| |
| |
| chat2_ui_list = [] |
| for u, a in updated_state: |
| chat2_ui_list.append({"role": "user", "content": u}) |
| chat2_ui_list.append({"role": "assistant", "content": a}) |
| |
| |
| return "", chat1_ui_data, chat2_ui_list, updated_state, len(updated_state) |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("Tabula Rasa vs Total Recall") |
| |
| |
| hidden_notebook = gr.State([]) |
|
|
| |
| with gr.Row(): |
| |
| |
| with gr.Column(scale=1, variant="panel"): |
| gr.Markdown("### βοΈ System Controls") |
| mem_counter = gr.Number(value=0, label="Memory Context (Turns)") |
| |
| |
| reset_btn = gr.Button("ποΈ Reset Stateful Memory", variant="stop") |
| |
| gr.Markdown("---") |
| gr.Info("Agent 2 rebuilds its context from the Hidden Notebook for every message.") |
|
|
| |
| with gr.Column(scale=4): |
| with gr.Row(): |
| |
| with gr.Column(): |
| gr.Markdown("### Agent Tabula Rasa") |
| gr.Markdown("_Stateless: This agent has no memory but knows everything") |
| chat1_ui = gr.Chatbot(height=450, show_label=False) |
| |
| |
| with gr.Column(): |
| gr.Markdown("### Agent Total Recall") |
| gr.Markdown("_Stateful: This agent knows and remembers everything") |
| chat2_ui = gr.Chatbot(height=450, show_label=False) |
| |
| |
| with gr.Row(): |
| user_input = gr.Textbox( |
| show_label=False, |
| placeholder="Ask both agents a question to compare memory...", |
| scale=4 |
| ) |
| submit_btn = gr.Button("Send π", variant="primary", scale=1) |
|
|
| |
| config = { |
| "fn": chat_coordinator, |
| "inputs": [user_input, chat1_ui, hidden_notebook], |
| "outputs": [user_input, chat1_ui, chat2_ui, hidden_notebook, mem_counter] |
| } |
| |
| user_input.submit(**config) |
| submit_btn.click(**config) |
| |
| |
| reset_btn.click( |
| lambda: ([], [], [], 0), |
| None, |
| [chat1_ui, chat2_ui, hidden_notebook, mem_counter] |
| ) |
|
|
| |
| demo.launch(theme=gr.themes.Soft()) |