Quick Start

Getting started with Graphiti

Graphiti uses OpenAI for LLM inference and embedding. Ensure that an OPENAI_API_KEY is set in your environment. Support for Anthropic and Groq LLM inferences is available, too.

Initialize graphiti

1from graphiti_core import Graphiti
2from graphiti_core.nodes import EpisodeType
3from datetime import datetime
4
5graphiti = Graphiti("bolt://localhost:7687", "neo4j", "password")
6
7# Initialize the graph database with graphiti's indices. This only needs to be done once.
8await graphiti.build_indices_and_constraints()

Add Episodes

1episodes = [
2 "Kamala Harris is the Attorney General of California. She was previously "
3 "the district attorney for San Francisco.",
4 "As AG, Harris was in office from January 3, 2011 – January 3, 2017",
5]
6for i, episode in enumerate(episodes):
7 await graphiti.add_episode(
8 name=f"Freakonomics Radio {i}",
9 episode_body=episode,
10 source=EpisodeType.text,
11 source_description="podcast",
12 reference_time=datetime.now()
13 )

Search the graph

Execute a hybrid search combining semantic similarity and BM25 retrieval Results are combined and reranked using Reciprocal Rank Fusion

1results = await graphiti.search('Who was the California Attorney General?')
2[
3 EntityEdge(
4 │ uuid='3133258f738e487383f07b04e15d4ac0',
5 │ source_node_uuid='2a85789b318d4e418050506879906e62',
6 │ target_node_uuid='baf7781f445945989d6e4f927f881556',
7 │ created_at=datetime.datetime(2024, 8, 26, 13, 13, 24, 861097),
8 │ name='HELD_POSITION',
9 # the fact reflects the updated state that Harris is
10 # no longer the AG of California
11 │ fact='Kamala Harris was the Attorney General of California',
12 │ fact_embedding=[
13 │ │ -0.009955154731869698,
14 │ ...
15 │ │ 0.00784289836883545
16 │ ],
17 │ episodes=['b43e98ad0a904088a76c67985caecc22'],
18 │ expired_at=datetime.datetime(2024, 8, 26, 20, 18, 1, 53812),
19 # These dates represent the date this edge was true.
20 │ valid_at=datetime.datetime(2011, 1, 3, 0, 0, tzinfo=<UTC>),
21 │ invalid_at=datetime.datetime(2017, 1, 3, 0, 0, tzinfo=<UTC>)
22 )
23]

Rerank search results based on graph distance

Provide a node UUID to prioritize results closer to that node in the graph. Results are weighted by their proximity, with distant edges receiving lower scores.

1await client.search('Who was the California Attorney General?', center_node_uuid)

Close the connection

1graphiti.close()