refactor: Refactor Wikipedia API component (#5432)
* refactor(wikipedia): Refactor Wikipedia API component * test: add unit tests for WikipediaAPIComponent * [autofix.ci] apply automated fixes * refactor: improve WikipediaAPIComponent tests and fix lint issues * [autofix.ci] apply automated fixes * fix: resolve lint issues in WikipediaAPIComponent tests --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Edwin Jose <edwin.jose@datastax.com>
This commit is contained in:
parent
b55c9f1698
commit
d89f8e8e18
2 changed files with 103 additions and 10 deletions
|
|
@ -1,15 +1,13 @@
|
|||
from typing import cast
|
||||
|
||||
from langchain_community.tools import WikipediaQueryRun
|
||||
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
||||
|
||||
from langflow.base.langchain_utilities.model import LCToolComponent
|
||||
from langflow.field_typing import Tool
|
||||
from langflow.custom import Component
|
||||
from langflow.inputs import BoolInput, IntInput, MessageTextInput, MultilineInput
|
||||
from langflow.io import Output
|
||||
from langflow.schema import Data
|
||||
from langflow.schema.message import Message
|
||||
|
||||
|
||||
class WikipediaAPIComponent(LCToolComponent):
|
||||
class WikipediaAPIComponent(Component):
|
||||
display_name = "Wikipedia API"
|
||||
description = "Call Wikipedia API."
|
||||
name = "WikipediaAPI"
|
||||
|
|
@ -19,6 +17,7 @@ class WikipediaAPIComponent(LCToolComponent):
|
|||
MultilineInput(
|
||||
name="input_value",
|
||||
display_name="Input",
|
||||
tool_mode=True,
|
||||
),
|
||||
MessageTextInput(name="lang", display_name="Language", value="en"),
|
||||
IntInput(name="k", display_name="Number of results", value=4, required=True),
|
||||
|
|
@ -28,16 +27,25 @@ class WikipediaAPIComponent(LCToolComponent):
|
|||
),
|
||||
]
|
||||
|
||||
def run_model(self) -> list[Data]:
|
||||
outputs = [
|
||||
Output(display_name="Data", name="data", method="fetch_content"),
|
||||
Output(display_name="Text", name="text", method="fetch_content_text"),
|
||||
]
|
||||
|
||||
def fetch_content(self) -> list[Data]:
|
||||
wrapper = self._build_wrapper()
|
||||
docs = wrapper.load(self.input_value)
|
||||
data = [Data.from_document(doc) for doc in docs]
|
||||
self.status = data
|
||||
return data
|
||||
|
||||
def build_tool(self) -> Tool:
|
||||
wrapper = self._build_wrapper()
|
||||
return cast("Tool", WikipediaQueryRun(api_wrapper=wrapper))
|
||||
def fetch_content_text(self) -> Message:
|
||||
data = self.fetch_content()
|
||||
result_string = ""
|
||||
for item in data:
|
||||
result_string += item.text + "\n"
|
||||
self.status = result_string
|
||||
return Message(text=result_string)
|
||||
|
||||
def _build_wrapper(self) -> WikipediaAPIWrapper:
|
||||
return WikipediaAPIWrapper(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue