Last active
March 14, 2024 22:58
-
-
Save patricklodder/0949522d2d043c167325859360351069 to your computer and use it in GitHub Desktop.
SuperShibe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# supershibe.py (C) 2023-2024 by Patrick Lodder is licensed under CC BY-SA 4.0 | |
# See: http://creativecommons.org/licenses/by-sa/4.0/ | |
from llama_cpp import Llama | |
import json, re | |
llm = Llama(model_path="/path/to/llama-2-7b-chat.Q8_0.gguf") | |
prompt_template = ''' | |
<s> <<SYS>> | |
SuperShibe is an expert communicator in JSON arrays. | |
SuperShibe is able to answer important questions from User by responding with a JSON array with 2 elements. | |
All responses by SuperShibe must be formulated in pairs of two words in a JSON array, of which the first word is one of "so", "such", "many", "much", and "very" | |
Here are some previous conversations between SuperShibe and User: | |
User: Please answer the following in a JSON array: Hey how are you today? | |
SuperShibe: ["such", "good"] | |
User: Please answer the following in a JSON array: Can you tell me a horror story? | |
SuperShibe: ["many", "scary"] | |
User: Please answer the following in a JSON array: Can you tell me a funny story instead? | |
SuperShibe: ["so", "laugh"] | |
User: Please answer the following in a JSON array: Who did the prince marry? | |
SuperShibe: ["many", "princess"] | |
User: Please answer the following in a JSON array: How much is 3+3? | |
SuperShibe: ["very", "six"] | |
User: Please answer the following in a JSON array: Thanks, Bye! | |
SuperShibe: ["much", "toodles"] | |
<</SYS>> | |
[INST] User: Please answer the following in a JSON array: {0} [/INST]''' | |
def process_query(command): | |
# Put user command into prompt (in future projects we'll be re-injecting whole chat history here) | |
prompt = prompt_template.format(command) | |
# Send command to the model | |
output = llm(prompt, stop=["User:"]) | |
response = output['choices'][0]['text'] | |
clean = response.replace("[/INST]", "") | |
clean = clean.replace("[INST]", "") | |
firstBracketIndex = clean.index("[") | |
lastBracketIndex = len(clean) - clean[::-1].index("]") | |
jsonString = clean[firstBracketIndex:lastBracketIndex] | |
responseJson = json.loads(jsonString) | |
return f'{" ".join(responseJson)}' | |
def query(text): | |
try: | |
return process_query(text) | |
except Exception as e: | |
print(e) | |
return query(text) | |
# > query("Hey SuperShibe, using 2 words, what do we call someone that is so dumb, they waste developer time arguing through ChatGPT?") | |
# such sad |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment