Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
wrochow authored Dec 21, 2024
2 parents 9a9990f + 6d00405 commit ac1fc4b
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
> [!NOTE]
> November 8, 2024
>
> - **Multimodal Support**: You can now us `-a` (attachment) for Multimodal submissions to OpenAI models that support it. Example: `fabric -a https://path/to/image "Give me a description of this image."`
> - **Multimodal Support**: You can now use `-a` (attachment) for Multimodal submissions to OpenAI models that support it. Example: `fabric -a https://path/to/image "Give me a description of this image."`
## What and why

Expand Down
68 changes: 68 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# YAML Configuration Support

## Overview
Fabric now supports YAML configuration files for commonly used options. This allows users to persist settings and share configurations across multiple runs.

## Usage
Use the `--config` flag to specify a YAML configuration file:
```bash
fabric --config ~/.config/fabric/config.yaml "Tell me about APIs"
```

## Configuration Precedence
1. CLI flags (highest priority)
2. YAML config values
3. Default values (lowest priority)

## Supported Configuration Options
```yaml
# Model selection
model: gpt-4
modelContextLength: 4096

# Model parameters
temperature: 0.7
topp: 0.9
presencepenalty: 0.0
frequencypenalty: 0.0
seed: 42

# Pattern selection
pattern: analyze # Use pattern name or filename

# Feature flags
stream: true
raw: false
```
## Rules and Behavior
- Only long flag names are supported in YAML (e.g., `temperature` not `-t`)
- CLI flags always override YAML values
- Unknown YAML declarations are ignored
- If a declaration appears multiple times in YAML, the last one wins
- The order of YAML declarations doesn't matter

## Type Conversions
The following string-to-type conversions are supported:
- String to number: `"42"` → `42`
- String to float: `"42.5"` → `42.5`
- String to boolean: `"true"` → `true`

## Example Config
```yaml
# ~/.config/fabric/config.yaml
model: gpt-4
temperature: 0.8
pattern: analyze
stream: true
topp: 0.95
presencepenalty: 0.1
frequencypenalty: 0.2
```

## CLI Override Example
```bash
# Override temperature from config
fabric --config ~/.config/fabric/config.yaml --temperature 0.9 "Query"
```

6 changes: 6 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func Cli(version string) (err error) {
return
}

if currentFlags.ServeOllama {
registry.ConfigureVendors()
err = restapi.ServeOllama(registry, currentFlags.ServeAddress, version)
return
}

if currentFlags.UpdatePatterns {
err = registry.PatternsLoader.PopulateDB()
return
Expand Down
21 changes: 21 additions & 0 deletions cli/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#this is an example yaml config file for fabric

# use fabric pattern names
pattern: ai

# or use a filename
# pattern: ~/testpattern.md

model: phi3:latest

# for models that support context length
modelContextLength: 2048

frequencypenalty: 0.5
presencepenalty: 0.5
topp: 0.67
temperature: 0.88
seed: 42

stream: true
raw: false
58 changes: 30 additions & 28 deletions cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"reflect"
"strconv"
"strings"

"github.com/jessevdk/go-flags"
Expand Down Expand Up @@ -60,6 +61,7 @@ type Flags struct {
InputHasVars bool `long:"input-has-vars" description:"Apply variables to user input"`
DryRun bool `long:"dry-run" description:"Show what would be sent to the model without actually sending it"`
Serve bool `long:"serve" description:"Serve the Fabric Rest API"`
ServeOllama bool `long:"serveOllama" description:"Serve the Fabric Rest API with ollama endpoints"`
ServeAddress string `long:"address" description:"The address to bind the REST API" default:":8080"`
Config string `long:"config" description:"Path to YAML config file"`
Version bool `long:"version" description:"Print current version"`
Expand All @@ -77,7 +79,7 @@ func Debugf(format string, a ...interface{}) {
func Init() (ret *Flags, err error) {
// Track which yaml-configured flags were set on CLI
usedFlags := make(map[string]bool)
args := os.Args[1:]
yamlArgsScan := os.Args[1:]

// Get list of fields that have yaml tags, could be in yaml config
yamlFields := make(map[string]bool)
Expand All @@ -90,7 +92,7 @@ func Init() (ret *Flags, err error) {
}

// Scan args for that are provided by cli and might be in yaml
for _, arg := range args {
for _, arg := range yamlArgsScan {
if strings.HasPrefix(arg, "--") {
flag := strings.TrimPrefix(arg, "--")
if i := strings.Index(flag, "="); i > 0 {
Expand All @@ -106,7 +108,8 @@ func Init() (ret *Flags, err error) {
// Parse CLI flags first
ret = &Flags{}
parser := flags.NewParser(ret, flags.Default)
if _, err = parser.Parse(); err != nil {
var args []string
if args, err = parser.Parse(); err != nil {
return nil, err
}

Expand Down Expand Up @@ -148,6 +151,7 @@ func Init() (ret *Flags, err error) {
info, _ := os.Stdin.Stat()
pipedToStdin := (info.Mode() & os.ModeCharDevice) == 0

// Append positional arguments to the message (custom message)
if len(args) > 0 {
ret.Message = AppendMessage(ret.Message, args[len(args)-1])
}
Expand All @@ -164,38 +168,36 @@ func Init() (ret *Flags, err error) {
}

func assignWithConversion(targetField, sourceField reflect.Value) error {
switch targetField.Kind() {
case reflect.Float64:
if sourceField.Kind() == reflect.Int || sourceField.Kind() == reflect.Float32 {
targetField.SetFloat(float64(sourceField.Convert(reflect.TypeOf(float64(0))).Float()))
Debugf("Converted field %s : %v\n", targetField.Type(), targetField.Interface())
return nil
}
case reflect.Int:
if sourceField.Kind() == reflect.Float64 || sourceField.Kind() == reflect.Float32 {
targetField.SetInt(int64(sourceField.Convert(reflect.TypeOf(int64(0))).Int()))
Debugf("Converted field %s : %v\n", targetField.Type(), targetField.Interface())
return nil
}
case reflect.String:
if sourceField.Kind() == reflect.Interface {
if str, ok := sourceField.Interface().(string); ok {
targetField.SetString(str)
Debugf("Converted field %s : %v\n", targetField.Type(), targetField.Interface())
// Handle string source values
if sourceField.Kind() == reflect.String {
str := sourceField.String()
switch targetField.Kind() {
case reflect.Int:
// Try parsing as float first to handle "42.9" -> 42
if val, err := strconv.ParseFloat(str, 64); err == nil {
targetField.SetInt(int64(val))
return nil
}
}
case reflect.Bool:
if sourceField.Kind() == reflect.Interface {
if b, ok := sourceField.Interface().(bool); ok {
targetField.SetBool(b)
Debugf("Converted field %s : %v\n", targetField.Type(), targetField.Interface())
// Try direct int parse
if val, err := strconv.ParseInt(str, 10, 64); err == nil {
targetField.SetInt(val)
return nil
}
case reflect.Float64:
if val, err := strconv.ParseFloat(str, 64); err == nil {
targetField.SetFloat(val)
return nil
}
case reflect.Bool:
if val, err := strconv.ParseBool(str); err == nil {
targetField.SetBool(val)
return nil
}
}
return fmt.Errorf("cannot convert string %q to %v", str, targetField.Kind())
}

return fmt.Errorf("unsupported conversion: %s to %s", sourceField.Type(), targetField.Type())
return fmt.Errorf("unsupported conversion from %v to %v", sourceField.Kind(), targetField.Kind())
}

func loadYAMLConfig(configPath string) (*Flags, error) {
Expand Down
4 changes: 2 additions & 2 deletions patterns/analyze_answers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Subject: Machine Learning
```

# Example run un bash:
# Example run bash:

Copy the input query to the clipboard and execute the following command:

``` bash
```bash
xclip -selection clipboard -o | fabric -sp analize_answers
```

Expand Down
6 changes: 3 additions & 3 deletions patterns/create_quiz/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Learning questionnaire generation

This pattern generates questions to help a learner/student review the main concepts of the learning objectives provided.
This pattern generates questions to help a learner/student review the main concepts of the learning objectives provided.

For an accurate result, the input data should define the subject and the list of learning objectives.

Expand All @@ -17,11 +17,11 @@ Learning Objectives:
* Define unsupervised learning
```

# Example run un bash:
# Example run bash:

Copy the input query to the clipboard and execute the following command:

``` bash
```bash
xclip -selection clipboard -o | fabric -sp create_quiz
```

Expand Down
6 changes: 3 additions & 3 deletions patterns/summarize_paper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ This pattern generates a summary of an academic paper based on the provided text

Copy the paper text to the clipboard and execute the following command:

``` bash
```bash
pbpaste | fabric --pattern summarize_paper
```

or

``` bash
```bash
pbpaste | summarize_paper
```

# Example output:

``` markdown
```markdown
### Title and authors of the Paper:
**Internet of Paint (IoP): Channel Modeling and Capacity Analysis for Terahertz Electromagnetic Nanonetworks Embedded in Paint**
Authors: Lasantha Thakshila Wedage, Mehmet C. Vuran, Bernard Butler, Yevgeni Koucheryavy, Sasitharan Balasubramaniam
Expand Down
2 changes: 1 addition & 1 deletion patterns/translate/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Take a step back, and breathe deeply and think step by step about how to achieve

- The original format of the input must remain intact.

- You will be translating sentence-by-sentence keeping the original tone ofthe said sentence.
- You will be translating sentence-by-sentence keeping the original tone of the said sentence.

- You will not be manipulate the wording to change the meaning.

Expand Down
2 changes: 1 addition & 1 deletion pkgs/fabric/version.nix
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"..1"
"1.4.124"
Loading

0 comments on commit ac1fc4b

Please sign in to comment.