2025-11-16 21:14:35 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/tmc/langchaingo/llms"
|
|
|
|
|
"github.com/tmc/langchaingo/llms/openai"
|
|
|
|
|
"github.com/tmc/langchaingo/memory"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-16 21:49:51 +08:00
|
|
|
const (
|
|
|
|
|
model = "Qwen/Qwen3-Coder-30B-A3B-Instruct"
|
|
|
|
|
base_url = "https://api.siliconflow.cn/v1"
|
|
|
|
|
token = "sk-udqgogvqgfriqvsehkpxgaejfclvoikbntjeaeijhqnhnviw"
|
|
|
|
|
cotPrompt = `请按照以下步骤分析这道算法题:
|
|
|
|
|
1. 算法判断:判断题目类型和所需算法
|
|
|
|
|
2. 基本思路:说明解题思路和关键点
|
|
|
|
|
3. 框架代码:给出代码框架
|
|
|
|
|
4. 易错解析:指出常见错误和注意事项
|
|
|
|
|
|
|
|
|
|
题目:%s
|
|
|
|
|
|
|
|
|
|
请按上述格式进行分析:`
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-16 21:14:35 +08:00
|
|
|
func main() {
|
|
|
|
|
llm, err := openai.New(
|
2025-11-16 21:49:51 +08:00
|
|
|
openai.WithModel(model),
|
|
|
|
|
openai.WithBaseURL(base_url),
|
|
|
|
|
openai.WithToken(token),
|
2025-11-16 21:14:35 +08:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 21:49:51 +08:00
|
|
|
// [Memory] Init
|
2025-11-16 21:14:35 +08:00
|
|
|
chatMemory := memory.NewConversationBuffer()
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
|
|
fmt.Println("Chat Application Started. Type 'exit' to quit.")
|
|
|
|
|
fmt.Println("-----------------------------------------------")
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
fmt.Print("You: ")
|
|
|
|
|
userInput, _ := reader.ReadString('\n')
|
|
|
|
|
userInput = strings.TrimSpace(userInput)
|
|
|
|
|
|
|
|
|
|
if userInput == "exit" {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 21:49:51 +08:00
|
|
|
var fullPrompt string
|
|
|
|
|
if isAlgorithmQuestion(userInput) {
|
|
|
|
|
fullPrompt = fmt.Sprintf(cotPrompt, userInput)
|
|
|
|
|
} else {
|
|
|
|
|
messages, _ := chatMemory.ChatHistory.Messages(ctx)
|
|
|
|
|
// Conversation
|
|
|
|
|
var conversation string
|
|
|
|
|
for _, msg := range messages {
|
|
|
|
|
conversation += msg.GetContent() + "\n"
|
|
|
|
|
}
|
|
|
|
|
fullPrompt = conversation + "Human: " + userInput + "\nAssistant:"
|
2025-11-16 21:14:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response, err := llms.GenerateFromSinglePrompt(
|
|
|
|
|
ctx,
|
|
|
|
|
llm,
|
|
|
|
|
fullPrompt,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chatMemory.ChatHistory.AddUserMessage(ctx, userInput)
|
|
|
|
|
chatMemory.ChatHistory.AddAIMessage(ctx, response)
|
|
|
|
|
|
|
|
|
|
fmt.Println("[AI Response] ", response)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-16 21:49:51 +08:00
|
|
|
|
|
|
|
|
func isAlgorithmQuestion(input string) bool {
|
|
|
|
|
keywords := []string{"算法", "编程", "题目", "解题", "代码实现"}
|
|
|
|
|
for _, keyword := range keywords {
|
|
|
|
|
if strings.Contains(input, keyword) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|