docqa

package module
v0.0.0-...-ea841f8 Latest Latest
Warning

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

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

README

docqa - A Typed Question/Answering Framework for LLMs

This is a toy project, it is not, nor will ever be stable

  • DocQA makes it easy to ask questions about a document and to recieve answers in a specific format.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone[T PureData](v T) T

Types

type Attributes

type Attributes struct {
	Position Range
	Evidence []Range
}

func (Attributes) Clone

func (a Attributes) Clone() PureData

Clone implements PureData.

func (Attributes) Eq

func (a Attributes) Eq(other PureData) bool

Eq implements PureData.

func (Attributes) Validate

func (a Attributes) Validate() error

Validate implements PureData.

type Client

type Client interface {
	// GetLLMResponse prompts the LLM and gets its text response, hopefully (not certainly) with the given schema.
	GetLLMResponse(systemPrompt string, userPrompt string, schema map[string]any) (string, LLMUsage, error)
}

Client is a wrapper around an LLM with schema capabilities.

func NewOpenAIClient

func NewOpenAIClient(key, model string) Client

NewOpenAIClient creates a new client that communicates with the OpenAI API.

type Entity

type Entity interface {
	PureData
	Attr() *Attributes
	// contains filtered or unexported methods
}

type LLMType

type LLMType interface {
	Parse(value string) (Entity, error)
	Instructions() LLMTypeInstructions
}

Type describes an object capable of parsing a response to a Question into an Entity.

type LLMTypeInstructions

type LLMTypeInstructions struct {
	OneLiner string
	Details  []string
}

TypeInstructions define how to respond with a [Type] for the LLM.

type LLMUsage

type LLMUsage struct {
	InputTokens  int
	OutputTokens int
}

func ExtractAnswers

func ExtractAnswers(client Client, qa Protocol, questions map[string]Question, documentText string) (map[string][]Entity, LLMUsage, error)

ExtractAnswers answers the given [Question]s about a document, returning lists of Entity keyed by question key.

type Protocol

type Protocol interface {
	// Schema creates a jsonschema given a set of keyed questions.
	Schema(qs map[string]Question) map[string]any
	// SystemPrompt builds a system prompt for the given set of [Question]s.
	SystemPrompt(qs map[string]Question) string
	// ParseResponse takes a raw LLM response and parses it into lists of [Entity], keyed by question key.
	ParseResponse(resp string) (map[string][]Entity, error)
}

Protocol defines a method of communication to and from the LLM.

func NewBasicProtocol

func NewBasicProtocol(roleAndTask RoleAndTask, types map[string]LLMType) Protocol

type PureData

type PureData interface {
	Validate() error
	Clone() PureData
	Eq(PureData) bool
}

type Question

type Question struct {
	// Question is the one-liner, for example `Who is the author of this document?`.
	Question string `json:"question"`
	// Details provides extra details, examples, and instructions on how to answer this question, as bullet points.
	Details []string `json:"details"`
	// AllowedTypeKeys lists all the keys of the types which the LLM may respond with.
	AllowedTypeKeys []string `json:"allowed_type_keys"`
}

Question defines a sepcific question to send to the LLM.

type Range

type Range struct {
	Start int `json:"start"`
	End   int `json:"end"`
}

Range defines a range of characters from the source document.

func IndefRange

func IndefRange() Range

IndefRange creates a new Range that is indefinite.

func (Range) IsIndef

func (r Range) IsIndef() bool

IsIndef checks whether the specific Range in indefinite.

func (Range) Len

func (r Range) Len() int

Len checks how far the Range spans.

type RoleAndTask

type RoleAndTask struct {
	Role string
	Task string
}

RoleAndTask defines a role and a task for the LLM, both of which should be short (~ 1 sentence).

func GetDefaultRoleAndTask

func GetDefaultRoleAndTask() RoleAndTask

GetDefaultRoleAndTask builds a RoleAndTask for a generic document information extraction task.

type Value

type Value interface {
	PureData
}

type ValueEntity

type ValueEntity[T Value] struct {
	Attributes Attributes
	Value      T
}

func NewValueEntity

func NewValueEntity[T Value](v T) *ValueEntity[T]

func (*ValueEntity[T]) Attr

func (e *ValueEntity[T]) Attr() *Attributes

func (*ValueEntity[T]) Clone

func (e *ValueEntity[T]) Clone() PureData

func (*ValueEntity[T]) Eq

func (e *ValueEntity[T]) Eq(other PureData) bool

Eq implements PureData.

func (*ValueEntity[T]) String

func (e *ValueEntity[T]) String() string

func (*ValueEntity[T]) Validate

func (e *ValueEntity[T]) Validate() error

Jump to

Keyboard shortcuts

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