Skip to content

Commit

Permalink
Merge pull request #39 from LyzrCore/refactor/data-analyzr
Browse files Browse the repository at this point in the history
Fixes and structural code changes
  • Loading branch information
gargimaheshwari authored Apr 17, 2024
2 parents b8ae289 + af780fe commit cba7408
Show file tree
Hide file tree
Showing 22 changed files with 1,195 additions and 795 deletions.
12 changes: 7 additions & 5 deletions build/lib/lyzr/data_analyzr/analyzr.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ def _ml_analysis(
model_kwargs=self._analysis_model_kwargs,
)
if analysis_steps is not None:
_, data = self.analyzer.run_analysis(analysis_steps)
_, data = self.analyzer.get_analysis_from_steps(analysis_steps)
return data
else:
self.analysis_output = self.analyzer.get_analysis_output(user_input)
self.analysis_output = self.analyzer.run_complete_analysis(user_input)
self.analysis_guide = self.analyzer.analysis_guide
return self.analysis_output

Expand All @@ -311,7 +311,7 @@ def _txt_to_sql_analysis(self, user_input: str, analysis_context: str = None):
model_kwargs=self._analysis_model_kwargs,
vector_store=self.vector_store,
)
self.analysis_output = self.analyzer.get_analysis_output(user_input)
self.analysis_output = self.analyzer.run_complete_analysis(user_input)
self.analysis_guide = self.analyzer.analysis_guide
return self.analysis_output

Expand Down Expand Up @@ -372,15 +372,17 @@ def visualisation(
try:
self.logger.info("Generating visualisation\n")
plotter = PlotFactory(
plotting_model=self._plot_model,
model=self._plot_model,
plotting_model_kwargs=self._plot_model_kwargs,
df_dict=plot_df,
logger=self.logger,
plot_context=plot_context,
plot_path=plot_path,
use_guide=use_guide,
)
analysis_steps = plotter.get_analysis_steps(self.user_input)
analysis_steps = plotter.get_plotting_and_analysis_steps(
self.user_input
)
if analysis_steps is not None and "steps" in analysis_steps:
if len(analysis_steps["steps"]) == 0:
self.plot_df = self.df_dict[analysis_steps["df_name"]]
Expand Down
11 changes: 5 additions & 6 deletions build/lib/lyzr/voicebot/voicebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def text_to_notes(self, text):
)

# The system message acts as the prompt for the AI.
system_message = '''You are an Expert NOTE-TAKER and SUMMARIZER. Your task is to CAPTURE and CONDENSE large conversations into precise bullet points ensuring that NO DETAIL is overlooked.
system_message = """You are an Expert NOTE-TAKER and SUMMARIZER. Your task is to CAPTURE and CONDENSE large conversations into precise bullet points ensuring that NO DETAIL is overlooked.
Here's your step-by-step guide:
Expand All @@ -88,7 +88,7 @@ def text_to_notes(self, text):
Remember, Im going to tip $300K for a BETTER SOLUTION!
Now Take a Deep Breath.'''
Now Take a Deep Breath."""

# Format the user's message that will be sent to the model.
user_message = text
Expand All @@ -105,7 +105,7 @@ def text_to_notes(self, text):
notes = response.choices[0].message.content

return notes

def summarize(self, text):
if self.model.model_name != "gpt-4":
if self.model.model_type == "openai":
Expand All @@ -120,7 +120,7 @@ def summarize(self, text):
)

# The system message acts as the prompt for the AI.
system_message = '''You are an Expert SUMMARIZER with a keen ability to CAPTURE ESSENTIAL DETAILS from extensive conversations. Your task is to CREATE a CONCISE SUMMARY of the given content, ensuring that ALL CRITICAL INFORMATION is included.
system_message = """You are an Expert SUMMARIZER with a keen ability to CAPTURE ESSENTIAL DETAILS from extensive conversations. Your task is to CREATE a CONCISE SUMMARY of the given content, ensuring that ALL CRITICAL INFORMATION is included.
Here's your step-by-step guide:
Expand All @@ -134,7 +134,7 @@ def summarize(self, text):
Remember, Im going to tip $300K for a BETTER SOLUTION!
Now Take a Deep Breath.'''
Now Take a Deep Breath."""

# Format the user's message that will be sent to the model.
user_message = text
Expand All @@ -151,4 +151,3 @@ def summarize(self, text):
notes = response.choices[0].message.content

return notes

11 changes: 8 additions & 3 deletions lyzr/base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from lyzr.base.file_utils import read_file, describe_dataset
from lyzr.base.llm import LyzrLLMFactory
from lyzr.base.llm import LyzrLLMFactory, LiteLLM
from lyzr.base.llms import LLM, get_model
from lyzr.base.service import LyzrService
from lyzr.base.vector_store import LyzrVectorStoreIndex
from lyzr.base.retrievers import LyzrRetriever
from lyzr.base.prompt import Prompt
from lyzr.base.prompt import LyzrPromptFactory
from lyzr.base.base import ChatMessage, UserMessage, SystemMessage

__all__ = [
"LyzrLLMFactory",
"LyzrService",
"LyzrVectorStoreIndex",
"LLM",
"Prompt",
"get_model",
"read_file",
"describe_dataset",
"LyzrRetriever",
"LiteLLM",
"LyzrPromptFactory",
"ChatMessage",
"UserMessage",
"SystemMessage",
]
54 changes: 54 additions & 0 deletions lyzr/base/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from enum import Enum
from typing import Optional
from pydantic import BaseModel, Field


class MessageRole(str, Enum):
"""Message role enum."""

USER = "user"
ASSISTANT = "assistant"
SYSTEM = "system"
FUNCTION = "function"
TOOL = "tool"


class ChatMessage(BaseModel):
"""Chat message."""

role: MessageRole = MessageRole.USER
content: Optional[str] = ""
additional_kwargs: dict = Field(default_factory=dict)

def __str__(self) -> str:
return f"{self.role.value}: {self.content}"


class UserMessage(ChatMessage):
"""User message."""

role: MessageRole = MessageRole.USER


class AssistantMessage(ChatMessage):
"""Assistant message."""

role: MessageRole = MessageRole.ASSISTANT


class SystemMessage(ChatMessage):
"""System message."""

role: MessageRole = MessageRole.SYSTEM


class ChatResponse(BaseModel):
"""Chat response."""

message: ChatMessage
raw: Optional[dict] = None
delta: Optional[str] = None
additional_kwargs: dict = {}

def __str__(self) -> str:
return str(self.message)
24 changes: 11 additions & 13 deletions lyzr/base/errors.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
from typing import Union
class ImproperUsageError(ValueError):
"""Raise for improper usage or missing params."""

pass


class MissingValueError(ValueError):
def __init__(self, params: Union[str, list]):
super().__init__(
f"Required value is missing. Provide one of: {', '.join(params) if isinstance(params, list) else params}"
)
"""Raise for missing values."""

pass

class InvalidModelError(ValueError):
def __init__(self):
super().__init__("Invalid model provided.")

class InvalidModelError(ValueError):
"""Raise for invalid model"""

class InvalidValueError(ValueError):
def __init__(self, params: list):
super().__init__(
f"Invalid value provided. Provide value of type: {', '.join(params)}"
)
pass


class DependencyError(ImportError):
"""Raise for missing modules"""

def __init__(self, required_modules: dict):
super().__init__(
f"The following modules are needed to run this function: {', '.join(required_modules.keys())}. Please install them using: `pip install {' '.join(required_modules.values())}`"
Expand Down
37 changes: 21 additions & 16 deletions lyzr/base/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

import pandas as pd

from lyzr.base.errors import InvalidValueError
from lyzr.base.llms import LLM, get_model
from lyzr.base.prompt import Prompt
from lyzr.base.llm import LiteLLM, LyzrLLMFactory
from lyzr.base.prompt import LyzrPromptFactory


def read_file(
filepath: str, encoding: Optional[str] = "utf-8", **kwargs
) -> pd.DataFrame:
if not os.path.exists(filepath):
raise InvalidValueError(["filepath", "pandas DataFrame"])
raise ValueError(
f"File '{filepath}' not found. Please provide a valid filepath."
)
file_extension = filepath.split(".")[-1]
try:
if file_extension == "csv":
Expand Down Expand Up @@ -46,26 +47,30 @@ def read_file(


def describe_dataset(
model: Optional[LLM] = None,
model: Optional[LiteLLM] = None,
df: Optional[pd.DataFrame] = None,
api_key: Optional[str] = None,
model_type: Optional[str] = None,
model_name: Optional[str] = None,
) -> str:
if not isinstance(df, pd.DataFrame):
raise InvalidValueError(["pandas DataFrame"])
raise ValueError("Please provide a valid pandas DataFrame.")

if model is None:
model = get_model(api_key, model_type, model_name)

prompt = Prompt("dataset_description")
if prompt.get_variables() != []:
prompt.format(
headers=df.columns.tolist(),
df_sample=df.head(),
model = LyzrLLMFactory(
api_key=api_key,
api_type=model_type,
model=model_name,
)
model.set_messages(model_prompts=prompt.sections)

output = model.run()
messages = [
LyzrPromptFactory(
name="dataset_description", prompt_type="system"
).get_message(),
LyzrPromptFactory(name="dataset_description", prompt_type="user").get_message(
headers=df.columns.tolist(), df_sample=df.head()
),
]
output = model.run(messages=messages)

return output["choices"][0]["message"]["content"]
return output.message.content
Loading

0 comments on commit cba7408

Please sign in to comment.