added better error response
Some checks failed
golangci-lint / lint (push) Failing after 21s
Run Go Tests / build (push) Failing after 0s
build / Build (push) Successful in 34s
Build and Push Docker Image / Build and push image (push) Successful in 2m19s

This commit is contained in:
2025-05-06 16:47:54 -07:00
parent 92496bd402
commit c570cd506a
2 changed files with 18 additions and 11 deletions

View File

@@ -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

View File

@@ -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})
}