Skip to content

Commit

Permalink
❇️ initial commit for release/1.0-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufcanb committed Feb 23, 2024
1 parent 3521e23 commit 5be6314
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 47 deletions.
61 changes: 30 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ tlm is your CLI companion which requires nothing then your workstation. It uses
- 🚀 One liner generation and command explanation.


## Usage

```
$ tlm help
NAME:
tlm - local terminal companion powered by CodeLLaMa.
USAGE:
tlm [global options] command [command options]
VERSION:
1.0-rc1
COMMANDS:
suggest, s suggest a command.
explain, e explain a command.
install, i deploy CodeLLaMa to your system.
config, c configure preferences.
version, v print version.
GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version
```


![](./assets/tlm-in-action.png)

## Installation
Expand All @@ -24,7 +52,7 @@ tlm is your CLI companion which requires nothing then your workstation. It uses
Download latest release;

```bash
curl -fsSL -o tlm https://github.com/yusufcanb/tlm/releases/download/1.0-alpha.0/tlama_1.0-alpha.0_linux_amd64
curl -fsSL -o tlm https://github.com/yusufcanb/tlm/releases/download/1.0-rc1/tlm_1.0-rc1_linux_amd64
```

Make it executable;
Expand Down Expand Up @@ -58,7 +86,7 @@ Finally, follow the instructions to install CodeLLaMa. This will install CodeLLa
Download latest release;

```powershell
Invoke-WebRequest -Uri "https://github.com/yusufcanb/tlm/releases/download/1.0-alpha.0/tlama_1.0-alpha.0_windows_amd64.exe" -OutFile "tlm.exe"
Invoke-WebRequest -Uri "https://github.com/yusufcanb/tlm/releases/download/1.0-rc1/tlama_1.0-rc1_windows_amd64.exe" -OutFile "tlm.exe"
```

> [!TIP]
Expand All @@ -76,35 +104,6 @@ Otherwise, you can use the following command to install CodeLLaMa;

And follow the instructions to install CodeLLaMa. This will install CodeLLaMa on your system and configure tlm to use it.

## Usage

```
> tlm help
NAME:
tlm - terminal intelligence with local language model.
USAGE:
tlm [global options] command [command options]
VERSION:
1.0
DESCRIPTION:
tlm is a command line tool to provide terminal intelligence using CodeLLaMa.
COMMANDS:
suggest, s Suggest a command.
explain, e Explain a command.
install, i Install LLM to your system.
config, c Configure tlama parameters.
version, v Print version.
GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version
```


## Uninstall

Expand Down
6 changes: 3 additions & 3 deletions cmd/Modelfile.explain
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM codellama:7b

PARAMETER temperature 0.1
PARAMETER temperature 0.3
PARAMETER top_p 0.5
PARAMETER top_k 40
PARAMETER seed 1
PARAMETER seed 42

SYSTEM You are software program specifically for Command Line Interface usage. User will ask you some thing that can be convertible to a UNIX or Windows command. You won't provide information or explanations and your output will be just an executable shell command inside three backticks.
SYSTEM You will be explaining given CLI command to user with shortest possible explanation. If given input is not a CLI command, you will respond with "I can only explain CLI commands. Please ask me a CLI command."
2 changes: 1 addition & 1 deletion cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var explainModelfile string
var suggestModelfile string

func main() {
tlm := app.New(version, explainModelfile, suggestModelfile)
tlm := app.New(version, suggestModelfile, explainModelfile)
if err := tlm.App.Run(os.Args); err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func New(version string, suggestModelfile string, explainModelfile string) *TlmA
o, _ := ollama.ClientFromEnvironment()
sug := suggest.New(o, suggestModelfile)
exp := explain.New(o, explainModelfile)
ins := install.New(o)
ins := install.New(o, suggestModelfile, explainModelfile)

cliApp := &cli.App{
Name: "tlm",
Expand Down
103 changes: 99 additions & 4 deletions pkg/install/api.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,116 @@
package install

func (i *Install) createVolume(volumeName string) error {
import (
"context"
"errors"
"fmt"
ollama "github.com/jmorganca/ollama/api"
"os/exec"
"strings"
)

func (i *Install) isVolumeInstalled(volumeName string) bool {
cmd := exec.Command("docker", "volume", "inspect", volumeName)
err := cmd.Run()
return err == nil
}

func (i *Install) isContainerRunning(containerName string) (bool, error) {
cmd := exec.Command("docker", "ps", "-aqf", fmt.Sprintf("name=%s", containerName))
out, err := cmd.Output()
if err != nil {
return false, err
}
return strings.TrimSpace(string(out)) != "", nil
}

func (i *Install) createVolume(volumeName string) error {
if !i.isVolumeInstalled(volumeName) {
cmd := exec.Command("docker", "volume", "create", volumeName)
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}

func (i *Install) installModelfile(modelfile string) error {

return nil
}

func (i *Install) removeContainer() error {
func (i *Install) removeContainer(containerName string) error {
cmd := exec.Command("docker", "rm", "-f", containerName)
if err := cmd.Run(); err != nil {
return err
}
return nil
}

func (i *Install) createContainer(containerName string) error {
return nil
}

func (i *Install) createContainer() error {
func (i *Install) installAndConfigureOllama(form *InstallForm2) error {
fmt.Println("Installing and configuring Ollama...")

// 1. Check if Ollama volume exists
if !i.isVolumeInstalled(form.ollamaVolume) {
fmt.Println("Ollama volume not found. Creating a new volume.")
if err := i.createVolume(form.ollamaVolume); err != nil {
return fmt.Errorf("error creating volume: %v", err)
}
} else {
fmt.Println("Ollama volume found. Using existing volume.")
}

// 2. Check if Ollama container exists
containerExists, err := i.isContainerRunning(i.defaultContainerName)
if err != nil {
return fmt.Errorf("error checking for existing container: %v", err)
}

// 3. Remove old container (if it exists) and recreate
if containerExists {
fmt.Println("Existing Ollama container found. Removing and recreating.")
if err := i.removeContainer(i.defaultContainerName); err != nil {
return fmt.Errorf("error removing existing container: %v", err)
}
}

// 4. Run the Docker command
cmd := exec.Command("docker", "run", "-d", "--gpus=all",
"-v", "ollama:/root/.ollama", "-p", "11434:11434", "--name", "ollama", "ollama/ollama")

stdout, stderr := cmd.CombinedOutput()
if stderr != nil {
return errors.New(stderr.Error())
}
fmt.Println(string(stdout))

// 5. Pull CodeLLaMa if not exists
onProgressResponse := func(res ollama.ProgressResponse) error {
return nil
}

err = i.api.Pull(context.Background(), &ollama.PullRequest{Model: "codellama:7b"}, onProgressResponse)
if err != nil {
return err
}

// 6. Install the modelfile (Suggest)
onModelResponse := func(res ollama.ProgressResponse) error {
return nil
}

err = i.api.Create(context.Background(), &ollama.CreateRequest{Model: "suggest:7b", Modelfile: form.suggestModelfile}, onModelResponse)
if err != nil {
return err
}

// 7. Install the modelfile (Suggest)
err = i.api.Create(context.Background(), &ollama.CreateRequest{Model: "explain:7b", Modelfile: form.explainModelfile}, onModelResponse)
if err != nil {
return err
}
return nil
}
22 changes: 21 additions & 1 deletion pkg/install/cli.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
package install

import (
"context"
"github.com/urfave/cli/v2"
)

func (i *Install) Action(c *cli.Context) error {
return NewInstallForm2().Run()
var err error
var version string

version, err = i.api.Version(context.Background())
if err != nil {
version = ""
}

form := NewInstallForm2(version, i.suggestModelfile, i.explainModelfile)
err = form.Run()
if err != nil {
return err
}

err = i.installAndConfigureOllama(form)
if err != nil {
return err
}

return nil
}

func (i *Install) Command() *cli.Command {
Expand Down
25 changes: 21 additions & 4 deletions pkg/install/form.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package install

import (
"fmt"
"github.com/charmbracelet/huh"
)

type InstallForm2 struct {
form *huh.Form

redeploy bool
version string
gpuEnabled bool
ollamaImage string
ollamaVolume string

suggestModelfile string
explainModelfile string
}

func (i *InstallForm2) Run() error {
Expand Down Expand Up @@ -39,7 +44,7 @@ func (i *InstallForm2) Run() error {
var c bool
err := huh.NewConfirm().
Title("Redeploy").
Description("An Ollama instance is running on 11434, redeploy?").
Description(fmt.Sprintf("Ollama (%s) instance is running on 11434, redeploy?", i.version)).
Affirmative("Proceed").
Negative("Abort").
Value(&c).
Expand All @@ -56,18 +61,30 @@ func (i *InstallForm2) Run() error {

return nil

} else {
return i.form.WithTheme(huh.ThemeBase16()).Run()
}

return nil
}

func NewInstallForm2() *InstallForm2 {
func NewInstallForm2(version string, suggestModelfile string, explainModelfile string) *InstallForm2 {
ollamaImage := "ollama:latest"
ollamaVolume := "ollama"

var redeploy bool
if version != "" {
redeploy = true
} else {
redeploy = false
}

return &InstallForm2{
ollamaImage: ollamaImage,
ollamaVolume: ollamaVolume,
redeploy: true,
redeploy: redeploy,

version: version,
suggestModelfile: suggestModelfile,
explainModelfile: explainModelfile,
}
}
13 changes: 11 additions & 2 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ import (

type Install struct {
api *ollama.Client

defaultContainerName string

suggestModelfile string
explainModelfile string
}

func New(api *ollama.Client) *Install {
func New(api *ollama.Client, suggestModelfile string, explainModelfile string) *Install {
return &Install{
api: api,
api: api,
defaultContainerName: "ollama",

suggestModelfile: suggestModelfile,
explainModelfile: explainModelfile,
}
}

0 comments on commit 5be6314

Please sign in to comment.