From c570cd506a0ccb6741067cdc11771c6af86a9508 Mon Sep 17 00:00:00 2001 From: rogueking Date: Tue, 6 May 2025 16:47:54 -0700 Subject: [PATCH] added better error response --- .env.example | 2 +- internal/ollama/ollama.go | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index ac336bc..ed4c2bb 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,6 @@ APP_PORT=8080 -OLLAMA_API_URL=https://openweb-ui.miguelmuniz.com +OLLAMA_API_URL=http://localhost:11434 OLLAMA_API_KEY=your_api_key_here ROBOWFLOW_API_KEY=your_api_key_here \ No newline at end of file diff --git a/internal/ollama/ollama.go b/internal/ollama/ollama.go index 80d1aec..f94e700 100644 --- a/internal/ollama/ollama.go +++ b/internal/ollama/ollama.go @@ -7,6 +7,8 @@ import ( "io" "net/http" "os" + + "gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response" ) type Ollama struct { @@ -43,7 +45,7 @@ func NewOllama() *Ollama { func (o *Ollama) SendRequest(userMessage string, fileID string) (string, error) { // Prepare request body reqBody := ChatCompletionRequest{ - Model: "gpt-4-turbo", + Model: "gemma3:12b", Messages: []ChatMessage{ {Role: "user", Content: userMessage}, }, @@ -59,12 +61,14 @@ func (o *Ollama) SendRequest(userMessage string, fileID string) (string, error) // Marshal the request body to JSON jsonBody, err := json.Marshal(reqBody) if err != nil { + response.RespondWithError(nil, nil, http.StatusInternalServerError, "Error marshalling request", err) return "", fmt.Errorf("error marshalling request: %w", err) } // Create the HTTP request req, err := http.NewRequest("POST", "http://localhost:3000/api/chat/completions", bytes.NewBuffer(jsonBody)) if err != nil { + response.RespondWithError(nil, nil, http.StatusInternalServerError, "Error creating request", err) return "", fmt.Errorf("error creating request: %w", err) } @@ -76,6 +80,7 @@ func (o *Ollama) SendRequest(userMessage string, fileID string) (string, error) client := &http.Client{} resp, err := client.Do(req) if err != nil { + response.RespondWithError(nil, nil, http.StatusInternalServerError, "Error sending request", err) return "", fmt.Errorf("error sending request: %w", err) } defer resp.Body.Close() @@ -83,11 +88,13 @@ func (o *Ollama) SendRequest(userMessage string, fileID string) (string, error) // Read the response body, err := io.ReadAll(resp.Body) if err != nil { + response.RespondWithError(nil, nil, http.StatusInternalServerError, "Error reading response", err) return "", fmt.Errorf("error reading response: %w", err) } // Check for non-200 status code if resp.StatusCode != http.StatusOK { + response.RespondWithError(nil, nil, http.StatusInternalServerError, "API request failed", err) return "", fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body)) } @@ -103,7 +110,7 @@ func OllamaRequest(w http.ResponseWriter, r *http.Request) { err := json.NewDecoder(r.Body).Decode(&requestData) if err != nil { - http.Error(w, "Invalid request body", http.StatusBadRequest) + response.RespondWithError(w, r, http.StatusBadRequest, "Invalid request body", err) return } @@ -111,13 +118,13 @@ func OllamaRequest(w http.ResponseWriter, r *http.Request) { ollama := NewOllama() // Send request to Ollama API - response, err := ollama.SendRequest(requestData.Message, requestData.FileID) - if err != nil { - http.Error(w, fmt.Sprintf("Error from Ollama API: %v", err), http.StatusInternalServerError) - return - } + apiResponse, err := ollama.SendRequest(requestData.Message, requestData.FileID) + if err != nil { + response.RespondWithError(w, r, http.StatusInternalServerError, "Error sending request to Ollama API", err) + http.Error(w, fmt.Sprintf("Error from Ollama API: %v", err), http.StatusInternalServerError) + return + } - // Return response - w.Header().Set("Content-Type", "application/json") - w.Write([]byte(response)) + // Return response + response.RespondWithJSON(w, http.StatusOK, map[string]string{"response": apiResponse}) } \ No newline at end of file