Spaces:
Sleeping
Sleeping
File size: 1,263 Bytes
cf450f7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import logging
from textwrap import dedent
import logfire
from surrealdb import RecordID
from kaig.db import DB
from ..definitions import Chunk, Concept, EdgeTypes, Tables
logger = logging.getLogger(__name__)
def inferrence_handler(db: DB, chunk: Chunk) -> list[str]:
if not db.llm:
logger.warning("No LLM configured, skipping inference")
return []
with logfire.span("Extract concepts {chunk=}", chunk=chunk.id):
instructions = dedent("""
- Only return concepts that are: names, places, people, organizations, events, products, services, etc.
- Do not include symbols or numbers
""")
concepts = db.llm.infer_concepts(chunk.content, instructions)
logger.info(f"Concepts: {concepts}")
for concept in concepts:
concept_id = RecordID(Tables.concept.value, concept)
_ = db.embed_and_insert(
Concept(content=concept, id=concept_id),
table=Tables.concept.value,
id=concept,
)
db.relate(
chunk.id,
EdgeTypes.MENTIONS_CONCEPT.value.name,
concept_id,
)
logger.info("Finished inference!")
return concepts
|