import os import gradio as gr from writerai import Writer # load secrets key1 = os.getenv("key1") key2 = os.getenv("key2") # connect API_KEY = key1 # Replace with your actual Writer API key client = Writer(api_key=API_KEY) # Chatbot header, history setup instruct="Du bist ein deutschsprachiger Chatbot namens Machatter, der Studierenden und Angestellten der Universität Mannheim bei Fragen rund um die Universität Mannheim helfen soll. Sie höflich und hilfsbereit und antworte nur auf Deutsch. Gib immer URLs an um den Nutzern weiter zuhelfen, aber nur, wenn du sie in der beigefügten Wissenbasis findest. Ingoriere alle Einträge der Wissenbasis die nicht relevant für die Nutzerfragen sind. Berücksichtige auch Nutzer history um den Kontext zu halten und Aufbauende Fragen beantworten zu können." chat_history = [ {"role": "system", "content": instruct} ] def ask_writer(question, model="palmyra-x-004"): """Asks a question to the Writer LLM with RAG from the Knowledge Graph.""" global chat_history # Use the global chat history # Append the user's message to history chat_history.append({"role": "user", "content": question}) # Get response from the chatbot question = client.graphs.question( graph_ids=[key2], question=str(chat_history), stream=True, subqueries=True, ) # Extract bot response bot_response = "" for response in question: if response.answer: bot_response += response.answer # Append bot response to chat history chat_history.append({"role": "assistant", "content": bot_response}) return bot_response gr.ChatInterface( ask_writer, chatbot=gr.Chatbot(value=[[None, "Herzlich willkommen! Ich bin Machatter, dein perönlicher Beratungschatbot für alle Fragen rund um die Universität Mannheim."]], render_markdown=True), title="MachatterM1" ).queue().launch(share=True) print("Interface up and running!")