router fixes and ollama
Some checks failed
golangci-lint / lint (push) Failing after 18s
build / Build (push) Successful in 44s
Build and Push Docker Image / Build and push image (push) Successful in 2m22s
Run Go Tests / build (push) Failing after 0s

This commit is contained in:
2025-05-06 16:28:02 -07:00
parent 02da82fea1
commit 92496bd402
3 changed files with 125 additions and 1 deletions

View File

@@ -1,4 +1,6 @@
APP_PORT=8080
OLLAMA_API_URL=https://openweb-ui.miguelmuniz.com
OLLAMA_API_KEY=your_api_key_here
ROBOWFLOW_API_KEY=your_api_key_here

View File

@@ -1,2 +1,123 @@
package ollama
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
type Ollama struct {
OllamaKey string
Message string
Image string
}
// ChatMessage represents a message in the chat completion request
type ChatMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
// FileInfo represents a file in the chat completion request
type FileInfo struct {
Type string `json:"type"`
ID string `json:"id"`
}
// ChatCompletionRequest represents the request body for the chat completion API
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Files []FileInfo `json:"files,omitempty"`
}
func NewOllama() *Ollama {
return &Ollama{
OllamaKey: os.Getenv("OLLAMA_API_KEY"),
}
}
func (o *Ollama) SendRequest(userMessage string, fileID string) (string, error) {
// Prepare request body
reqBody := ChatCompletionRequest{
Model: "gpt-4-turbo",
Messages: []ChatMessage{
{Role: "user", Content: userMessage},
},
}
// Add file if fileID is provided
if fileID != "" {
reqBody.Files = []FileInfo{
{Type: "file", ID: fileID},
}
}
// Marshal the request body to JSON
jsonBody, err := json.Marshal(reqBody)
if err != nil {
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 {
return "", fmt.Errorf("error creating request: %w", err)
}
// Add headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+o.OllamaKey)
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()
// Read the response
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response: %w", err)
}
// Check for non-200 status code
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
return string(body), nil
}
func OllamaRequest(w http.ResponseWriter, r *http.Request) {
// Parse request body
var requestData struct {
Message string `json:"message"`
FileID string `json:"fileId,omitempty"`
}
err := json.NewDecoder(r.Body).Decode(&requestData)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
// Create ollama client using API key from environment
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
}
// Return response
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(response))
}

View File

@@ -12,6 +12,7 @@ import (
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/limiter"
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/logger"
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/ollama"
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response"
)
@@ -67,7 +68,7 @@ func SetupRouter(origins []string) *Router {
response.RespondWithJSON(w, http.StatusTeapot, map[string]string{"error": "I'm A Teapot!"})
})
// subRouter.Post("/text", handlers.TextPromptHandler())
subRouter.Post("/text", ollama.OllamaRequest)
// subRouter.Post("/visual", handlers.VisualAIHandler())
})
})