mirror of
https://github.com/tcsenpai/ollama.git
synced 2025-06-07 11:45:21 +00:00
cleanup stop code
This commit is contained in:
parent
a4d402c403
commit
8f79a2e86a
@ -121,65 +121,6 @@ func (s *Server) allNil() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func findStop(sequence string, stops []string) (bool, string) {
|
|
||||||
for _, stop := range stops {
|
|
||||||
if strings.Contains(sequence, stop) {
|
|
||||||
return true, stop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func containsStopSuffix(sequence string, stops []string) bool {
|
|
||||||
for _, stop := range stops {
|
|
||||||
for i := 1; i <= len(stop); i++ {
|
|
||||||
if strings.HasSuffix(sequence, stop[:i]) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// truncateStop removes the provided stop string from pieces,
|
|
||||||
// returning the partial pieces with stop removed, including truncating
|
|
||||||
// the last piece if required
|
|
||||||
func truncateStop(pieces []string, stop string) []string {
|
|
||||||
joined := strings.Join(pieces, "")
|
|
||||||
|
|
||||||
index := strings.Index(joined, stop)
|
|
||||||
if index == -1 {
|
|
||||||
return pieces
|
|
||||||
}
|
|
||||||
|
|
||||||
joined = joined[:index]
|
|
||||||
|
|
||||||
// Split truncated string back into pieces of original lengths
|
|
||||||
lengths := make([]int, len(pieces))
|
|
||||||
for i, piece := range pieces {
|
|
||||||
lengths[i] = len(piece)
|
|
||||||
}
|
|
||||||
|
|
||||||
var result []string
|
|
||||||
start := 0
|
|
||||||
for _, length := range lengths {
|
|
||||||
if start >= len(joined) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
end := start + length
|
|
||||||
if end > len(joined) {
|
|
||||||
end = len(joined)
|
|
||||||
}
|
|
||||||
result = append(result, joined[start:end])
|
|
||||||
start = end
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) run(ctx context.Context) {
|
func (s *Server) run(ctx context.Context) {
|
||||||
batch := llama.NewBatch(s.batchSize, 0, s.parallel)
|
batch := llama.NewBatch(s.batchSize, 0, s.parallel)
|
||||||
defer batch.Free()
|
defer batch.Free()
|
||||||
|
64
llama/runner/stop.go
Normal file
64
llama/runner/stop.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func findStop(sequence string, stops []string) (bool, string) {
|
||||||
|
for _, stop := range stops {
|
||||||
|
if strings.Contains(sequence, stop) {
|
||||||
|
return true, stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsStopSuffix(sequence string, stops []string) bool {
|
||||||
|
for _, stop := range stops {
|
||||||
|
for i := 1; i <= len(stop); i++ {
|
||||||
|
if strings.HasSuffix(sequence, stop[:i]) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// truncateStop removes the provided stop string from pieces,
|
||||||
|
// returning the partial pieces with stop removed, including truncating
|
||||||
|
// the last piece if required
|
||||||
|
func truncateStop(pieces []string, stop string) []string {
|
||||||
|
joined := strings.Join(pieces, "")
|
||||||
|
|
||||||
|
index := strings.Index(joined, stop)
|
||||||
|
if index == -1 {
|
||||||
|
return pieces
|
||||||
|
}
|
||||||
|
|
||||||
|
joined = joined[:index]
|
||||||
|
|
||||||
|
// Split truncated string back into pieces of original lengths
|
||||||
|
lengths := make([]int, len(pieces))
|
||||||
|
for i, piece := range pieces {
|
||||||
|
lengths[i] = len(piece)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result []string
|
||||||
|
start := 0
|
||||||
|
for _, length := range lengths {
|
||||||
|
if start >= len(joined) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
end := start + length
|
||||||
|
if end > len(joined) {
|
||||||
|
end = len(joined)
|
||||||
|
}
|
||||||
|
result = append(result, joined[start:end])
|
||||||
|
start = end
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user