Spaces:
Runtime error
Runtime error
File size: 3,162 Bytes
26ce77e 6428a2d 48f5500 26ce77e 6428a2d 26ce77e 6428a2d 26ce77e 6428a2d 26ce77e 6428a2d 8780e50 6428a2d 8780e50 26ce77e 81e6627 01cd081 26ce77e 81e6627 8780e50 81e6627 26ce77e 81e6627 8780e50 81e6627 a616720 ae0863a 81e6627 ae0863a 26ce77e 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import streamlit as st
from ollama import chat
from loguru import logger
import re
available_models = [
'openhermes',
'deepseek-coder',
'deepseek-coder:6.7b',
'falcon:7b',
'mistral:7b',
'phi',
'starling-lm'
]
def ask(model, system_prompt, pre_prompt, question):
messages = [
{
'role': 'system',
'content': f"{system_prompt} {pre_prompt}",
},
{
'role': 'user',
'content': f"{question}",
},
]
logger.debug(f"<< {model} << {question}")
response = chat(model=model, messages=messages)
answer = response['message']['content']
logger.debug(f">> {model} >> {answer}")
return answer
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!")
role = target(question)
max_steps = 10
for step, _ in enumerate(range(max_steps), start=1):
with st.spinner(f"({step}/{max_steps}) Asking {role}..."):
actor = Actor[role]
answer = ask(actor.model, actor.system_prompt, actor.pre_prompt, question)
st.write(f":blue[{actor.role} says:] {answer}")
question = sanitize(answer)
role = target(question)
def target(question):
return re.split(r'\s|,|:', question.strip())[0].strip()
def sanitize(question):
return re.sub(r"\([^)]*\)", "", question)
if __name__ == "__main__":
main()
|