-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmain.go
More file actions
197 lines (158 loc) · 4.11 KB
/
Copy pathmain.go
File metadata and controls
197 lines (158 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/memory"
)
func main() {
if len(os.Args) > 1 {
switch os.Args[1] {
case "step3", "basic":
runBasicChat()
case "step4", "interactive":
runInteractiveChat()
case "step5", "memory":
runChatWithMemory()
case "step6", "advanced":
runAdvancedChat()
default:
fmt.Println("Usage: go run . [step3|step4|step5|step6|basic|interactive|memory|advanced]")
fmt.Println("If no argument provided, runs the advanced chat (step6)")
}
} else {
// Default to advanced chat
runAdvancedChat()
}
}
// Step 3: Basic Chat Application
func runBasicChat() {
fmt.Println("=== Step 3: Basic Chat ===")
// Initialize the OpenAI LLM
llm, err := openai.New()
if err != nil {
log.Fatal(err)
}
// Create a context
ctx := context.Background()
// Send a message to the LLM
response, err := llms.GenerateFromSinglePrompt(
ctx,
llm,
"Hello! How can you help me today?",
)
if err != nil {
log.Fatal(err)
}
fmt.Println("AI:", response)
}
// Step 4: Interactive Chat
func runInteractiveChat() {
fmt.Println("=== Step 4: Interactive Chat ===")
// Initialize LLM
llm, err := openai.New()
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
reader := bufio.NewReader(os.Stdin)
fmt.Println("Chat Application Started (type 'quit' to exit)")
fmt.Println("----------------------------------------")
for {
fmt.Print("You: ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
if input == "quit" {
break
}
response, err := llms.GenerateFromSinglePrompt(ctx, llm, input)
if err != nil {
fmt.Printf("Error: %v\n", err)
continue
}
fmt.Printf("AI: %s\n\n", response)
}
}
// Step 5: Chat with Memory
func runChatWithMemory() {
fmt.Println("=== Step 5: Chat with Memory ===")
// Initialize LLM
llm, err := openai.New()
if err != nil {
log.Fatal(err)
}
// Create conversation memory
chatMemory := memory.NewConversationBuffer()
ctx := context.Background()
reader := bufio.NewReader(os.Stdin)
fmt.Println("Chat with Memory (type 'quit' to exit)")
fmt.Println("----------------------------------------")
for {
fmt.Print("You: ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
if input == "quit" {
break
}
// Get conversation history
messages, _ := chatMemory.ChatHistory.Messages(ctx)
// Format the conversation
var conversation string
for _, msg := range messages {
conversation += msg.GetContent() + "\n"
}
// Add current input to the conversation
fullPrompt := conversation + "Human: " + input + "\nAssistant:"
// Generate response
response, err := llms.GenerateFromSinglePrompt(ctx, llm, fullPrompt)
if err != nil {
fmt.Printf("Error: %v\n", err)
continue
}
// Save to memory
chatMemory.ChatHistory.AddUserMessage(ctx, input)
chatMemory.ChatHistory.AddAIMessage(ctx, response)
fmt.Printf("AI: %s\n\n", response)
}
}
// Step 6: Advanced Chat with Chains and Prompt Templates
func runAdvancedChat() {
fmt.Println("=== Step 6: Advanced Chat with Chains ===")
// Initialize LLM
llm, err := openai.New()
if err != nil {
log.Fatal(err)
}
// Create conversation memory
chatMemory := memory.NewConversationBuffer()
// Create conversation chain
// This uses the default conversation template with built-in memory handling
conversationChain := chains.NewConversation(llm, chatMemory)
ctx := context.Background()
reader := bufio.NewReader(os.Stdin)
fmt.Println("Advanced Chat Application (type 'quit' to exit)")
fmt.Println("----------------------------------------")
for {
fmt.Print("You: ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
if input == "quit" {
break
}
// Run the chain with the input
// The chain automatically manages conversation history
result, err := chains.Run(ctx, conversationChain, input)
if err != nil {
fmt.Printf("Error: %v\n", err)
continue
}
fmt.Printf("AI: %s\n\n", result)
}
fmt.Println("Goodbye!")
}