Skip to content

Commit

Permalink
chore: improve the recap prompt to fix the missing topics issue, patc…
Browse files Browse the repository at this point in the history
…hed missing field in logs (#156)

* chore: improve the recap prompt to fix the missing topics issue, patched missing field in logs
* chore: improve prompt
  • Loading branch information
nekomeowww committed Jul 12, 2023
1 parent 1e82127 commit 7fdcf3b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/models/chathistories/chat_histories.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (m *Model) SummarizeChatHistories(chatID int64, histories []*ent.ChatHistor

chatHistories := strings.Join(historiesLLMFriendly, "\n")

summarizations, statusUsage, err := m.summarizeChatHistories(historiesIncludedMessageIDs, chatHistories)
summarizations, statusUsage, err := m.summarizeChatHistories(chatID, historiesIncludedMessageIDs, chatHistories)
if err != nil {
return make([]string, 0), err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/models/chathistories/private_forwarded.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (m *Model) SummarizePrivateForwardedChatHistories(userID int64, histories [

chatHistories := strings.Join(historiesLLMFriendly, "\n")

summarizations, statusUsage, err := m.summarizeChatHistories(historiesIncludedMessageIDs, chatHistories)
summarizations, statusUsage, err := m.summarizeChatHistories(userID, historiesIncludedMessageIDs, chatHistories)
if err != nil {
return make([]string, 0), err
}
Expand Down
22 changes: 14 additions & 8 deletions internal/models/chathistories/recap.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ var RecapOutputTemplate = lo.Must(template.
- {{ escape $d.Point }}{{ if len $d.KeyIDs }} {{ range $cIndex, $c := $d.KeyIDs }}<a href="https://t.me/c/{{ $chatID }}/{{ $c }}">[{{ add $cIndex 1 }}]</a>{{ if not (eq $cIndex (sub (len $d.KeyIDs) 1)) }} {{ end }}{{ end }}{{ end }}{{ end }}{{ if .Recap.Conclusion }}
结论:{{ escape .Recap.Conclusion }}{{ end }}`))

func (m *Model) summarizeChatHistoriesSlice(s string) ([]*openai.ChatHistorySummarizationOutputs, goopenai.Usage, error) {
func (m *Model) summarizeChatHistoriesSlice(chatID int64, s string) ([]*openai.ChatHistorySummarizationOutputs, goopenai.Usage, error) {
if s == "" {
return make([]*openai.ChatHistorySummarizationOutputs, 0), goopenai.Usage{}, nil
}

m.logger.Info(fmt.Sprintf("✍️ summarizing chat histories:\n%s", s))
m.logger.Info(fmt.Sprintf("✍️ summarizing chat histories:\n%s", s), zap.Int64("chat_id", chatID))

resp, err := m.openAI.SummarizeChatHistories(context.Background(), s)
if err != nil {
Expand All @@ -61,7 +61,7 @@ func (m *Model) summarizeChatHistoriesSlice(s string) ([]*openai.ChatHistorySumm
return nil, goopenai.Usage{}, nil
}

m.logger.Info("✅ summarized chat histories")
m.logger.Info("✅ summarized chat histories", zap.Int64("chat_id", chatID))
if resp.Choices[0].Message.Content == "" {
return nil, goopenai.Usage{}, nil
}
Expand All @@ -70,16 +70,22 @@ func (m *Model) summarizeChatHistoriesSlice(s string) ([]*openai.ChatHistorySumm

err = json.Unmarshal([]byte(resp.Choices[0].Message.Content), &outputs)
if err != nil {
m.logger.Error("failed to unmarshal chat history summarization output", zap.String("content", resp.Choices[0].Message.Content))
m.logger.Error("failed to unmarshal chat history summarization output",
zap.String("content", resp.Choices[0].Message.Content),
zap.Int64("chat_id", chatID),
)

return nil, resp.Usage, err
}

m.logger.Info(fmt.Sprintf("✅ unmarshaled chat history summarization output: %s", fo.May(json.Marshal(outputs))))
m.logger.Info(fmt.Sprintf("✅ unmarshaled chat history summarization output: %s", fo.May(json.Marshal(outputs))),
zap.Int64("chat_id", chatID),
)

return outputs, resp.Usage, nil
}

func (m *Model) summarizeChatHistories(messageIDs []int64, llmFriendlyChatHistories string) ([]*openai.ChatHistorySummarizationOutputs, goopenai.Usage, error) {
func (m *Model) summarizeChatHistories(chatID int64, messageIDs []int64, llmFriendlyChatHistories string) ([]*openai.ChatHistorySummarizationOutputs, goopenai.Usage, error) {
chatHistoriesSlices := m.openAI.SplitContentBasedByTokenLimitations(llmFriendlyChatHistories, 15000)
chatHistoriesSummarizations := make([]*openai.ChatHistorySummarizationOutputs, 0, len(chatHistoriesSlices))

Expand All @@ -89,13 +95,13 @@ func (m *Model) summarizeChatHistories(messageIDs []int64, llmFriendlyChatHistor
var outputs []*openai.ChatHistorySummarizationOutputs

_, _, err := lo.AttemptWithDelay(3, time.Second, func(tried int, delay time.Duration) error {
o, usage, err := m.summarizeChatHistoriesSlice(s)
o, usage, err := m.summarizeChatHistoriesSlice(chatID, s)
statusUsage.CompletionTokens += usage.CompletionTokens
statusUsage.PromptTokens += usage.PromptTokens
statusUsage.TotalTokens += usage.TotalTokens

if err != nil {
m.logger.Error(fmt.Sprintf("failed to summarize chat histories slice: %s, tried %d...", s, tried))
m.logger.Error(fmt.Sprintf("failed to summarize chat histories slice: %s, tried %d...", s, tried), zap.Int64("chat_id", chatID))
return err
}

Expand Down
6 changes: 0 additions & 6 deletions internal/thirdparty/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/pkoukk/tiktoken-go"
"github.com/sashabaranov/go-openai"
"github.com/sourcegraph/conc/pool"
"go.uber.org/fx"
"go.uber.org/ratelimit"
"go.uber.org/zap"
Expand Down Expand Up @@ -91,11 +90,6 @@ func NewClient() func(NewClientParams) (Client, error) {
limiter := ratelimit.New(1)
limiter.Take()

p := pool.New().WithMaxGoroutines(10)
p.Go(func() {

})

return &OpenAIClient{
client: client,
tiktokenEncoding: tokenizer,
Expand Down
9 changes: 7 additions & 2 deletions internal/thirdparty/openai/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@ var ChatHistorySummarizationPrompt = lo.Must(template.New("chat histories summar
{{ .ChatHistory }}
"""
You are my chat histories summary and review assistant. Above are chat histories, each message starts with msgId, please summarize these chats as 1 to 5 topics, each topic should contain the following fields: sinceId (msgId at the beginning of the topic)、keyIds (key msgId in the discussion, max 5 msgIds) and conclusion (ignore this field if no clear conclusion). Please output as the following JSON format without additional explanation using {{ .Language }}:"""
[{"topicName":"..","sinceId":123456789,"participantsNamesWithoutUsername":[".."],"discussion":[{"point":"..","keyIds":[123456789]}],"conclusion":".."}]"""`))
Read through the provided chat history and identify all distinct discussion topics that took place. Summarize each topic by extracting the most relevant points and key message IDs.
Output topics in the following JSON format in language {{ .Language }}:"""
[{"topicName":"Topic 1 Name","sinceId":123456789,"participantsNamesWithoutUsername":["John","Mary"],"discussion":[{"point":"Key point 1","keyIds":[123456789,987654321]},{"point":"Key point 2","keyIds":[456789123]}],"conclusion":"Optional conclusion"},{"topicName":"Topic 2 Name","sinceId":987654321,"participantsNamesWithoutUsername":["Bob","Alice"],"discussion":[{"point":"Key point 1","keyIds":[987654321]}],"conclusion":"Optional conclusion"}]
"""
Extract all distinct topics discussed in the chat history, including the topic name, starting message ID, participating members, key discussion points, and optional conclusion. Only include the most relevant points and message IDs. Be concise yet comprehensive.`))

0 comments on commit 7fdcf3b

Please sign in to comment.