-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfbe808
commit 1510451
Showing
6 changed files
with
245 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/adrianliechti/llama/pkg/template" | ||
) | ||
|
||
func parseTemplate(val string) (*template.Template, error) { | ||
if val == "" { | ||
return nil, errors.New("empty template") | ||
} | ||
|
||
if data, err := os.ReadFile(val); err == nil { | ||
return template.NewTemplate(string(data)) | ||
} | ||
|
||
return template.NewTemplate(val) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package rag | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/adrianliechti/llama/pkg/chain" | ||
"github.com/adrianliechti/llama/pkg/index" | ||
"github.com/adrianliechti/llama/pkg/provider" | ||
"github.com/adrianliechti/llama/pkg/template" | ||
"github.com/adrianliechti/llama/pkg/text" | ||
) | ||
|
||
var _ chain.Provider = &Chain{} | ||
|
||
type Chain struct { | ||
completer provider.Completer | ||
|
||
template *template.Template | ||
messages []provider.Message | ||
|
||
index index.Provider | ||
|
||
limit *int | ||
temperature *float32 | ||
} | ||
|
||
type Option func(*Chain) | ||
|
||
func New(options ...Option) (*Chain, error) { | ||
c := &Chain{ | ||
template: template.MustTemplate(promptTemplate), | ||
} | ||
|
||
for _, option := range options { | ||
option(c) | ||
} | ||
|
||
if c.completer == nil { | ||
return nil, errors.New("missing completer provider") | ||
} | ||
|
||
if c.index == nil { | ||
return nil, errors.New("missing index provider") | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func WithCompleter(completer provider.Completer) Option { | ||
return func(c *Chain) { | ||
c.completer = completer | ||
} | ||
} | ||
|
||
func WithTemplate(template *template.Template) Option { | ||
return func(c *Chain) { | ||
c.template = template | ||
} | ||
} | ||
|
||
func WithMessages(messages ...provider.Message) Option { | ||
return func(c *Chain) { | ||
c.messages = messages | ||
} | ||
} | ||
|
||
func WithIndex(index index.Provider) Option { | ||
return func(c *Chain) { | ||
c.index = index | ||
} | ||
} | ||
|
||
func WithTemperature(temperature float32) Option { | ||
return func(c *Chain) { | ||
c.temperature = &temperature | ||
} | ||
} | ||
|
||
func (c *Chain) Complete(ctx context.Context, messages []provider.Message, options *provider.CompleteOptions) (*provider.Completion, error) { | ||
if options == nil { | ||
options = new(provider.CompleteOptions) | ||
} | ||
|
||
if options.Temperature == nil { | ||
options.Temperature = c.temperature | ||
} | ||
|
||
message := messages[len(messages)-1] | ||
|
||
if message.Role != provider.MessageRoleUser { | ||
return nil, errors.New("last message must be from user") | ||
} | ||
|
||
query := strings.TrimSpace(message.Content) | ||
|
||
results, err := c.index.Query(ctx, query, &index.QueryOptions{ | ||
Limit: c.limit, | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data := promptData{ | ||
Input: query, | ||
} | ||
|
||
for _, r := range results { | ||
data.Results = append(data.Results, promptResult{ | ||
Title: r.Title, | ||
Content: text.Normalize(r.Content), | ||
Location: r.Location, | ||
|
||
Metadata: r.Metadata, | ||
}) | ||
} | ||
|
||
prompt, err := c.template.Execute(data) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
message = provider.Message{ | ||
Role: provider.MessageRoleUser, | ||
Content: prompt, | ||
} | ||
|
||
messages[len(messages)-1] = message | ||
|
||
result, err := c.completer.Complete(ctx, messages, options) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package rag | ||
|
||
import ( | ||
_ "embed" | ||
) | ||
|
||
var ( | ||
//go:embed prompt.tmpl | ||
promptTemplate string | ||
) | ||
|
||
type promptData struct { | ||
Input string | ||
Results []promptResult | ||
} | ||
|
||
type promptResult struct { | ||
Title string | ||
Content string | ||
Location string | ||
|
||
Metadata map[string]string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{{- if .Results -}} | ||
Use the provided documents to answer questions: | ||
{{ range .Results }} | ||
--- | ||
{{- if .Title }} | ||
Title: {{ .Title }} | ||
{{- end }} | ||
{{- if .Location }} | ||
Location: {{ .Location }} | ||
{{- end }} | ||
{{ .Content }} | ||
{{ end }} | ||
--- | ||
{{- end -}} | ||
|
||
Question: {{ .Input }} |