Skip to content

Commit

Permalink
Added dynamic graph creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasilije1990 committed Nov 10, 2023
1 parent 0b6f9b0 commit ceba0d3
Show file tree
Hide file tree
Showing 23 changed files with 986 additions and 447 deletions.
102 changes: 0 additions & 102 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,109 +149,7 @@ Run



### Run the level 3

Make sure you have Docker, Poetry, and Python 3.11 installed and postgres installed.

Copy the .env.example to .env and fill in the variables


Two ways to run the level 3:

#### Docker:

Copy the .env.template to .env and fill in the variables
Specify the environment variable in the .env file to "docker"




Launch the docker image:

```docker compose up promethai_mem ```

Send the request to the API:

```
curl -X POST -H "Content-Type: application/json" -d '{
"payload": {
"user_id": "97980cfea0067",
"data": [".data/3ZCCCW.pdf"],
"test_set": "sample",
"params": ["chunk_size"],
"metadata": "sample",
"retriever_type": "single_document_context"
}
}' http://0.0.0.0:8000/rag-test/rag_test_run
```
Params:

- data -> list of URLs or path to the file, located in the .data folder (pdf, docx, txt, html)
- test_set -> sample, manual (list of questions and answers)
- metadata -> sample, manual (json) or version (in progress)
- params -> chunk_size, chunk_overlap, search_type (hybrid, bm25), embeddings
- retriever_type -> llm_context, single_document_context, multi_document_context, cognitive_architecture(coming soon)

Inspect the results in the DB:

``` docker exec -it postgres psql -U bla ```

``` \c bubu ```

``` select * from test_outputs; ```

Or set up the superset to visualize the results.
The base SQL query is in the example_data folder.



#### Poetry environment:


Copy the .env.template to .env and fill in the variables
Specify the environment variable in the .env file to "local"

Use the poetry environment:

``` poetry shell ```

Change the .env file Environment variable to "local"

Launch the postgres DB

``` docker compose up postgres ```

Launch the superset

``` docker compose up superset ```

Open the superset in your browser

``` http://localhost:8088 ```
Add the Postgres datasource to the Superset with the following connection string:

``` postgres://bla:bla@postgres:5432/bubu ```

Make sure to run to initialize DB tables

``` python scripts/create_database.py ```

After that, you can run the RAG test manager from your command line.


```
python rag_test_manager.py \
--file ".data" \
--test_set "example_data/test_set.json" \
--user_id "97980cfea0067" \
--params "chunk_size" "search_type" \
--metadata "example_data/metadata.json" \
--retriever_type "single_document_context"
```

Examples of metadata structure and test set are in the folder "example_data"



Expand Down
6 changes: 5 additions & 1 deletion level_4/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ POSTGRES_PASSWORD = bla
POSTGRES_DB = bubu
POSTGRES_HOST = localhost
POSTGRES_HOST_DOCKER = postgres
SEGMENT_KEY = Etl4WJwzOkeDPAjaOXOMgyU16hO7mV7B
SEGMENT_KEY = Etl4WJwzOkeDPAjaOXOMgyU16hO7mV7B
COG_ARCH_DIR = cognitive_architecture
GRAPH_DB_URL =
GRAPH_DB_PW =
GRAPH_DB_USER =
Empty file.
15 changes: 15 additions & 0 deletions level_4/cognitive_architecture/classifiers/classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@



#TO DO, ADD ALL CLASSIFIERS HERE



# classify retrievals according to type of retrieval
def classify_retrieval():
pass


# classify documents according to type of document
def classify_call():
pass
84 changes: 84 additions & 0 deletions level_4/cognitive_architecture/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import os
import json
import configparser
import uuid
from typing import Optional, List, Dict, Any
from dataclasses import dataclass, field
from pathlib import Path
from dotenv import load_dotenv


base_dir = Path(__file__).resolve().parent.parent
# Load the .env file from the base directory
dotenv_path = base_dir / '.env'
load_dotenv(dotenv_path=dotenv_path)

@dataclass
class Config:
# Paths and Directories
memgpt_dir: str = field(default_factory=lambda: os.getenv('COG_ARCH_DIR', 'cognitive_achitecture'))
config_path: str = field(default_factory=lambda: os.path.join(os.getenv('COG_ARCH_DIR', 'cognitive_achitecture'), 'config'))

# Model parameters
model: str = 'gpt-4-1106-preview'
model_endpoint: str = 'openai'
openai_key: Optional[str] = os.getenv('OPENAI_API_KEY')

# Embedding parameters
embedding_model: str = 'openai'
embedding_dim: int = 1536
embedding_chunk_size: int = 300

# Database parameters
graph_database_url: str = os.getenv('GRAPH_DB_URL')
graph_database_username: str = os.getenv('GRAPH_DB_USER')
graph_database_password: str = os.getenv('GRAPH_DB_PW')


# Client ID
anon_clientid: Optional[str] = field(default_factory=lambda: uuid.uuid4().hex)

def load(self):
"""Loads the configuration from a file or environment variables."""
config = configparser.ConfigParser()
config.read(self.config_path)

# Override with environment variables if they exist
for attr in self.__annotations__:
env_value = os.getenv(attr.upper())
if env_value is not None:
setattr(self, attr, env_value)

# Load from config file
if config.sections():
for section in config.sections():
for key, value in config.items(section):
if hasattr(self, key):
setattr(self, key, value)

def save(self):
"""Saves the current configuration to a file."""
config = configparser.ConfigParser()

# Save the current settings to the config file
for attr, value in self.__dict__.items():
section, option = attr.split('_', 1)
if not config.has_section(section):
config.add_section(section)
config.set(section, option, str(value))

with open(self.config_path, 'w') as configfile:
config.write(configfile)

def to_dict(self) -> Dict[str, Any]:
"""Returns a dictionary representation of the configuration."""
return {attr: getattr(self, attr) for attr in self.__annotations__}

@classmethod
def from_dict(cls, config_dict: Dict[str, Any]) -> "Config":
"""Creates a Config instance from a dictionary."""
config = cls()
for attr, value in config_dict.items():
if hasattr(config, attr):
setattr(config, attr, value)
return config
16 changes: 15 additions & 1 deletion level_4/cognitive_architecture/database/database_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ async def session_scope(session):
async def add_entity(session, entity):
async with session_scope(session) as s: # Use your async session_scope
s.add(entity) # No need to commit; session_scope takes care of it
return "Successfully added entity"
return "Successfully added entity"



async def update_entity(session, model, entity_id, new_value):
async with session_scope(session) as s:
# Retrieve the entity from the database
entity = await s.get(model, entity_id)

if entity:
# Update the relevant column and 'updated_at' will be automatically updated
entity.operation_status = new_value
return "Successfully updated entity"
else:
return "Entity not found"
Empty file.
Loading

0 comments on commit ceba0d3

Please sign in to comment.