<!-- .github/pull_request_template.md --> ## Description <!-- Provide a clear description of the changes in this PR --> ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. --------- Co-authored-by: vasilije <vas.markovic@gmail.com> Co-authored-by: Igor Ilic <30923996+dexters1@users.noreply.github.com> Co-authored-by: Vasilije <8619304+Vasilije1990@users.noreply.github.com> Co-authored-by: Igor Ilic <igorilic03@gmail.com> Co-authored-by: Hande <159312713+hande-k@users.noreply.github.com> Co-authored-by: Matea Pesic <80577904+matea16@users.noreply.github.com> Co-authored-by: hajdul88 <52442977+hajdul88@users.noreply.github.com> Co-authored-by: Daniel Molnar <soobrosa@gmail.com> Co-authored-by: Diego Baptista Theuerkauf <34717973+diegoabt@users.noreply.github.com>
833 lines
2.4 MiB
Vendored
833 lines
2.4 MiB
Vendored
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "d35ac8ce-0f92-46f5-9ba4-a46970f0ce19",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Cognee - Get Started"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "bd981778-0c84-4542-8e6f-1a7712184873",
|
||
"metadata": {
|
||
"editable": true,
|
||
"slideshow": {
|
||
"slide_type": ""
|
||
},
|
||
"tags": []
|
||
},
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "d8e606b1-94d3-43ce-bb4b-dbadff7f4ca6",
|
||
"metadata": {},
|
||
"source": [
|
||
"## How to enable LLM to connect to your data\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"#### Let's try and convert what is a large amount of unorganized data into structured graph that we can give to LLMs\n",
|
||
"#### Here is an example of some data chunks you sent to LLM to get a better answer to a question when sending LLM prompts"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "cb74c44c58f052f1",
|
||
"metadata": {},
|
||
"source": [
|
||
""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "fd7d5dab855e3234",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### What if we could let LLM interact in natural language with a structured system like this:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "683c557088782e47",
|
||
"metadata": {},
|
||
"source": [
|
||
""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "1bf1fa3631dc03ed",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### How we do it\n",
|
||
"We use Knowledge Graphs. Knowledge graphs simply map out knowledge, linking specific facts and their connections. When Large Language Models (LLMs) process text, they infer these links, leading to occasional inaccuracies due to their probabilistic nature. Clearly defined relationships enhance their accuracy. This structured approach can extend beyond concepts to document layouts, pages, or other organizational schemas.\n",
|
||
"\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "074f0ea8-c659-4736-be26-be4b0e5ac665",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Demo time"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "0587d91d",
|
||
"metadata": {},
|
||
"source": [
|
||
"First let's define some data that we will cognify and perform a search on"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "df16431d0f48b006",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:32:37.309629Z",
|
||
"start_time": "2025-02-09T21:32:37.305105Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"job_position = \"\"\"Senior Data Scientist (Machine Learning)\n",
|
||
"\n",
|
||
"Company: TechNova Solutions\n",
|
||
"Location: San Francisco, CA\n",
|
||
"\n",
|
||
"Job Description:\n",
|
||
"\n",
|
||
"TechNova Solutions is seeking a Senior Data Scientist specializing in Machine Learning to join our dynamic analytics team. The ideal candidate will have a strong background in developing and deploying machine learning models, working with large datasets, and translating complex data into actionable insights.\n",
|
||
"\n",
|
||
"Responsibilities:\n",
|
||
"\n",
|
||
"Develop and implement advanced machine learning algorithms and models.\n",
|
||
"Analyze large, complex datasets to extract meaningful patterns and insights.\n",
|
||
"Collaborate with cross-functional teams to integrate predictive models into products.\n",
|
||
"Stay updated with the latest advancements in machine learning and data science.\n",
|
||
"Mentor junior data scientists and provide technical guidance.\n",
|
||
"Qualifications:\n",
|
||
"\n",
|
||
"Master’s or Ph.D. in Data Science, Computer Science, Statistics, or a related field.\n",
|
||
"5+ years of experience in data science and machine learning.\n",
|
||
"Proficient in Python, R, and SQL.\n",
|
||
"Experience with deep learning frameworks (e.g., TensorFlow, PyTorch).\n",
|
||
"Strong problem-solving skills and attention to detail.\n",
|
||
"Candidate CVs\n",
|
||
"\"\"\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "9086abf3af077ab4",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:32:37.869475Z",
|
||
"start_time": "2025-02-09T21:32:37.867374Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"job_1 = \"\"\"\n",
|
||
"CV 1: Relevant\n",
|
||
"Name: Dr. Emily Carter\n",
|
||
"Contact Information:\n",
|
||
"\n",
|
||
"Email: emily.carter@example.com\n",
|
||
"Phone: (555) 123-4567\n",
|
||
"Summary:\n",
|
||
"\n",
|
||
"Senior Data Scientist with over 8 years of experience in machine learning and predictive analytics. Expertise in developing advanced algorithms and deploying scalable models in production environments.\n",
|
||
"\n",
|
||
"Education:\n",
|
||
"\n",
|
||
"Ph.D. in Computer Science, Stanford University (2014)\n",
|
||
"B.S. in Mathematics, University of California, Berkeley (2010)\n",
|
||
"Experience:\n",
|
||
"\n",
|
||
"Senior Data Scientist, InnovateAI Labs (2016 – Present)\n",
|
||
"Led a team in developing machine learning models for natural language processing applications.\n",
|
||
"Implemented deep learning algorithms that improved prediction accuracy by 25%.\n",
|
||
"Collaborated with cross-functional teams to integrate models into cloud-based platforms.\n",
|
||
"Data Scientist, DataWave Analytics (2014 – 2016)\n",
|
||
"Developed predictive models for customer segmentation and churn analysis.\n",
|
||
"Analyzed large datasets using Hadoop and Spark frameworks.\n",
|
||
"Skills:\n",
|
||
"\n",
|
||
"Programming Languages: Python, R, SQL\n",
|
||
"Machine Learning: TensorFlow, Keras, Scikit-Learn\n",
|
||
"Big Data Technologies: Hadoop, Spark\n",
|
||
"Data Visualization: Tableau, Matplotlib\n",
|
||
"\"\"\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "a9de0cc07f798b7f",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:32:38.269062Z",
|
||
"start_time": "2025-02-09T21:32:38.267194Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"job_2 = \"\"\"\n",
|
||
"CV 2: Relevant\n",
|
||
"Name: Michael Rodriguez\n",
|
||
"Contact Information:\n",
|
||
"\n",
|
||
"Email: michael.rodriguez@example.com\n",
|
||
"Phone: (555) 234-5678\n",
|
||
"Summary:\n",
|
||
"\n",
|
||
"Data Scientist with a strong background in machine learning and statistical modeling. Skilled in handling large datasets and translating data into actionable business insights.\n",
|
||
"\n",
|
||
"Education:\n",
|
||
"\n",
|
||
"M.S. in Data Science, Carnegie Mellon University (2013)\n",
|
||
"B.S. in Computer Science, University of Michigan (2011)\n",
|
||
"Experience:\n",
|
||
"\n",
|
||
"Senior Data Scientist, Alpha Analytics (2017 – Present)\n",
|
||
"Developed machine learning models to optimize marketing strategies.\n",
|
||
"Reduced customer acquisition cost by 15% through predictive modeling.\n",
|
||
"Data Scientist, TechInsights (2013 – 2017)\n",
|
||
"Analyzed user behavior data to improve product features.\n",
|
||
"Implemented A/B testing frameworks to evaluate product changes.\n",
|
||
"Skills:\n",
|
||
"\n",
|
||
"Programming Languages: Python, Java, SQL\n",
|
||
"Machine Learning: Scikit-Learn, XGBoost\n",
|
||
"Data Visualization: Seaborn, Plotly\n",
|
||
"Databases: MySQL, MongoDB\n",
|
||
"\"\"\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"id": "185ff1c102d06111",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:32:38.738093Z",
|
||
"start_time": "2025-02-09T21:32:38.736147Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"job_3 = \"\"\"\n",
|
||
"CV 3: Relevant\n",
|
||
"Name: Sarah Nguyen\n",
|
||
"Contact Information:\n",
|
||
"\n",
|
||
"Email: sarah.nguyen@example.com\n",
|
||
"Phone: (555) 345-6789\n",
|
||
"Summary:\n",
|
||
"\n",
|
||
"Data Scientist specializing in machine learning with 6 years of experience. Passionate about leveraging data to drive business solutions and improve product performance.\n",
|
||
"\n",
|
||
"Education:\n",
|
||
"\n",
|
||
"M.S. in Statistics, University of Washington (2014)\n",
|
||
"B.S. in Applied Mathematics, University of Texas at Austin (2012)\n",
|
||
"Experience:\n",
|
||
"\n",
|
||
"Data Scientist, QuantumTech (2016 – Present)\n",
|
||
"Designed and implemented machine learning algorithms for financial forecasting.\n",
|
||
"Improved model efficiency by 20% through algorithm optimization.\n",
|
||
"Junior Data Scientist, DataCore Solutions (2014 – 2016)\n",
|
||
"Assisted in developing predictive models for supply chain optimization.\n",
|
||
"Conducted data cleaning and preprocessing on large datasets.\n",
|
||
"Skills:\n",
|
||
"\n",
|
||
"Programming Languages: Python, R\n",
|
||
"Machine Learning Frameworks: PyTorch, Scikit-Learn\n",
|
||
"Statistical Analysis: SAS, SPSS\n",
|
||
"Cloud Platforms: AWS, Azure\n",
|
||
"\"\"\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "d55ce4c58f8efb67",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:32:39.237542Z",
|
||
"start_time": "2025-02-09T21:32:39.235742Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"job_4 = \"\"\"\n",
|
||
"CV 4: Not Relevant\n",
|
||
"Name: David Thompson\n",
|
||
"Contact Information:\n",
|
||
"\n",
|
||
"Email: david.thompson@example.com\n",
|
||
"Phone: (555) 456-7890\n",
|
||
"Summary:\n",
|
||
"\n",
|
||
"Creative Graphic Designer with over 8 years of experience in visual design and branding. Proficient in Adobe Creative Suite and passionate about creating compelling visuals.\n",
|
||
"\n",
|
||
"Education:\n",
|
||
"\n",
|
||
"B.F.A. in Graphic Design, Rhode Island School of Design (2012)\n",
|
||
"Experience:\n",
|
||
"\n",
|
||
"Senior Graphic Designer, CreativeWorks Agency (2015 – Present)\n",
|
||
"Led design projects for clients in various industries.\n",
|
||
"Created branding materials that increased client engagement by 30%.\n",
|
||
"Graphic Designer, Visual Innovations (2012 – 2015)\n",
|
||
"Designed marketing collateral, including brochures, logos, and websites.\n",
|
||
"Collaborated with the marketing team to develop cohesive brand strategies.\n",
|
||
"Skills:\n",
|
||
"\n",
|
||
"Design Software: Adobe Photoshop, Illustrator, InDesign\n",
|
||
"Web Design: HTML, CSS\n",
|
||
"Specialties: Branding and Identity, Typography\n",
|
||
"\"\"\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"id": "ca4ecc32721ad332",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:32:39.740387Z",
|
||
"start_time": "2025-02-09T21:32:39.738768Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"job_5 = \"\"\"\n",
|
||
"CV 5: Not Relevant\n",
|
||
"Name: Jessica Miller\n",
|
||
"Contact Information:\n",
|
||
"\n",
|
||
"Email: jessica.miller@example.com\n",
|
||
"Phone: (555) 567-8901\n",
|
||
"Summary:\n",
|
||
"\n",
|
||
"Experienced Sales Manager with a strong track record in driving sales growth and building high-performing teams. Excellent communication and leadership skills.\n",
|
||
"\n",
|
||
"Education:\n",
|
||
"\n",
|
||
"B.A. in Business Administration, University of Southern California (2010)\n",
|
||
"Experience:\n",
|
||
"\n",
|
||
"Sales Manager, Global Enterprises (2015 – Present)\n",
|
||
"Managed a sales team of 15 members, achieving a 20% increase in annual revenue.\n",
|
||
"Developed sales strategies that expanded customer base by 25%.\n",
|
||
"Sales Representative, Market Leaders Inc. (2010 – 2015)\n",
|
||
"Consistently exceeded sales targets and received the 'Top Salesperson' award in 2013.\n",
|
||
"Skills:\n",
|
||
"\n",
|
||
"Sales Strategy and Planning\n",
|
||
"Team Leadership and Development\n",
|
||
"CRM Software: Salesforce, Zoho\n",
|
||
"Negotiation and Relationship Building\n",
|
||
"\"\"\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "4415446a",
|
||
"metadata": {},
|
||
"source": [
|
||
" Please add the necessary environment information bellow:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 22,
|
||
"id": "bce39dc6",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:31:46.855966Z",
|
||
"start_time": "2025-02-09T21:31:46.847681Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import os\n",
|
||
"\n",
|
||
"# Setting environment variables\n",
|
||
"if \"GRAPHISTRY_USERNAME\" not in os.environ:\n",
|
||
" os.environ[\"GRAPHISTRY_USERNAME\"] = \"\"\n",
|
||
"\n",
|
||
"if \"GRAPHISTRY_PASSWORD\" not in os.environ:\n",
|
||
" os.environ[\"GRAPHISTRY_PASSWORD\"] = \"\"\n",
|
||
"\n",
|
||
"if \"LLM_API_KEY\" not in os.environ:\n",
|
||
" os.environ[\"LLM_API_KEY\"] = \"\"\n",
|
||
"\n",
|
||
"# \"neo4j\" or \"networkx\"\n",
|
||
"os.environ[\"GRAPH_DATABASE_PROVIDER\"] = \"networkx\"\n",
|
||
"# Not needed if using networkx\n",
|
||
"# os.environ[\"GRAPH_DATABASE_URL\"]=\"\"\n",
|
||
"# os.environ[\"GRAPH_DATABASE_USERNAME\"]=\"\"\n",
|
||
"# os.environ[\"GRAPH_DATABASE_PASSWORD\"]=\"\"\n",
|
||
"\n",
|
||
"# \"pgvector\", \"qdrant\", \"weaviate\" or \"lancedb\"\n",
|
||
"os.environ[\"VECTOR_DB_PROVIDER\"] = \"lancedb\"\n",
|
||
"# Not needed if using \"lancedb\" or \"pgvector\"\n",
|
||
"# os.environ[\"VECTOR_DB_URL\"]=\"\"\n",
|
||
"# os.environ[\"VECTOR_DB_KEY\"]=\"\"\n",
|
||
"\n",
|
||
"# Relational Database provider \"sqlite\" or \"postgres\"\n",
|
||
"os.environ[\"DB_PROVIDER\"] = \"sqlite\"\n",
|
||
"\n",
|
||
"# Database name\n",
|
||
"os.environ[\"DB_NAME\"] = \"cognee_db\"\n",
|
||
"\n",
|
||
"# Postgres specific parameters (Only if Postgres or PGVector is used)\n",
|
||
"# os.environ[\"DB_HOST\"]=\"127.0.0.1\"\n",
|
||
"# os.environ[\"DB_PORT\"]=\"5432\"\n",
|
||
"# os.environ[\"DB_USERNAME\"]=\"cognee\"\n",
|
||
"# os.environ[\"DB_PASSWORD\"]=\"cognee\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "9f1a1dbd",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:32:56.703003Z",
|
||
"start_time": "2025-02-09T21:32:47.375684Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Reset the cognee system with the following command:\n",
|
||
"\n",
|
||
"import cognee\n",
|
||
"\n",
|
||
"await cognee.prune.prune_data()\n",
|
||
"await cognee.prune.prune_system(metadata=True)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "383d6971",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### After we have defined and gathered our data let's add it to cognee "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "904df61ba484a8e5",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:33:07.412263Z",
|
||
"start_time": "2025-02-09T21:33:04.807282Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import cognee\n",
|
||
"\n",
|
||
"await cognee.add([job_1, job_2, job_3, job_4, job_5, job_position], \"example\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "0f15c5b1",
|
||
"metadata": {},
|
||
"source": [
|
||
"All good, let's cognify it."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "7c431fdef4921ae0",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:33:21.450585Z",
|
||
"start_time": "2025-02-09T21:33:21.446326Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from cognee.shared.data_models import KnowledgeGraph\n",
|
||
"from cognee.modules.data.models import Dataset, Data\n",
|
||
"from cognee.modules.data.methods.get_dataset_data import get_dataset_data\n",
|
||
"from cognee.modules.cognify.config import get_cognify_config\n",
|
||
"from cognee.modules.pipelines.tasks.task import Task\n",
|
||
"from cognee.modules.pipelines import run_tasks\n",
|
||
"from cognee.modules.users.models import User\n",
|
||
"from cognee.tasks.documents import (\n",
|
||
" check_permissions_on_documents,\n",
|
||
" classify_documents,\n",
|
||
" extract_chunks_from_documents,\n",
|
||
")\n",
|
||
"from cognee.infrastructure.llm import get_max_chunk_tokens\n",
|
||
"from cognee.tasks.graph import extract_graph_from_data\n",
|
||
"from cognee.tasks.storage import add_data_points\n",
|
||
"from cognee.tasks.summarization import summarize_text\n",
|
||
"\n",
|
||
"\n",
|
||
"async def run_cognify_pipeline(dataset: Dataset, user: User = None):\n",
|
||
" data_documents: list[Data] = await get_dataset_data(dataset_id=dataset.id)\n",
|
||
"\n",
|
||
" try:\n",
|
||
" cognee_config = get_cognify_config()\n",
|
||
"\n",
|
||
" tasks = [\n",
|
||
" Task(classify_documents),\n",
|
||
" Task(check_permissions_on_documents, user=user, permissions=[\"write\"]),\n",
|
||
" Task(\n",
|
||
" extract_chunks_from_documents, max_chunk_size=get_max_chunk_tokens()\n",
|
||
" ), # Extract text chunks based on the document type.\n",
|
||
" Task(\n",
|
||
" extract_graph_from_data, graph_model=KnowledgeGraph,\n",
|
||
" task_config={\"batch_size\": 10}\n",
|
||
" ), # Generate knowledge graphs from the document chunks.\n",
|
||
" Task(\n",
|
||
" summarize_text,\n",
|
||
" summarization_model=cognee_config.summarization_model,\n",
|
||
" task_config={\"batch_size\": 10},\n",
|
||
" ),\n",
|
||
" Task(add_data_points, task_config={\"batch_size\": 10}),\n",
|
||
" ]\n",
|
||
"\n",
|
||
" pipeline_run = run_tasks(tasks, dataset.id, data_documents, user, \"cognify_pipeline\")\n",
|
||
" pipeline_run_status = None\n",
|
||
"\n",
|
||
" async for run_status in pipeline_run:\n",
|
||
" pipeline_run_status = run_status\n",
|
||
"\n",
|
||
" except Exception as error:\n",
|
||
" raise error"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "f0a91b99c6215e09",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:33:50.228396Z",
|
||
"start_time": "2025-02-09T21:33:27.935791Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from cognee.modules.users.methods import get_default_user\n",
|
||
"from cognee.modules.data.methods import get_datasets_by_name\n",
|
||
"from cognee.modules.users.methods import get_user\n",
|
||
"\n",
|
||
"default_user = await get_default_user()\n",
|
||
"\n",
|
||
"user = await get_user(default_user.id)\n",
|
||
"\n",
|
||
"datasets = await get_datasets_by_name([\"example\"], user.id)\n",
|
||
"\n",
|
||
"await run_cognify_pipeline(datasets[0], user)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "9dd29caf28c272d1",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:42:39.479622Z",
|
||
"start_time": "2025-02-09T21:42:39.469795Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import pathlib\n",
|
||
"import os\n",
|
||
"from cognee.api.v1.visualize import visualize_graph\n",
|
||
"\n",
|
||
"# Use the current working directory instead of __file__:\n",
|
||
"notebook_dir = pathlib.Path.cwd()\n",
|
||
"\n",
|
||
"graph_file_path = (notebook_dir / \".artifacts\" / \"graph_visualization.html\").resolve()\n",
|
||
"\n",
|
||
"# Make sure to convert to string if visualize_graph expects a string\n",
|
||
"b = await visualize_graph(str(graph_file_path))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "9f941848c418d713",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-02-09T21:42:59.312945Z",
|
||
"start_time": "2025-02-09T21:42:59.311015Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import http.server\n",
|
||
"import socketserver\n",
|
||
"from threading import Thread\n",
|
||
"\n",
|
||
"PORT = 8001\n",
|
||
"\n",
|
||
"\n",
|
||
"class ServerThread(Thread):\n",
|
||
" def run(self):\n",
|
||
" Handler = http.server.SimpleHTTPRequestHandler\n",
|
||
" with socketserver.TCPServer((\"\", PORT), Handler) as httpd:\n",
|
||
" print(\"serving at port\", PORT)\n",
|
||
" httpd.serve_forever()\n",
|
||
"\n",
|
||
"\n",
|
||
"ServerThread().start()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "77d5794d-3561-4c9c-a001-df7d6b0c1968",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from cognee.api.v1.visualize import visualization_server\n",
|
||
"\n",
|
||
"visualization_server(port=8002)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "030c6aac-650c-42dc-a89b-d21a5f422474",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from IPython.display import IFrame, display, HTML\n",
|
||
"\n",
|
||
"IFrame(\"http://127.0.0.1:8002/.artifacts/graph_visualization.html\", width=800, height=600)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "219a6d41",
|
||
"metadata": {},
|
||
"source": [
|
||
" We get the url to the graph on graphistry in the notebook cell bellow, showing nodes and connections made by the cognify process."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "080389e5",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2024-12-29T16:55:51.378129Z",
|
||
"start_time": "2024-12-29T16:55:46.922951Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import os\n",
|
||
"from cognee.shared.utils import render_graph\n",
|
||
"from cognee.infrastructure.databases.graph import get_graph_engine\n",
|
||
"import graphistry\n",
|
||
"\n",
|
||
"# from dotenv import load_dotenv\n",
|
||
"graphistry.login(\n",
|
||
" username=os.getenv(\"GRAPHISTRY_USERNAME\"), password=os.getenv(\"GRAPHISTRY_PASSWORD\")\n",
|
||
")\n",
|
||
"\n",
|
||
"graph_engine = await get_graph_engine()\n",
|
||
"\n",
|
||
"graph_url = await render_graph(graph_engine.graph)\n",
|
||
"print(graph_url)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "8f69caa0e353a889",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2024-12-29T16:56:06.571404Z",
|
||
"start_time": "2024-12-29T16:56:06.569280Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"graph_engine = await get_graph_engine()\n",
|
||
"print(graph_url)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "59e6c3c3",
|
||
"metadata": {},
|
||
"source": [
|
||
" We can also do a search on the data to explore the knowledge."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e5e7dfc8",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2024-12-24T13:44:16.575843Z",
|
||
"start_time": "2024-12-24T13:44:16.047897Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"async def search(\n",
|
||
" vector_engine,\n",
|
||
" collection_name: str,\n",
|
||
" query_text: str = None,\n",
|
||
"):\n",
|
||
" query_vector = (await vector_engine.embedding_engine.embed_text([query_text]))[0]\n",
|
||
"\n",
|
||
" connection = await vector_engine.get_connection()\n",
|
||
" collection = await connection.open_table(collection_name)\n",
|
||
"\n",
|
||
" results = await collection.vector_search(query_vector).limit(10).to_pandas()\n",
|
||
"\n",
|
||
" result_values = list(results.to_dict(\"index\").values())\n",
|
||
"\n",
|
||
" return [\n",
|
||
" dict(\n",
|
||
" id=str(result[\"id\"]),\n",
|
||
" payload=result[\"payload\"],\n",
|
||
" score=result[\"_distance\"],\n",
|
||
" )\n",
|
||
" for result in result_values\n",
|
||
" ]\n",
|
||
"\n",
|
||
"\n",
|
||
"from cognee.infrastructure.databases.vector import get_vector_engine\n",
|
||
"\n",
|
||
"vector_engine = get_vector_engine()\n",
|
||
"results = await search(vector_engine, \"Entity_name\", \"sarah.nguyen@example.com\")\n",
|
||
"for result in results:\n",
|
||
" print(result)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "81fa2b00",
|
||
"metadata": {},
|
||
"source": [
|
||
" We normalize search output scores so the lower the score of the search result is the higher the chance that it's what you're looking for. In the example above we have searched for node entities in the knowledge graph related to \"sarah.nguyen@example.com\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "1b94ff96",
|
||
"metadata": {},
|
||
"source": [
|
||
" In the example bellow we'll use cognee search to summarize information regarding the node most related to \"sarah.nguyen@example.com\" in the knowledge graph"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "21a3e9a6",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from cognee.api.v1.search import SearchType\n",
|
||
"\n",
|
||
"node = (await vector_engine.search(\"Entity_name\", \"sarah.nguyen@example.com\"))[0]\n",
|
||
"node_name = node.payload[\"text\"]\n",
|
||
"\n",
|
||
"search_results = await cognee.search(query_type=SearchType.SUMMARIES, query_text=node_name)\n",
|
||
"print(\"\\n\\Extracted summaries are:\\n\")\n",
|
||
"for result in search_results:\n",
|
||
" print(f\"{result}\\n\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "fd6e5fe2",
|
||
"metadata": {},
|
||
"source": [
|
||
"In this example we'll use cognee search to find chunks in which the node most related to \"sarah.nguyen@example.com\" is a part of"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "c7a8abff",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"search_results = await cognee.search(query_type=SearchType.CHUNKS, query_text=node_name)\n",
|
||
"print(\"\\n\\nExtracted chunks are:\\n\")\n",
|
||
"for result in search_results:\n",
|
||
" print(f\"{result}\\n\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "47f0112f",
|
||
"metadata": {},
|
||
"source": [
|
||
" In this example we'll use cognee search to give us insights from the knowledge graph related to the node most related to \"sarah.nguyen@example.com\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "706a3954",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"search_results = await cognee.search(query_type=SearchType.INSIGHTS, query_text=node_name)\n",
|
||
"print(\"\\n\\nExtracted sentences are:\\n\")\n",
|
||
"for result in search_results:\n",
|
||
" print(f\"{result}\\n\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "288ab570",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Give us a star if you like it!\n",
|
||
"https://github.com/topoteretes/cognee"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "py312",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.12.8"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|