added context to cli. edit context.md and add -C to add context to your queries
This commit is contained in:
parent
b127fbec15
commit
8f4ab672c6
3
installer/client/cli/context.md
Normal file
3
installer/client/cli/context.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Context
|
||||||
|
|
||||||
|
The user should place a context here
|
@ -5,6 +5,8 @@ import os
|
|||||||
|
|
||||||
|
|
||||||
script_directory = os.path.dirname(os.path.realpath(__file__))
|
script_directory = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
context_file = os.path.join(script_directory, "context.md")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
@ -31,7 +33,8 @@ def main():
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--list", "-l", help="List available patterns", action="store_true"
|
"--list", "-l", help="List available patterns", action="store_true"
|
||||||
)
|
)
|
||||||
parser.add_argument("--update", "-u", help="Update patterns", action="store_true")
|
parser.add_argument(
|
||||||
|
"--update", "-u", help="Update patterns", action="store_true")
|
||||||
parser.add_argument("--pattern", "-p", help="The pattern (prompt) to use")
|
parser.add_argument("--pattern", "-p", help="The pattern (prompt) to use")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--setup", help="Set up your fabric instance", action="store_true"
|
"--setup", help="Set up your fabric instance", action="store_true"
|
||||||
@ -42,6 +45,8 @@ def main():
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--listmodels", help="List all available models", action="store_true"
|
"--listmodels", help="List all available models", action="store_true"
|
||||||
)
|
)
|
||||||
|
parser.add_argument('--context', '-C',
|
||||||
|
help="Use Context file (context.md) to add context to your pattern", action="store_true")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
home_holder = os.path.expanduser("~")
|
home_holder = os.path.expanduser("~")
|
||||||
@ -80,10 +85,19 @@ def main():
|
|||||||
text = args.text
|
text = args.text
|
||||||
else:
|
else:
|
||||||
text = standalone.get_cli_input()
|
text = standalone.get_cli_input()
|
||||||
if args.stream:
|
if args.stream and not args.context:
|
||||||
standalone.streamMessage(text)
|
standalone.streamMessage(text)
|
||||||
|
if args.stream and args.context:
|
||||||
|
with open(context_file, "r") as f:
|
||||||
|
context = f.read()
|
||||||
|
standalone.streamMessage(text, context=context)
|
||||||
|
elif args.context:
|
||||||
|
with open(context_file, "r") as f:
|
||||||
|
context = f.read()
|
||||||
|
standalone.sendMessage(text, context=context)
|
||||||
else:
|
else:
|
||||||
standalone.sendMessage(text)
|
standalone.sendMessage(text)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
@ -13,7 +13,6 @@ config_directory = os.path.expanduser("~/.config/fabric")
|
|||||||
env_file = os.path.join(config_directory, ".env")
|
env_file = os.path.join(config_directory, ".env")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Standalone:
|
class Standalone:
|
||||||
def __init__(self, args, pattern="", env_file="~/.config/fabric/.env"):
|
def __init__(self, args, pattern="", env_file="~/.config/fabric/.env"):
|
||||||
""" Initialize the class with the provided arguments and environment file.
|
""" Initialize the class with the provided arguments and environment file.
|
||||||
@ -49,7 +48,7 @@ class Standalone:
|
|||||||
self.args = args
|
self.args = args
|
||||||
self.model = args.model
|
self.model = args.model
|
||||||
|
|
||||||
def streamMessage(self, input_data: str):
|
def streamMessage(self, input_data: str, context=""):
|
||||||
""" Stream a message and handle exceptions.
|
""" Stream a message and handle exceptions.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -71,6 +70,9 @@ class Standalone:
|
|||||||
if self.pattern:
|
if self.pattern:
|
||||||
try:
|
try:
|
||||||
with open(wisdom_File, "r") as f:
|
with open(wisdom_File, "r") as f:
|
||||||
|
if context:
|
||||||
|
system = context + '\n\n' + f.read()
|
||||||
|
else:
|
||||||
system = f.read()
|
system = f.read()
|
||||||
system_message = {"role": "system", "content": system}
|
system_message = {"role": "system", "content": system}
|
||||||
messages = [system_message, user_message]
|
messages = [system_message, user_message]
|
||||||
@ -109,7 +111,7 @@ class Standalone:
|
|||||||
with open(self.args.output, "w") as f:
|
with open(self.args.output, "w") as f:
|
||||||
f.write(buffer)
|
f.write(buffer)
|
||||||
|
|
||||||
def sendMessage(self, input_data: str):
|
def sendMessage(self, input_data: str, context=""):
|
||||||
""" Send a message using the input data and generate a response.
|
""" Send a message using the input data and generate a response.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -130,6 +132,9 @@ class Standalone:
|
|||||||
if self.pattern:
|
if self.pattern:
|
||||||
try:
|
try:
|
||||||
with open(wisdom_File, "r") as f:
|
with open(wisdom_File, "r") as f:
|
||||||
|
if context:
|
||||||
|
system = context + '\n\n' + f.read()
|
||||||
|
else:
|
||||||
system = f.read()
|
system = f.read()
|
||||||
system_message = {"role": "system", "content": system}
|
system_message = {"role": "system", "content": system}
|
||||||
messages = [system_message, user_message]
|
messages = [system_message, user_message]
|
||||||
@ -162,12 +167,14 @@ class Standalone:
|
|||||||
"Authorization": f"Bearer {self.client.api_key}"
|
"Authorization": f"Bearer {self.client.api_key}"
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.get("https://api.openai.com/v1/models", headers=headers)
|
response = requests.get(
|
||||||
|
"https://api.openai.com/v1/models", headers=headers)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
models = response.json().get("data", [])
|
models = response.json().get("data", [])
|
||||||
# Filter only gpt models
|
# Filter only gpt models
|
||||||
gpt_models = [model for model in models if model.get("id", "").startswith(("gpt"))]
|
gpt_models = [model for model in models if model.get(
|
||||||
|
"id", "").startswith(("gpt"))]
|
||||||
# Sort the models alphabetically by their ID
|
# Sort the models alphabetically by their ID
|
||||||
sorted_gpt_models = sorted(gpt_models, key=lambda x: x.get("id"))
|
sorted_gpt_models = sorted(gpt_models, key=lambda x: x.get("id"))
|
||||||
|
|
||||||
@ -191,7 +198,8 @@ class Standalone:
|
|||||||
if not sys.stdin.isatty(): # Check if input is being piped
|
if not sys.stdin.isatty(): # Check if input is being piped
|
||||||
return sys.stdin.read().strip() # Read piped input
|
return sys.stdin.read().strip() # Read piped input
|
||||||
else:
|
else:
|
||||||
return input("Enter Question: ") # Prompt user for input from console
|
# Prompt user for input from console
|
||||||
|
return input("Enter Question: ")
|
||||||
else:
|
else:
|
||||||
return sys.stdin.read()
|
return sys.stdin.read()
|
||||||
|
|
||||||
@ -209,7 +217,8 @@ class Update:
|
|||||||
|
|
||||||
self.root_api_url = "https://api.github.com/repos/danielmiessler/fabric/contents/patterns?ref=main"
|
self.root_api_url = "https://api.github.com/repos/danielmiessler/fabric/contents/patterns?ref=main"
|
||||||
self.config_directory = os.path.expanduser("~/.config/fabric")
|
self.config_directory = os.path.expanduser("~/.config/fabric")
|
||||||
self.pattern_directory = os.path.join(self.config_directory, "patterns")
|
self.pattern_directory = os.path.join(
|
||||||
|
self.config_directory, "patterns")
|
||||||
os.makedirs(self.pattern_directory, exist_ok=True)
|
os.makedirs(self.pattern_directory, exist_ok=True)
|
||||||
self.update_patterns() # Call the update process from a method.
|
self.update_patterns() # Call the update process from a method.
|
||||||
|
|
||||||
@ -313,7 +322,9 @@ class Update:
|
|||||||
)
|
)
|
||||||
self.progress_bar.close() # Ensure the progress bar is cleaned up properly
|
self.progress_bar.close() # Ensure the progress bar is cleaned up properly
|
||||||
else:
|
else:
|
||||||
print(f"Failed to fetch directory contents due to an HTTP error: {e}")
|
print(
|
||||||
|
f"Failed to fetch directory contents due to an HTTP error: {e}")
|
||||||
|
|
||||||
|
|
||||||
class Setup:
|
class Setup:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -324,7 +335,8 @@ class Setup:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
self.config_directory = os.path.expanduser("~/.config/fabric")
|
self.config_directory = os.path.expanduser("~/.config/fabric")
|
||||||
self.pattern_directory = os.path.join(self.config_directory, "patterns")
|
self.pattern_directory = os.path.join(
|
||||||
|
self.config_directory, "patterns")
|
||||||
os.makedirs(self.pattern_directory, exist_ok=True)
|
os.makedirs(self.pattern_directory, exist_ok=True)
|
||||||
self.env_file = os.path.join(self.config_directory, ".env")
|
self.env_file = os.path.join(self.config_directory, ".env")
|
||||||
|
|
||||||
@ -399,5 +411,3 @@ class Transcribe:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error:", e)
|
print("Error:", e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
14
setup.sh
14
setup.sh
@ -10,6 +10,9 @@ commands=("fabric" "fabric-api" "fabric-webui")
|
|||||||
# List of shell configuration files to update
|
# List of shell configuration files to update
|
||||||
config_files=(~/.bashrc ~/.zshrc ~/.bash_profile)
|
config_files=(~/.bashrc ~/.zshrc ~/.bash_profile)
|
||||||
|
|
||||||
|
# Initialize an empty string to hold the path of the sourced file
|
||||||
|
source_command=""
|
||||||
|
|
||||||
for config_file in "${config_files[@]}"; do
|
for config_file in "${config_files[@]}"; do
|
||||||
# Check if the configuration file exists
|
# Check if the configuration file exists
|
||||||
if [ -f "$config_file" ]; then
|
if [ -f "$config_file" ]; then
|
||||||
@ -29,9 +32,18 @@ for config_file in "${config_files[@]}"; do
|
|||||||
echo "Added alias for $cmd to $config_file."
|
echo "Added alias for $cmd to $config_file."
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
# Set source_command to source the updated file
|
||||||
|
source_command="source $config_file"
|
||||||
else
|
else
|
||||||
echo "$config_file does not exist."
|
echo "$config_file does not exist."
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Please close this terminal window to have new aliases work."
|
# Provide instruction to source the updated file
|
||||||
|
if [ ! -z "$source_command" ]; then
|
||||||
|
echo "To apply the changes, please run the following command in your terminal:"
|
||||||
|
echo "$source_command"
|
||||||
|
else
|
||||||
|
echo "No configuration files were updated. No need to source."
|
||||||
|
fi
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user