File size: 2,851 Bytes
4577a87
 
 
6428a2d
8a8ca58
 
26ce77e
8a8ca58
26ce77e
 
81e6627
 
 
 
 
 
 
 
 
 
 
 
01cd081
26ce77e
81e6627
8780e50
81e6627
 
 
26ce77e
 
81e6627
 
 
 
 
 
 
 
 
 
 
 
8780e50
81e6627
4577a87
 
a616720
4577a87
81e6627
 
 
4577a87
81e6627
 
8a8ca58
4577a87
 
 
 
 
 
 
26ce77e
735bc92
48f5500
 
 
 
26ce77e
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import re

import streamlit as st
from loguru import logger
# from ollamachat import ask, models
from transformerschat import ask, models

available_models = models()


class Actor:
    actors = {}

    def __init__(self, role, model, system_prompt, pre_prompt):
        self.role = role
        self.model = model
        self.system_prompt = system_prompt
        self.pre_prompt = pre_prompt
        Actor.actors[role] = self

    def __class_getitem__(cls, item):
        return cls.actors[item]


def setup(question):
    pp1 = pp2 = pp3 = "Ask the other two by always starting your sentence with their role. Never start your sentence with your own name. Share your inner thoughts inside parentheses. SAY ONLY ONE SINGLE SENTENCE!"
    priest = Actor("Priest", available_models[0], "You are the Priest. There are 3 people standing in a circle: the Priest (that's you), the Teacher and the Kid.", pp1)
    teacher = Actor("Teacher", available_models[0], "You are the Teacher. There are 3 people standing in a circle: the Priest, the Teacher (that's you) and the Kid.", pp2)
    kid = Actor("Kid", available_models[0], "You are the Kid. There are 3 people standing in a circle: the Priest, the Teacher and the Kid (that's you).", pp3)
    st.set_page_config(layout="wide")
    col1, col2, col3 = st.columns(3)
    for actor, col in [(priest, col1), (teacher, col2), (kid, col3)]:
        with col:
            role = actor.role
            st.title(role)
            actor.model = st.selectbox("model", available_models, key=f"{role}-model")
            actor.system_prompt = st.text_area("system-prompt", actor.system_prompt, key=f"{role}-sp")
            actor.pre_prompt = st.text_area("pre-prompt", actor.pre_prompt, key=f"{role}-pp")
    st.text_input("Priest's task", f"{question}")
    return question


def main():
    question = setup("Priest, your task is to figure out their names and where they live. Do not ask directly, they must not realize what information you are after!")

    actor = target(sanitize(question))
    max_steps = 1
    for step, _ in enumerate(range(max_steps), start=1):
        with st.spinner(f"({step}/{max_steps}) Asking {actor.role}..."):
            answer = ask(actor.model, actor.system_prompt, actor.pre_prompt, question)
            st.write(f":blue[{actor.role} says:] {answer}")
            question = sanitize(answer)
            actor = target(question)


# noinspection PyTypeChecker
def target(question) -> Actor:
    try:
        role = re.split(r'\s|,|:', question.strip())[0].strip()
        return Actor[role]
    except KeyError:
        logger.warning(f"no actor found in question: {question}, trying to return the first actor")
        return next(iter(Actor.actors.items()))[1]


def sanitize(question):
    return re.sub(r"\([^)]*\)", "", question)


if __name__ == "__main__":
    main()