mirror of
https://github.com/dogkeeper886/ollama37.git
synced 2025-12-11 08:17:03 +00:00
add session expiration
This commit is contained in:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user