Browse Source

send all debug, info, and debug messages to stderr

pull/217/head
Meir Michanie 1 year ago
parent
commit
97efaa80fc
  1. 23
      installer/client/cli/fabric.py
  2. 70
      installer/client/cli/utils.py

23
installer/client/cli/fabric.py

@ -1,4 +1,4 @@
from .utils import Standalone, Update, Setup, Alias
from .utils import Standalone, Update, Setup, Alias, eprint
import argparse
import sys
import os
@ -73,7 +73,7 @@ def main():
Alias().execute()
sys.exit()
if not os.path.exists(env_file) or not os.path.exists(config_patterns_directory):
print("Please run --setup to set up your API key and download patterns.")
eprint("Please run --setup to set up your API key and download patterns.")
sys.exit()
if not os.path.exists(config_patterns_directory):
Update()
@ -81,6 +81,7 @@ def main():
sys.exit()
if args.changeDefaultModel:
Setup().default_model(args.changeDefaultModel)
eprint(f"Default model changed to {args.changeDefaultModel}")
sys.exit()
if args.agents:
# Handle the agents logic
@ -99,11 +100,11 @@ def main():
sys.exit()
if args.context:
if not os.path.exists(os.path.join(config, "context.md")):
print("Please create a context.md file in ~/.config/fabric")
eprint("Please create a context.md file in ~/.config/fabric")
sys.exit()
if args.clear:
Setup().clean_env()
print("Model choice cleared. please restart your session to use the --model flag.")
eprint("Model choice cleared. please restart your session to use the --model flag.")
sys.exit()
standalone = Standalone(args, args.pattern)
if args.list:
@ -113,19 +114,19 @@ def main():
print(d)
sys.exit()
except FileNotFoundError:
print("No patterns found")
eprint("No patterns found")
sys.exit()
if args.listmodels:
gptmodels, localmodels, claudemodels = standalone.fetch_available_models()
print("GPT Models:")
eprint("GPT Models:")
for model in gptmodels:
print(model)
print("\nLocal Models:")
eprint(model)
eprint("\nLocal Models:")
for model in localmodels:
print(model)
print("\nClaude Models:")
eprint(model)
eprint("\nClaude Models:")
for model in claudemodels:
print(model)
eprint(model)
sys.exit()
if args.text is not None:
text = args.text

70
installer/client/cli/utils.py

@ -16,6 +16,9 @@ config_directory = os.path.expanduser("~/.config/fabric")
env_file = os.path.join(config_directory, ".env")
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
class Standalone:
def __init__(self, args, pattern="", env_file="~/.config/fabric/.env"):
""" Initialize the class with the provided arguments and environment file.
@ -37,7 +40,7 @@ class Standalone:
env_file = os.path.expanduser(env_file)
load_dotenv(env_file)
if "OPENAI_API_KEY" not in os.environ:
print("Error: OPENAI_API_KEY not found in environment variables.")
eprint("Error: OPENAI_API_KEY not found in environment variables.")
self.client = OpenAI()
else:
api_key = os.environ['OPENAI_API_KEY']
@ -141,7 +144,7 @@ class Standalone:
system_message = {"role": "system", "content": system}
messages = [system_message, user_message]
except FileNotFoundError:
print("pattern not found")
eprint("pattern not found")
return
else:
if context:
@ -181,17 +184,17 @@ class Standalone:
sys.stdout.flush()
except Exception as e:
if "All connection attempts failed" in str(e):
print(
eprint(
"Error: cannot connect to llama2. If you have not already, please visit https://ollama.com for installation instructions")
if "CLAUDE_API_KEY" in str(e):
print(
eprint(
"Error: CLAUDE_API_KEY not found in environment variables. Please run --setup and add the key")
if "overloaded_error" in str(e):
print(
eprint(
"Error: Fabric is working fine, but claude is overloaded. Please try again later.")
else:
print(f"Error: {e}")
print(e)
eprint(f"Error: {e}")
eprint(e)
if self.args.copy:
pyperclip.copy(buffer)
if self.args.output:
@ -227,7 +230,7 @@ class Standalone:
system_message = {"role": "system", "content": system}
messages = [system_message, user_message]
except FileNotFoundError:
print("pattern not found")
eprint("pattern not found")
return
else:
if context:
@ -260,19 +263,19 @@ class Standalone:
f.write(response.choices[0].message.content)
except Exception as e:
if "All connection attempts failed" in str(e):
print(
eprint(
"Error: cannot connect to llama2. If you have not already, please visit https://ollama.com for installation instructions")
if "CLAUDE_API_KEY" in str(e):
print(
eprint(
"Error: CLAUDE_API_KEY not found in environment variables. Please run --setup and add the key")
if "overloaded_error" in str(e):
print(
eprint(
"Error: Fabric is working fine, but claude is overloaded. Please try again later.")
if "Attempted to call a sync iterator on an async stream" in str(e):
print("Error: There is a problem connecting fabric with your local ollama installation. Please visit https://ollama.com for installation instructions. It is possible that you have chosen the wrong model. Please run fabric --listmodels to see the available models and choose the right one with fabric --model <model> or fabric --changeDefaultModel. If this does not work. Restart your computer (always a good idea) and try again. If you are still having problems, please visit https://ollama.com for installation instructions.")
eprint("Error: There is a problem connecting fabric with your local ollama installation. Please visit https://ollama.com for installation instructions. It is possible that you have chosen the wrong model. Please run fabric --listmodels to see the available models and choose the right one with fabric --model <model> or fabric --changeDefaultModel. If this does not work. Restart your computer (always a good idea) and try again. If you are still having problems, please visit https://ollama.com for installation instructions.")
else:
print(f"Error: {e}")
print(e)
eprint(f"Error: {e}")
eprint(e)
def fetch_available_models(self):
@ -299,10 +302,10 @@ class Standalone:
for model in sorted_gpt_models:
gptlist.append(model.get("id"))
else:
print(f"Failed to fetch models: HTTP {response.status_code}")
eprint(f"Failed to fetch models: HTTP {response.status_code}")
sys.exit()
except:
print('No OpenAI API key found. Please run fabric --setup and add the key if you wish to interact with openai')
eprint('No OpenAI API key found. Please run fabric --setup and add the key if you wish to interact with openai')
import ollama
try:
default_modelollamaList = ollama.list()['models']
@ -341,7 +344,7 @@ class Update:
self.pattern_directory = os.path.join(
self.config_directory, "patterns")
os.makedirs(self.pattern_directory, exist_ok=True)
print("Updating patterns...")
eprint("Updating patterns...")
self.update_patterns() # Start the update process immediately
def update_patterns(self):
@ -358,9 +361,9 @@ class Update:
if os.path.exists(self.pattern_directory):
shutil.rmtree(self.pattern_directory)
shutil.copytree(patterns_source_path, self.pattern_directory)
print("Patterns updated successfully.")
eprint("Patterns updated successfully.")
else:
print("Patterns folder not found in the downloaded zip.")
eprint("Patterns folder not found in the downloaded zip.")
def download_zip(self, url, save_path):
"""Download the zip file from the specified URL."""
@ -368,13 +371,13 @@ class Update:
response.raise_for_status() # Check if the download was successful
with open(save_path, 'wb') as f:
f.write(response.content)
print("Downloaded zip file successfully.")
eprint("Downloaded zip file successfully.")
def extract_zip(self, zip_path, extract_to):
"""Extract the zip file to the specified directory."""
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_to)
print("Extracted zip file successfully.")
eprint("Extracted zip file successfully.")
return extract_to # Return the path to the extracted contents
@ -461,7 +464,7 @@ class Setup:
for model in sorted_gpt_models:
self.gptlist.append(model.get("id"))
else:
print(f"Failed to fetch models: HTTP {response.status_code}")
eprint(f"Failed to fetch models: HTTP {response.status_code}")
sys.exit()
import ollama
try:
@ -488,8 +491,8 @@ class Setup:
api_key = api_key.strip()
if not os.path.exists(self.env_file) and api_key:
with open(self.env_file, "w") as f:
f.write(f'OPENAI_API_KEY="{api_key}"\n')
print(f"OpenAI API key set to {api_key}")
f.write(f"OPENAI_API_KEY={api_key}\n")
eprint(f"OpenAI API key set to {api_key}")
elif api_key:
# erase the line OPENAI_API_KEY=key and write the new key
with open(self.env_file, "r") as f:
@ -589,7 +592,7 @@ class Setup:
# Write or update the DEFAULT_MODEL in env_file
allModels = self.claudeList + self.fullOllamaList + self.gptlist
if model not in allModels:
print(
eprint(
f"Error: {model} is not a valid model. Please run fabric --listmodels to see the available models.")
sys.exit()
@ -614,10 +617,10 @@ class Setup:
else:
with open(env, "a") as f:
f.write(f'DEFAULT_MODEL={model}\n')
print(f"""Default model changed to {
eprint(f"""Default model changed to {
model}. Please restart your terminal to use it.""")
else:
print("No shell configuration file found.")
eprint("No shell configuration file found.")
def patterns(self):
""" Method to update patterns and exit the system.
@ -637,7 +640,7 @@ class Setup:
None
"""
print("Welcome to Fabric. Let's get started.")
eprint("Welcome to Fabric. Let's get started.")
apikey = input(
"Please enter your OpenAI API key. If you do not have one or if you have already entered it, press enter.\n")
self.api_key(apikey.strip())
@ -645,11 +648,10 @@ class Setup:
"Please enter the OpenAI API Base URL. If you want to use the default, press enter.\n")
self.api_base_url(apiBaseURL.strip())
print("Please enter your claude API key. If you do not have one, or if you have already entered it, press enter.\n")
claudekey = input()
claudekey = input("Please enter your claude API key. If you do not have one, or if you have already entered it, press enter.\n")
self.claude_key(claudekey)
print("Please enter your YouTube API key. If you do not have one, or if you have already entered it, press enter.\n")
youtubekey = input()
youtubekey = input("Please enter your YouTube API key. If you do not have one, or if you have already entered it, press enter.\n")
self.youtube_key(youtubekey)
self.patterns()
self.update_shconfigs()
@ -681,7 +683,7 @@ class Transcribe:
transcript += segment['text'] + " "
return transcript.strip()
except Exception as e:
print("Error:", e)
eprint("Error:", e)
return None
@ -693,7 +695,7 @@ class AgentSetup:
None
"""
print("Welcome to Fabric. Let's get started.")
eprint("Welcome to Fabric. Let's get started.")
browserless = input("Please enter your Browserless API key\n").strip()
serper = input("Please enter your Serper API key\n").strip()

Loading…
Cancel
Save