Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EOF of chat #42

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading