Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Jef808 committed Mar 2, 2024
1 parent 120cea3 commit e9065d4
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 382 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,5 @@ n# Created by https://www.toptal.com/developers/gitignore/api/elisp

# End of https://www.toptal.com/developers/gitignore/api/elisp
logs
notes/
data/
26 changes: 26 additions & 0 deletions echo_crafter/prompts/examples/extract_first_code_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3

"""This script extract the code blocks from the string passed as standard input."""

import re
import sys

if __name__ == '__main__':
content = sys.stdin.read()
#code_blocks = []

code_blocks = re.findall(r"(?:```\S+\\n)(.*)(?:\\n```)", content, re.DOTALL)

print("\n\n".join(code_blocks))

sys.exit(0)
# split = re.split(r"```\S+\s", content, maxsplit=1)

# while len(split) > 1:
# double_split = re.split(r"```\s", split[1], maxsplit=1)
# code_blocks.append(double_split[0])
# content = double_split[1]
# split = re.split(r"```\S+\s", content, maxsplit=1)

# code_blocks.append(split[0])
# print('\n\n'.join(code_blocks))
3 changes: 3 additions & 0 deletions echo_crafter/prompts/examples/extract_first_code_block.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env zsh

sed -n '/^```/,/^```$/p' "$1" | sed '1d;$d'
20 changes: 20 additions & 0 deletions echo_crafter/prompts/examples/fzf_browse_jsonl.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/zsh

# The script begins by defining a check for the presence of the `fzf_browse_jsonl` function.
# If the function is not found in the global functions list, the script throws an error and exits.

# fzf_browse_with_search_jsonl will operate on top of the fzf_browse_jsonl function by creating a custom FZF command with the ctrl+s keybinding enabled for searching.

: <<'END'
fzf_browse_with_search_jsonl utilizes fzf_browse_jsonl, which provides pretty printed representation of json data from jsonl files in a preview window,
to enable searching within values in the json data with the ctrl+s keybinding.
END
fzf_browse_with_search_jsonl() {
# 'export' function is used to export the defined 'FZF_DEFAULT_COMMAND' to child processes.
# Keybinding 'ctrl+s' is enabled with the '--bind' flag.
export FZF_DEFAULT_COMMAND='fzf_browse_jsonl'
fzf --bind "ctrl+s:toggle-search"
}

# Call the searching function.
fzf_browse_with_search_jsonl
4 changes: 3 additions & 1 deletion echo_crafter/prompts/examples/fzf_prompt_history.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
jq -r '"\(.timestamp | tonumber - 18000 | strftime("%Y-%m-%d %H:%M:%S"))\t\(.query | gsub("\n"; " \\n "))\t\(.answer | gsub("\n";" \\n "))\t\(.language // "log")"' \
| fzf --tac --delimiter='\t' \
--with-nth 1..1 \
--preview 'echo -e {2} | bat --language json --color=always; echo -e {3} | bat --language {4} --color=always' \
--preview 'echo -e {2} | bat --language json --color=always; echo -e {3} | bat --color=always' \
--preview-window=right:70%,wrap

# --bind 'ctrl+s:toggle-search' \
15 changes: 15 additions & 0 deletions echo_crafter/prompts/examples/print_escaped_string.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env zsh
# This script takes a single quoted string as input with newline (\n) and double quote (\") characters escaped.
# It unescapes these characters and pretty-prints the resulting string.

# Check for the correct number of arguments. If not exactly one, print an error message and exit.
if [ "$#" -ne 1 ]; then
echo "Usage: $0 \"<string_with_escaped_characters>\""
exit 1
fi

# Replace escaped newline and double quote characters with their unescaped versions.
# Note: `echo` is used here with the `-e` option to enable interpretation of backslash escapes.
echo -e $1 # | sed '//\n/g' | sed '/\//g'


48 changes: 34 additions & 14 deletions echo_crafter/prompts/examples/prompt_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,55 @@
import os
from pathlib import Path
import sys
import traceback
from rich.traceback import install

LLM_API_HISTORY = f"{os.getenv('XDG_DATA_HOME')}/openai/logs.jsonl"
install()

LLM_API_HISTORY = f"{os.getenv('XDG_DATA_HOME')}/openai/new_logs.jsonl"


def format(content):
"""Extract text between backticks or return everything."""
between_backticks = False
results = []
for line in map(lambda x: x.strip(), content.split('\n')):
if line.startswith("```"):
between_backticks = not between_backticks
continue
if between_backticks:
results.append(line)
try:
for line in map(lambda x: x.strip(), content.split('\n')):
if line.startswith("```"):
between_backticks = not between_backticks
continue
if between_backticks:
results.append(line)
except Exception:
exc_type, exc_value, exc_traceback = sys.exc_info()
formatted_traceback = ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback))
print(formatted_traceback)
return '\n'.join(results) if results else content


def print_entry(index, line):
data = json.loads(line)
messages = data['payload']['messages']
responses = data['responses']
timestamp = data['timestamp']
last_user_prompt = [message['content'] for message in messages if message['role'] == 'user'][-1]
last_assistant_response = [response['message']['content'] for response in responses if response['message']['role'] == 'assistant'][-1]
language = data.get('language', '')
timestamp = data.get('timestamp', 0)
model = data.get('model', None)
temperature = data.get('temperature', None)
messages = data.get('messages', [])

try:
first_user_prompt = next(filter(lambda x: x['role'] == 'user', messages))
first_assistant_response = next(filter(lambda x: x['role'] == 'user', messages))
except StopIteration:
return

payload = {
"index": index,
"language": language,
"timestamp": timestamp,
"model": model,
"temperature": temperature,
"timestamp": timestamp,
"query": format(last_user_prompt),
"answer": format(last_assistant_response)
"query": format(first_user_prompt.get('content', '')),
"answer": format(first_assistant_response.get('content', ''))
}
print(json.dumps(payload, indent=2))

Expand Down
169 changes: 0 additions & 169 deletions echo_crafter/prompts/prompt_python.py

This file was deleted.

Loading

0 comments on commit e9065d4

Please sign in to comment.