add session expiration

This commit is contained in:
Michael Yang
2023-07-19 15:00:28 -07:00
parent 3003fc03fc
commit f62a882760
3 changed files with 100 additions and 20 deletions

View File

@@ -92,6 +92,7 @@ import (
"log"
"os"
"strings"
"sync"
"unicode/utf8"
"unsafe"
@@ -107,6 +108,9 @@ type LLM struct {
embd []C.llama_token
cursor int
mu sync.Mutex
gc bool
api.Options
}
@@ -156,6 +160,11 @@ func New(model string, opts api.Options) (*LLM, error) {
}
func (llm *LLM) Close() {
llm.gc = true
llm.mu.Lock()
defer llm.mu.Unlock()
defer C.llama_free_model(llm.model)
defer C.llama_free(llm.ctx)
@@ -163,6 +172,9 @@ func (llm *LLM) Close() {
}
func (llm *LLM) Predict(ctx []int, prompt string, fn func(api.GenerateResponse)) error {
llm.mu.Lock()
defer llm.mu.Unlock()
C.llama_reset_timings(llm.ctx)
tokens := make([]C.llama_token, len(ctx))
@@ -185,6 +197,8 @@ func (llm *LLM) Predict(ctx []int, prompt string, fn func(api.GenerateResponse))
break
} else if err != nil {
return err
} else if llm.gc {
return io.EOF
}
b.WriteString(llm.detokenize(token))