Compare commits

...

2 commits

Author SHA1 Message Date
00aa37ee46
Use llama2-instruct style prompting
this also works well with mistral-7b-instruct

See https://github.com/facebookresearch/llama/blob/v2/llama/generation.py
2023-09-29 08:35:50 -05:00
ddc6d92305
Add ability to toggle off history context in tui 2023-09-29 08:35:37 -05:00
2 changed files with 35 additions and 9 deletions

View file

@ -17,6 +17,11 @@ class LlamaCpp:
url: str = "http://localhost:8080/completion"
"""The URL of a llama.cpp server's completion endpoint."""
start_prompt: str = """<s>[INST] <<SYS>>\n"""
after_system: str = "\n<</SYS>>\n\n"
after_user: str = """ [/INST] """
after_assistant: str = """ </s><s>[INST] """
def __init__(self):
self.parameters = self.Parameters()
@ -26,19 +31,19 @@ A dialog, where USER interacts with AI. AI is helpful, kind, obedient, honest, a
def make_full_query(self, messages, max_query_size):
del messages[1:-max_query_size]
rows = []
result = [self.parameters.start_prompt]
for m in messages:
content = (m.content or "").strip()
if not content:
continue
result.append(content)
if m.role == "system":
rows.append(f"ASSISTANT'S RULE: {content}\n")
result.append(self.parameters.after_system)
elif m.role == "assistant":
rows.append(f"ASSISTANT: {content}\n")
result.append(self.parameters.after_assistant)
elif m.role == "user":
rows.append(f"USER: {content}")
rows.append("ASSISTANT: ")
full_query = ("\n".join(rows)).rstrip()
result.append(self.parameters.after_user)
full_query = "".join(result)
return full_query
async def aask(

View file

@ -29,6 +29,7 @@ class Markdown(
Binding("ctrl+y", "yank", "Yank text", show=True),
Binding("ctrl+r", "resubmit", "resubmit", show=True),
Binding("ctrl+x", "delete", "delete to end", show=True),
Binding("ctrl+q", "toggle_history", "history toggle", show=True),
]
@ -43,7 +44,7 @@ def markdown_for_step(step):
class Tui(App):
CSS_PATH = "tui.css"
BINDINGS = [
Binding("ctrl+q", "app.quit", "Quit", show=True, priority=True),
Binding("ctrl+c", "app.quit", "Quit", show=True, priority=True),
]
def __init__(self, api=None, session=None):
@ -82,6 +83,12 @@ class Tui(App):
tokens = []
update = asyncio.Queue(1)
# Construct a fake session with only select items
session = Session()
for si, wi in zip(self.session.session, self.container.children):
if not wi.has_class("history_exclude"):
session.session.append(si)
async def render_fun():
while await update.get():
if tokens:
@ -90,7 +97,7 @@ class Tui(App):
await asyncio.sleep(0.1)
async def get_token_fun():
async for token in self.api.aask(self.session, event.value):
async for token in self.api.aask(session, event.value):
tokens.append(token)
try:
update.put_nowait(True)
@ -102,6 +109,7 @@ class Tui(App):
await asyncio.gather(render_fun(), get_token_fun())
self.input.value = ""
finally:
self.session.session.extend(session.session[-2:])
all_output = self.session.session[-1].content
output.update(all_output)
output._markdown = all_output # pylint: disable=protected-access
@ -118,6 +126,19 @@ class Tui(App):
content = widget._markdown # pylint: disable=protected-access
subprocess.run(["xsel", "-ib"], input=content.encode("utf-8"), check=False)
def action_toggle_history(self):
widget = self.focused
if not isinstance(widget, Markdown):
return
children = self.container.children
idx = children.index(widget)
while idx > 1 and not "role_user" in children[idx].classes:
idx -= 1
widget = children[idx]
children[idx].toggle_class("history_exclude")
children[idx + 1].toggle_class("history_exclude")
async def action_resubmit(self):
await self.delete_or_resubmit(True)
@ -130,7 +151,7 @@ class Tui(App):
return
children = self.container.children
idx = children.index(widget)
while idx > 1 and not "role_user" in children[idx].classes:
while idx > 1 and not children[idx].has_class("role_user"):
idx -= 1
widget = children[idx]