Last active
September 2, 2024 15:22
-
-
Save tos-kamiya/637b2925e346f16fdf6651d72db0432e to your computer and use it in GitHub Desktop.
howdoi command with leverage of Phind-CodeLlama
This file contains 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 | |
# ref: https://stackoverflow.com/questions/2804543/read-subprocess-stdout-line-by-line#2813530 | |
import io | |
import os | |
import subprocess | |
import sys | |
from pygments import highlight | |
from pygments.lexers.markup import MarkdownLexer | |
from pygments.formatters import Terminal256Formatter | |
_llama_cpp_dir = "/path/of/llama.cpp/directory" | |
_model_file = "phind-codellama-34b-v2.Q4_K_M.gguf" | |
__doc__ = """Ask a programming question to Phind-CodeLlama-34b-v2. | |
Usage: | |
codellama-howdoi [-c] [-q] | |
codellama-howdoi [-c] [-q] - | |
codellama-howdoi [-c] [-q] text... | |
Options: | |
-q --quiet No verbose output. | |
""" | |
argv = sys.argv[1:] | |
option_color_always = False | |
option_quiet = False | |
while argv[0].startswith("-"): | |
if argv[0] in ["-h", "--help"]: | |
print(__doc__) | |
exit() | |
elif argv[0] in ["-c", "--color-always"]: | |
option_color_always = True | |
argv.pop(0) | |
elif argv[0] in ["-q", "--quiet"]: | |
option_quiet = True | |
argv.pop(0) | |
else: | |
break # while | |
if not argv: | |
print("Input question text (empty line to end): ") | |
r = [] | |
while True: | |
t = input("> ") | |
if t == "": | |
break | |
r.append(t) | |
text = "\n".join(r) | |
elif argv == ["-"]: | |
text = sys.stdin.read() | |
else: | |
text = " ".join(argv) | |
text = text.strip() | |
if not text: | |
exit("Error: no question") | |
prompt = f""" | |
### System Prompt | |
You are an expert programmer that writes simple, concise code and explanations. | |
### User Message | |
{text} | |
### Assistant | |
""" | |
model = os.path.join("models", _model_file) | |
llama_exe = os.path.join(os.curdir, "main") | |
cmd = [llama_exe, "-m", model, "-p", prompt] | |
# cmd = [llama_exe, "--help"] | |
stderr = subprocess.DEVNULL if option_quiet else None | |
proc = subprocess.Popen(cmd, cwd=_llama_cpp_dir, stdout=subprocess.PIPE, stderr=stderr) | |
lines = [] | |
for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): # or another encoding | |
if not option_quiet: | |
print(line, end="", flush=True, file=sys.stderr) | |
lines.append(line) | |
proc.wait() | |
print("", file=sys.stderr) | |
if not option_quiet: | |
print("---", flush=True, file=sys.stderr) | |
output_text = "".join(lines) | |
if option_color_always or sys.stdout.isatty(): | |
formatter = Terminal256Formatter(style="fruity") | |
sys.stdout.write(highlight(output_text, MarkdownLexer(), formatter)) | |
else: | |
sys.stdout.write(output_text) |
Author
tos-kamiya
commented
Nov 21, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment