Python API reference
ragna.local_root(path=None)
Get or set the local root directory Ragna uses for storing files.
Defaults to the value of the RAGNA_LOCAL_ROOT
environment variable or otherwise to
~/.cache/ragna
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Optional[Union[str, Path]]
|
If passed, this is set as new local root directory. |
None
|
Returns:
Type | Description |
---|---|
Path
|
Ragnas local root directory. |
ragna.core
Assistant
Abstract base class for assistants used in ragna.core.Chat
Chat
Tip
This object is usually not instantiated manually, but rather through ragna.core.Rag.chat.
A chat needs to be prepare
d before prompts can be
answer
ed.
Can be used as context manager to automatically invoke
prepare
:
rag = Rag()
async with rag.chat(
documents=[path],
source_storage=ragna.core.RagnaDemoSourceStorage,
assistant=ragna.core.RagnaDemoAssistant,
) as chat:
print(await chat.answer("What is Ragna?"))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rag |
Rag
|
The RAG workflow this chat is associated with. |
required |
documents |
Iterable[Any]
|
Documents to use. If any item is not a ragna.core.Document, ragna.core.LocalDocument.from_path is invoked on it. |
required |
source_storage |
Union[Type[SourceStorage], SourceStorage]
|
Source storage to use. |
required |
assistant |
Union[Type[Assistant], Assistant]
|
Assistant to use. |
required |
**params |
Any
|
Additional parameters passed to the source storage and assistant. |
{}
|
answer(prompt, *, stream=False)
async
Answer a prompt.
Returns:
Type | Description |
---|---|
Message
|
Answer. |
Raises:
Type | Description |
---|---|
RagnaException
|
If chat is not
|
prepare()
async
Prepare the chat.
This store
s the documents in the selected
source storage. Afterwards prompts can be answer
ed.
Returns:
Type | Description |
---|---|
Message
|
Welcome message. |
Raises:
Type | Description |
---|---|
RagnaException
|
If chat is already
|
Component
Base class for RAG components.
display_name()
classmethod
Returns:
Type | Description |
---|---|
str
|
Component name. |
Document
Abstract base class for all documents.
get_handler(name)
staticmethod
Get a document handler based on a suffix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the document. |
required |
supported_suffixes()
staticmethod
Returns:
Type | Description |
---|---|
set[str]
|
Suffixes, i.e. |
DocumentHandler
Base class for all document handlers.
extract_pages(document)
abstractmethod
DocxDocumentHandler
Document handler for .docx
documents.
Note
This does not extract text from headers or footers.
Package requirements
LocalDocument
Document class for files on the local file system.
Tip
This object is usually not instantiated manually, but rather through ragna.core.LocalDocument.from_path.
from_path(path, *, id=None, metadata=None, handler=None)
classmethod
Create a ragna.core.LocalDocument from a path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, Path]
|
Local path to the file. |
required |
id |
Optional[UUID]
|
ID of the document. If omitted, one is generated. |
None
|
metadata |
Optional[dict[str, Any]]
|
Optional metadata of the document. |
None
|
handler |
Optional[DocumentHandler]
|
Document handler. If omitted, a builtin handler is selected based
on the suffix of the |
None
|
Raises:
Type | Description |
---|---|
RagnaException
|
If |
Message
Data class for messages.
Attributes:
Name | Type | Description |
---|---|---|
role |
The message producer. |
|
sources |
The sources used to produce the message. |
MessageRole
Message role
Attributes:
Name | Type | Description |
---|---|---|
SYSTEM |
The message was produced by the system. This includes the welcome message when preparing a new chat as well as error messages. |
|
USER |
The message was produced by the user. |
|
ASSISTANT |
The message was produced by an assistant. |
Page
PdfDocumentHandler
PlainTextDocumentHandler
Document handler for plain-text documents.
Currently supports .txt
and .md
extensions.
PptxDocumentHandler
Rag
RAG workflow.
Tip
This class can be imported from ragna
directly, e.g.
from ragna import Rag
chat(*, documents, source_storage, assistant, **params)
Create a new ragna.core.Chat.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
documents |
Iterable[Any]
|
Documents to use. If any item is not a ragna.core.Document, ragna.core.LocalDocument.from_path is invoked on it. |
required |
source_storage |
Union[Type[SourceStorage], SourceStorage]
|
Source storage to use. |
required |
assistant |
Union[Type[Assistant], Assistant]
|
Assistant to use. |
required |
**params |
Any
|
Additional parameters passed to the source storage and assistant. |
{}
|
RagnaException
Ragna exception.
Source
Data class for sources stored inside a source storage.
Attributes:
Name | Type | Description |
---|---|---|
id |
str
|
Unique ID of the source. |
document |
Document
|
Document this source belongs to. |
location |
str
|
Location of the source inside the document. |
content |
str
|
Content of the source. |
num_tokens |
int
|
Number of tokens of the content. |
SourceStorage
retrieve(documents, prompt)
abstractmethod
ragna.source_storages
Chroma
LanceDB
RagnaDemoSourceStorage
Demo assistant without requirements.
Note
As the name implies, this source storage is just for demo purposes and cannot retrieve relevant sources for a given prompt. It returns a single ragna.core.Source per stored ragna.core.Document with potentially shortened text extracted from the first ragna.core.Page.
ragna.assistants
Claude
ClaudeInstant
Command
CommandLight
GeminiPro
GeminiUltra
Gpt35Turbo16k
Gpt4
Jurassic2Ultra
RagnaDemoAssistant
Demo assistant without requirements.
Note
As the name implies, this assistant is just for demo purposes and cannot answer any questions. By default it replies with the prompt and the given sources.
If you include the phrase "markdown"
into your prompt, it will return a
Markdown table including emojis.
ragna.deploy
Authentication
Abstract base class for authentication used by the REST API.
create_token(request)
abstractmethod
async
Config
Ragna configuration
RagnaDemoAuthentication
Demo OAuth2 password authentication without requirements.
Danger
As the name implies, this authentication is just for demo purposes and should not be used in production.
create_token(request)
async
Authenticate user and create an authorization token.
User name is arbitrary. Authentication is possible in two ways:
- If the
RAGNA_DEMO_AUTHENTICATION_PASSWORD
environment variable is set, the password is checked against that. - Otherwise, the password has to match the user name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
Request send to the |
required |
Returns:
Type | Description |
---|---|
str
|
Authorization JWT that expires after one week. |
get_user(request)
async
Get user from an authorization token.
Token has to be supplied in the
Bearer authentication scheme,
i.e. including a Authorization: Bearer {token}
header.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
Request send to any endpoint of the REST API that requires authorization. |
required |
Returns:
Type | Description |
---|---|
str
|
Authorized user. |