mistral

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 8 Imported by: 0

README

mistral-api-go

A Go client library for accessing the Mistral AI API's OCR capabilities.

Still under development and has limited functionality. Mostly focused on the OCR + File Upload endpoints.

Installation

go get github.com/tforrest/mistral-api-go

Usage

import "github.com/tforrest/mistral-api-go"

// Create a new client
client, err := mistral.NewClient("your-api-key")
if err != nil {
    log.Fatal(err)
}

// Process a document with OCR
resp, err := client.OCR.Process(&OCRRequest{
    Document: OCRRequestModel{
        Type:         "document_url",
        DocumentURL:  "https://arxiv.org/pdf/2201.04234",
        DocumentName: "2201.04234.pdf",
    },
    Model: "mistral-ocr-latest",
})
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Processed %d pages\n", len(resp.Pages))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {

	// User agent used when communicating with Mistral AI API.
	UserAgent string

	// Services used for communicating with different parts of the Mistral AI API.
	OCR *OCRService

	Files *FilesService

	Embedding *EmbeddingService
	// contains filtered or unexported fields
}

Client manages communication with Mistral AI API.

func NewClient

func NewClient(apiKey string, opts ...ClientOption) (*Client, error)

NewClient creates a new Mistral AI API client.

func (*Client) AuthHeader added in v0.0.2

func (c *Client) AuthHeader() string

func (*Client) FormUrl added in v0.0.2

func (c *Client) FormUrl(path string) string

type ClientOption

type ClientOption func(*Client) error

ClientOption is a function that modifies the client.

func WithBaseURL

func WithBaseURL(baseURL string) ClientOption

WithBaseURL sets a custom base URL for the client.

type Dimensions added in v0.0.2

type Dimensions struct {
	DPI    int `json:"dpi"`
	Width  int `json:"width"`
	Height int `json:"height"`
}

type DocumentUrlResponse added in v0.0.2

type DocumentUrlResponse struct {
	Url string `json:"url"`
}

type EmbeddingData

type EmbeddingData struct {
	Object    string    `json:"object"`
	Index     int       `json:"index"`
	Embedding []float64 `json:"embedding"`
}

type EmbeddingRequest

type EmbeddingRequest struct {
	Model string   `json:"model"`
	Input []string `json:"input"`
}

type EmbeddingResponse

type EmbeddingResponse struct {
	Id     string          `json:"id"`
	Object string          `json:"object"`
	Model  string          `json:"model"`
	Data   []EmbeddingData `json:"data"`
	Usage  EmbeddingUsage  `json:"usage"`
}

type EmbeddingService added in v0.0.2

type EmbeddingService struct {
	// contains filtered or unexported fields
}

func (*EmbeddingService) Create added in v0.0.2

func (s *EmbeddingService) Create(request *EmbeddingRequest) (*EmbeddingResponse, error)

type EmbeddingUsage added in v0.0.2

type EmbeddingUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

type Error

type Error struct {
	Response *http.Response
	Message  string
}

Error represents an error response from the API.

func (*Error) Error

func (e *Error) Error() string

type FileType added in v0.0.2

type FileType string
const (
	FileTypePDF   FileType = "pdf"
	FileTypeImage FileType = "image"
)

type FileUploadRequest added in v0.0.2

type FileUploadRequest struct {
	File    *os.File
	Purpose string
	Type    FileType
}

type FileUploadResponse added in v0.0.2

type FileUploadResponse struct {
	ID         string `json:"id"`
	Object     string `json:"object"`
	Bytes      int    `json:"bytes"`
	CreatedAt  int64  `json:"created_at"`
	Filename   string `json:"filename"`
	Purpose    string `json:"purpose"`
	SampleType string `json:"sample_type"`
	NumLines   *int   `json:"num_lines,omitempty"`
	Source     string `json:"source"`
}

type FilesService

type FilesService struct {
	// contains filtered or unexported fields
}

func (*FilesService) GetDocumentUrl added in v0.0.2

func (s *FilesService) GetDocumentUrl(id string, expiry int) (*DocumentUrlResponse, error)

func (*FilesService) GetFiles added in v0.0.2

func (s *FilesService) GetFiles(getFilesRequest *GetFilesRequest) (*GetFilesResponse, error)

func (*FilesService) Upload

func (s *FilesService) Upload(fileUploadRequest *FileUploadRequest) (*FileUploadResponse, error)

type GetFilesRequest added in v0.0.2

type GetFilesRequest struct {
	Page       int          `json:"page"`
	PageSize   int          `json:"page_size"`
	Source     string       `json:"source"`
	Search     string       `json:"search"`
	Purpose    string       `json:"purpose"`
	SampleType []SampleType `json:"sample_type"`
}

type GetFilesResponse added in v0.0.2

type GetFilesResponse struct {
	Data   []map[string]interface{} `json:"data"`
	Total  int                      `json:"total"`
	Object string                   `json:"object"`
}

type OCRImage added in v0.0.2

type OCRImage struct {
	ID           int    `json:"id"`
	TopLeftX     int    `json:"top_left_x"`
	TopLeftY     int    `json:"top_left_y"`
	BottomRightX int    `json:"bottom_right_x"`
	BottomRightY int    `json:"bottom_right_y"`
	ImageBase64  string `json:"image_base64"`
}

type OCRPage added in v0.0.2

type OCRPage struct {
	Index      int        `json:"index"`
	Markdown   string     `json:"markdown"`
	Images     []OCRImage `json:"images"`
	Dimensions Dimensions `json:"dimensions"`
}

type OCRRequest

type OCRRequest struct {
	Model              string          `json:"model"`
	ID                 string          `json:"id"`
	Document           OCRRequestModel `json:"document"`
	Pages              []int           `json:"pages"`
	IncludeImageBase64 bool            `json:"include_image_base64"`
	ImageLimit         int             `json:"image_limit"`
	ImageMinSize       int             `json:"image_min_size"`
}

OCRRequest represents a request to perform OCR on an image.

type OCRRequestModel added in v0.0.2

type OCRRequestModel struct {
	Type         string `json:"type" enum:"document_url"`
	DocumentURL  string `json:"document_url,omitempty" binding:"required_if=Type document_url"`
	ImageURL     string `json:"image_url,omitempty" binding:"required_if=Type image_url"`
	DocumentName string `json:"document_name,omitempty"`
}

type OCRResponse

type OCRResponse struct {
	Pages []OCRPage `json:"pages" binding:"required"`
	Model string    `json:"model" binding:"required"`
	Usage OCRUsage  `json:"usage" binding:"required"`
}

type OCRService

type OCRService struct {
	// contains filtered or unexported fields
}

OCRService handles communication with the OCR related methods of the Mistral AI API.

func (*OCRService) Process added in v0.0.2

func (s *OCRService) Process(request *OCRRequest) (*OCRResponse, error)

Process submits an OCR request

type OCRUsage added in v0.0.2

type OCRUsage struct {
	PagesProcessed int `json:"pages_processed"`
	DocSizeBytes   int `json:"doc_size_bytes"`
}

type SampleType added in v0.0.2

type SampleType string
const (
	SampleTypePretrain     SampleType = "pretrain"
	SampleTypeInstruct     SampleType = "instruct"
	SampleTypeBatchRequest SampleType = "batch_request"
	SampleTypeBatchResult  SampleType = "batch_result"
	SampleTypeBatchError   SampleType = "batch_error"
	SampleTypeOCRInput     SampleType = "ocr_input"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL