Skip to content

Commit

Permalink
Merge pull request #42 from basenana/fix/chat
Browse files Browse the repository at this point in the history
fix: EOF of chat
  • Loading branch information
zwwhdls committed Apr 22, 2024
2 parents 3c9cf98 + 27f8fa4 commit 9359a34
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
1 change: 0 additions & 1 deletion cmd/apps/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func chat(dirId int64, history []map[string]string) error {
}
go func() {
f = f.Chat(res)
close(resp)
}()
if f.Error != nil {
return f.Error
Expand Down
2 changes: 1 addition & 1 deletion pkg/friday/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func (f *Friday) chat(res *ChatState) *Friday {
err error
)
go func() {
defer close(sumBuf)
_, err = f.LLM.Chat(f.statement.context, false, sumDialogue, sumBuf)
if err != nil {
f.Error = err
Expand All @@ -114,6 +113,7 @@ func (f *Friday) chat(res *ChatState) *Friday {
}()
select {
case <-f.statement.context.Done():
f.Error = errors.New("context canceled")
return f
case sum = <-sumBuf:
// add context prompt for dialogue
Expand Down
13 changes: 9 additions & 4 deletions pkg/llm/client/gemini/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (g *Gemini) GetAssistantModel() string {
}

func (g *Gemini) Chat(ctx context.Context, stream bool, history []map[string]string, answers chan<- map[string]string) (tokens map[string]int, err error) {
defer close(answers)
var path string
path = fmt.Sprintf("v1beta/models/%s:generateContent", *g.conf.Model)
if stream {
Expand Down Expand Up @@ -65,11 +66,15 @@ func (g *Gemini) Chat(ctx context.Context, stream bool, history []map[string]str
ans := make(map[string]string)
l := strings.TrimSpace(string(line))
if stream {
if !strings.HasPrefix(l, "\"text\"") {
continue
if l == "EOF" {
ans["content"] = "EOF"
} else {
if !strings.HasPrefix(l, "\"text\"") {
continue
}
// it should be: "text": "xxx"
ans["content"] = l[9 : len(l)-2]
}
// it should be: "text": "xxx"
ans["content"] = l[9 : len(l)-2]
} else {
var res ChatResult
err = json.Unmarshal(line, &res)
Expand Down
25 changes: 15 additions & 10 deletions pkg/llm/client/openai/v1/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (o *OpenAIV1) GetAssistantModel() string {
}

func (o *OpenAIV1) Chat(ctx context.Context, stream bool, history []map[string]string, resp chan<- map[string]string) (tokens map[string]int, err error) {
defer close(resp)
path := "v1/chat/completions"

data := map[string]interface{}{
Expand Down Expand Up @@ -92,17 +93,21 @@ func (o *OpenAIV1) Chat(ctx context.Context, stream bool, history []map[string]s
var delta map[string]string
if stream {
var res ChatStreamResult
if !strings.HasPrefix(string(line), "data:") || strings.Contains(string(line), "data: [DONE]") {
continue
if string(line) == "EOF" {
delta = map[string]string{"content": "EOF"}
} else {
if !strings.HasPrefix(string(line), "data:") || strings.Contains(string(line), "data: [DONE]") {
continue
}
// it should be: data: xxx
l := string(line)[6:]
err = json.Unmarshal([]byte(l), &res)
if err != nil {
err = fmt.Errorf("cannot marshal msg: %s, err: %v", line, err)
return
}
delta = res.Choices[0].Delta
}
// it should be: data: xxx
l := string(line)[6:]
err = json.Unmarshal([]byte(l), &res)
if err != nil {
err = fmt.Errorf("cannot marshal msg: %s, err: %v", line, err)
return
}
delta = res.Choices[0].Delta
} else {
var res ChatResult
err = json.Unmarshal(line, &res)
Expand Down

0 comments on commit 9359a34

Please sign in to comment.