forked from reVrost/go-openrouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_helper.go
More file actions
54 lines (49 loc) · 1.36 KB
/
pdf_helper.go
File metadata and controls
54 lines (49 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package openrouter
import (
"os"
"strings"
)
// CreatePDFPlugin creates a completion plugin to process PDFs using the specified engine.
// The engine can be: "mistral-ocr" (for scanned documents/PDFs with images),
// "pdf-text" (for well-structured PDFs - free), or "native" (only for models that support file input).
func CreatePDFPlugin(engine PDFEngine) ChatCompletionPlugin {
return ChatCompletionPlugin{
ID: PluginIDFileParser,
PDF: &PDFPlugin{
Engine: string(engine),
},
}
}
// UserMessageWithPDFFromFile creates a user message with text and PDF content from a file.
// It reads the PDF file and creates a message with the embedded PDF data.
func UserMessageWithPDFFromFile(text, filePath string) (ChatCompletionMessage, error) {
fileData, err := os.ReadFile(filePath)
if err != nil {
return ChatCompletionMessage{}, err
}
filename := filePath
if idx := strings.LastIndex(filePath, "\\"); idx != -1 {
filename = filePath[idx+1:]
}
if idx := strings.LastIndex(filename, "/"); idx != -1 {
filename = filename[idx+1:]
}
return ChatCompletionMessage{
Role: ChatMessageRoleUser,
Content: Content{
Multi: []ChatMessagePart{
{
Type: ChatMessagePartTypeText,
Text: text,
},
{
Type: ChatMessagePartTypeFile,
File: &FileContent{
Filename: filename,
FileData: string(fileData),
},
},
},
},
}, nil
}