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
Changes from 1 commit
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
46 changes: 44 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,23 @@ func createLLM() (llms.Model, error) {
if openaiAPIKey == "" {
return nil, fmt.Errorf("OpenAI API key is not set")
}

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Reduce code duplication in transport creation.

The custom transport creation is duplicated between createLLM and createVisionLLM. Consider extracting this into a helper function:

+// createCustomHTTPClient returns an http.Client with custom headers
+func createCustomHTTPClient() *http.Client {
+	customTransport := &headerTransport{
+		transport: http.DefaultTransport,
+		headers: map[string]string{
+			"X-Title": "paperless-gpt",
+		},
+	}
+	client := http.DefaultClient
+	client.Transport = customTransport
+	return client
+}

 func createLLM() (llms.Model, error) {
 	switch strings.ToLower(llmProvider) {
 	case "openai":
 		if openaiAPIKey == "" {
 			return nil, fmt.Errorf("OpenAI API key is not set")
 		}
-		// 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
+		httpClient := createCustomHTTPClient()

Also applies to: 681-692

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

// 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 openai.New(
openai.WithModel(visionLlmModel),
openai.WithToken(openaiAPIKey),
openai.WithHTTPClient(httpClient),
)
case "ollama":
host := os.Getenv("OLLAMA_HOST")
Expand All @@ -681,3 +709,17 @@ func createVisionLLM() (llms.Model, error) {
return nil, nil
}
}

// 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)
}
Loading