uh_halp
uh_halp.backend.openai
For calling the OpenAI completion model.
query
def query(model: str, system_prompt: str, user_prompt: str, key: str) -> str
Calls the OpenAI API with the query from the command line.
uh_halp.backend
uh_halp.backend.web_api
Generic web API functions
query
def query(endpoint: str, method: str, query: str, post_data: str,
headers: dict[str, str], response_path: list[str | int]) -> str
Standard web API request.
extract
def extract(response: requests.Response,
response_path: list[str | int]) -> str
If there’s a response_path list, treat the response as JSON and the list as the path to the key we want. Use strings for property names, ints for list indices, and you can use negative indices to reference the end of a list (i.e. -1 for the last item.)
uh_halp.backend.util
clean
def clean(response: str) -> str
Cleans the response from the API, because we can’t trust it to play nicely.
uh_halp.keys
prompt_key
def prompt_key(service: str) -> str
Prompt the user for a key for the given service, returning it as a string
load_keys
def load_keys(key_file: str = KEY_FILE) -> dict[str, str]
Load the keys from disk and return them
save_keys
def save_keys(keys: dict, key_file: str = KEY_FILE)
Save all the keys to disk
save_key
def save_key(service: str, key: str, key_file: str = KEY_FILE)
Save the service’s key to disk
load_key
def load_key(service: str, key_file: str = KEY_FILE) -> str | None
Load the key from disk, returning it.
get_key
def get_key(service: str) -> str
Loads the service’s key from ~/.uh-keys, or prompts for it and saves it there.
uh_halp.config
reset_config
def reset_config(path: str = CONFIG_FILE)
Resets the config file to the template
save_config
def save_config(config, path: str = CONFIG_FILE)
Saves the config file
get_config
def get_config(path: str = CONFIG_FILE)
Reads the config file, creating it from template if it doesn’t exist
uh_halp.vars
Gets template variables that can be inserted into the context.
get_os
def get_os() -> str
Returns a string describing the OS.
get_shell
def get_shell() -> str
Returns the user’s shell executable name.
apply_vars
def apply_vars(vars: dict, template: Any) -> Any
Replaces strings in a json-style object tree. Use {var}
get_vars
def get_vars() -> dict
Gets variables that can be replaced with templates. To use one, reference it like {var} in any string field.
Current keys are:
- shell: The user’s shell executable name.
- os: A string describing the OS and its version.
- query: The query string.
- pwd: The current working directory.
- key: the secret key for the service (blank here, requested interactively if needed)
uh_halp.main
Contains main entrypoint
show_help
def show_help()
uh command line help on the command line
Usage: uh [query]
Example:
user@host$ uh get the time date +%H:%M:%S
config is in ~/.uh_config. Each field is parsed with mustache. Access tokens go in ~/.uh-keys
get_func
def get_func(module_name: str, func_name="query") -> Callable
Returns a function in the given module name.
main
def main() -> int
Entrypoint for the CLI.