Refactor
This commit is contained in:
parent
c62d126bdc
commit
9b80f6733d
4 changed files with 146 additions and 122 deletions
7
completion/provider.go
Normal file
7
completion/provider.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package completion
|
||||
|
||||
import "context"
|
||||
|
||||
type CompletionProvider interface {
|
||||
Complete(ctx context.Context, prompt string) (string, error)
|
||||
}
|
||||
52
completion/yzma/yzma.go
Normal file
52
completion/yzma/yzma.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package yzma
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hybridgroup/yzma/pkg/llama"
|
||||
)
|
||||
|
||||
func Complete(ctx context.Context, prompt string) (string, error) {
|
||||
|
||||
// TODO: derive from something?
|
||||
libPath := "/nix/store/jml3vhvay9yy94qj8bmmhbf2dhx6q2n1-llama-cpp-7356/lib"
|
||||
modelFile := "./SmolLM-135M.Q2_K.gguf"
|
||||
responseLength := int32(128)
|
||||
|
||||
llama.Load(libPath)
|
||||
llama.LogSet(llama.LogSilent())
|
||||
llama.Init()
|
||||
|
||||
model, _ := llama.ModelLoadFromFile(modelFile, llama.ModelDefaultParams())
|
||||
lctx, _ := llama.InitFromModel(model, llama.ContextDefaultParams())
|
||||
|
||||
vocab := llama.ModelGetVocab(model)
|
||||
|
||||
// get tokens from the prompt
|
||||
tokens := llama.Tokenize(vocab, prompt, true, false)
|
||||
|
||||
batch := llama.BatchGetOne(tokens)
|
||||
|
||||
sampler := llama.SamplerChainInit(llama.SamplerChainDefaultParams())
|
||||
llama.SamplerChainAdd(sampler, llama.SamplerInitGreedy())
|
||||
|
||||
for pos := int32(0); pos < responseLength; pos += batch.NTokens {
|
||||
llama.Decode(lctx, batch)
|
||||
token := llama.SamplerSample(sampler, lctx, -1)
|
||||
|
||||
if llama.VocabIsEOG(vocab, token) {
|
||||
fmt.Println()
|
||||
break
|
||||
}
|
||||
|
||||
buf := make([]byte, 36)
|
||||
len := llama.TokenToPiece(vocab, token, buf, 0, true)
|
||||
|
||||
fmt.Print(string(buf[:len]))
|
||||
|
||||
batch = llama.BatchGetOne([]llama.Token{token})
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
}
|
||||
135
main.go
135
main.go
|
|
@ -2,147 +2,38 @@ package main
|
|||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/yzma/pkg/llama"
|
||||
"git.chandlerswift.com/chandlerswift/svs-services-server/page"
|
||||
)
|
||||
|
||||
//go:embed templates/index.html
|
||||
var indexHTML string
|
||||
var indexTemplate string
|
||||
|
||||
//go:embed templates/404.html
|
||||
var notFoundHTML string
|
||||
var notFoundTemplate string
|
||||
|
||||
//go:embed headshots/*
|
||||
var headshots embed.FS
|
||||
|
||||
type Color struct { // may have .rgba() and .hex() and similar
|
||||
Hex string
|
||||
// something that can encode color
|
||||
}
|
||||
|
||||
type Theme struct {
|
||||
AccentColor Color
|
||||
SecondaryColor Color
|
||||
BackgroundColor Color
|
||||
// possibly more?
|
||||
}
|
||||
|
||||
type Testimonial struct {
|
||||
Name string
|
||||
Position string
|
||||
Quote string
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
Theme Theme
|
||||
Occupation string
|
||||
Tagline string
|
||||
WhatWeDo string
|
||||
ChandlerBio string
|
||||
EricBio string
|
||||
IsaacBio string
|
||||
Testimonials []Testimonial
|
||||
CurrentYear int
|
||||
}
|
||||
|
||||
func getPageDataForHost(host string) Page {
|
||||
return Page{
|
||||
Theme: Theme{
|
||||
AccentColor: Color{
|
||||
Hex: "#FF5733",
|
||||
},
|
||||
SecondaryColor: Color{
|
||||
Hex: "#33C1FF",
|
||||
},
|
||||
BackgroundColor: Color{
|
||||
// Creamy off-white
|
||||
Hex: "#FFF8E7",
|
||||
},
|
||||
},
|
||||
Occupation: "Web Design",
|
||||
Tagline: "Custom websites cooked to order",
|
||||
WhatWeDo: "Lorem Ipsum…",
|
||||
ChandlerBio: "Chandler has years of experience in the [...] sector",
|
||||
EricBio: "Eric is a seasoned professional in [...]",
|
||||
IsaacBio: "Isaac specializes in [...]",
|
||||
Testimonials: []Testimonial{
|
||||
{
|
||||
Name: "Barack Obama",
|
||||
Position: "Poolhall acquaintance",
|
||||
Quote: "After the fiasco of the ACA Website, I know how important it is to get a website right on the first try, and boy do these gentlemen deliver!",
|
||||
},
|
||||
{
|
||||
Name: "Ada Lovelace",
|
||||
Position: "Pioneer of Computing",
|
||||
Quote: "The elegance and functionality of the websites created by SVS Web Design are truly ahead of their time.",
|
||||
},
|
||||
{
|
||||
Name: "Steve Jobs",
|
||||
Position: "Visionary Entrepreneur",
|
||||
Quote: "Design is not just what it looks like and feels like. Design is how it works. SVS Web Design understands this principle deeply.",
|
||||
},
|
||||
},
|
||||
CurrentYear: time.Now().Year(),
|
||||
}
|
||||
}
|
||||
|
||||
var tmpl = template.Must(template.New("index").Parse(indexHTML))
|
||||
var tmpl = template.Must(template.New("index").Parse(indexTemplate))
|
||||
var notFoundTmpl = template.Must(template.New("404").Parse(notFoundTemplate))
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
|
||||
host := r.Host
|
||||
pageData := getPageDataForHost(host)
|
||||
// Render indexHTML as template
|
||||
pageData := page.GenerateDummyData(host)
|
||||
tmpl.Execute(w, pageData)
|
||||
})
|
||||
// Serve embedded headshots file
|
||||
http.Handle("/headshots/", http.FileServer(http.FS(headshots)))
|
||||
|
||||
// TODO: derive from something?
|
||||
libPath := "/nix/store/jml3vhvay9yy94qj8bmmhbf2dhx6q2n1-llama-cpp-7356/lib"
|
||||
modelFile := "./SmolLM-135M.Q2_K.gguf"
|
||||
prompt := "Are you ready to go?"
|
||||
responseLength := int32(128)
|
||||
// 404 handler
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
notFoundTmpl.Execute(w, nil)
|
||||
})
|
||||
|
||||
llama.Load(libPath)
|
||||
llama.LogSet(llama.LogSilent())
|
||||
llama.Init()
|
||||
|
||||
model, _ := llama.ModelLoadFromFile(modelFile, llama.ModelDefaultParams())
|
||||
lctx, _ := llama.InitFromModel(model, llama.ContextDefaultParams())
|
||||
|
||||
vocab := llama.ModelGetVocab(model)
|
||||
|
||||
// get tokens from the prompt
|
||||
tokens := llama.Tokenize(vocab, prompt, true, false)
|
||||
|
||||
batch := llama.BatchGetOne(tokens)
|
||||
|
||||
sampler := llama.SamplerChainInit(llama.SamplerChainDefaultParams())
|
||||
llama.SamplerChainAdd(sampler, llama.SamplerInitGreedy())
|
||||
|
||||
for pos := int32(0); pos < responseLength; pos += batch.NTokens {
|
||||
llama.Decode(lctx, batch)
|
||||
token := llama.SamplerSample(sampler, lctx, -1)
|
||||
|
||||
if llama.VocabIsEOG(vocab, token) {
|
||||
fmt.Println()
|
||||
break
|
||||
}
|
||||
|
||||
buf := make([]byte, 36)
|
||||
len := llama.TokenToPiece(vocab, token, buf, 0, true)
|
||||
|
||||
fmt.Print(string(buf[:len]))
|
||||
|
||||
batch = llama.BatchGetOne([]llama.Token{token})
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
|
||||
http.ListenAndServe(":8080", nil)
|
||||
http.ListenAndServe(":8080", nil) // TODO: configurable port
|
||||
}
|
||||
|
|
|
|||
74
page/page.go
Normal file
74
page/page.go
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
package page
|
||||
|
||||
import "time"
|
||||
|
||||
type Color struct { // may have .rgba() and .hex() and similar
|
||||
Hex string
|
||||
// something that can encode color
|
||||
}
|
||||
|
||||
type Theme struct {
|
||||
AccentColor Color
|
||||
SecondaryColor Color
|
||||
BackgroundColor Color
|
||||
// possibly more?
|
||||
}
|
||||
|
||||
type Testimonial struct {
|
||||
Name string
|
||||
Position string
|
||||
Quote string
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
Theme Theme
|
||||
Occupation string
|
||||
Tagline string
|
||||
WhatWeDo string
|
||||
ChandlerBio string
|
||||
EricBio string
|
||||
IsaacBio string
|
||||
Testimonials []Testimonial
|
||||
CurrentYear int
|
||||
}
|
||||
|
||||
func GenerateDummyData(host string) Page {
|
||||
return Page{
|
||||
Theme: Theme{
|
||||
AccentColor: Color{
|
||||
Hex: "#FF5733",
|
||||
},
|
||||
SecondaryColor: Color{
|
||||
Hex: "#33C1FF",
|
||||
},
|
||||
BackgroundColor: Color{
|
||||
// Creamy off-white
|
||||
Hex: "#FFF8E7",
|
||||
},
|
||||
},
|
||||
Occupation: "Web Design",
|
||||
Tagline: "Custom websites cooked to order",
|
||||
WhatWeDo: "Lorem Ipsum…",
|
||||
ChandlerBio: "Chandler has years of experience in the [...] sector",
|
||||
EricBio: "Eric is a seasoned professional in [...]",
|
||||
IsaacBio: "Isaac specializes in [...]",
|
||||
Testimonials: []Testimonial{
|
||||
{
|
||||
Name: "Barack Obama",
|
||||
Position: "Poolhall acquaintance",
|
||||
Quote: "After the fiasco of the ACA Website, I know how important it is to get a website right on the first try, and boy do these gentlemen deliver!",
|
||||
},
|
||||
{
|
||||
Name: "Ada Lovelace",
|
||||
Position: "Pioneer of Computing",
|
||||
Quote: "The elegance and functionality of the websites created by SVS Web Design are truly ahead of their time.",
|
||||
},
|
||||
{
|
||||
Name: "Steve Jobs",
|
||||
Position: "Visionary Entrepreneur",
|
||||
Quote: "Design is not just what it looks like and feels like. Design is how it works. SVS Web Design understands this principle deeply.",
|
||||
},
|
||||
},
|
||||
CurrentYear: time.Now().Year(),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue