✨Feat: 实现思维链问答
This commit is contained in:
@@ -13,16 +13,32 @@ import (
|
|||||||
"github.com/tmc/langchaingo/memory"
|
"github.com/tmc/langchaingo/memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
model = "Qwen/Qwen3-Coder-30B-A3B-Instruct"
|
||||||
|
base_url = "https://api.siliconflow.cn/v1"
|
||||||
|
token = "sk-udqgogvqgfriqvsehkpxgaejfclvoikbntjeaeijhqnhnviw"
|
||||||
|
cotPrompt = `请按照以下步骤分析这道算法题:
|
||||||
|
1. 算法判断:判断题目类型和所需算法
|
||||||
|
2. 基本思路:说明解题思路和关键点
|
||||||
|
3. 框架代码:给出代码框架
|
||||||
|
4. 易错解析:指出常见错误和注意事项
|
||||||
|
|
||||||
|
题目:%s
|
||||||
|
|
||||||
|
请按上述格式进行分析:`
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
llm, err := openai.New(
|
llm, err := openai.New(
|
||||||
openai.WithModel("Qwen/Qwen3-Coder-30B-A3B-Instruct"),
|
openai.WithModel(model),
|
||||||
openai.WithBaseURL("https://api.siliconflow.cn/v1"),
|
openai.WithBaseURL(base_url),
|
||||||
openai.WithToken("sk-udqgogvqgfriqvsehkpxgaejfclvoikbntjeaeijhqnhnviw"),
|
openai.WithToken(token),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// [Memory] Init
|
||||||
chatMemory := memory.NewConversationBuffer()
|
chatMemory := memory.NewConversationBuffer()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
@@ -39,16 +55,19 @@ func main() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
messages, _ := chatMemory.ChatHistory.Messages(ctx)
|
var fullPrompt string
|
||||||
|
if isAlgorithmQuestion(userInput) {
|
||||||
// Conversation
|
fullPrompt = fmt.Sprintf(cotPrompt, userInput)
|
||||||
var conversation string
|
} else {
|
||||||
for _, msg := range messages {
|
messages, _ := chatMemory.ChatHistory.Messages(ctx)
|
||||||
conversation += msg.GetContent() + "\n"
|
// Conversation
|
||||||
|
var conversation string
|
||||||
|
for _, msg := range messages {
|
||||||
|
conversation += msg.GetContent() + "\n"
|
||||||
|
}
|
||||||
|
fullPrompt = conversation + "Human: " + userInput + "\nAssistant:"
|
||||||
}
|
}
|
||||||
|
|
||||||
fullPrompt := conversation + "Human: " + userInput + "\nAssistant:"
|
|
||||||
|
|
||||||
response, err := llms.GenerateFromSinglePrompt(
|
response, err := llms.GenerateFromSinglePrompt(
|
||||||
ctx,
|
ctx,
|
||||||
llm,
|
llm,
|
||||||
@@ -66,3 +85,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isAlgorithmQuestion(input string) bool {
|
||||||
|
keywords := []string{"算法", "编程", "题目", "解题", "代码实现"}
|
||||||
|
for _, keyword := range keywords {
|
||||||
|
if strings.Contains(input, keyword) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user