Skip to content

Commit

Permalink
fix: system info template
Browse files Browse the repository at this point in the history
Signed-off-by: zwwhdls <zww@hdls.me>
  • Loading branch information
zwwhdls committed May 12, 2024
1 parent 4996b19 commit fd27195
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
10 changes: 5 additions & 5 deletions pkg/friday/friday.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ type Statement struct {
context context.Context

// for chat
summary string // summary of doc
Summary string // Summary of doc
HistorySummary string // Summary of chat history
Info string // Info of embedding
history []map[string]string // real chat history
question string // question for chat
query *models.DocQuery // search in doc or dir
historySummary string // summary of chat history
info string // info of embedding

// for ingest or summary
// for ingest or Summary
file *models.File // a whole file providing models.File
elementFile *string // a whole file given an element-style origin file
originFile *string // a whole file given an origin file
Expand All @@ -78,7 +78,7 @@ type Statement struct {
// for keywords
content string

// for summary
// for Summary
summaryType summary.SummaryType
}

Expand Down
31 changes: 15 additions & 16 deletions pkg/friday/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (f *Friday) Question(q string) *Friday {
}

func (f *Friday) OfSummary(summary string) *Friday {
f.statement.summary = summary
f.statement.Summary = summary
return f
}

Expand Down Expand Up @@ -92,29 +92,28 @@ func (f *Friday) Chat(res *ChatState) *Friday {

// If the number of dialogue rounds exceeds some rounds, should conclude it.
if len(f.statement.history) >= remainHistoryNum {
f.statement.historySummary = f.summaryHistory().statement.historySummary
f.statement.HistorySummary = f.summaryHistory().statement.HistorySummary
if f.Error != nil {
return f
}
}

// if it already has system info, rewrite it
// if it already has system Info, rewrite it
if (f.statement.history)[0]["role"] == "system" {
f.statement.history = f.statement.history[1:]
}

// regenerate system info
// regenerate system Info
systemInfo = f.generateSystemInfo()
dialogues = []map[string]string{
{"role": f.LLM.GetSystemModel(), "content": systemInfo},
{"role": f.LLM.GetAssistantModel(), "content": ""},
}

if len(f.statement.history) >= remainHistoryNum {
dialogues = []map[string]string{
{"role": f.LLM.GetSystemModel(), "content": systemInfo},
{"role": f.LLM.GetAssistantModel(), "content": ""},
}
dialogues = append(dialogues, f.statement.history[len(f.statement.history)-remainHistoryNum:len(f.statement.history)]...)
} else {
dialogues = make([]map[string]string, len(f.statement.history))
copy(dialogues, f.statement.history)
dialogues = append(dialogues, f.statement.history...)
}

// go for llm
Expand All @@ -125,13 +124,13 @@ func (f *Friday) Chat(res *ChatState) *Friday {

func (f *Friday) generateSystemInfo() string {
systemTemplate := "基于以下内容,简洁和专业的来回答用户的问题。答案请使用中文。\n"
if f.statement.summary != "" {
if f.statement.Summary != "" {
systemTemplate += "\n这是文章简介: {{ .Summary }}\n"
}
if f.statement.info != "" {
if f.statement.Info != "" {
systemTemplate += "\n这是已知内容: {{ .Info }}\n"
}
if f.statement.historySummary != "" {
if f.statement.HistorySummary != "" {
systemTemplate += "\n这是历史聊天总结作为前情提要: {{ .HistorySummary }}\n"
}

Expand Down Expand Up @@ -167,7 +166,7 @@ func (f *Friday) summaryHistory() *Friday {
f.Error = err
}
case sum = <-sumBuf:
f.statement.historySummary = sum["content"]
f.statement.HistorySummary = sum["content"]
}
return f
}
Expand All @@ -194,7 +193,7 @@ func (f *Friday) Complete(res *ChatState) *Friday {
}
}
ans, usage, err := f.LLM.Completion(f.statement.context, prompt, map[string]string{
"context": f.statement.info,
"context": f.statement.Info,
"question": f.statement.question,
})
if err != nil {
Expand Down Expand Up @@ -229,7 +228,7 @@ func (f *Friday) searchDocs(q string) {
//f.Log.Debugf("searched from [%s] for %s", c.Name, c.Content)
cs = append(cs, c.Content)
}
f.statement.info = strings.Join(cs, "\n")
f.statement.Info = strings.Join(cs, "\n")
return
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/friday/question_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var _ = Describe("TestQuestion", func() {
f := loFriday.WithContext(context.TODO())
f.searchDocs("I am a question")
Expect(f.Error).Should(BeNil())
Expect(f.statement.info).Should(Equal("There are logs of questions"))
Expect(f.statement.Info).Should(Equal("There are logs of questions"))
})
})

Expand All @@ -81,6 +81,7 @@ var _ = Describe("TestQuestion", func() {
Expect(f.Error).Should(BeNil())
Expect(len(resp)).Should(Equal(2))
Expect(resp["role"]).Should(Equal("assistant"))
Expect(len(f.statement.history)).Should(Equal(3))
})
It("chat for second time", func() {
history := []map[string]string{
Expand Down Expand Up @@ -110,6 +111,7 @@ var _ = Describe("TestQuestion", func() {
Expect(f.Error).Should(BeNil())
Expect(len(resp)).Should(Equal(2))
Expect(resp["role"]).Should(Equal("assistant"))
Expect(len(f.statement.history)).Should(Equal(5))
})
It("chat for three times", func() {
history := []map[string]string{
Expand Down Expand Up @@ -148,6 +150,7 @@ var _ = Describe("TestQuestion", func() {
Expect(f.Error).Should(BeNil())
Expect(len(resp)).Should(Equal(2))
Expect(resp["role"]).Should(Equal("assistant"))
Expect(len(f.statement.history)).Should(Equal(7))
})
It("chat for four times", func() {
history := []map[string]string{
Expand Down Expand Up @@ -193,6 +196,7 @@ var _ = Describe("TestQuestion", func() {
Expect(f.Error).Should(BeNil())
Expect(len(resp)).Should(Equal(2))
Expect(resp["role"]).Should(Equal("assistant"))
Expect(len(f.statement.history)).Should(Equal(7))
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/friday/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (f *Friday) Summary(res *SummaryState) *Friday {
if f.statement.file != nil {
// split doc
docs := f.Spliter.Split(f.statement.file.Content)
// summary
// Summary
summaryOfFile, usage, err := s.Summary(f.statement.context, docs, f.statement.summaryType)
if err != nil {
f.Error = err
Expand Down
16 changes: 8 additions & 8 deletions pkg/friday/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ var _ = Describe("TestStuffSummary", func() {
}
})

Context("summary", func() {
It("summary should be succeed", func() {
Context("Summary", func() {
It("Summary should be succeed", func() {
res := SummaryState{}
f := loFriday.WithContext(context.TODO()).Element(elements).OfType(summaryType).Summary(&res)
Expect(f.Error).Should(BeNil())
Expand Down Expand Up @@ -82,7 +82,7 @@ var _ = Describe("TestMapReduceSummary", func() {
)

BeforeEach(func() {
loFriday.Log = logger.NewLogger("test-mapreduce-summary")
loFriday.Log = logger.NewLogger("test-mapreduce-Summary")
loFriday.LLM = FakeSummaryLLM{}
loFriday.LimitToken = 50
loFriday.Spliter = spliter.NewTextSpliter(loFriday.Log, 8, 2, "\n")
Expand All @@ -97,8 +97,8 @@ var _ = Describe("TestMapReduceSummary", func() {
}
})

Context("summary", func() {
It("summary should be succeed", func() {
Context("Summary", func() {
It("Summary should be succeed", func() {
res := SummaryState{}
f := loFriday.WithContext(context.TODO()).Element(elements).OfType(summaryType).Summary(&res)
Expect(f.Error).Should(BeNil())
Expand Down Expand Up @@ -126,7 +126,7 @@ var _ = Describe("TestRefineSummary", func() {
)

BeforeEach(func() {
loFriday.Log = logger.NewLogger("test-refine-summary")
loFriday.Log = logger.NewLogger("test-refine-Summary")
loFriday.LLM = FakeSummaryLLM{}
loFriday.Spliter = spliter.NewTextSpliter(loFriday.Log, spliter.DefaultChunkSize, spliter.DefaultChunkOverlap, "\n")
elements = []models.Element{{
Expand All @@ -140,8 +140,8 @@ var _ = Describe("TestRefineSummary", func() {
}
})

Context("summary", func() {
It("summary should be succeed", func() {
Context("Summary", func() {
It("Summary should be succeed", func() {
res := SummaryState{}
_ = loFriday.WithContext(context.TODO()).Element(elements).OfType(summaryType).Summary(&res)
// todo
Expand Down

0 comments on commit fd27195

Please sign in to comment.