Skip to content

Commit b80b5af

Browse files
committed
fix: error handling and set ollama stream mode to disable
1 parent 31a01fd commit b80b5af

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

pkg/gpt/gpt.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,22 @@ func init() {
131131
OAIClient = OpenAI.NewClient(OAIAPIKey)
132132

133133
default:
134+
var OHostPort string
135+
134136
OHostSchema, OHostURL, isOK := strings.Cut(OHost, "://")
135137

136138
if !isOK {
137139
OHostSchema = "http"
138140
OHostURL = OHost
141+
OHostPort = "11434"
139142
}
140143

141-
var OHostPort string
142144
switch OHostSchema {
143145
case "http":
144146
OHostPort = "80"
145147

146148
case "https":
147149
OHostPort = "443"
148-
149-
default:
150-
OHostPort = "11434"
151150
}
152151

153152
OClient = Ollama.NewClient(&url.URL{
@@ -238,6 +237,9 @@ func GPT3Response(question string) (response string, err error) {
238237
default:
239238
var OGPTResponseText string
240239

240+
isStream := new(bool)
241+
*isStream = false
242+
241243
OGPTPrompt := &Ollama.ChatRequest{
242244
Model: OGPTModelName,
243245
Messages: []Ollama.Message{
@@ -246,17 +248,18 @@ func GPT3Response(question string) (response string, err error) {
246248
Content: question,
247249
},
248250
},
251+
Stream: isStream,
252+
}
253+
254+
OGTPResponseFunc := func(OGPTResponse Ollama.ChatResponse) error {
255+
OGPTResponseText = OGPTResponse.Message.Content
256+
return nil
249257
}
250258

251259
err := OClient.Chat(
252260
context.Background(),
253261
OGPTPrompt,
254-
func(OGPTResponse Ollama.ChatResponse) error {
255-
if len(OGPTResponse.Message.Content) > 0 {
256-
OGPTResponseText = OGPTResponse.Message.Content
257-
}
258-
return nil
259-
},
262+
OGTPResponseFunc,
260263
)
261264

262265
if err != nil {

pkg/whatsapp/whatsapp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func WhatsAppHandler(event interface{}) {
358358
}()
359359

360360
response, err := gpt.GPT3Response(question)
361-
if err != nil {
361+
if err != nil || len(response) == 0 {
362362
log.Println(log.LogLevelError, err.Error())
363363
response = "Sorry, the AI can not response for this time. Please try again after a few moment 🥺"
364364
}

0 commit comments

Comments
 (0)