embeddings: added embedding command for cl (#12795)

Co-authored-by: A-Akhil <akhilrahul70@gmail.com>

This PR introduces a new ollama embed command that allows users to generate embeddings directly from the command line.

Added ollama embed MODEL [TEXT...] command for generating text embeddings
Supports both direct text arguments and stdin piping for scripted workflows

Outputs embeddings as JSON arrays (one per line)
This commit is contained in:
nicole pardal
2025-11-05 11:58:03 -08:00
committed by GitHub
parent 6aa7283076
commit 1ca608bcd1
4 changed files with 416 additions and 1 deletions

View File

@@ -322,6 +322,44 @@ func StopHandler(cmd *cobra.Command, args []string) error {
return nil
}
func generateEmbedding(cmd *cobra.Command, modelName, input string, keepAlive *api.Duration, truncate *bool, dimensions int) error {
client, err := api.ClientFromEnvironment()
if err != nil {
return err
}
req := &api.EmbedRequest{
Model: modelName,
Input: input,
}
if keepAlive != nil {
req.KeepAlive = keepAlive
}
if truncate != nil {
req.Truncate = truncate
}
if dimensions > 0 {
req.Dimensions = dimensions
}
resp, err := client.Embed(cmd.Context(), req)
if err != nil {
return err
}
if len(resp.Embeddings) == 0 {
return errors.New("no embeddings returned")
}
output, err := json.Marshal(resp.Embeddings[0])
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
func RunHandler(cmd *cobra.Command, args []string) error {
interactive := true
@@ -386,7 +424,11 @@ func RunHandler(cmd *cobra.Command, args []string) error {
return err
}
prompts = append([]string{string(in)}, prompts...)
// Only prepend stdin content if it's not empty
stdinContent := string(in)
if len(stdinContent) > 0 {
prompts = append([]string{stdinContent}, prompts...)
}
opts.ShowConnect = false
opts.WordWrap = false
interactive = false
@@ -452,6 +494,29 @@ func RunHandler(cmd *cobra.Command, args []string) error {
opts.ParentModel = info.Details.ParentModel
// Check if this is an embedding model
isEmbeddingModel := slices.Contains(info.Capabilities, model.CapabilityEmbedding)
// If it's an embedding model, handle embedding generation
if isEmbeddingModel {
if opts.Prompt == "" {
return errors.New("embedding models require input text. Usage: ollama run " + name + " \"your text here\"")
}
// Get embedding-specific flags
var truncate *bool
if truncateFlag, err := cmd.Flags().GetBool("truncate"); err == nil && cmd.Flags().Changed("truncate") {
truncate = &truncateFlag
}
dimensions, err := cmd.Flags().GetInt("dimensions")
if err != nil {
return err
}
return generateEmbedding(cmd, name, opts.Prompt, opts.KeepAlive, truncate, dimensions)
}
if interactive {
if err := loadOrUnloadModel(cmd, &opts); err != nil {
var sErr api.AuthorizationError
@@ -1684,6 +1749,8 @@ func NewCLI() *cobra.Command {
runCmd.Flags().String("think", "", "Enable thinking mode: true/false or high/medium/low for supported models")
runCmd.Flags().Lookup("think").NoOptDefVal = "true"
runCmd.Flags().Bool("hidethinking", false, "Hide thinking output (if provided)")
runCmd.Flags().Bool("truncate", false, "For embedding models: truncate inputs exceeding context length (default: true). Set --truncate=false to error instead")
runCmd.Flags().Int("dimensions", 0, "Truncate output embeddings to specified dimension (embedding models only)")
stopCmd := &cobra.Command{
Use: "stop MODEL",