fix: Refactor import statements and enhance error logging (#4071)
* Refactor import paths for `get_patched_openai_client` in astra_assistants components * Enhance error logging with file information in directory_reader.py * Refactor MetaphorToolkit to use new input/output structure and update imports * Enhance error message with code snippet preview in class validation function * update import statements and refactoring input handling in JSON files. * [autofix.ci] apply automated fixes * Remove unused import of 'Tool' from Metaphor.py --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
4a745aae5d
commit
f74b58f22a
24 changed files with 89 additions and 73 deletions
56
src/backend/base/langflow/base/astra_assistants/util.py
Normal file
56
src/backend/base/langflow/base/astra_assistants/util.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import importlib
|
||||
import inspect
|
||||
import json
|
||||
import os
|
||||
import pkgutil
|
||||
import threading
|
||||
|
||||
import astra_assistants.tools as astra_assistants_tools
|
||||
import requests
|
||||
from astra_assistants import OpenAI, patch
|
||||
from astra_assistants.tools.tool_interface import ToolInterface
|
||||
|
||||
client_lock = threading.Lock()
|
||||
client = None
|
||||
|
||||
|
||||
def get_patched_openai_client(shared_component_cache):
|
||||
os.environ["ASTRA_ASSISTANTS_QUIET"] = "true"
|
||||
client = shared_component_cache.get("client")
|
||||
if client is None:
|
||||
client = patch(OpenAI())
|
||||
shared_component_cache.set("client", client)
|
||||
return client
|
||||
|
||||
|
||||
url = "https://raw.githubusercontent.com/BerriAI/litellm/refs/heads/main/model_prices_and_context_window.json"
|
||||
response = requests.get(url)
|
||||
data = json.loads(response.text)
|
||||
|
||||
# Extract the model names into a Python list
|
||||
litellm_model_names = [model for model, _ in data.items() if model != "sample_spec"]
|
||||
|
||||
|
||||
# To store the class names that extend ToolInterface
|
||||
tool_names = []
|
||||
tools_and_names = {}
|
||||
|
||||
|
||||
def tools_from_package(your_package):
|
||||
# Iterate over all modules in the package
|
||||
package_name = your_package.__name__
|
||||
for module_info in pkgutil.iter_modules(your_package.__path__):
|
||||
module_name = f"{package_name}.{module_info.name}"
|
||||
|
||||
# Dynamically import the module
|
||||
module = importlib.import_module(module_name)
|
||||
|
||||
# Iterate over all members of the module
|
||||
for name, obj in inspect.getmembers(module, inspect.isclass):
|
||||
# Check if the class is a subclass of ToolInterface and is not ToolInterface itself
|
||||
if issubclass(obj, ToolInterface) and obj is not ToolInterface:
|
||||
tool_names.append(name)
|
||||
tools_and_names[name] = obj
|
||||
|
||||
|
||||
tools_from_package(astra_assistants_tools)
|
||||
|
|
@ -2,7 +2,7 @@ import asyncio
|
|||
|
||||
from astra_assistants.astra_assistants_manager import AssistantManager
|
||||
|
||||
from langflow.components.astra_assistants.util import (
|
||||
from langflow.base.astra_assistants.util import (
|
||||
get_patched_openai_client,
|
||||
litellm_model_names,
|
||||
tool_names,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from langflow.components.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.base.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.custom.custom_component.component_with_cache import ComponentWithCache
|
||||
from langflow.inputs import MultilineInput, StrInput
|
||||
from langflow.schema.message import Message
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from langflow.components.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.base.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.custom.custom_component.component_with_cache import ComponentWithCache
|
||||
from langflow.inputs import MultilineInput
|
||||
from langflow.schema.message import Message
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from langflow.components.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.base.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.custom.custom_component.component_with_cache import ComponentWithCache
|
||||
from langflow.inputs import MultilineInput, StrInput
|
||||
from langflow.schema.message import Message
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from langflow.components.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.base.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.custom.custom_component.component_with_cache import ComponentWithCache
|
||||
from langflow.schema.message import Message
|
||||
from langflow.template.field.base import Output
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ from astra_assistants import patch
|
|||
from openai import OpenAI
|
||||
from openai.lib.streaming import AssistantEventHandler
|
||||
|
||||
from langflow.components.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.base.astra_assistants.util import get_patched_openai_client
|
||||
from langflow.custom.custom_component.component_with_cache import ComponentWithCache
|
||||
from langflow.inputs import MultilineInput
|
||||
from langflow.schema import dotdict
|
||||
|
|
@ -12,10 +12,6 @@ from langflow.schema.message import Message
|
|||
from langflow.template import Output
|
||||
|
||||
|
||||
class AssistantsRunError(Exception):
|
||||
"""Error running assistant"""
|
||||
|
||||
|
||||
class AssistantsRun(ComponentWithCache):
|
||||
display_name = "Run Assistant"
|
||||
description = "Executes an Assistant Run against a thread"
|
||||
|
|
@ -101,3 +97,7 @@ class AssistantsRun(ComponentWithCache):
|
|||
print(e)
|
||||
msg = f"Error running assistant: {e}"
|
||||
raise AssistantsRunError(msg) from e
|
||||
|
||||
|
||||
class AssistantsRunError(Exception):
|
||||
"""AssistantsRun error"""
|
||||
|
|
|
|||
|
|
@ -6,10 +6,6 @@ from langflow.io import BoolInput, DictInput, DropdownInput, IntInput, Output, S
|
|||
from langflow.schema import Data
|
||||
|
||||
|
||||
class SpiderToolError(Exception):
|
||||
"""SpiderTool error"""
|
||||
|
||||
|
||||
class SpiderTool(Component):
|
||||
display_name: str = "Spider Web Crawler & Scraper"
|
||||
description: str = "Spider API for web crawling and scraping."
|
||||
|
|
@ -130,3 +126,7 @@ class SpiderTool(Component):
|
|||
else:
|
||||
records.append(Data(data={"content": record["content"], "url": record["url"]}))
|
||||
return records
|
||||
|
||||
|
||||
class SpiderToolError(Exception):
|
||||
"""SpiderTool error"""
|
||||
|
|
|
|||
|
|
@ -1,36 +1,51 @@
|
|||
from langchain_community.agent_toolkits.base import BaseToolkit
|
||||
from langchain_core.tools import Tool, tool
|
||||
from langchain_core.tools import tool
|
||||
from metaphor_python import Metaphor
|
||||
|
||||
from langflow.custom import CustomComponent
|
||||
from langflow.custom import Component
|
||||
from langflow.field_typing import Tool
|
||||
from langflow.io import BoolInput, IntInput, Output, SecretStrInput
|
||||
|
||||
|
||||
class MetaphorToolkit(CustomComponent):
|
||||
display_name: str = "Metaphor"
|
||||
description: str = "Metaphor Toolkit"
|
||||
class MetaphorToolkit(Component):
|
||||
display_name = "Metaphor"
|
||||
description = "Metaphor Toolkit for search and content retrieval"
|
||||
documentation = "https://python.langchain.com/docs/integrations/tools/metaphor_search"
|
||||
beta: bool = True
|
||||
name = "Metaphor"
|
||||
# api key should be password = True
|
||||
field_config = {
|
||||
"metaphor_api_key": {"display_name": "Metaphor API Key", "password": True},
|
||||
"code": {"advanced": True},
|
||||
}
|
||||
beta = True
|
||||
|
||||
def build(
|
||||
self,
|
||||
metaphor_api_key: str,
|
||||
use_autoprompt: bool = True,
|
||||
search_num_results: int = 5,
|
||||
similar_num_results: int = 5,
|
||||
) -> Tool | BaseToolkit:
|
||||
# If documents, then we need to create a Vectara instance using .from_documents
|
||||
client = Metaphor(api_key=metaphor_api_key)
|
||||
inputs = [
|
||||
SecretStrInput(
|
||||
name="metaphor_api_key",
|
||||
display_name="Metaphor API Key",
|
||||
password=True,
|
||||
),
|
||||
BoolInput(
|
||||
name="use_autoprompt",
|
||||
display_name="Use Autoprompt",
|
||||
value=True,
|
||||
),
|
||||
IntInput(
|
||||
name="search_num_results",
|
||||
display_name="Search Number of Results",
|
||||
value=5,
|
||||
),
|
||||
IntInput(
|
||||
name="similar_num_results",
|
||||
display_name="Similar Number of Results",
|
||||
value=5,
|
||||
),
|
||||
]
|
||||
|
||||
outputs = [
|
||||
Output(name="tools", display_name="Tools", method="build_toolkit"),
|
||||
]
|
||||
|
||||
def build_toolkit(self) -> Tool:
|
||||
client = Metaphor(api_key=self.metaphor_api_key)
|
||||
|
||||
@tool
|
||||
def search(query: str):
|
||||
"""Call search engine with a query."""
|
||||
return client.search(query, use_autoprompt=use_autoprompt, num_results=search_num_results)
|
||||
return client.search(query, use_autoprompt=self.use_autoprompt, num_results=self.search_num_results)
|
||||
|
||||
@tool
|
||||
def get_contents(ids: list[str]):
|
||||
|
|
@ -46,6 +61,6 @@ class MetaphorToolkit(CustomComponent):
|
|||
|
||||
The url passed in should be a URL returned from `search`
|
||||
"""
|
||||
return client.find_similar(url, num_results=similar_num_results)
|
||||
return client.find_similar(url, num_results=self.similar_num_results)
|
||||
|
||||
return [search, get_contents, find_similar]
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ from typing import TYPE_CHECKING, Any, ClassVar, get_type_hints
|
|||
|
||||
import nanoid
|
||||
import yaml
|
||||
from loguru import logger
|
||||
from pydantic import BaseModel
|
||||
|
||||
from langflow.base.tools.constants import TOOL_OUTPUT_NAME
|
||||
|
|
@ -340,7 +339,6 @@ class Component(CustomComponent):
|
|||
source_code = inspect.getsource(method)
|
||||
ast_tree = ast.parse(dedent(source_code))
|
||||
except Exception: # noqa: BLE001
|
||||
logger.opt(exception=True).debug(f"Could not get source code for method {method}")
|
||||
source_code = self._code
|
||||
ast_tree = ast.parse(dedent(source_code))
|
||||
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ class DirectoryReader:
|
|||
component_tuple = (*build_component(component), component)
|
||||
components.append(component_tuple)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.opt(exception=True).debug(f"Error while loading component {component['name']}")
|
||||
logger.debug(f"Error while loading component {component['name']} from {component['file']}")
|
||||
continue
|
||||
items.append({"name": menu["name"], "path": menu["path"], "components": components})
|
||||
filtered = [menu for menu in items if menu["components"]]
|
||||
|
|
|
|||
|
|
@ -370,9 +370,12 @@ class Vertex:
|
|||
val = field.get("value")
|
||||
if field.get("type") == "code":
|
||||
try:
|
||||
params[field_name] = ast.literal_eval(val) if val else None
|
||||
if field_name == "code":
|
||||
params[field_name] = val
|
||||
else:
|
||||
params[field_name] = ast.literal_eval(val) if val else None
|
||||
except Exception: # noqa: BLE001
|
||||
logger.opt(exception=True).debug(f"Error evaluating code for {field_name}")
|
||||
logger.debug(f"Error evaluating code for {field_name}")
|
||||
params[field_name] = val
|
||||
elif field.get("type") in ["dict", "NestedDict"]:
|
||||
# When dict comes from the frontend it comes as a
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue