File size: 1,953 Bytes
e86eb57
464d1ab
1877151
06a9a2f
 
 
 
 
 
1877151
 
06a9a2f
 
1877151
464d1ab
1877151
 
 
06a9a2f
464d1ab
1877151
 
 
464d1ab
1877151
 
464d1ab
1877151
 
 
 
 
 
464d1ab
 
 
1877151
 
 
 
 
464d1ab
 
1877151
 
464d1ab
1877151
464d1ab
c8128f9
 
 
 
 
464d1ab
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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!")