fix(semantic_extract): Pass API key to Groq LLM provider in extraction methods

- Add API key handling in extract_entities_llm(), extract_relations_llm(), and extract_triplets_llm()
- Add explicit api_key handling in NERExtractor and RelationExtractor
- Add llm_model parameter support in extract_triplets_llm() for consistency
- Fix relation extraction bug with type checking for subject_text/object_text
- Add environment variable fallback for API keys
- Update notebook with standard API key pattern

Fixes #147
This commit is contained in:
KaifAhmad1
2026-01-07 01:07:56 +05:30
parent 9c59f97542
commit 2790132e8e
6 changed files with 131 additions and 13 deletions
@@ -73,11 +73,28 @@
"from semantica.llms import Groq\n",
"import os\n",
"\n",
"GROQ_API_KEY = \"\"\n",
"# Set your Groq API key here or as an environment variable\n",
"# Option 1: Set environment variable (recommended): export GROQ_API_KEY=\"your-api-key-here\"\n",
"# Option 2: For Google Colab: from google.colab import userdata; GROQ_API_KEY = userdata.get(\"GROQ_API_KEY\")\n",
"# Option 3: Set directly below (not recommended for production)\n",
"GROQ_API_KEY = os.getenv(\"GROQ_API_KEY\", \"\")\n",
"\n",
"if not GROQ_API_KEY:\n",
" try:\n",
" from google.colab import userdata\n",
" GROQ_API_KEY = userdata.get(\"GROQ_API_KEY\", \"\")\n",
" except ImportError:\n",
" pass\n",
"\n",
"if not GROQ_API_KEY:\n",
" raise ValueError(\"GROQ_API_KEY not found. Please set it as an environment variable or update this cell.\")\n",
"\n",
"os.environ[\"GROQ_API_KEY\"] = GROQ_API_KEY\n",
" \n",
"groq_llm = Groq(\n",
" model=\"llama-3.1-8b-instant\",\n",
" api_key=os.getenv(\"GROQ_API_KEY\", GROQ_API_KEY))\n",
" model=\"llama-3.1-8b-instant\",\n",
" api_key=GROQ_API_KEY\n",
")\n",
"\n",
"print(f\"✓ Groq LLM initialized: {groq_llm.model}\")\n"
]
@@ -216,6 +233,7 @@
"source": [
"# Step 3: Extract entities using NERExtractor with Groq\n",
"from semantica.semantic_extract import NERExtractor\n",
"import os\n",
"\n",
"text_for_extraction = parsed_doc[\"full_text\"]\n",
"\n",
@@ -224,7 +242,8 @@
" provider=\"groq\",\n",
" llm_model=\"llama-3.1-8b-instant\",\n",
" min_confidence=0.5,\n",
" temperature=0.0\n",
" temperature=0.0,\n",
" api_key=os.getenv(\"GROQ_API_KEY\")\n",
")\n",
"\n",
"entity_types = [\n",
@@ -324,6 +343,7 @@
"source": [
"# Step 5: Extract relationships using RelationExtractor with Groq LLM\n",
"from semantica.semantic_extract import RelationExtractor\n",
"import os\n",
"\n",
"if not entities:\n",
" print(\"⚠️ No entities found. Skipping relationship extraction.\")\n",
@@ -339,7 +359,8 @@
" \"COMPARED_TO\", \"INCREASED_BY\", \"DECREASED_BY\", \"CHANGED_BY\",\n",
" \"DURING\", \"IN_QUARTER\", \"FOR_PERIOD\",\n",
" \"RELATED_TO\", \"PART_OF\", \"AFFECTS\"\n",
" ]\n",
" ],\n",
" api_key=os.getenv(\"GROQ_API_KEY\")\n",
" )\n",
"\n",
" relationships = relation_extractor.extract_relations(\n",
@@ -408,6 +429,7 @@
"source": [
"# Step 6: Extract RDF triplets using TripletExtractor with Groq LLM\n",
"from semantica.semantic_extract import TripletExtractor\n",
"import os\n",
"\n",
"if not entities:\n",
" print(\"⚠️ No entities found. Skipping triplet extraction.\")\n",
@@ -417,16 +439,17 @@
" triplet_extractor = TripletExtractor(\n",
" method=\"llm\",\n",
" include_temporal=True,\n",
" include_provenance=True\n",
" include_provenance=True,\n",
" provider=\"groq\",\n",
" llm_model=\"llama-3.1-8b-instant\",\n",
" temperature=0.0,\n",
" api_key=os.getenv(\"GROQ_API_KEY\")\n",
" )\n",
"\n",
" triplets = triplet_extractor.extract_triplets(\n",
" text_for_extraction,\n",
" entities=entities,\n",
" relations=relationships if relationships else None,\n",
" provider=\"groq\",\n",
" llm_model=\"llama-3.1-8b-instant\",\n",
" temperature=0.0\n",
" relations=relationships if relationships else None\n",
" )\n",
"\n",
" if hasattr(triplet_extractor, 'triplet_validator'):\n",