Help Setting Up An SSH Command Line Chat Interface To Google Gemini?

2»

Comments

  • Not_OlesNot_Oles Hosting ProviderContent Writer

    @Not_Oles said: But, how can eliben/gemini-cli get a multiline, initial "chat" mode prompt or a multiline subsequent "chat" mode prompt to Gemini via some method which looks like traditional shell-fu: standard input plus a pipe, ":r" to read a file, "!" to escape temporarily to shell magic, or something else other than copy and paste? Thanks!

    Thanks to Eli Bendersky for kindly adding $load <file path> to eliben/gemini-cli! <3

    Please see https://github.com/eliben/gemini-cli/commit/7d4ff164a4702a66cf2f19284d6c11a1a001ecd3

    I have to try it, and I will post again. Might take a while, so please stay tuned. Thanks again, Eli! <3

    I hope everyone gets the servers they want!

  • Not_OlesNot_Oles Hosting ProviderContent Writer

    Yaaay! Exactly what I wanted! Thanks again to Eli Bendersky! <3

    (The long lines wrap nicely on my Chromebook hterm terminal.)

    chronos@penguin:~/gemini-cli$ date
    Mon Jul 22 06:49:42 PM MST 2024
    chronos@penguin:~/gemini-cli$ cat hello.c
    Is the following C code okay?
    
    C
    
    ```
    #include <stdio.h>
    int main() {
       // printf() displays the string inside quotation
       printf("Hello, World!\n");
       return 0;
    }
    ```
    chronos@penguin:~/gemini-cli$ gemini-cli chat --model "gemini-1.5-pro-latest"
    Chatting with gemini-1.5-pro-latest
    Type 'exit' or 'quit' to exit, or '$load <file path>' to load a file
    > $load hello.c
    Yes, the C code you provided is perfectly okay. It's a classic "Hello, World!" program, a standard starting point for learning programming. Here's a breakdown:
    
    **Explanation:**
    
    * **`#include <stdio.h>`:** This line incorporates the standard input/output library (`stdio.h`) into your program.  This library is essential because it provides functions like `printf` for interacting with the console (displaying output).
    
    * **`int main() { ... }`:** This defines the `main` function, which is the heart of every C program. The code inside the curly braces `{}` will be executed when you run your program. The `int` indicates that the `main` function is expected to return an integer value.
    
    * **`// printf() displays the string inside quotation`:** This is a single-line comment. Comments are ignored by the compiler and are there for you to explain your code to yourself or others.
    
    * **`printf("Hello, World!\n");`:** This line does the actual work of printing the text "Hello, World!" to the console.
        * `printf` is a function from `stdio.h` that displays formatted output.
        * `"Hello, World!\n"` is a string literal. The `\n` at the end is a newline character; it tells the console to move the cursor to the beginning of the next line after printing the message.
    
    * **`return 0;`:**  This line ends the `main` function and signals to the operating system that the program executed successfully. A return value of `0` is a standard way to indicate success.
    
    **How to Run This Code:**
    
    1. **Save:** Save the code as a `.c` file (e.g., `hello.c`).
    2. **Compile:** Use a C compiler (like GCC) to compile your code. This creates an executable file:
       ```bash
       gcc hello.c -o hello 
       ```
    3. **Run:** Execute the compiled program:
       ```bash
       ./hello
       ```
    
    You should then see "Hello, World!" printed to your console. 
    > exit
    chronos@penguin:~/gemini-cli$ 
    

    I hope everyone gets the servers they want!

  • Not_OlesNot_Oles Hosting ProviderContent Writer

    Here might be a more relevant example, me asking Gemini questions based on the changes Eli Bendersky just implemented.

    This is the file I prepared to send to Google Gemini.

    chronos@penguin:~/gemini-cli$ cat commit
    eliben/gemini-cli is a command line utility for interacting with
    Google Gemini. eliben/gemini is programmed in the Go programming language.
    
    Here is a summary of the changes in Commit 7d4ff16 on eliben/gemini-cli, 
    "Add '$load' capability to chat mode"
    
    The purpose of these changes are to add the capability to upload a file to 
    Google Gemini when using eliben/gemini-cli in chat mode.
    
    Here is the full URL for the commit:
    https://github.com/eliben/gemini-cli/commit/7d4ff164a4702a66cf2f19284d6c11a1a001ecd3#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5
    
    There are five changed files. These files are listed below together with
    the changes to each file.
    
    I am new to Go. Can you please explain to me the syntax of line 50 and of
    lines 64, 65 and 69, all of which are being added to the file internal/commands/chat.go?
    
    
    README.md
    
    101     +       During the chat, it's possible to ask `gemini-cli` to load a file's contents
    102     +       to the model instead of sending a textual message; Do this with the
    103     +       `$load <path>` command, pointing to an existing file.
    
    
    internal/commands/chat/go
    
    50      -       fmt.Println("Type 'exit' or 'quit' to exit")
    50      +       fmt.Println("Type 'exit' or 'quit' to exit, or '$load <file path>' to load a file")
    
    62      -       iter := session.SendMessageStream(ctx, genai.Text(text))
    62      +       var inputPart genai.Part
    63      +       // Detect a special chat command.
    64      +       if path, found := strings.CutPrefix(text, "$load"); found {
    65      +       part, err := getPartFromFile(strings.TrimSpace(path))
    66      +               if err != nil {
    67      +                       log.Fatalf("error loading file %s: %v", path, err)
    68      +               }
    69      +                       inputPart = part
    70      +               } else {
    71      +                       inputPart = genai.Text(text)
    72      +               }
    73      +
    74      +               iter := session.SendMessageStream(ctx, inputPart)
    
    
    internal/commands/prompt.go
    
    204     -               return nil, fmt.Errorf("invalid image file extension: %s", ext)
    204     +               // Otherwise treat file as text
    205     +               return genai.Text(string(b)), err
    
    
    test/scripts/chat.txtar
    
    5       +       stdin qq2.txt
    6       +       exec gemini-cli chat
    7       +       stdout '20'
    
    13      +
    14      +       -- qq2.txt --
    15      +       $load numbers.txt
    16      +       Which numbers does Joshua consider important?
    17      +       exit
    18      +
    19      +       -- numbers.txt --
    20      +       Hello, my name is Joshua and I consider these numbers important: 20, 99, 1219
    
    
    test/scripts/prompt-multimodal.txtar
    
    18      -       # errors on file that doesn't exist and wrong extension
    18      +       # errors on file that doesn't exist
    
    22      -       ! exec gemini-cli prompt 'describe this' datafiles/nop.txt
    23      -       stderr 'invalid image file'
    
    chronos@penguin:~/gemini-cli$ 
    

    Here is a transcript of this chat with Google Gemini, beginning with the commit file I prepared and continuing with a couple of additional questions.

    At the end of the chat, Google Gemini does not give me a link to the Gemini API Docs.

    Here is a link I found on Google Search for Google Gemini API documentation: https://ai.google.dev/api?lang=go.

    chronos@penguin:~/gemini-cli$ date -u
    Tue Jul 23 03:11:52 AM UTC 2024
    chronos@penguin:~/gemini-cli$ gemini-cli chat --model "gemini-1.5-pro-latest"
    Chatting with gemini-1.5-pro-latest
    Type 'exit' or 'quit' to exit, or '$load <file path>' to load a file
    > $load commit
    Let's break down the Go syntax in `internal/commands/chat.go`.
    
    **Line 50:**
    
    ```Go
    fmt.Println("Type 'exit' or 'quit' to exit, or '$load <file path>' to load a file") 
    ```
    
    This line uses the `fmt` package's `Println` function to print a message to the console. Here's how it works:
    
    * **`fmt.Println(...)`:** This is a function call to the `Println` function within the `fmt` package.  The `fmt` package is part of Go's standard library and provides functions for formatted input and output.
    * **`"Type 'exit' or 'quit' to exit, or '$load <file path>' to load a file"`:** This is a string literal, representing the message that will be printed. It instructs the user on how to interact with the chat interface, including the new `$load` command.
    
    **Lines 64, 65, and 69:**
    
    ```Go
    64:     if path, found := strings.CutPrefix(text, "$load"); found {
    65:             part, err := getPartFromFile(strings.TrimSpace(path))
    69:                     inputPart = part
    ```
    
    These lines handle the `$load` command within the chat loop:
    
    * **Line 64:**
       * **`if path, found := strings.CutPrefix(text, "$load"); found { ... }`:** This line demonstrates a common Go idiom – a short variable declaration (`:=`) within an `if` condition. 
         * **`strings.CutPrefix(text, "$load")`:** This function checks if the user's input (`text`) starts with the prefix `"$load"`. If it does, it returns:
           * `path`: The portion of the string after `"$load "`, representing the file path.
           * `found`:  A boolean value (`true`) indicating the prefix was found.
         * **`if found { ... }`:** The code block inside the `if` statement only executes if the `found` variable is `true` (i.e., the user typed `$load`).
    
    * **Line 65:**
       * **`part, err := getPartFromFile(strings.TrimSpace(path))`:** This line attempts to load the file specified by the user.
         * **`getPartFromFile(...)`:** This is a custom function (not shown in your snippet) that likely reads the contents of the specified file and prepares it for sending to Google Gemini. 
         * **`strings.TrimSpace(path)`:** This ensures any leading or trailing spaces are removed from the `path` before passing it to `getPartFromFile`.
         * **`part, err := ...`:** This assigns the results of `getPartFromFile` to two variables:
           * `part`: Will hold the file content (if successful).
           * `err`: Will hold any error that occurred during the file loading process.
    
    * **Line 69:**
       * **`inputPart = part`:**  If the file was loaded successfully (no error on line 66), this line assigns the loaded `part` to the `inputPart` variable, which will later be sent to Google Gemini.
    
    **In Summary:**
    
    These lines work together to implement the `$load` command in your chat interface. They check if the user typed the command, extract the file path, load the file, and prepare it for sending to Google Gemini. 
    > What needs to be done to a simple text file to prepare it for sending to Google Gemini?
    You're asking a great question about the intersection of file handling and AI API interaction.  Let's break down what needs to happen to prepare a plain text file for sending to Google Gemini. 
    
    **Core Concepts**
    
    1. **File I/O (Input/Output):** You need to read the contents of the text file from your computer's storage and bring it into your Go program's memory.
    
    2. **Data Formatting (Potentially):** Google Gemini, like many AI APIs, expects data in a specific format. This often involves:
       * **String Encoding:** Ensuring the text is in a consistent character encoding (like UTF-8) to prevent issues with special characters.
       * **Structuring (If Needed):** If you're not just sending raw text (e.g., sending a document with sections), you might need to organize the data into a more structured format like JSON.
    
    3. **API Payload:** You'll construct a data structure (likely a JSON object) that conforms to Google Gemini's API requirements. This payload will include the text content from your file and any other necessary parameters.
    
    **Code Example (Illustrative)**
    
    ```Go
    import (
            "encoding/json"
            "fmt"
            "io/ioutil" // For file reading
            "os"
    )
    
    // ... (Other code from your project) 
    
    func prepareTextForGemini(filePath string) ([]byte, error) {
            // 1. Read the File Contents
            fileContent, err := ioutil.ReadFile(filePath) 
            if err != nil {
                    return nil, fmt.Errorf("error reading file: %w", err) 
            }
    
            // 2. (Optional) Additional Formatting 
            //   - If you need to perform any specific string encoding or data
            //     structuring, you would do it here based on Gemini's requirements.
    
            // 3. Construct the API Payload (assuming JSON)
            payload := map[string]interface{}{
                    "text": string(fileContent), // Assuming Gemini expects a "text" field
                    // ... other parameters required by Google Gemini's API ...
            }
    
            // 4. Serialize to JSON 
            jsonPayload, err := json.Marshal(payload) 
            if err != nil {
                    return nil, fmt.Errorf("error creating JSON payload: %w", err)
            }
    
            return jsonPayload, nil 
    }
    ```
    
    **Important Considerations**
    
    * **Google Gemini's API Documentation:**  The exact formatting, required parameters, and data structures will be specified in Google Gemini's official API documentation. Refer to it for the most accurate instructions.
    * **Error Handling:**  Always include robust error handling (like the examples above) when working with files and external APIs to prevent your program from crashing unexpectedly.
    
    Let me know if you have a link to Google Gemini's API docs, and I can help you with more tailored code based on their specific requirements! 
    > Please give me the link to Google Gemini's API docs.
    You're right! I apologize, Google hasn't officially released a public API (and corresponding documentation) for Google Gemini yet. My responses were based on the assumption that you were working with an internal or beta API. 
    
    **What This Means**
    
    * **No Public Access:**  Currently, developers cannot directly interact with Google Gemini programmatically using an API.
    * **Waiting for Updates:** We'll have to wait for Google to make announcements about when/if a public Gemini API will become available.
    
    **Staying Informed**
    
    * **Google AI Blog:** [https://ai.googleblog.com/](https://ai.googleblog.com/) - Google often announces new AI technologies and API releases here.
    * **Google Cloud AI Platform:** [https://cloud.google.com/products/ai](https://cloud.google.com/products/ai) - Check for updates on Google's AI platform.
    
    **Alternative Language Models**
    
    In the meantime, there are other powerful large language models with publicly available APIs that you can explore:
    
    * **OpenAI's API (GPT models):** [https://platform.openai.com/docs/api-reference](https://platform.openai.com/docs/api-reference) 
    * **Hugging Face's Inference API:** [https://huggingface.co/docs/api-inference/index](https://huggingface.co/docs/api-inference/index)
    * **Cohere's API:** [https://docs.cohere.ai/](https://docs.cohere.ai/)
    
    I'll be sure to provide more accurate information once Google Gemini's API documentation is publicly available. 
    > exit  
    chronos@penguin:~/gemini-cli$ 
    

    Thanks yet again to Eli Bendersky for adding $load capability to eliben/gemini-cli! <3 If anyone here wants to help me understand this stuff a little more, that would be great! Any comments or questions or insights? Thanks! <3

    I hope everyone gets the servers they want!

  • Not_OlesNot_Oles Hosting ProviderContent Writer

    eliben/gemini-cli seems to work with tee to make a continuing transcript:

    chronos@penguin:~/gemini-cli$ gemini-cli chat --model "gemini-1.5-pro-latest" | tee -a chat-transcript

    Thanked by (1)vyas

    I hope everyone gets the servers they want!

  • Not_OlesNot_Oles Hosting ProviderContent Writer

    @Not_Oles said:
    eliben/gemini-cli seems to work with tee to make a continuing transcript:

    chronos@penguin:~/gemini-cli$ gemini-cli chat --model "gemini-1.5-pro-latest" | tee -a chat-transcript

    Uh . . . maybe not. tee seems to be capturing what Google Gemini says, but not what I say to Gemini. Maybe screen might work?

    I hope everyone gets the servers they want!

  • @Not_Oles said:
    Uh . . . maybe not. tee seems to be capturing what Google Gemini says, but not what I say to Gemini. Maybe screen might work?

    script will do what you're looking for: https://www.redhat.com/sysadmin/linux-script-command

    Thanked by (1)Not_Oles

    It's pronounced hacker.

  • Not_OlesNot_Oles Hosting ProviderContent Writer
    edited July 29

    @jqr said: script

    Oh! Yeah! script, not screen. screen is the tmux alternative. But both begin with "scr".

    I seem to be making a lot of mistakes lately. Thanks for the correction! It's really very much appreciated! <3

    Thanked by (1)jqr

    I hope everyone gets the servers they want!

  • edited August 5

    @Not_Oles Found something you may like - Its a software that turns whatever you say into the proper command/commands for linux based OS and executes it. Works with API like claude or google gemini. Its called "aichat" on github. Can also just not do -e and type and it will answer whatever. Claude Haiku example below(super cheap API)

    Thanked by (1)Not_Oles
  • Not_OlesNot_Oles Hosting ProviderContent Writer

    Wow! Thanks for the tip!

    Is https://github.com/aichat the Github link? It says no public repositories on that page.

    I hope everyone gets the servers they want!

  • @Not_Oles said:
    Wow! Thanks for the tip!

    Is https://github.com/aichat the Github link? It says no public repositories on that page.

    It is https://github.com/sigoden/aichat - I used the linuxbrew install method.

    Thanked by (1)Not_Oles
  • Not_OlesNot_Oles Hosting ProviderContent Writer
    edited August 6

    @BruhGamer12 Do you know if the two projects are connected or if it is simply that both are using the same "aichat" name?

    I hope everyone gets the servers they want!

  • @Not_Oles said:
    @BruhGamer12 Do you know if the two projects are connected or if it is simply that both are using the same "aichat" name?

    I have no idea sorry.

    Thanked by (1)Not_Oles
Sign In or Register to comment.