es wäre gut wenn ein bearbeitungsprozess des llm pausiert werden kann damit der nutzer feedback eintippen kann . Im konversationsfenster sollte nicht nur jeder output vom llm vorkommen sondernauch wichtige warnmeldungen und fehlermeldungen. Im gui sollen alle llm einstellungen wie auch system prompt einstellbar sein. Die fähigkeiten des llm sollten verbessert und optimiert werden damit auch komplexere aufgaben bewältigt werden können. Verbesser die anwendung weiter

import tkinter as tk
from tkinter import ttk, scrolledtext
import threading
import requests
import json

# Constants for context management
MAX_CONTEXT_LENGTH = 1000  # Adjust based on your model's capacity
SUMMARIZE_THRESHOLD = 800

# Function to communicate with Ollama API and get installed models
def get_installed_models():
    try:
        response = requests.get('http://localhost:11434/api/models')
        if response.status_code == 200:
            data = response.json()
            models = [model['name'] for model in data['models']]
            return models
        else:
            return []
    except requests.ConnectionError:
        return []

# Function to send a message to the LLM and receive the response
def generate_response(model_name, prompt, update_callback):
    url = 'http://localhost:11434/api/generate'
    headers = {'Content-Type': 'application/json'}
    payload = {
        "model": model_name,
        "prompt": prompt,
        "options": {
            "stream": True
        }
    }

    try:
        with requests.post(url, headers=headers, data=json.dumps(payload), stream=True) as response:
            buffer = ''
            for line in response.iter_lines():
                if line:
                    decoded_line = line.decode('utf-8')
                    if decoded_line.startswith('data:'):
                        data_content = decoded_line[len('data:'):].strip()
                        if data_content == '[DONE]':
                            break
                        try:
                            data_json = json.loads(data_content)
                            token = data_json.get('response', '')
                            buffer += token
                            update_callback(token)
                        except json.JSONDecodeError:
                            continue
    except Exception as e:
        update_callback(f"\n[Error communicating with Ollama: {e}]")

# Function to handle user's message sending
def send_message():
    user_input = input_field.get()
    if not user_input.strip():
        return

    # Display user's message
    chat_display.configure(state='normal')
    chat_display.insert(tk.END, f"You: {user_input}\n")
    chat_display.configure(state='disabled')

    # Clear input field
    input_field.delete(0, tk.END)

    # Prepare conversation history and context management
    conversation_history.append(f"User: {user_input}")
    context = '\n'.join(conversation_history)

    # Summarize context if it exceeds MAX_CONTEXT_LENGTH
    if len(context) > MAX_CONTEXT_LENGTH:
        if len(conversation_history) > 2:
            # Simple summarization: Keep the last exchange
            summarized_context = 'The conversation so far: (summary)\n'
            summarized_context += '\n'.join(conversation_history[-2:])
            context = summarized_context
        else:
            # If context is still too long, truncate from the beginning
            context = context[-MAX_CONTEXT_LENGTH:]

    # Append assistant prompt
    context += "\nAssistant: Let's think step by step."

    model_name = model_var.get()

    # Function to update the chat display with the LLM's response
    def update_display(token):
        chat_display.configure(state='normal')
        chat_display.insert(tk.END, token)
        chat_display.see(tk.END)
        chat_display.configure(state='disabled')

    # Function to run the LLM generation in a separate thread
    def run_generation():
        generate_response(model_name, context, update_display)
        # After response is complete, append to conversation history
        conversation_history.append(f"Assistant: {chat_display.get('end-2l', 'end-1c').split('Assistant:',1)[-1].strip()}")

    # Start the generation in a new thread
    threading.Thread(target=run_generation).start()

# Initialize the main window
root = tk.Tk()
root.title("LLM Agent Chat")
root.geometry("600x500")

# Model selection
model_var = tk.StringVar()
models = get_installed_models()
if models:
    model_var.set(models[0])
else:
    model_var.set("No models found")
model_label = tk.Label(root, text="Select LLM Model:")
model_label.pack(pady=5)
model_dropdown = ttk.Combobox(root, textvariable=model_var, values=models, state='readonly')
model_dropdown.pack(pady=5)

# Chat display area
chat_display = scrolledtext.ScrolledText(root, wrap=tk.WORD, state='disabled')
chat_display.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)

# Input field and send button
input_frame = tk.Frame(root)
input_frame.pack(padx=10, pady=5, fill=tk.X)
input_field = tk.Entry(input_frame)
input_field.pack(side=tk.LEFT, fill=tk.X, expand=True)
send_button = tk.Button(input_frame, text="Send", command=send_message)
send_button.pack(side=tk.LEFT, padx=5)

# Conversation history
conversation_history = []

# Start the GUI event loop
root.mainloop()