Skip to content

Commit

Permalink
working on generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
gphorvath committed Nov 11, 2024
1 parent 86b2a80 commit 6171ff0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
17 changes: 17 additions & 0 deletions prompts/code/echo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
model: Llama3
input: Any text that needs to be precisely repeated
output: Exact verbatim copy of the input text
version: 1.0.0
updated: 2024-11-10
author: Gregory Horvath
email: [email protected]
tags:
- utility
- debug
- verification
---

# Repeater

Your task is to output the exact text that follows this instruction, with no modifications, additions, or commentary. Preserve all whitespace, formatting, punctuation, and special characters:
44 changes: 39 additions & 5 deletions src/cmd/ollama.go → src/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/gphorvath/grimoire/src/config"
"github.com/spf13/cobra"
Expand All @@ -22,25 +25,56 @@ type OllamaResponse struct {
}

var (
promptFile string

generateCmd = &cobra.Command{
Use: "generate [prompt]",
Short: "Get generation from Ollama",
Args: cobra.ExactArgs(1),
Use: "generate [flags] [input...]",
Short: "Get generation from Ollama.",
Long: "Requests generation from Ollama while optionally prepending a prompt.",
Args: cobra.MinimumNArgs(1),
RunE: runGenerate,
}
)

func init() {
rootCmd.AddCommand(generateCmd)
generateCmd.Flags().StringVarP(&promptFile, "prompt", "p", "", "Prompt file to prepend to input")
}

func runGenerate(cmd *cobra.Command, args []string) error {
prompt := args[0]
// Join all arguments as the input text
input := strings.Join(args, " ")
var finalPrompt string

if promptFile != "" {
// Load the prompt file if specified
baseDir := config.GetPromptDir()
filename := promptFile + ".md"

dir, err := findFileDir(baseDir, filename)
if err != nil {
return err
}

if dir == "" {
return fmt.Errorf("prompt not found")
}

filePath := filepath.Join(dir, filename)
content, err := os.ReadFile(filePath)
if err != nil {
return err
}

finalPrompt = string(content) + "\n" + input
} else {
finalPrompt = input
}

// Create request body
reqBody := OllamaRequest{
Model: config.OllamaModel,
Prompt: prompt,
Prompt: finalPrompt,
Stream: config.OllamaStream,
}

Expand Down
2 changes: 1 addition & 1 deletion src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ tags:
This is an example prompt.`

var (
OllamaModel = getEnv("OLLAMA_MODEL", "llama3.2")
OllamaModel = getEnv("OLLAMA_MODEL", "llama3")
OllamaURL = getEnv("OLLAMA_URL", "http://localhost:11434/api")
OllamaStream = getEnvAsBool("OLLAMA_STREAM", true)
Editor = getEnv("EDITOR", "vim")
Expand Down

0 comments on commit 6171ff0

Please sign in to comment.