Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DYNARAG_API_TOKEN=<your_jwt_token>
DYNARAG_BASE_URL=<your_base_url>
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
# DynaRAG

This is the Python client to the [DynaRAG API](https://www.dynarag.com).
This is the Python client to [DynaRAG](https://github.com/Predixus/DynaRAG).

DynaRAG provides a simple and fast interface to implement RAG (Retrieval Augemented Generation) into your application.
DynaRAG provides a simple and fast interface to implement RAG (Retrieval Augemented Generation)
into your application.

## Configuration

DynaRAG only requires an API token to get started. You can obtain an API token by going to the [DynaRAG App](https://app.dynarag.com),
DynaRAG requires some environment variables to get started:
- `DYNARAG_API_TOKEN` - a signed JWT that contains data needed by DynaRAG. Follow the spec in the [DynaRAG](https://github.com/Predixus/DynaRAG)
service repo.
- `DYNARAG_BASE_URL` - url to the DynaRAG service. e.g. http://localhost:7890

You can obtain an API token by going to the [DynaRAG App](https://app.dynarag.com),
logging in and navigating to dashboard>developer. From here you can generate an API token.

> :warning: **API Token**: Put your API token into an environment varibale called `DYNARAG_API_TOKEN` in order for the DynaRAG Client to discover it.
> :warning: **API Token**: Put your API token into an environment variable called `DYNARAG_API_TOKEN`
> in order for the DynaRAG Client to discover it. This must be a JWT that follows the spec of the
> [DynaRAG service](https://github.com/Predixus/DynaRAG).
## Usage

Expand Down
1 change: 0 additions & 1 deletion dynarag/constants.py

This file was deleted.

4 changes: 4 additions & 0 deletions dynarag/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ class MissingAPIToken(BaseException):

class BadAPIRequest(BaseException):
"""Raised if a request to the DynaRAG API fails"""


class BadEnvironment(BaseException):
"""Raised when the environment is bad"""
30 changes: 23 additions & 7 deletions dynarag/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
import urllib.request
from typing import Any, Dict, List, TypeVar, Optional, TypedDict, cast

from dynarag.constants import DYNARAG_BASE_URL
from dynarag.exceptions import BadAPIRequest, MissingAPIToken
from dynarag.exceptions import BadAPIRequest, BadEnvironment, MissingAPIToken

LOGGER = logging.getLogger(__name__)

try:
from dotenv import load_dotenv

load_dotenv()
LOGGER.info("Environment variables loaded from .env file if it exists")
except ImportError:
LOGGER.debug("python-dotenv not installed, skipping .env loading")

T = TypeVar("T")


Expand Down Expand Up @@ -51,17 +58,26 @@ class DynaRAGClient:
def __init__(self) -> None:
"""Initialise the DynaRAG client."""
api_token = os.environ.get("DYNARAG_API_TOKEN", None)
base_url = os.environ.get("DYNARAG_BASE_URL", None)
if not api_token:
error_str = "Could not find the `DYNARAG_API_TOKEN` environment variable. "
LOGGER.error(error_str)
raise MissingAPIToken(error_str)
if not base_url:
error_str = (
"Could not find the `DYNARAG_API_TOKEN` environment variable."
"You can obtain one by going to https://app.dynarag.com/dashboard/developer and generate a token."
"Could not find the `DYNARAG_BASE_URL` environment variable. "
"Set it to the URL of your DynaRAG service, e.g. http://localhost:7890. "
"Follow the instructions in the DynaRAG service repo to get started: "
"https://github.com/predixus/dynarag?tab=readme-ov-file#getting-started"
)
LOGGER.error(error_str)
raise MissingAPIToken(error_str)
raise BadEnvironment(error_str)

LOGGER.info("Obtained DynaRAG API key. Successfully initialised.")
LOGGER.info(
"Obtained DynaRAG API key and service URL. Successfully initialised."
)

self.base_url = DYNARAG_BASE_URL
self.base_url = base_url
self.api_token = api_token

def _make_request(
Expand Down
18 changes: 16 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ poethepoet = "^0.31.1"
pytest = "^8.3.4"
ruff = "^0.8.4"
mypy = "^1.13.0"
python-dotenv = "^1.0.1"

[build-system]
requires = ["poetry-core"]
Expand Down