✨Feat: 实现对话记忆
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
llm, err := openai.New(
|
||||
openai.WithModel("Qwen/Qwen3-Coder-30B-A3B-Instruct"),
|
||||
openai.WithBaseURL("https://api.siliconflow.cn/v1"),
|
||||
openai.WithToken("sk-udqgogvqgfriqvsehkpxgaejfclvoikbntjeaeijhqnhnviw"),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
messages, _ := chatMemory.ChatHistory.Messages(ctx)
|
||||
|
||||
// Conversation
|
||||
var conversation string
|
||||
for _, msg := range messages {
|
||||
conversation += msg.GetContent() + "\n"
|
||||
}
|
||||
|
||||
fullPrompt := conversation + "Human: " + userInput + "\nAssistant:"
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user