Enha: client stop string for completion only
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
version: "2"
|
version: "2"
|
||||||
run:
|
run:
|
||||||
concurrency: 2
|
timeout: 1m
|
||||||
|
concurrency: 4
|
||||||
tests: false
|
tests: false
|
||||||
linters:
|
linters:
|
||||||
default: none
|
default: none
|
||||||
@@ -14,7 +15,13 @@ linters:
|
|||||||
- prealloc
|
- prealloc
|
||||||
- staticcheck
|
- staticcheck
|
||||||
- unused
|
- unused
|
||||||
|
- gocritic
|
||||||
|
- unconvert
|
||||||
|
- wastedassign
|
||||||
settings:
|
settings:
|
||||||
|
gocritic:
|
||||||
|
enabled-tags:
|
||||||
|
- performance
|
||||||
funlen:
|
funlen:
|
||||||
lines: 80
|
lines: 80
|
||||||
statements: 50
|
statements: 50
|
||||||
|
|||||||
6
bot.go
6
bot.go
@@ -682,8 +682,10 @@ func sendMsgToLLM(body io.Reader) {
|
|||||||
answerText = strings.ReplaceAll(chunk.Chunk, "\n\n", "\n")
|
answerText = strings.ReplaceAll(chunk.Chunk, "\n\n", "\n")
|
||||||
// Accumulate text to check for stop strings that might span across chunks
|
// Accumulate text to check for stop strings that might span across chunks
|
||||||
// check if chunk is in stopstrings => stop
|
// check if chunk is in stopstrings => stop
|
||||||
if slices.Contains(stopStrings, answerText) {
|
// this check is needed only for openrouter /v1/completion, since it does not respect stop slice
|
||||||
logger.Debug("Stop string detected and handled", "stop_string", answerText)
|
if chunkParser.GetAPIType() == models.APITypeCompletion &&
|
||||||
|
slices.Contains(stopStrings, answerText) {
|
||||||
|
logger.Debug("stop string detected on client side for completion endpoint", "stop_string", answerText)
|
||||||
streamDone <- true
|
streamDone <- true
|
||||||
}
|
}
|
||||||
chunkChan <- answerText
|
chunkChan <- answerText
|
||||||
|
|||||||
27
llm.go
27
llm.go
@@ -78,6 +78,7 @@ type ChunkParser interface {
|
|||||||
ParseChunk([]byte) (*models.TextChunk, error)
|
ParseChunk([]byte) (*models.TextChunk, error)
|
||||||
FormMsg(msg, role string, cont bool) (io.Reader, error)
|
FormMsg(msg, role string, cont bool) (io.Reader, error)
|
||||||
GetToken() string
|
GetToken() string
|
||||||
|
GetAPIType() models.APIType
|
||||||
}
|
}
|
||||||
|
|
||||||
func choseChunkParser() {
|
func choseChunkParser() {
|
||||||
@@ -127,6 +128,10 @@ type OpenRouterChat struct {
|
|||||||
Model string
|
Model string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (lcp LCPCompletion) GetAPIType() models.APIType {
|
||||||
|
return models.APITypeCompletion
|
||||||
|
}
|
||||||
|
|
||||||
func (lcp LCPCompletion) GetToken() string {
|
func (lcp LCPCompletion) GetToken() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -233,7 +238,11 @@ func (lcp LCPCompletion) ParseChunk(data []byte) (*models.TextChunk, error) {
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (op LCPChat) GetToken() string {
|
func (lcp LCPChat) GetAPIType() models.APIType {
|
||||||
|
return models.APITypeChat
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lcp LCPChat) GetToken() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,6 +380,10 @@ func (op LCPChat) FormMsg(msg, role string, resume bool) (io.Reader, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// deepseek
|
// deepseek
|
||||||
|
func (ds DeepSeekerCompletion) GetAPIType() models.APIType {
|
||||||
|
return models.APITypeCompletion
|
||||||
|
}
|
||||||
|
|
||||||
func (ds DeepSeekerCompletion) ParseChunk(data []byte) (*models.TextChunk, error) {
|
func (ds DeepSeekerCompletion) ParseChunk(data []byte) (*models.TextChunk, error) {
|
||||||
llmchunk := models.DSCompletionResp{}
|
llmchunk := models.DSCompletionResp{}
|
||||||
if err := json.Unmarshal(data, &llmchunk); err != nil {
|
if err := json.Unmarshal(data, &llmchunk); err != nil {
|
||||||
@@ -453,6 +466,10 @@ func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader
|
|||||||
return bytes.NewReader(data), nil
|
return bytes.NewReader(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ds DeepSeekerChat) GetAPIType() models.APIType {
|
||||||
|
return models.APITypeChat
|
||||||
|
}
|
||||||
|
|
||||||
func (ds DeepSeekerChat) ParseChunk(data []byte) (*models.TextChunk, error) {
|
func (ds DeepSeekerChat) ParseChunk(data []byte) (*models.TextChunk, error) {
|
||||||
llmchunk := models.DSChatStreamResp{}
|
llmchunk := models.DSChatStreamResp{}
|
||||||
if err := json.Unmarshal(data, &llmchunk); err != nil {
|
if err := json.Unmarshal(data, &llmchunk); err != nil {
|
||||||
@@ -539,6 +556,10 @@ func (ds DeepSeekerChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// openrouter
|
// openrouter
|
||||||
|
func (or OpenRouterCompletion) GetAPIType() models.APIType {
|
||||||
|
return models.APITypeCompletion
|
||||||
|
}
|
||||||
|
|
||||||
func (or OpenRouterCompletion) ParseChunk(data []byte) (*models.TextChunk, error) {
|
func (or OpenRouterCompletion) ParseChunk(data []byte) (*models.TextChunk, error) {
|
||||||
llmchunk := models.OpenRouterCompletionResp{}
|
llmchunk := models.OpenRouterCompletionResp{}
|
||||||
if err := json.Unmarshal(data, &llmchunk); err != nil {
|
if err := json.Unmarshal(data, &llmchunk); err != nil {
|
||||||
@@ -618,6 +639,10 @@ func (or OpenRouterCompletion) FormMsg(msg, role string, resume bool) (io.Reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
// chat
|
// chat
|
||||||
|
func (or OpenRouterChat) GetAPIType() models.APIType {
|
||||||
|
return models.APITypeChat
|
||||||
|
}
|
||||||
|
|
||||||
func (or OpenRouterChat) ParseChunk(data []byte) (*models.TextChunk, error) {
|
func (or OpenRouterChat) ParseChunk(data []byte) (*models.TextChunk, error) {
|
||||||
llmchunk := models.OpenRouterChatResp{}
|
llmchunk := models.OpenRouterChatResp{}
|
||||||
if err := json.Unmarshal(data, &llmchunk); err != nil {
|
if err := json.Unmarshal(data, &llmchunk); err != nil {
|
||||||
|
|||||||
@@ -558,3 +558,10 @@ type ChatRoundReq struct {
|
|||||||
Regen bool
|
Regen bool
|
||||||
Resume bool
|
Resume bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type APIType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
APITypeChat APIType = iota
|
||||||
|
APITypeCompletion
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user