Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom HTTP transport with headers for OpenAI client #245

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"paperless-gpt/ocr"
"path/filepath"
"runtime"
"strconv"
"slices"
"strings"
"strconv"
"strings"
"sync"
"text/template"
"time"
Expand Down Expand Up @@ -639,9 +639,11 @@ func createLLM() (llms.Model, error) {
if openaiAPIKey == "" {
return nil, fmt.Errorf("OpenAI API key is not set")
}

return openai.New(
openai.WithModel(llmModel),
openai.WithToken(openaiAPIKey),
openai.WithHTTPClient(createCustomHTTPClient()),
)
case "ollama":
host := os.Getenv("OLLAMA_HOST")
Expand All @@ -663,9 +665,11 @@ func createVisionLLM() (llms.Model, error) {
if openaiAPIKey == "" {
return nil, fmt.Errorf("OpenAI API key is not set")
}

return openai.New(
openai.WithModel(visionLlmModel),
openai.WithToken(openaiAPIKey),
openai.WithHTTPClient(createCustomHTTPClient()),
)
case "ollama":
host := os.Getenv("OLLAMA_HOST")
Expand All @@ -681,3 +685,33 @@ func createVisionLLM() (llms.Model, error) {
return nil, nil
}
}

func createCustomHTTPClient() *http.Client {
// Create custom transport that adds headers
customTransport := &headerTransport{
transport: http.DefaultTransport,
headers: map[string]string{
"X-Title": "paperless-gpt",
},
}

// Create custom client with the transport
httpClient := http.DefaultClient
httpClient.Transport = customTransport

return httpClient
}

// headerTransport is a custom http.RoundTripper that adds custom headers to requests
type headerTransport struct {
transport http.RoundTripper
headers map[string]string
}

// RoundTrip implements the http.RoundTripper interface
func (t *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
for key, value := range t.headers {
req.Header.Add(key, value)
}
return t.transport.RoundTrip(req)
}
22 changes: 22 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"slices"
"testing"
"text/template"
Expand Down Expand Up @@ -175,3 +176,24 @@ func TestProcessAutoTagDocuments(t *testing.T) {
})
}
}

func TestCreateCustomHTTPClient(t *testing.T) {
// Create a test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify custom header
assert.Equal(t, "paperless-gpt", r.Header.Get("X-Title"), "Expected X-Title header")
w.WriteHeader(http.StatusOK)
}))
defer server.Close()

// Get custom client
client := createCustomHTTPClient()
require.NotNil(t, client, "HTTP client should not be nil")

// Make a request
resp, err := client.Get(server.URL)
require.NoError(t, err, "Request should not fail")
defer resp.Body.Close()

assert.Equal(t, http.StatusOK, resp.StatusCode, "Expected 200 OK response")
}
Loading