From 2c93a4b99cd5bd3de20d9ed1efe115fe277da96c Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Thu, 4 May 2023 20:11:38 -0500 Subject: Add README --- README.md | 103 +++++++++++++++++++++++++++++++++++++++++++++++ src/gpt_chat_cli/gcli.py | 2 +- 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a7d5d9f..72d7986 100644 --- a/README.md +++ b/README.md @@ -1 +1,104 @@ gpt-chat-cli +------------ + +A simple ChatGPT CLI. + +### Examples: + +``` +[#] gpt-chat-cli "Tell me about Joseph Weizenbaum" + +[gpt-3.5-turbo-0301] Joseph Weizenbaum (1923-2008) was a German-American computer scientist and philosopher who is best known for creating the ELIZA program, one of the earliest examples of natural language processing. ELIZA was a computer program that simulated a psychotherapist and could engage in a conversation with a human user by using simple pattern-matching techniques to respond to the user's input. + +Weizenbaum was born in Berlin, Germany, and fled the country with his family in 1936 to escape Nazi persecution. He later studied mathematics and physics at Wayne State University in Detroit, Michigan, and received a Ph.D. in computer science from the Massachusetts Institute of Technology (MIT) in 1956. + +In addition to his work on ELIZA, Weizenbaum was also a prominent critic of artificial intelligence and the use of computers in society. He argued that computers could not truly understand human language or emotions, and that the increasing reliance on technology could lead to dehumanization and loss of personal autonomy. + +Weizenbaum's writings and lectures on the social and ethical implications of technology have had a lasting impact on the field of computer science and continue to be studied and debated today. +``` + +``` +[#] gpt-chat-cli -n 2 "Write Rust code to find the variance of a list of floating point numbers" +[gpt-3.5-turbo-0301 1/2] Here's the Rust code to find the variance of a list of floating point numbers: + +fn variance(numbers: &[f64]) -> Option { + let n = numbers.len() as f64; + if n <= 1.0 { + return None; + } + let mean = numbers.iter().sum::() / n; + let variance = numbers.iter().map(|x| (x - mean).powi(2)).sum::() / (n - 1.0); + Some(variance) +} + +This function takes a slice of floating point numbers as input and returns an `Option` which is the variance of the numbers. If the length of the slice is less than or equal to 1, the function returns `None` since variance cannot be calculated for such cases. + +The function calculates the mean of the numbers by summing up all the numbers and dividing by the length of the slice. It then calculates the variance by iterating over the numbers, subtracting the mean from each number, squaring the result and summing up all the squares. Finally, it divides the sum of squares by (n-1) to get the variance. + +Note that we use `powi(2)` instead of `powf(2.0)` to avoid floating point errors. + +[gpt-3.5-turbo-0301 2/2] Here's one possible implementation: + +fn variance(data: &[f64]) -> Option { + let n = data.len() as f64; + if n <= 1.0 { + return None; // need at least 2 data points + } + let mean = data.iter().sum::() / n; + let variance = data.iter().map(|x| (x - mean).powi(2)).sum::() / (n - 1.0); + Some(variance) +} + +This function takes a slice of `f64` values as input, and returns an `Option` which is `None` if the input slice has fewer than 2 elements (since we need at least 2 data points to compute a variance), or the computed variance otherwise. + +The variance is computed using the formula: + +variance = sum((x - mean)^2) / (n - 1) + +where `x` is each data point, `mean` is the mean of the data, `n` is the number of data points, and `^2` means "squared". The `map()` method is used to compute the squared differences `(x - mean)^2`, and the `sum()` method is used to add them up. Finally, the result is divided by `n - 1` to get the unbiased sample variance (which is what we usually want). + +Note that this implementation uses the `powi()` method to compute the squared differences, which is faster than using the `powf()` method with a constant exponent of 2. +``` + +### Usage: + +``` +usage: gpt-chat-cli [-h] [-m MODEL] [-t TEMPERATURE] [-f FREQUENCY_PENALTY] [-p PRESENCE_PENALTY] [-k MAX_TOKENS] [-s TOP_P] [-n N_COMPLETIONS] + [--adornments {AutoDetectedOption.ON,AutoDetectedOption.OFF,AutoDetectedOption.AUTO}] [--color {AutoDetectedOption.ON,AutoDetectedOption.OFF,AutoDetectedOption.AUTO}] + message + +positional arguments: + message The contents of the message. When used in chat mode, this is the initial message if provided. + +options: + -h, --help show this help message and exit + -m MODEL, --model MODEL + ID of the model to use + -t TEMPERATURE, --temperature TEMPERATURE + What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + -f FREQUENCY_PENALTY, --frequency-penalty FREQUENCY_PENALTY + Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. + -p PRESENCE_PENALTY, --presence-penalty PRESENCE_PENALTY + Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. + -k MAX_TOKENS, --max-tokens MAX_TOKENS + The maximum number of tokens to generate in the chat completion. Defaults to 2048. + -s TOP_P, --top-p TOP_P + An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens + comprising the top 10% probability mass are considered. + -n N_COMPLETIONS, --n-completions N_COMPLETIONS + How many chat completion choices to generate for each input message. + --adornments {AutoDetectedOption.ON,AutoDetectedOption.OFF,AutoDetectedOption.AUTO} + Show adornments to indicate the model and response. Can be set to 'on', 'off', or 'auto'. + --color {AutoDetectedOption.ON,AutoDetectedOption.OFF,AutoDetectedOption.AUTO} + Set color to 'on', 'off', or 'auto'. +``` + +### Features: + +- [x] Streaming real-time output +- [x] Color and adornments +- [x] Support for multiple completions +- [x] Support for any model which is supported through the chat completions API. [See model endpoint compatibility.](https://platform.openai.com/docs/models/model-endpoint-compatibility) +- [x] Capability to modify parameters including temperature, frequency penalty, presence penalty, top p, and the maximum number of tokens emitted. + +> This is a WIP. diff --git a/src/gpt_chat_cli/gcli.py b/src/gpt_chat_cli/gcli.py index ded6d6c..dd7dbbb 100644 --- a/src/gpt_chat_cli/gcli.py +++ b/src/gpt_chat_cli/gcli.py @@ -105,7 +105,7 @@ def print_streamed_response(args : argparse.Namespace, completion : OpenAIChatRe if not prompt_printed and ADORNMENTS: res_indicator = '' if N_COMPLETIONS == 1 else \ - f' {display_idx + 1}/{n_completions}' + f' {display_idx + 1}/{N_COMPLETIONS}' PROMPT = f'[{COLOR_CODE.GREEN}{update.model}{COLOR_CODE.RESET}{COLOR_CODE.RED}{res_indicator}{COLOR_CODE.RESET}]' prompt_printed = True print(PROMPT, end=' ', flush=True) -- cgit v1.2.3