Skip to content

Commit

Permalink
[MLC-38] server: Allow server to take custom instructions into account
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkerSm1th committed Mar 4, 2024
1 parent c14b628 commit 4a3fcf9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ def format_messages(messages, context):
return messages


def add_instructions(messages, instructions):
if len(instructions.get('personalization', '')) > 0:
messages[-1]['content'] = f"""
Before answering the prompt, remember that user wanted you to know:
<BEGIN_PERSONALIZATION>
{instructions.get('personalization', '')}
<END_PERSONALIZATION>
{messages[-1]['content']}
""".strip()

if len(instructions.get('response', '')) > 0:
messages[-1]['content'] = f"""
Before answering the prompt, remember that the user asked for you to respond in the following way:
<BEGIN_RESPONSE_SPECIFICATIONS>
{instructions.get('response', '')}
<END_RESPONSE_SPECIFICATIONS>
{messages[-1]['content']}
""".strip()

return messages


class APIHandler(BaseHTTPRequestHandler):
def _set_headers(self, status_code=200):
self.send_response(status_code)
Expand Down Expand Up @@ -177,6 +199,7 @@ def query(self, body):

directory = body.get('directory', None)
messages = body.get('messages', [])
instructions = body.get('instructions', None)

if directory:
# emperically better than `similarity_search`
Expand All @@ -191,6 +214,7 @@ def query(self, body):
print(('\n'+'--'*10+'\n').join([
f'{doc.metadata}\n{doc.page_content}' for doc in docs]), flush=True)

add_instructions(messages, instructions)
print(messages, flush=True)
prompt = mx.array(_tokenizer.encode(_tokenizer.apply_chat_template(
messages,
Expand Down

0 comments on commit 4a3fcf9

Please sign in to comment.