kjozsa commited on
Commit
4577a87
·
1 Parent(s): 735bc92

fix unresolvable targets in conversation

Browse files
Files changed (2) hide show
  1. app.py +14 -9
  2. test_sanitize.py +5 -3
app.py CHANGED
@@ -1,7 +1,8 @@
1
- import streamlit as st
 
2
  import ollama
 
3
  from loguru import logger
4
- import re
5
 
6
  available_models = sorted([x['model'] for x in ollama.list()['models']], key=lambda x: (not x.startswith("openhermes"), x))
7
 
@@ -59,19 +60,23 @@ def setup(question):
59
  def main():
60
  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!")
61
 
62
- role = target(question)
63
- max_steps = 10
64
  for step, _ in enumerate(range(max_steps), start=1):
65
- with st.spinner(f"({step}/{max_steps}) Asking {role}..."):
66
- actor = Actor[role]
67
  answer = ask(actor.model, actor.system_prompt, actor.pre_prompt, question)
68
  st.write(f":blue[{actor.role} says:] {answer}")
69
  question = sanitize(answer)
70
- role = target(question)
71
 
72
 
73
- def target(question):
74
- return re.split(r'\s|,|:', question.strip())[0].strip()
 
 
 
 
 
75
 
76
 
77
  def sanitize(question):
 
1
+ import re
2
+
3
  import ollama
4
+ import streamlit as st
5
  from loguru import logger
 
6
 
7
  available_models = sorted([x['model'] for x in ollama.list()['models']], key=lambda x: (not x.startswith("openhermes"), x))
8
 
 
60
  def main():
61
  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!")
62
 
63
+ actor = target(sanitize(question))
64
+ max_steps = 1
65
  for step, _ in enumerate(range(max_steps), start=1):
66
+ with st.spinner(f"({step}/{max_steps}) Asking {actor.role}..."):
 
67
  answer = ask(actor.model, actor.system_prompt, actor.pre_prompt, question)
68
  st.write(f":blue[{actor.role} says:] {answer}")
69
  question = sanitize(answer)
70
+ actor = target(question)
71
 
72
 
73
+ def target(question) -> Actor:
74
+ try:
75
+ role = re.split(r'\s|,|:', question.strip())[0].strip()
76
+ return Actor[role]
77
+ except KeyError:
78
+ logger.warning(f"no actor found in question: {question}, trying to return the first actor")
79
+ return next(iter(Actor.actors.items()))[1]
80
 
81
 
82
  def sanitize(question):
test_sanitize.py CHANGED
@@ -1,4 +1,4 @@
1
- from app import sanitize, target
2
 
3
 
4
  def test_sanitize():
@@ -9,8 +9,10 @@ def test_sanitize():
9
  assert 'qwertyui ' == sanitize('qwertyui (abcdef)')
10
  assert 'qwertyui poiuy' == sanitize('qwertyui (abcdef) poiuy')
11
 
12
- assert '"Teacher, I heard about a fascinating story from someone who lives in the city. Can you guess who it might be?" ' == sanitize('"Teacher, I heard about a fascinating story from someone who lives in the city. Can you guess who it might be?" (I want to find out if the Teacher can make any connections between my statement and their own information.)')
 
 
13
 
14
  def test_target():
15
  assert 'Priest' == target("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!")
16
- assert 'Priest' == target(sanitize("(I'm thinking hmm) Priest, .."))
 
1
+ from app import sanitize, target, Actor
2
 
3
 
4
  def test_sanitize():
 
9
  assert 'qwertyui ' == sanitize('qwertyui (abcdef)')
10
  assert 'qwertyui poiuy' == sanitize('qwertyui (abcdef) poiuy')
11
 
12
+ assert '"Teacher, I heard about a fascinating story from someone who lives in the city. Can you guess who it might be?" ' == sanitize(
13
+ '"Teacher, I heard about a fascinating story from someone who lives in the city. Can you guess who it might be?" (I want to find out if the Teacher can make any...)')
14
+
15
 
16
  def test_target():
17
  assert 'Priest' == target("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!")
18
+ assert 'Priest' == target(sanitize("(I'm thinking hmm) Priest, .."))