-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_extract.go
More file actions
71 lines (63 loc) · 2.44 KB
/
Copy pathparse_extract.go
File metadata and controls
71 lines (63 loc) · 2.44 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2025 SIXT SE
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tensorlake
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// ExtractDocumentRequest holds options for extracting structured data from a document.
type ExtractDocumentRequest struct {
FileSource
StructuredExtractionOptions []StructuredExtractionOptions `json:"structured_extraction_options"`
PageRange string `json:"page_range,omitempty"`
MimeType string `json:"mime_type,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
// FileName is the name of the file. This is populated when file_id is used.
//
// Optional.
FileName string `json:"file_name,omitempty"`
}
// ExtractDocument submits a document for structured data extraction.
//
// See also: [Extract Document API Reference]
//
// [Extract Document API Reference]: https://docs.tensorlake.ai/api-reference/v2/parse/extract
func (c *Client) ExtractDocument(ctx context.Context, in *ExtractDocumentRequest) (*ParseJob, error) {
// Validate that exactly one source is provided
if !in.SourceProvided() {
return nil, fmt.Errorf("exactly one of file_id, file_url, or raw_text must be provided")
}
if len(in.StructuredExtractionOptions) == 0 {
return nil, fmt.Errorf("at least one structured_extraction_options must be provided")
}
body, err := json.Marshal(in)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/extract", bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
return do(c, req, func(r io.Reader) (*ParseJob, error) {
var result ParseJob
if err := json.NewDecoder(r).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result, nil
})
}