Skip to main content

Sematic AI RAG System

Project description

Semantic AI Logo

Semantic AI Lib

Python versionPyPI versionLicense

An open-source framework for Retrieval-Augmented System (RAG) uses semantic search to retrieve the expected results and generate human-readable conversational responses with the help of LLM (Large Language Model).

Semantic AI Library Documentation Docs here

Requirements

Python 3.10+ asyncio

Installation

# Using pip
$ python -m pip install semantic-ai

# Manual install
$ python -m pip install .

Set the environment variable

Set the credentials in .env file. Only give the credential for an one connector, an one indexer and an one llm model config. other fields put as empty

# Default
FILE_DOWNLOAD_DIR_PATH= # default directory name 'download_file_dir'
EXTRACTED_DIR_PATH= # default directory name 'extracted_dir'

# Connector (SharePoint, S3, GCP Bucket, GDrive, Confluence etc.,)
CONNECTOR_TYPE="connector_name" # sharepoint
SHAREPOINT_CLIENT_ID="client_id"
SHAREPOINT_CLIENT_SECRET="client_secret"
SHAREPOINT_TENANT_ID="tenant_id"
SHAREPOINT_HOST_NAME='<tenant_name>.sharepoint.com'
SHAREPOINT_SCOPE='https://graph.microsoft.com/.default'
SHAREPOINT_SITE_ID="site_id"
SHAREPOINT_DRIVE_ID="drive_id"
SHAREPOINT_FOLDER_URL="folder_url" # /My_folder/child_folder/

# Indexer
INDEXER_TYPE="<vector_db_name>" # elasticsearch, qdrant, opensearch
ELASTICSEARCH_URL="<elasticsearch_url>" # give valid url
ELASTICSEARCH_USER="<elasticsearch_user>" # give valid user
ELASTICSEARCH_PASSWORD="<elasticsearch_password>" # give valid password
ELASTICSEARCH_INDEX_NAME="<index_name>"
ELASTICSEARCH_SSL_VERIFY="<ssl_verify>" # True or False

# Qdrant
QDRANT_URL="<qdrant_url>"
QDRANT_INDEX_NAME="<index_name>"
QDRANT_API_KEY="<apikey>"

# Opensearch
OPENSEARCH_URL="<opensearch_url>"
OPENSEARCH_USER="<opensearch_user>"
OPENSEARCH_PASSWORD="<opensearch_password>"
OPENSEARCH_INDEX_NAME="<index_name>"

# LLM
LLM_MODEL="<llm_model>" # llama, openai
LLM_MODEL_NAME_OR_PATH="" # model name
OPENAI_API_KEY="<openai_api_key>" # if using openai

# SQL
SQLITE_SQL_PATH="<database_path>" # sqlit db path

# MYSQL
MYSQL_HOST="<host_name>" # localhost or Ip Address
MYSQL_USER="<user_name>"
MYSQL_PASSWORD="<password>"
MYSQL_DATABASE="<database_name>"
MYSQL_PORT="<port>" # default port is 3306

Method 1: To load the .env file. Env file should have the credentials

%load_ext dotenv
%dotenv
%dotenv relative/or/absolute/path/to/.env

(or)

dotenv -f .env run -- python

Method 2:

from semantic_ai.config import Settings
settings = Settings()

Un-Structure

1. Import the module

import asyncio
import semantic_ai

2. To download the files from a given source, extract the content from the downloaded files and index the extracted data in the given vector db.

await semantic_ai.download()
await semantic_ai.extract()
await semantic_ai.index()

After completion of download, extract and index, we can generate the answer from indexed vector db. That code given below.

3. To generate the answer from indexed vector db using retrieval LLM model.

search_obj = await semantic_ai.search()
query = ""
search = await search_obj.generate(query)

Suppose the job is running for a long time, we can watch the number of files processed, the number of files failed, and that filename stored in the text file that is processed and failed in the 'EXTRACTED_DIR_PATH/meta' directory.

Example

To connect the source and get the connection object. We can see that in the examples folder. Example: SharePoint connector

from semantic_ai.connectors import Sharepoint

CLIENT_ID = '<client_id>'  # sharepoint client id
CLIENT_SECRET = '<client_secret>'  # sharepoint client seceret
TENANT_ID = '<tenant_id>'  # sharepoint tenant id
SCOPE = 'https://graph.microsoft.com/.default'  # scope
HOST_NAME = "<tenant_name>.sharepoint.com"  # for example 'contoso.sharepoint.com'

# Sharepoint object creation
connection = Sharepoint(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    tenant_id=TENANT_ID,
    host_name=HOST_NAME,
    scope=SCOPE
)

Structure

1. Import the module

import asyncio
import semantic_ai

2. The database connection

Sqlite:

from semantic_ai.connectors import Sqlite

file_path= <database_file_path>

sql = Sqlite(sql_path=file_path)

Mysql:

from semantic_ai.connectors import Mysql

sql = Mysql(
    host=<host_name>,
    user=<user_name>,
    password=<password>,
    database=<database>,
    port=<port_number>  # 3306 is default port
)

3. To generate the answer from db using retrieval LLM model.

query = ""
search_obj = await semantic_ai.db_search(query=query)

Run in the server

$ semantic_ai serve -f .env

INFO:     Loading environment from '.env'
INFO:     Started server process [43973]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Open your browser at http://127.0.0.1:8000/semantic-ai

Interactive API docs

Now go to http://127.0.0.1:8000/docs. You will see the automatic interactive API documentation (provided by Swagger UI): Swagger UI

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

semantic_ai-0.0.6.1.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

semantic_ai-0.0.6.1-py3-none-any.whl (37.8 kB view details)

Uploaded Python 3

File details

Details for the file semantic_ai-0.0.6.1.tar.gz.

File metadata

  • Download URL: semantic_ai-0.0.6.1.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for semantic_ai-0.0.6.1.tar.gz
Algorithm Hash digest
SHA256 646087fdf1d05ddc7c1c7c77d1d36ce84107f4a1323d1ae5597c002717dca5c6
MD5 e0d5e8f5d2b61578f2f37714fb5e59b5
BLAKE2b-256 74543436f7742c31b4461b54fbcc4f79ae7bc25ee741ff792e45777be6074610

See more details on using hashes here.

File details

Details for the file semantic_ai-0.0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for semantic_ai-0.0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d9b8277a0e88ca5517aa33dd6042fa320708379a8186f5d45b40d3da01d9384f
MD5 966050ea0333785ea2fccc4cf753ee7f
BLAKE2b-256 5bd92fbdcd3c1ba94c7b0d000a631b7bbddb69a50c0318a3b0798f4092de17d2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page