8
8
"paperless-gpt/ocr"
9
9
"path/filepath"
10
10
"runtime"
11
- "strconv"
12
11
"slices"
13
- "strings"
12
+ "strconv"
13
+ "strings"
14
14
"sync"
15
15
"text/template"
16
16
"time"
@@ -639,9 +639,23 @@ func createLLM() (llms.Model, error) {
639
639
if openaiAPIKey == "" {
640
640
return nil , fmt .Errorf ("OpenAI API key is not set" )
641
641
}
642
+
643
+ // Create custom transport that adds headers
644
+ customTransport := & headerTransport {
645
+ transport : http .DefaultTransport ,
646
+ headers : map [string ]string {
647
+ "X-Title" : "paperless-gpt" ,
648
+ },
649
+ }
650
+
651
+ // Create custom client with the transport
652
+ httpClient := http .DefaultClient
653
+ httpClient .Transport = customTransport
654
+
642
655
return openai .New (
643
656
openai .WithModel (llmModel ),
644
657
openai .WithToken (openaiAPIKey ),
658
+ openai .WithHTTPClient (httpClient ),
645
659
)
646
660
case "ollama" :
647
661
host := os .Getenv ("OLLAMA_HOST" )
@@ -663,9 +677,23 @@ func createVisionLLM() (llms.Model, error) {
663
677
if openaiAPIKey == "" {
664
678
return nil , fmt .Errorf ("OpenAI API key is not set" )
665
679
}
680
+
681
+ // Create custom transport that adds headers
682
+ customTransport := & headerTransport {
683
+ transport : http .DefaultTransport ,
684
+ headers : map [string ]string {
685
+ "X-Title" : "paperless-gpt" ,
686
+ },
687
+ }
688
+
689
+ // Create custom client with the transport
690
+ httpClient := http .DefaultClient
691
+ httpClient .Transport = customTransport
692
+
666
693
return openai .New (
667
694
openai .WithModel (visionLlmModel ),
668
695
openai .WithToken (openaiAPIKey ),
696
+ openai .WithHTTPClient (httpClient ),
669
697
)
670
698
case "ollama" :
671
699
host := os .Getenv ("OLLAMA_HOST" )
@@ -681,3 +709,17 @@ func createVisionLLM() (llms.Model, error) {
681
709
return nil , nil
682
710
}
683
711
}
712
+
713
+ // headerTransport is a custom http.RoundTripper that adds custom headers to requests
714
+ type headerTransport struct {
715
+ transport http.RoundTripper
716
+ headers map [string ]string
717
+ }
718
+
719
+ // RoundTrip implements the http.RoundTripper interface
720
+ func (t * headerTransport ) RoundTrip (req * http.Request ) (* http.Response , error ) {
721
+ for key , value := range t .headers {
722
+ req .Header .Add (key , value )
723
+ }
724
+ return t .transport .RoundTrip (req )
725
+ }
0 commit comments