Skip to content

Commit

Permalink
fix: error handling and set ollama stream mode to disable
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaskiddo committed Sep 14, 2024
1 parent 31a01fd commit b80b5af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions pkg/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,22 @@ func init() {
OAIClient = OpenAI.NewClient(OAIAPIKey)

default:
var OHostPort string

OHostSchema, OHostURL, isOK := strings.Cut(OHost, "://")

if !isOK {
OHostSchema = "http"
OHostURL = OHost
OHostPort = "11434"
}

var OHostPort string
switch OHostSchema {
case "http":
OHostPort = "80"

case "https":
OHostPort = "443"

default:
OHostPort = "11434"
}

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

isStream := new(bool)
*isStream = false

OGPTPrompt := &Ollama.ChatRequest{
Model: OGPTModelName,
Messages: []Ollama.Message{
Expand All @@ -246,17 +248,18 @@ func GPT3Response(question string) (response string, err error) {
Content: question,
},
},
Stream: isStream,
}

OGTPResponseFunc := func(OGPTResponse Ollama.ChatResponse) error {
OGPTResponseText = OGPTResponse.Message.Content
return nil
}

err := OClient.Chat(
context.Background(),
OGPTPrompt,
func(OGPTResponse Ollama.ChatResponse) error {
if len(OGPTResponse.Message.Content) > 0 {
OGPTResponseText = OGPTResponse.Message.Content
}
return nil
},
OGTPResponseFunc,
)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func WhatsAppHandler(event interface{}) {
}()

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

0 comments on commit b80b5af

Please sign in to comment.