A toolset for extracting knowledge from various sources, including files, text input, and Wikipedia articles.
- Extract text from files (TXT, PDF)
- Extract text from direct input
- Extract text from Wikipedia articles
- Split text into chunks using various strategies
- Process text using AI agents for knowledge extraction, summarization, and more
- Save extracted knowledge and summaries to files in various formats (JSON, TXT, MD)
- Python 3.13 or higher
- Poetry (for dependency management)
- Clone the repository
- Install dependencies using Poetry:
poetry install
The application can be run using the provided shell script:
./run.shOr manually with Poetry:
poetry run streamlit run src/knowledge_extract_toolset/web-app.pyThe application provides a web interface with the following tabs:
- Extract Knowledge From File: Upload and extract text from files
- Extract Knowledge From Text: Input text directly
- Extract Knowledge From Wikipedia Article: Search and extract text from Wikipedia articles
- AI Processing: Process extracted text using AI agents
- Knowledge Extraction: Extract structured knowledge from text
- Summarization: Generate summaries of extracted knowledge
When extracting knowledge, you can choose to process only a specific range of chunks instead of all chunks:
- Split your content into chunks in the Content Preparation section
- In the Knowledge Extraction tab, check the "Process specific chunk range" option
- Specify the start and end chunk numbers (e.g., chunks 1-5)
- Click "Extract Knowledge" to process only the selected chunks
This feature is useful for:
- Testing extraction on a small subset of chunks
- Focusing on specific sections of content
- Reducing processing time for large documents
After processing text with AI agents, you can save the results to files:
- Extracted Knowledge: Save in JSON, TXT, MD, or CSV formats
- Summaries: Save in TXT or MD formats
To save a result:
- Process text using the appropriate AI agent
- Scroll down to the "Save Extracted Knowledge" or "Save Summary" section
- Enter a file name (or use the default)
- Select the desired file format
- Click the "Save to File" button
The toolset includes a standalone utility for converting extracted knowledge JSON files to CSV format. This is useful for:
- Analyzing extracted knowledge in spreadsheet applications
- Importing knowledge triplets into database systems
- Processing knowledge with other tools that support CSV format
To use the utility:
# Convert JSON to CSV (output will be named extracted_knowledge.csv)
python src/knowledge_extract_toolset/utils/json_to_csv.py extracted_knowledge.json
# Convert JSON to CSV with a specific output filename
python src/knowledge_extract_toolset/utils/json_to_csv.py extracted_knowledge.json output.csvThe CSV file will have the following headers:
subject: The subject of the knowledge tripletpredicate: The predicate (relationship) of the knowledge tripletobject: The object of the knowledge triplet
Each triplet from the JSON file will be written as a separate line in the CSV file.
The application includes an AI agent system that can process text using various LLM models. The following agents are available:
- Knowledge Extraction Agent: Extracts structured knowledge from text
- Summarization Agent: Summarizes text with configurable length and focus
AI agents are configured using HOCON configuration files in the config/agents directory. Each agent has its own configuration file with the following structure:
# Agent identification
name = "Agent Name"
description = "Agent description"
agent_type = "agent_type" # knowledge_extraction, summarization, etc.
# Model configuration
model_provider = "provider" # openai, gemini, etc.
model_name = "model_name" # gpt-3.5-turbo, gemini-pro, etc.
temperature = 0.7
max_tokens = 1000
# Prompt configuration
prompt_template = """
Prompt template with {variables}
"""
prompt_variables = []
# Provider-specific configuration
provider_config {
api_key = ${?API_KEY_ENV_VAR}
}To add a new AI agent:
- Create a new configuration file in the
config/agentsdirectory - Implement the agent class in the
src/knowledge_extract_toolset/ai_agent/agentsdirectory - Register the agent with the
AiAgentFactory
src/knowledge_extract_toolset/- Main application codeweb-app.py- Streamlit web applicationapp.py- Main application classconstants.py- Application constantsdata_sources/- Data source implementationstext_splitter/- Text splitting functionalityai_agent/- AI agent functionalitybase.py- Base classes for AI agentsconfig.py- Configuration managementproviders/- Model provider implementationsagents/- Agent implementations
utils/- Utility functionsjson_to_csv.py- Utility to convert JSON to CSV
tests/- Test filesconfig/- Configuration filesagents/- AI agent configurations
Tests are implemented using the Python unittest framework. To run all tests:
poetry run python -m unittest discover testsTo run a specific test file:
poetry run python -m unittest tests/test_wikipedia_api.py- Create a new test file in the
testsdirectory with a name starting withtest_. - Import the necessary modules and the
unittestframework. - Create a test class that inherits from
unittest.TestCase. - Implement test methods that start with
test_. - Use assertions to verify expected behavior.
Example:
import unittest
from wikipedia import wikipedia
class TestWikipediaAPI(unittest.TestCase):
def setUp(self):
wikipedia.set_lang("en")
def test_search(self):
results = wikipedia.search("Python programming language", results=5)
self.assertTrue(len(results) > 0)A simple test for the Wikipedia API functionality has been created in tests/test_wikipedia_api.py. This test verifies:
- The search functionality returns relevant results
- The page retrieval functionality returns pages with content, title, and URL
- Follow PEP 8 guidelines for Python code
- Use docstrings for classes and functions
- Keep functions small and focused on a single responsibility
The application is built using Streamlit and provides three main functionalities:
- Extract knowledge from files
- Extract knowledge from text
- Extract knowledge from Wikipedia articles
When extending the application:
- Keep the tab-based interface for different data sources
- Use Streamlit session state for managing application state
- Handle exceptions appropriately with user-friendly error messages
The application uses the Wikipedia API for retrieving article data. When working with this API:
- Set the language using
wikipedia.set_lang("en") - Handle disambiguation errors and page errors
- Use
auto_suggest=Falseto get exact matches
This project is licensed under the MIT License - see the LICENSE file for details.