Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Refactor Wikipedia API component #5432

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/backend/base/langflow/components/tools/wikipedia_api.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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),
Expand All @@ -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(
Expand Down
Loading