Skip to content

Commit

Permalink
Minor tweaks to retrieval skill and file ingestion (#9093)
Browse files Browse the repository at this point in the history
* Update default params and handle missing embedding args

The default chunk size and overlap values in file_handler have been increased for better performance. In langchain_handler, checks have been added to use the same provider and model for embeddings if 'embedding_model_args' are not provided in input params. Skill_tool's retriever_config has been renamed to 'config' for clarity.

* Handle invoke errors in langchain_handler

This update modifies the langchain handler to accommodate errors during the invocation process. Specifically, it implements exception handling for the agent_executor's invoke method. If an error occurs, instead of crashing, it will now return the error message. However, if the error doesn't match a specific format, the exception will still be raised.

* Update file_handler.py

* Add logging and refactor default vector store in langchain handler

This change introduces logging for situations where 'vector_store_config' is not present in the `langchain_handler` tool configuration. It also modifies the condition that checks for the absence of this property to add a persisting directory. Furthermore, it refactors the code in `langchain_handler.py` related to the absence of 'embedding_model_args'.

* Remove default vector store from langchain handler

The default vector store setup was removed from the langchain handler. The 'vector_store_config' is no longer automatically assigned, and an alert for this missing parameter is no longer logged. The code was adjusted to operate without the previously used 'mindsdb_path'.
  • Loading branch information
dusvyat committed Apr 19, 2024
1 parent 25a07c9 commit 8abf1c1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions mindsdb/integrations/handlers/file_handler/file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

logger = log.getLogger(__name__)

DEFAULT_CHUNK_SIZE = 200
DEFAULT_CHUNK_OVERLAP = 50
DEFAULT_CHUNK_SIZE = 500
DEFAULT_CHUNK_OVERLAP = 250


def clean_cell(val):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,22 @@ def create_agent(self, df: pd.DataFrame, args: Dict=None, pred_args: Dict=None)

# Set up embeddings model if needed.
if args.get('mode') == 'retrieval':
# get args for embeddings model

embeddings_args = args.pop('embedding_model_args', {})

# no embedding model args provided, use same provider as llm
if not embeddings_args:
logger.warning("'embedding_model_args' not found in input params, "
"Trying to use the same provider used for llm. "
f"provider: {args['provider']}"
)

# get args for embeddings model
embeddings_args['class'] = args['provider']

# create embeddings model
pred_args['embeddings_model'] = self._create_embeddings_model(embeddings_args)
pred_args['llm'] = llm
pred_args['mindsdb_path'] = self.engine_storage.folder_get

tools = setup_tools(llm,
model_kwargs,
Expand Down Expand Up @@ -291,8 +301,13 @@ def run_agent(self, df: pd.DataFrame, agent: AgentExecutor, args: Dict, pred_arg
def _invoke_agent_executor_with_prompt(agent_executor, prompt):
if not prompt:
return ''

answer = agent_executor.invoke(prompt)
try:
answer = agent_executor.invoke(prompt)
except Exception as e:
answer = str(e)
if not answer.startswith("Could not parse LLM output: `"):
raise e
answer = {'output': answer.removeprefix("Could not parse LLM output: `").removesuffix("`")}

if 'output' not in answer:
# This should never happen unless Langchain changes invoke output format, but just in case.
Expand Down
10 changes: 4 additions & 6 deletions mindsdb/integrations/handlers/langchain_handler/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from mindsdb.integrations.utilities.rag.rag_pipeline_builder import RAG
from mindsdb.integrations.utilities.rag.settings import RAGPipelineModel
from mindsdb.interfaces.skills.skill_tool import skill_tool, SkillType
from mindsdb.utilities import log

logger = log.getLogger(__name__)

# Individual tools
# Note: all tools are defined in a closure to pass required args (apart from LLM input) through it, as custom tools don't allow custom field assignment. # noqa
Expand Down Expand Up @@ -165,17 +168,11 @@ def _build_retrieval_tool(tool: dict, pred_args: dict):

tools_config = tool['config']

mindsdb_path = pred_args['mindsdb_path']

# we update the config with the pred_args to allow for custom config
tools_config.update(pred_args)

rag_params = _get_rag_params(tools_config)

if 'vector_store_config' not in rag_params:

rag_params['vector_store_config'] = {'persist_directory': mindsdb_path('persisted_chroma')}

rag_config = RAGPipelineModel(**rag_params)

# build retriever
Expand All @@ -188,6 +185,7 @@ def _build_retrieval_tool(tool: dict, pred_args: dict):
description=tool['description']
)


def langchain_tool_from_skill(skill, pred_args):
# Makes Langchain compatible tools from a skill
tool = skill_tool.get_tool_from_skill(skill)
Expand Down
2 changes: 1 addition & 1 deletion mindsdb/interfaces/skills/skill_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _make_retrieval_tools(self, skill: db.Skills) -> dict:
params = skill.params
return dict(
name=params.get('name', skill.name),
config=params.get('retriever_config', {}),
config=params.get('config', {}),
description=f'You must use this tool to get more context or information '
f'to answer a question about {params["description"]}. '
f'The input should be the exact question the user is asking.',
Expand Down

0 comments on commit 8abf1c1

Please sign in to comment.