Documentation
¶
Overview ¶
Package parser provides functionality for parsing prescription images using different AI backends. It supports multiple AI services (OpenAI and Gemini) for processing prescription forms and extracting structured data from them, with vector embedding generation for similarity search.
Index ¶
- Constants
- Variables
- func GenerateSchema[T any]() map[string]interface{}
- type GeminiParser
- func (p *GeminiParser) GetEmbedding(ctx context.Context, prescription models.Prescription) ([]float32, error)
- func (p *GeminiParser) ParseImage(ctx context.Context, fileName string, file io.Reader) (string, error)
- func (p *GeminiParser) ScoreResult(ctx context.Context, expectedJSON, outputJSON string) (models.ParserResultScore, error)
- func (p *GeminiParser) UploadImage(ctx context.Context, fileName string, file io.Reader) (string, error)
- type OpenAIParser
- func (p *OpenAIParser) GetEmbedding(ctx context.Context, prescription models.Prescription) ([]float32, error)
- func (p *OpenAIParser) ParseImage(ctx context.Context, fileName string, file io.Reader) (string, error)
- func (p *OpenAIParser) ScoreResult(ctx context.Context, expectedJSON, outputJSON string) (models.ParserResultScore, error)
- func (p *OpenAIParser) UploadImage(ctx context.Context, fileName string, file io.Reader) (string, error)
- type Parser
Constants ¶
const (
// JobTypeParsePrescription represents a job for parsing a prescription image.
JobTypeParsePrescription = "parse_prescription"
)
Job type constants used to identify different types of asynchronous jobs in the job tracking system.
Variables ¶
var PrescriptionResponseSchema = GenerateSchema[models.Prescription]()
PrescriptionResponseSchema is a JSON schema that defines the structure for prescription data extraction when using the OpenAI API. It's generated from the models.Prescription struct using reflection.
var ResultScoreSchema = GenerateSchema[models.ParserResultScore]()
ResultScoreSchema is a JSON schema that defines the structure for result score data extraction when using the OpenAI API. It's generated from the models.ParserResultScore struct using reflection.
Functions ¶
func GenerateSchema ¶
GenerateSchema creates a JSON schema from a Go type using reflection. It configures the schema generator to comply with the subset of JSON schema that is supported by the OpenAI API.
The generic type T is the struct type from which to generate the schema. Returns a map representation of the JSON schema that can be provided to the API.
Types ¶
type GeminiParser ¶
type GeminiParser struct {
// contains filtered or unexported fields
}
GeminiParser implements the Parser interface using Google Gemini services. It leverages Gemini's multimodal capabilities to process prescription images and extract structured data from them.
func NewGeminiParser ¶
func NewGeminiParser(cfg config.Config, ds datastore.Datastore, logger *zap.Logger) (*GeminiParser, error)
NewGeminiParser creates a new Gemini-based parser. It initializes a client for the Gemini API with the provided API key and returns a parser instance ready for processing prescription images.
func (*GeminiParser) GetEmbedding ¶
func (p *GeminiParser) GetEmbedding(ctx context.Context, prescription models.Prescription) ([]float32, error)
GetEmbedding generates embeddings for a prescription using Gemini embeddings API. It converts the prescription to JSON and sends it to the Gemini API to generate a vector representation for similarity search.
func (*GeminiParser) ParseImage ¶
func (p *GeminiParser) ParseImage(ctx context.Context, fileName string, file io.Reader) (string, error)
ParseImage handles parsing a prescription image using Gemini multimodal API. It creates an asynchronous job to process the image and returns the job ID. The actual processing is done in a separate goroutine.
func (*GeminiParser) ScoreResult ¶
func (p *GeminiParser) ScoreResult(ctx context.Context, expectedJSON, outputJSON string) (models.ParserResultScore, error)
ScoreResult scores the result of a parser against a validated expected JSON.
func (*GeminiParser) UploadImage ¶
func (p *GeminiParser) UploadImage(ctx context.Context, fileName string, file io.Reader) (string, error)
UploadImage uploads an image using the Gemini Files API. It validates the file type, uploads the file to Gemini, and returns a URI that can be used to reference the image in subsequent API calls.
type OpenAIParser ¶
type OpenAIParser struct {
// contains filtered or unexported fields
}
OpenAIParser implements the Parser interface using OpenAI services. It uses OpenAI's vision and embedding capabilities to process prescription images and extract structured data from them.
func NewOpenAIParser ¶
func NewOpenAIParser(cfg config.Config, ds datastore.Datastore, logger *zap.Logger) (*OpenAIParser, error)
NewOpenAIParser creates a new OpenAI-based parser. It initializes a connection to the OpenAI API with the provided API key and returns a parser instance ready for processing prescription images.
func (*OpenAIParser) GetEmbedding ¶
func (p *OpenAIParser) GetEmbedding(ctx context.Context, prescription models.Prescription) ([]float32, error)
GetEmbedding generates embeddings for a prescription using OpenAI. It converts the prescription to JSON and sends it to the OpenAI API to generate a vector representation for similarity search.
func (*OpenAIParser) ParseImage ¶
func (p *OpenAIParser) ParseImage(ctx context.Context, fileName string, file io.Reader) (string, error)
ParseImage handles parsing a prescription image using OpenAI vision API. It creates an asynchronous job to process the image and returns the job ID. The actual processing is done in a separate goroutine.
func (*OpenAIParser) ScoreResult ¶
func (p *OpenAIParser) ScoreResult(ctx context.Context, expectedJSON, outputJSON string) (models.ParserResultScore, error)
ScoreResult scores the result of a parser against a validated expected JSON.
func (*OpenAIParser) UploadImage ¶
func (p *OpenAIParser) UploadImage(ctx context.Context, fileName string, file io.Reader) (string, error)
UploadImage uploads an image to the OpenAI API. It validates the file type, uploads the file to OpenAI, and returns an ID that can be used to reference the image in subsequent API calls.
type Parser ¶
type Parser interface {
// ParseImage processes a prescription image asynchronously and returns a job ID for tracking parsing progress.
// It takes a filename and file reader, initiates an asynchronous job, and returns the job ID.
ParseImage(ctx context.Context, fileName string, file io.Reader) (string, error)
// GetEmbedding generates an embedding vector for a prescription.
// This vector representation can be used for similarity searches and document clustering.
GetEmbedding(ctx context.Context, prescription models.Prescription) ([]float32, error)
// UploadImage uploads an image to persistent storage and returns its ID.
// The image can then be referenced in subsequent API calls.
UploadImage(ctx context.Context, fileName string, file io.Reader) (string, error)
// ScoreResult scores the result of a parser against a validated expected JSON.
ScoreResult(ctx context.Context, expectedJSON, outputJSON string) (models.ParserResultScore, error)
}
Parser defines the interface for prescription parsing services