|
15 | 15 | package controllers |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + "sync" |
| 21 | + |
18 | 22 | "github.com/casibase/casibase/carrier" |
| 23 | + "github.com/casibase/casibase/model" |
19 | 24 | "github.com/casibase/casibase/object" |
20 | 25 | ) |
21 | 26 |
|
@@ -72,3 +77,98 @@ func parseAnswerWithCarriers(answer string, suggestionCount int, needTitle bool) |
72 | 77 |
|
73 | 78 | return parsedAnswer, suggestions, title, nil |
74 | 79 | } |
| 80 | + |
| 81 | +func isReasonModel(typ string) bool { |
| 82 | + typ = strings.ToLower(typ) |
| 83 | + if strings.Contains(typ, "r1") { |
| 84 | + return true |
| 85 | + } else if strings.Contains(typ, "reasoner") { |
| 86 | + return true |
| 87 | + } |
| 88 | + return false |
| 89 | +} |
| 90 | + |
| 91 | +func getResultWithSuggestionsAndTitle(modelResult *model.ModelResult, writer *CarrierWriter, question string, modelProviderObj model.ModelProvider, needTitle bool, suggestionCount int) (*model.ModelResult, error) { |
| 92 | + var fullPrompt strings.Builder |
| 93 | + |
| 94 | + fullPrompt.WriteString(fmt.Sprintf("User question: %s\n\n", question)) |
| 95 | + if suggestionCount > 0 { |
| 96 | + divider := "|||" |
| 97 | + suggestionPrompt := fmt.Sprintf(`**Based on the user question, generate %d possible follow-up questions. No need to answer user quesion. |
| 98 | +They must: |
| 99 | +- Be in the same language as the original question. |
| 100 | +- Start with the separator "%s". |
| 101 | +- Be separated by "%s" without any other formatting or explanation. |
| 102 | +- Do not include any explanation, analysis, or answers—only output the %d questions. |
| 103 | +
|
| 104 | +`, suggestionCount, divider, divider, suggestionCount) |
| 105 | + fullPrompt.WriteString(suggestionPrompt) |
| 106 | + } |
| 107 | + if needTitle { |
| 108 | + fullPrompt.WriteString(` |
| 109 | +**Finally, generate a concise and meaningful title for the original question. No need to answer user quesion. |
| 110 | +
|
| 111 | +- The title must be in the same language. |
| 112 | +- The title must start with "=====" (five equals signs, no space). |
| 113 | +- Do not include the divider or title if a meaningful title cannot be generated. |
| 114 | +- Do NOT include any explanations or extra text—just output the title.`) |
| 115 | + } |
| 116 | + |
| 117 | + carrierResult, err := modelProviderObj.QueryText(fullPrompt.String(), writer, nil, "", nil, nil) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + return carrierResult, nil |
| 123 | +} |
| 124 | + |
| 125 | +func QueryCarrierText(question string, writer *RefinedWriter, history []*model.RawMessage, prompt string, knowledge []*model.RawMessage, modelProviderObj model.ModelProvider, needTitle bool, suggestionCount int) (*model.ModelResult, error) { |
| 126 | + var ( |
| 127 | + wg sync.WaitGroup |
| 128 | + mainErr error |
| 129 | + carrierErr error |
| 130 | + ) |
| 131 | + |
| 132 | + var modelResult *model.ModelResult |
| 133 | + |
| 134 | + wg.Add(1) |
| 135 | + go func() { |
| 136 | + defer wg.Done() |
| 137 | + var err error |
| 138 | + modelResult, err = modelProviderObj.QueryText(question, writer, history, prompt, knowledge, nil) |
| 139 | + if err != nil { |
| 140 | + mainErr = err |
| 141 | + } |
| 142 | + }() |
| 143 | + |
| 144 | + CarrierWriter := &CarrierWriter{*NewCleaner(6), []byte{}} |
| 145 | + var carrierResult *model.ModelResult |
| 146 | + |
| 147 | + wg.Add(1) |
| 148 | + go func() { |
| 149 | + defer wg.Done() |
| 150 | + var err error |
| 151 | + carrierResult, err = getResultWithSuggestionsAndTitle(modelResult, CarrierWriter, question, modelProviderObj, needTitle, suggestionCount) |
| 152 | + if err != nil { |
| 153 | + carrierErr = err |
| 154 | + } |
| 155 | + }() |
| 156 | + |
| 157 | + wg.Wait() |
| 158 | + |
| 159 | + if mainErr != nil { |
| 160 | + return nil, mainErr |
| 161 | + } |
| 162 | + if carrierErr != nil { |
| 163 | + return nil, carrierErr |
| 164 | + } |
| 165 | + |
| 166 | + modelResult.PromptTokenCount += carrierResult.PromptTokenCount |
| 167 | + modelResult.ResponseTokenCount += carrierResult.ResponseTokenCount |
| 168 | + modelResult.TotalPrice += carrierResult.TotalPrice |
| 169 | + modelResult.TotalTokenCount += carrierResult.TotalTokenCount |
| 170 | + |
| 171 | + writer.Write(CarrierWriter.messageBuf) |
| 172 | + |
| 173 | + return modelResult, nil |
| 174 | +} |
0 commit comments