Local Search
In [1]:
Copied!
# Copyright (c) 2024 Microsoft Corporation.
# Licensed under the MIT License.
# Copyright (c) 2024 Microsoft Corporation.
# Licensed under the MIT License.
In [2]:
Copied!
import os
import pandas as pd
from graphrag.query.context_builder.entity_extraction import EntityVectorStoreKey
from graphrag.query.indexer_adapters import (
read_indexer_covariates,
read_indexer_entities,
read_indexer_relationships,
read_indexer_reports,
read_indexer_text_units,
)
from graphrag.query.question_gen.local_gen import LocalQuestionGen
from graphrag.query.structured_search.local_search.mixed_context import (
LocalSearchMixedContext,
)
from graphrag.query.structured_search.local_search.search import LocalSearch
from graphrag_vectors import (
IndexSchema,
VectorStoreConfig,
VectorStoreType,
create_vector_store,
)
import os
import pandas as pd
from graphrag.query.context_builder.entity_extraction import EntityVectorStoreKey
from graphrag.query.indexer_adapters import (
read_indexer_covariates,
read_indexer_entities,
read_indexer_relationships,
read_indexer_reports,
read_indexer_text_units,
)
from graphrag.query.question_gen.local_gen import LocalQuestionGen
from graphrag.query.structured_search.local_search.mixed_context import (
LocalSearchMixedContext,
)
from graphrag.query.structured_search.local_search.search import LocalSearch
from graphrag_vectors import (
IndexSchema,
VectorStoreConfig,
VectorStoreType,
create_vector_store,
)
Local Search Example¶
Local search method generates answers by combining relevant data from the AI-extracted knowledge-graph with text chunks of the raw documents. This method is suitable for questions that require an understanding of specific entities mentioned in the documents (e.g. What are the healing properties of chamomile?).
Load text units and graph data tables as context for local search¶
- In this test we first load indexing outputs from parquet files to dataframes, then convert these dataframes into collections of data objects aligning with the knowledge model.
Load tables to dataframes¶
In [3]:
Copied!
INPUT_DIR = "./inputs/operation dulce"
LANCEDB_URI = f"{INPUT_DIR}/lancedb"
COMMUNITY_REPORT_TABLE = "community_reports"
ENTITY_TABLE = "entities"
COMMUNITY_TABLE = "communities"
RELATIONSHIP_TABLE = "relationships"
COVARIATE_TABLE = "covariates"
TEXT_UNIT_TABLE = "text_units"
COMMUNITY_LEVEL = 2
INPUT_DIR = "./inputs/operation dulce"
LANCEDB_URI = f"{INPUT_DIR}/lancedb"
COMMUNITY_REPORT_TABLE = "community_reports"
ENTITY_TABLE = "entities"
COMMUNITY_TABLE = "communities"
RELATIONSHIP_TABLE = "relationships"
COVARIATE_TABLE = "covariates"
TEXT_UNIT_TABLE = "text_units"
COMMUNITY_LEVEL = 2
Read entities¶
In [4]:
Copied!
# read nodes table to get community and degree data
entity_df = pd.read_parquet(f"{INPUT_DIR}/{ENTITY_TABLE}.parquet")
community_df = pd.read_parquet(f"{INPUT_DIR}/{COMMUNITY_TABLE}.parquet")
entities = read_indexer_entities(entity_df, community_df, COMMUNITY_LEVEL)
# load description embeddings to an in-memory lancedb vectorstore
# to connect to a remote db, specify url and port values.
description_embedding_store = create_vector_store(
config=VectorStoreConfig(type=VectorStoreType.LanceDB, uri=LANCEDB_URI),
index_schema=IndexSchema(index_name="default-entity-description"),
)
description_embedding_store.connect()
print(f"Entity count: {len(entity_df)}")
entity_df.head()
# read nodes table to get community and degree data
entity_df = pd.read_parquet(f"{INPUT_DIR}/{ENTITY_TABLE}.parquet")
community_df = pd.read_parquet(f"{INPUT_DIR}/{COMMUNITY_TABLE}.parquet")
entities = read_indexer_entities(entity_df, community_df, COMMUNITY_LEVEL)
# load description embeddings to an in-memory lancedb vectorstore
# to connect to a remote db, specify url and port values.
description_embedding_store = create_vector_store(
config=VectorStoreConfig(type=VectorStoreType.LanceDB, uri=LANCEDB_URI),
index_schema=IndexSchema(index_name="default-entity-description"),
)
description_embedding_store.connect()
print(f"Entity count: {len(entity_df)}")
entity_df.head()
Entity count: 39
Out[4]:
| id | human_readable_id | title | type | description | text_unit_ids | frequency | degree | |
|---|---|---|---|---|---|---|---|---|
| 0 | bbb2ad85-3d0b-4c26-b6f9-71ed76ee64bd | 0 | ALEX MERCER | PERSON | Agent Alex Mercer is a determined and thoughtf... | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... | 5 | 17 |
| 1 | 66112f07-d45e-4394-92ad-66e87c8579ff | 1 | TAYLOR CRUZ | PERSON | Taylor Cruz is a confident, driven, and ambiti... | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... | 4 | 17 |
| 2 | e1274d98-d87d-4307-9e9d-851146e85637 | 2 | JORDAN HAYES | PERSON | Dr. Jordan Hayes is a highly analytical and sk... | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... | 4 | 13 |
| 3 | d1135bbc-3bb8-450f-acf9-f3199e9c45cf | 3 | SAM RIVERA | PERSON | Sam Rivera is a highly skilled and adaptable m... | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... | 4 | 17 |
| 4 | 6e9b5c3b-2ec4-4e48-b111-cebfbd02ee0f | 4 | PARANORMAL MILITARY SQUAD | ORGANIZATION | The Paranormal Military Squad is an elite mili... | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... | 2 | 12 |
Read relationships¶
In [5]:
Copied!
relationship_df = pd.read_parquet(f"{INPUT_DIR}/{RELATIONSHIP_TABLE}.parquet")
relationships = read_indexer_relationships(relationship_df)
print(f"Relationship count: {len(relationship_df)}")
relationship_df.head()
relationship_df = pd.read_parquet(f"{INPUT_DIR}/{RELATIONSHIP_TABLE}.parquet")
relationships = read_indexer_relationships(relationship_df)
print(f"Relationship count: {len(relationship_df)}")
relationship_df.head()
Relationship count: 107
Out[5]:
| id | human_readable_id | source | target | description | weight | combined_degree | text_unit_ids | |
|---|---|---|---|---|---|---|---|---|
| 0 | 7c9039f8-43d7-4685-a317-f7bbbfac8e3a | 0 | ALEX MERCER | PARANORMAL MILITARY SQUAD | Alex Mercer is a prominent agent and operative... | 17.0 | 29 | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... |
| 1 | f0310841-7ad1-4286-8e1d-1641c180b3ab | 1 | TAYLOR CRUZ | PARANORMAL MILITARY SQUAD | Taylor Cruz is a leading agent in the Paranorm... | 9.0 | 29 | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... |
| 2 | 84441d65-6f4e-40eb-92ef-743ac6d1229c | 2 | JORDAN HAYES | PARANORMAL MILITARY SQUAD | Dr. Jordan Hayes is a key member of the Parano... | 17.0 | 25 | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... |
| 3 | ec2c0109-3a2f-479e-89d9-7a64abe07a63 | 3 | SAM RIVERA | PARANORMAL MILITARY SQUAD | Sam Rivera is a technical expert and agent wit... | 9.0 | 29 | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... |
| 4 | 19d249b1-487e-4db9-85db-410cfb6ab986 | 4 | PARANORMAL MILITARY SQUAD | OPERATION: DULCE | The Paranormal Military Squad is the team assi... | 10.0 | 20 | [ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc... |
In [6]:
Copied!
# NOTE: covariates are turned off by default, because they generally need prompt tuning to be valuable
# Please see the GRAPHRAG_CLAIM_* settings
covariate_df = pd.read_parquet(f"{INPUT_DIR}/{COVARIATE_TABLE}.parquet")
claims = read_indexer_covariates(covariate_df)
print(f"Claim records: {len(claims)}")
covariates = {"claims": claims}
# NOTE: covariates are turned off by default, because they generally need prompt tuning to be valuable
# Please see the GRAPHRAG_CLAIM_* settings
covariate_df = pd.read_parquet(f"{INPUT_DIR}/{COVARIATE_TABLE}.parquet")
claims = read_indexer_covariates(covariate_df)
print(f"Claim records: {len(claims)}")
covariates = {"claims": claims}
Claim records: 41
Read community reports¶
In [7]:
Copied!
report_df = pd.read_parquet(f"{INPUT_DIR}/{COMMUNITY_REPORT_TABLE}.parquet")
reports = read_indexer_reports(report_df, community_df, COMMUNITY_LEVEL)
print(f"Report records: {len(report_df)}")
report_df.head()
report_df = pd.read_parquet(f"{INPUT_DIR}/{COMMUNITY_REPORT_TABLE}.parquet")
reports = read_indexer_reports(report_df, community_df, COMMUNITY_LEVEL)
print(f"Report records: {len(report_df)}")
report_df.head()
Report records: 10
Out[7]:
| id | human_readable_id | community | level | parent | children | title | summary | full_content | rank | rating_explanation | findings | full_content_json | period | size | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 9785e64b429dacd70cce8238051f3438b387ebcd6920ef... | 7 | 7 | 1 | 0 | [] | Paranormal Military Squad and Operation Dulce | This community centers on the Paranormal Milit... | # Paranormal Military Squad and Operation Dulc... | 8.5 | The impact severity rating is high due to the ... | [{'explanation': 'The Paranormal Military Squa... | {\n "title": "Paranormal Military Squad and... | 2026-01-13 | 4 |
| 1 | 212c8ff1796bff731abe3d54f3216511ab15d5fb0d97eb... | 8 | 8 | 1 | 0 | [] | Paranormal Military Squad: Leadership and Scie... | This community centers on the Paranormal Milit... | # Paranormal Military Squad: Leadership and Sc... | 8.0 | The community poses a high impact due to its i... | [{'explanation': 'Taylor Cruz, often referred ... | {\n "title": "Paranormal Military Squad: Le... | 2026-01-13 | 3 |
| 2 | dbff99eaacade8ac4337a9bde0f533313445f0643ab3d0... | 9 | 9 | 1 | 0 | [] | Paranormal Military Squad and Operation: Dulce... | This community centers around the Paranormal M... | # Paranormal Military Squad and Operation: Dul... | 8.5 | The community poses a high impact severity due... | [{'explanation': 'The military complex is the ... | {\n "title": "Paranormal Military Squad and... | 2026-01-13 | 3 |
| 3 | b6e8ab1ac8ecc605bbec09ad78819b95ac08d62c5a9991... | 0 | 0 | 0 | -1 | [7, 8, 9] | Paranormal Military Squad and Operation: Dulce | This community centers on the Paranormal Milit... | # Paranormal Military Squad and Operation: Dul... | 8.5 | The community poses a high impact due to its e... | [{'explanation': 'Taylor Cruz is the central a... | {\n "title": "Paranormal Military Squad and... | 2026-01-13 | 10 |
| 4 | 97038bee58306d13b709b615fb2b816cae0c11273a9e73... | 1 | 1 | 0 | -1 | [] | Team of Agents Investigating Dulce Base | This community centers on a specialized team o... | # Team of Agents Investigating Dulce Base\n\nT... | 7.5 | The impact severity rating is high due to the ... | [{'explanation': 'The core of this community i... | {\n "title": "Team of Agents Investigating ... | 2026-01-13 | 5 |
Read text units¶
In [8]:
Copied!
text_unit_df = pd.read_parquet(f"{INPUT_DIR}/{TEXT_UNIT_TABLE}.parquet")
text_units = read_indexer_text_units(text_unit_df)
print(f"Text unit records: {len(text_unit_df)}")
text_unit_df.head()
text_unit_df = pd.read_parquet(f"{INPUT_DIR}/{TEXT_UNIT_TABLE}.parquet")
text_units = read_indexer_text_units(text_unit_df)
print(f"Text unit records: {len(text_unit_df)}")
text_unit_df.head()
Text unit records: 5
Out[8]:
| id | human_readable_id | text | n_tokens | document_id | entity_ids | relationship_ids | covariate_ids | |
|---|---|---|---|---|---|---|---|---|
| 0 | ff65e3e3764d360b35c55a83aaa35b0138af32551d0bc0... | 0 | # Operation: Dulce\n\n## Chapter 1\n\nThe thru... | 1200 | 6e81f882f89dd5596e1925dd3ae8a4f0a0edcb55b35a80... | [bbb2ad85-3d0b-4c26-b6f9-71ed76ee64bd, 66112f0... | [7c9039f8-43d7-4685-a317-f7bbbfac8e3a, f031084... | [4c6e019b-29c5-456c-951c-0cd3df308634, b2d2c91... |
| 1 | 56388c08c6e58a2aeaa235be38fe973bbdcbfb5ca72fd2... | 1 | Taylor offered a brief nod, perhaps too curt,... | 1200 | 6e81f882f89dd5596e1925dd3ae8a4f0a0edcb55b35a80... | [bbb2ad85-3d0b-4c26-b6f9-71ed76ee64bd, 66112f0... | [e15afdbb-6757-4f10-897d-54b9bb3dddc7, 319a7dc... | [9cdfde65-90c1-46a2-bfa2-9808ea4c821a, 83a704b... |
| 2 | 6012990fbcfad49511dce9a360dcc28d0c9bd37dc7b337... | 2 | team was descending into the earth, carrying ... | 1200 | 6e81f882f89dd5596e1925dd3ae8a4f0a0edcb55b35a80... | [bbb2ad85-3d0b-4c26-b6f9-71ed76ee64bd, 66112f0... | [4f42f1c3-b769-48f6-a01d-6a4a89b1c084, 319a7dc... | [d3d99801-1d52-4398-9e36-3dd3d8a06a56, 1a2f60a... |
| 3 | 402ae07f78c0154e8c24a93c00a90ee7c85e7478a755da... | 3 | Cruz squared their shoulders, clearly ruffled ... | 1200 | 6e81f882f89dd5596e1925dd3ae8a4f0a0edcb55b35a80... | [bbb2ad85-3d0b-4c26-b6f9-71ed76ee64bd, e1274d9... | [7c9039f8-43d7-4685-a317-f7bbbfac8e3a, 84441d6... | [b39dd61f-5a5a-4894-9047-23bee3e03f73, 23029bd... |
| 4 | 999a787eccebd488865de53a048f60889847f42057d5ad... | 4 | to him, and he couldn't help but feel the wei... | 382 | 6e81f882f89dd5596e1925dd3ae8a4f0a0edcb55b35a80... | [bbb2ad85-3d0b-4c26-b6f9-71ed76ee64bd, 66112f0... | [d3ea1355-14f3-494e-94cf-d43ac1077ab4, 72e568c... | [7c71874a-1443-40a6-bbad-6bc78ef23ee4, dac744e... |
In [9]:
Copied!
from graphrag.tokenizer.get_tokenizer import get_tokenizer
from graphrag_llm.completion import create_completion
from graphrag_llm.config import ModelConfig
from graphrag_llm.embedding import create_embedding
api_key = os.environ["GRAPHRAG_API_KEY"]
chat_config = ModelConfig(
type="litellm",
model_provider="openai",
model="gpt-4.1",
api_key=api_key,
)
chat_model = create_completion(chat_config)
embedding_config = ModelConfig(
type="litellm",
model_provider="openai",
model="text-embedding-3-small",
api_key=api_key,
)
text_embedder = create_embedding(embedding_config)
tokenizer = get_tokenizer(chat_config)
from graphrag.tokenizer.get_tokenizer import get_tokenizer
from graphrag_llm.completion import create_completion
from graphrag_llm.config import ModelConfig
from graphrag_llm.embedding import create_embedding
api_key = os.environ["GRAPHRAG_API_KEY"]
chat_config = ModelConfig(
type="litellm",
model_provider="openai",
model="gpt-4.1",
api_key=api_key,
)
chat_model = create_completion(chat_config)
embedding_config = ModelConfig(
type="litellm",
model_provider="openai",
model="text-embedding-3-small",
api_key=api_key,
)
text_embedder = create_embedding(embedding_config)
tokenizer = get_tokenizer(chat_config)
Create local search context builder¶
In [10]:
Copied!
context_builder = LocalSearchMixedContext(
community_reports=reports,
text_units=text_units,
entities=entities,
relationships=relationships,
# if you did not run covariates during indexing, set this to None
covariates=covariates,
entity_text_embeddings=description_embedding_store,
embedding_vectorstore_key=EntityVectorStoreKey.ID, # if the vectorstore uses entity title as ids, set this to EntityVectorStoreKey.TITLE
text_embedder=text_embedder,
tokenizer=tokenizer,
)
context_builder = LocalSearchMixedContext(
community_reports=reports,
text_units=text_units,
entities=entities,
relationships=relationships,
# if you did not run covariates during indexing, set this to None
covariates=covariates,
entity_text_embeddings=description_embedding_store,
embedding_vectorstore_key=EntityVectorStoreKey.ID, # if the vectorstore uses entity title as ids, set this to EntityVectorStoreKey.TITLE
text_embedder=text_embedder,
tokenizer=tokenizer,
)
Create local search engine¶
In [11]:
Copied!
# text_unit_prop: proportion of context window dedicated to related text units
# community_prop: proportion of context window dedicated to community reports.
# The remaining proportion is dedicated to entities and relationships. Sum of text_unit_prop and community_prop should be <= 1
# conversation_history_max_turns: maximum number of turns to include in the conversation history.
# conversation_history_user_turns_only: if True, only include user queries in the conversation history.
# top_k_mapped_entities: number of related entities to retrieve from the entity description embedding store.
# top_k_relationships: control the number of out-of-network relationships to pull into the context window.
# include_entity_rank: if True, include the entity rank in the entity table in the context window. Default entity rank = node degree.
# include_relationship_weight: if True, include the relationship weight in the context window.
# include_community_rank: if True, include the community rank in the context window.
# return_candidate_context: if True, return a set of dataframes containing all candidate entity/relationship/covariate records that
# could be relevant. Note that not all of these records will be included in the context window. The "in_context" column in these
# dataframes indicates whether the record is included in the context window.
# max_tokens: maximum number of tokens to use for the context window.
local_context_params = {
"text_unit_prop": 0.5,
"community_prop": 0.1,
"conversation_history_max_turns": 5,
"conversation_history_user_turns_only": True,
"top_k_mapped_entities": 10,
"top_k_relationships": 10,
"include_entity_rank": True,
"include_relationship_weight": True,
"include_community_rank": False,
"return_candidate_context": False,
"embedding_vectorstore_key": EntityVectorStoreKey.ID, # set this to EntityVectorStoreKey.TITLE if the vectorstore uses entity title as ids
"max_tokens": 12_000, # change this based on the token limit you have on your model (if you are using a model with 8k limit, a good setting could be 5000)
}
model_params = {
"max_tokens": 2_000, # change this based on the token limit you have on your model (if you are using a model with 8k limit, a good setting could be 1000=1500)
"temperature": 0.0,
}
# text_unit_prop: proportion of context window dedicated to related text units
# community_prop: proportion of context window dedicated to community reports.
# The remaining proportion is dedicated to entities and relationships. Sum of text_unit_prop and community_prop should be <= 1
# conversation_history_max_turns: maximum number of turns to include in the conversation history.
# conversation_history_user_turns_only: if True, only include user queries in the conversation history.
# top_k_mapped_entities: number of related entities to retrieve from the entity description embedding store.
# top_k_relationships: control the number of out-of-network relationships to pull into the context window.
# include_entity_rank: if True, include the entity rank in the entity table in the context window. Default entity rank = node degree.
# include_relationship_weight: if True, include the relationship weight in the context window.
# include_community_rank: if True, include the community rank in the context window.
# return_candidate_context: if True, return a set of dataframes containing all candidate entity/relationship/covariate records that
# could be relevant. Note that not all of these records will be included in the context window. The "in_context" column in these
# dataframes indicates whether the record is included in the context window.
# max_tokens: maximum number of tokens to use for the context window.
local_context_params = {
"text_unit_prop": 0.5,
"community_prop": 0.1,
"conversation_history_max_turns": 5,
"conversation_history_user_turns_only": True,
"top_k_mapped_entities": 10,
"top_k_relationships": 10,
"include_entity_rank": True,
"include_relationship_weight": True,
"include_community_rank": False,
"return_candidate_context": False,
"embedding_vectorstore_key": EntityVectorStoreKey.ID, # set this to EntityVectorStoreKey.TITLE if the vectorstore uses entity title as ids
"max_tokens": 12_000, # change this based on the token limit you have on your model (if you are using a model with 8k limit, a good setting could be 5000)
}
model_params = {
"max_tokens": 2_000, # change this based on the token limit you have on your model (if you are using a model with 8k limit, a good setting could be 1000=1500)
"temperature": 0.0,
}
In [12]:
Copied!
search_engine = LocalSearch(
model=chat_model,
context_builder=context_builder,
tokenizer=tokenizer,
model_params=model_params,
context_builder_params=local_context_params,
response_type="multiple paragraphs", # free form text describing the response type and format, can be anything, e.g. prioritized list, single paragraph, multiple paragraphs, multiple-page report
)
search_engine = LocalSearch(
model=chat_model,
context_builder=context_builder,
tokenizer=tokenizer,
model_params=model_params,
context_builder_params=local_context_params,
response_type="multiple paragraphs", # free form text describing the response type and format, can be anything, e.g. prioritized list, single paragraph, multiple paragraphs, multiple-page report
)
Run local search on sample queries¶
In [13]:
Copied!
result = await search_engine.search("Tell me about Agent Mercer")
print(result.response)
result = await search_engine.search("Tell me about Agent Mercer")
print(result.response)
Request failed with exception=litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
Traceback (most recent call last):
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py", line 1483, in embedding
raw_response: Final = self.make_sync_openai_embedding_request(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
openai_client=openai_client,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<2 lines>...
logging_obj=logging_obj,
^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/logging_utils.py", line 419, in sync_wrapper
result: Final = func(*args, **kwargs)
~~~~^^^^^^^^^^^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py", line 1343, in make_sync_openai_embedding_request
bypass_response: Final = openai_client.post(
~~~~~~~~~~~~~~~~~~^
"/embeddings", body=body, options=options, cast_to=CreateEmbeddingResponse
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1474, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1248, in request
raise self._make_status_error_from_response(err.response) from None
openai.AuthenticationError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py", line 7167, in embedding
response = openai_chat_completions.embedding(
model=model,
...<11 lines>...
litellm_params=litellm_params_dict,
)
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py", line 1516, in embedding
raise OpenAIError(
...<4 lines>...
)
litellm.llms.openai.common_utils.OpenAIError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_logging.py", line 49, in _request_count_middleware
return sync_middleware(**kwargs)
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_request_count.py", line 53, in _request_count_middleware
result = sync_middleware(**kwargs)
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_metrics.py", line 56, in _metrics_middleware
response = sync_middleware(**kwargs)
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/embedding/lite_llm_embedding.py", line 193, in _base_embedding
response = cast("litellm.EmbeddingResponse", litellm.embedding(**new_args))
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/embeddings/dispatch.py", line 77, in embedding
return _DISPATCH.run(
~~~~~~~~~~~~~^
args,
^^^^^
...<3 lines>...
native=native_call_hook,
^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/rust_bridge/dispatch.py", line 72, in run
return python(*args, **kwargs)
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py", line 2296, in wrapper
raise e
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py", line 2147, in wrapper
result = original_function(*args, **kwargs)
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py", line 7996, in embedding
raise exception_type(
~~~~~~~~~~~~~~^
model=model,
^^^^^^^^^^^^
...<2 lines>...
extra_kwargs=kwargs,
^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2915, in exception_type
raise e # it's already mapped
^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2660, in exception_type
_map_openai_exception(
~~~~~~~~~~~~~~~~~~~~~^
model=model,
^^^^^^^^^^^^
...<5 lines>...
extra_information=extra_information,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 510, in _map_openai_exception
raise AuthenticationError(
...<5 lines>...
)
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
--------------------------------------------------------------------------- AuthenticationError Traceback (most recent call last) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py:1483, in OpenAIChatCompletion.embedding(self, model, input, timeout, logging_obj, model_response, optional_params, api_key, api_base, client, aembedding, max_retries, shared_session, litellm_params) 1482 ## embedding CALL -> 1483 raw_response: Final = self.make_sync_openai_embedding_request( 1484 openai_client=openai_client, 1485 data=data, 1486 timeout=timeout, 1487 logging_obj=logging_obj, 1488 ) 1489 headers: Final = dict(raw_response.headers) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/logging_utils.py:419, in track_llm_api_timing.<locals>.decorator.<locals>.sync_wrapper(*args, **kwargs) 418 try: --> 419 result: Final = func(*args, **kwargs) 420 return result File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py:1343, in OpenAIChatCompletion.make_sync_openai_embedding_request(self, openai_client, data, timeout, logging_obj) 1342 body, options = _request_without_sdk_transform(data, timeout) -> 1343 bypass_response: Final = openai_client.post( 1344 "/embeddings", body=body, options=options, cast_to=CreateEmbeddingResponse 1345 ) 1346 assert isinstance(bypass_response, LegacyAPIResponse) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py:1474, in SyncAPIClient.post(self, path, cast_to, body, content, options, files, stream, stream_cls) 1471 opts = FinalRequestOptions.construct( 1472 method="post", url=path, json_data=body, content=content, files=to_httpx_files(files), **options 1473 ) -> 1474 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py:1248, in SyncAPIClient.request(self, cast_to, options, stream, stream_cls) 1247 log.debug("Re-raising status error") -> 1248 raise self._make_status_error_from_response(err.response) from None 1250 break AuthenticationError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys. During handling of the above exception, another exception occurred: OpenAIError Traceback (most recent call last) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py:7167, in embedding(model, input, dimensions, encoding_format, timeout, api_base, api_version, api_key, api_type, caching, user, custom_llm_provider, litellm_call_id, logger_fn, **kwargs) 7166 ## EMBEDDING CALL -> 7167 response = openai_chat_completions.embedding( 7168 model=model, 7169 input=input, 7170 api_base=api_base, 7171 api_key=api_key, 7172 logging_obj=logging, 7173 timeout=timeout, 7174 model_response=EmbeddingResponse(), 7175 optional_params=optional_params, 7176 client=client, 7177 aembedding=aembedding, 7178 max_retries=max_retries, 7179 shared_session=shared_session, 7180 litellm_params=litellm_params_dict, 7181 ) 7182 elif custom_llm_provider == "databricks": File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py:1516, in OpenAIChatCompletion.embedding(self, model, input, timeout, logging_obj, model_response, optional_params, api_key, api_base, client, aembedding, max_retries, shared_session, litellm_params) 1515 error_headers = getattr(error_response, "headers", None) -> 1516 raise OpenAIError( 1517 status_code=status_code, 1518 message=error_text, 1519 response=upstream_error_response(e), 1520 headers=error_headers, 1521 ) OpenAIError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys. During handling of the above exception, another exception occurred: AuthenticationError Traceback (most recent call last) Cell In[13], line 1 ----> 1 result = await search_engine.search("Tell me about Agent Mercer") 2 print(result.response) File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/structured_search/local_search/search.py:66, in LocalSearch.search(self, query, conversation_history, **kwargs) 64 search_prompt = "" 65 llm_calls, prompt_tokens, output_tokens = {}, {}, {} ---> 66 context_result = self.context_builder.build_context( 67 query=query, 68 conversation_history=conversation_history, 69 **kwargs, 70 **self.context_builder_params, 71 ) 72 llm_calls["build_context"] = context_result.llm_calls 73 prompt_tokens["build_context"] = context_result.prompt_tokens File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/structured_search/local_search/mixed_context.py:139, in LocalSearchMixedContext.build_context(self, query, conversation_history, include_entity_names, exclude_entity_names, conversation_history_max_turns, conversation_history_user_turns_only, max_context_tokens, text_unit_prop, community_prop, top_k_mapped_entities, top_k_relationships, include_community_rank, include_entity_rank, rank_description, include_relationship_weight, relationship_ranking_attribute, return_candidate_context, use_community_summary, min_community_rank, community_context_name, column_delimiter, **kwargs) 134 pre_user_questions = "\n".join( 135 conversation_history.get_user_turns(conversation_history_max_turns) 136 ) 137 query = f"{query}\n{pre_user_questions}" --> 139 selected_entities = map_query_to_entities( 140 query=query, 141 text_embedding_vectorstore=self.entity_text_embeddings, 142 text_embedder=self.text_embedder, 143 all_entities_dict=self.entities, 144 embedding_vectorstore_key=self.embedding_vectorstore_key, 145 include_entity_names=include_entity_names, 146 exclude_entity_names=exclude_entity_names, 147 k=top_k_mapped_entities, 148 oversample_scaler=2, 149 ) 151 # build context 152 final_context = list[str]() File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/context_builder/entity_extraction.py:62, in map_query_to_entities(query, text_embedding_vectorstore, text_embedder, all_entities_dict, embedding_vectorstore_key, include_entity_names, exclude_entity_names, k, oversample_scaler) 58 matched_entities = [] 59 if query != "": 60 # get entities with highest semantic similarity to query 61 # oversample to account for excluded entities ---> 62 search_results = text_embedding_vectorstore.similarity_search_by_text( 63 text=query, 64 text_embedder=lambda t: text_embedder.embedding(input=[t]).first_embedding, 65 k=k * oversample_scaler, 66 ) 67 for result in search_results: 68 if embedding_vectorstore_key == EntityVectorStoreKey.ID and isinstance( 69 result.document.id, str 70 ): File ~/work/graphrag/graphrag/packages/graphrag-vectors/graphrag_vectors/vector_store.py:186, in VectorStore.similarity_search_by_text(self, text, text_embedder, k, select, filters, include_vectors) 176 def similarity_search_by_text( 177 self, 178 text: str, (...) 183 include_vectors: bool = True, 184 ) -> list[VectorStoreSearchResult]: 185 """Perform a text-based similarity search.""" --> 186 query_embedding = text_embedder(text) 187 if query_embedding: 188 return self.similarity_search_by_vector( 189 query_embedding=query_embedding, 190 k=k, (...) 193 include_vectors=include_vectors, 194 ) File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/context_builder/entity_extraction.py:64, in map_query_to_entities.<locals>.<lambda>(t) 58 matched_entities = [] 59 if query != "": 60 # get entities with highest semantic similarity to query 61 # oversample to account for excluded entities 62 search_results = text_embedding_vectorstore.similarity_search_by_text( 63 text=query, ---> 64 text_embedder=lambda t: text_embedder.embedding(input=[t]).first_embedding, 65 k=k * oversample_scaler, 66 ) 67 for result in search_results: 68 if embedding_vectorstore_key == EntityVectorStoreKey.ID and isinstance( 69 result.document.id, str 70 ): File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/embedding/lite_llm_embedding.py:130, in LiteLLMEmbedding.embedding(self, **kwargs) 127 request_metrics = None 129 try: --> 130 return self._embedding(metrics=request_metrics, **kwargs) 131 finally: 132 if request_metrics: File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_logging.py:49, in with_logging.<locals>._request_count_middleware(**kwargs) 47 metrics: Metrics | None = kwargs.get("metrics") 48 try: ---> 49 return sync_middleware(**kwargs) 50 except Exception as e: 51 retries = metrics.get("retries", None) if metrics else None File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_request_count.py:53, in with_request_count.<locals>._request_count_middleware(**kwargs) 51 metrics["failed_response_count"] = 0 52 try: ---> 53 result = sync_middleware(**kwargs) 54 if metrics is not None: 55 metrics["successful_response_count"] = 1 File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_metrics.py:56, in with_metrics.<locals>._metrics_middleware(**kwargs) 54 metrics: Metrics | None = kwargs.get("metrics") 55 start_time = time.time() ---> 56 response = sync_middleware(**kwargs) 57 end_time = time.time() 59 if metrics is not None: File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/embedding/lite_llm_embedding.py:193, in _create_base_embeddings.<locals>._base_embedding(**kwargs) 190 new_args: dict[str, Any] = {**base_args, **kwargs} 191 model_config.validate_provider_auth_args(new_args) --> 193 response = cast("litellm.EmbeddingResponse", litellm.embedding(**new_args)) 194 return _convert_embedding_response(response, new_args["model"]) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/embeddings/dispatch.py:77, in embedding(*args, **kwargs) 75 if _FLAG.requested(kwargs): 76 return aembedding(**_FLAG.keywords(args, kwargs)) ---> 77 return _DISPATCH.run( 78 args, 79 _FLAG.dropped(kwargs), 80 python=_PYTHON_EMBEDDING, 81 binding=NATIVE_EMBEDDING, 82 native=native_call_hook, 83 ) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/rust_bridge/dispatch.py:72, in PublicDispatch.run(self, args, kwargs, python, binding, native, rules) 64 return runtime.run( 65 self.context(native_request), 66 binding=binding, (...) 69 rules=selected_rules, 70 ) 71 if not catalog.rust_might_handle(self.route, selected_rules): ---> 72 return python(*args, **kwargs) 73 request: Final = self.request(args, kwargs) 74 if request is None: File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py:2296, in client.<locals>.wrapper(*args, **kwargs) 2292 if logging_obj: 2293 logging_obj.failure_handler( 2294 e, traceback_exception, start_time, end_time 2295 ) # DO NOT MAKE THREADED - router retry fallback relies on this! -> 2296 raise e File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py:2296, in client.<locals>.wrapper(*args, **kwargs) 2292 if logging_obj: 2293 logging_obj.failure_handler( 2294 e, traceback_exception, start_time, end_time 2295 ) # DO NOT MAKE THREADED - router retry fallback relies on this! -> 2296 raise e File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py:7996, in embedding(model, input, dimensions, encoding_format, timeout, api_base, api_version, api_key, api_type, caching, user, custom_llm_provider, litellm_call_id, logger_fn, **kwargs) 7990 litellm_logging_obj.post_call( 7991 input=input, 7992 api_key=api_key, 7993 original_response=str(e), 7994 ) 7995 ## Map to OpenAI Exception -> 7996 raise exception_type( 7997 model=model, 7998 original_exception=e, 7999 custom_llm_provider=custom_llm_provider, 8000 extra_kwargs=kwargs, 8001 ) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py:2915, in exception_type(model, original_exception, custom_llm_provider, completion_kwargs, extra_kwargs) 2913 if isinstance(e, error_type): 2914 setattr(e, "litellm_response_headers", litellm_response_headers) -> 2915 raise e # it's already mapped 2916 raised_exc: Final = APIConnectionError( 2917 message=f"{original_exception}\n{redact_string(traceback.format_exc())}", 2918 llm_provider="", 2919 model="", 2920 ) 2921 setattr(raised_exc, "litellm_response_headers", litellm_response_headers) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py:2660, in exception_type(model, original_exception, custom_llm_provider, completion_kwargs, extra_kwargs) 2645 extract_and_raise_litellm_exception( 2646 response=_without_credentials(_litellm_proxy_response(mappable_exception, custom_llm_provider)), 2647 error_str=error_str, (...) 2650 body=getattr(mappable_exception, "body", None), 2651 ) 2652 if ( 2653 custom_llm_provider == "openai" 2654 or custom_llm_provider == "text-completion-openai" (...) 2658 or custom_llm_provider == "runwayml" 2659 ): -> 2660 _map_openai_exception( 2661 model=model, 2662 original_exception=mappable_exception, 2663 custom_llm_provider=custom_llm_provider, 2664 error_str=error_str, 2665 exception_type=exception_type, 2666 exception_provider=exception_provider, 2667 extra_information=extra_information, 2668 ) 2669 elif custom_llm_provider == "anthropic" or custom_llm_provider == "anthropic_text": # one of the anthropics 2670 _map_anthropic_exception( 2671 model=model, 2672 original_exception=mappable_exception, (...) 2677 extra_information=extra_information, 2678 ) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py:510, in _map_openai_exception(model, original_exception, custom_llm_provider, error_str, exception_type, exception_provider, extra_information) 501 raise BadRequestError( 502 message=f"{exception_provider} - {message}", 503 llm_provider=custom_llm_provider, (...) 507 body=getattr(original_exception, "body", None), 508 ) 509 elif original_exception.status_code == 401: --> 510 raise AuthenticationError( 511 message=f"AuthenticationError: {exception_provider} - {message}", 512 llm_provider=custom_llm_provider, 513 model=model, 514 response=response, 515 litellm_debug_info=extra_information, 516 ) 517 elif original_exception.status_code == 404: 518 raise NotFoundError( 519 message=f"NotFoundError: {exception_provider} - {message}", 520 model=model, (...) 523 litellm_debug_info=extra_information, 524 ) AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
In [14]:
Copied!
question = "Tell me about Dr. Jordan Hayes"
result = await search_engine.search(question)
print(result.response)
question = "Tell me about Dr. Jordan Hayes"
result = await search_engine.search(question)
print(result.response)
Request failed with exception=litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
Traceback (most recent call last):
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py", line 1483, in embedding
raw_response: Final = self.make_sync_openai_embedding_request(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
openai_client=openai_client,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<2 lines>...
logging_obj=logging_obj,
^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/logging_utils.py", line 419, in sync_wrapper
result: Final = func(*args, **kwargs)
~~~~^^^^^^^^^^^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py", line 1343, in make_sync_openai_embedding_request
bypass_response: Final = openai_client.post(
~~~~~~~~~~~~~~~~~~^
"/embeddings", body=body, options=options, cast_to=CreateEmbeddingResponse
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1474, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1248, in request
raise self._make_status_error_from_response(err.response) from None
openai.AuthenticationError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py", line 7167, in embedding
response = openai_chat_completions.embedding(
model=model,
...<11 lines>...
litellm_params=litellm_params_dict,
)
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py", line 1516, in embedding
raise OpenAIError(
...<4 lines>...
)
litellm.llms.openai.common_utils.OpenAIError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_logging.py", line 49, in _request_count_middleware
return sync_middleware(**kwargs)
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_request_count.py", line 53, in _request_count_middleware
result = sync_middleware(**kwargs)
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_metrics.py", line 56, in _metrics_middleware
response = sync_middleware(**kwargs)
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/embedding/lite_llm_embedding.py", line 193, in _base_embedding
response = cast("litellm.EmbeddingResponse", litellm.embedding(**new_args))
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/embeddings/dispatch.py", line 77, in embedding
return _DISPATCH.run(
~~~~~~~~~~~~~^
args,
^^^^^
...<3 lines>...
native=native_call_hook,
^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/rust_bridge/dispatch.py", line 72, in run
return python(*args, **kwargs)
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py", line 2296, in wrapper
raise e
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py", line 2147, in wrapper
result = original_function(*args, **kwargs)
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py", line 7996, in embedding
raise exception_type(
~~~~~~~~~~~~~~^
model=model,
^^^^^^^^^^^^
...<2 lines>...
extra_kwargs=kwargs,
^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2915, in exception_type
raise e # it's already mapped
^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2660, in exception_type
_map_openai_exception(
~~~~~~~~~~~~~~~~~~~~~^
model=model,
^^^^^^^^^^^^
...<5 lines>...
extra_information=extra_information,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 510, in _map_openai_exception
raise AuthenticationError(
...<5 lines>...
)
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
--------------------------------------------------------------------------- AuthenticationError Traceback (most recent call last) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py:1483, in OpenAIChatCompletion.embedding(self, model, input, timeout, logging_obj, model_response, optional_params, api_key, api_base, client, aembedding, max_retries, shared_session, litellm_params) 1482 ## embedding CALL -> 1483 raw_response: Final = self.make_sync_openai_embedding_request( 1484 openai_client=openai_client, 1485 data=data, 1486 timeout=timeout, 1487 logging_obj=logging_obj, 1488 ) 1489 headers: Final = dict(raw_response.headers) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/logging_utils.py:419, in track_llm_api_timing.<locals>.decorator.<locals>.sync_wrapper(*args, **kwargs) 418 try: --> 419 result: Final = func(*args, **kwargs) 420 return result File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py:1343, in OpenAIChatCompletion.make_sync_openai_embedding_request(self, openai_client, data, timeout, logging_obj) 1342 body, options = _request_without_sdk_transform(data, timeout) -> 1343 bypass_response: Final = openai_client.post( 1344 "/embeddings", body=body, options=options, cast_to=CreateEmbeddingResponse 1345 ) 1346 assert isinstance(bypass_response, LegacyAPIResponse) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py:1474, in SyncAPIClient.post(self, path, cast_to, body, content, options, files, stream, stream_cls) 1471 opts = FinalRequestOptions.construct( 1472 method="post", url=path, json_data=body, content=content, files=to_httpx_files(files), **options 1473 ) -> 1474 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py:1248, in SyncAPIClient.request(self, cast_to, options, stream, stream_cls) 1247 log.debug("Re-raising status error") -> 1248 raise self._make_status_error_from_response(err.response) from None 1250 break AuthenticationError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys. During handling of the above exception, another exception occurred: OpenAIError Traceback (most recent call last) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py:7167, in embedding(model, input, dimensions, encoding_format, timeout, api_base, api_version, api_key, api_type, caching, user, custom_llm_provider, litellm_call_id, logger_fn, **kwargs) 7166 ## EMBEDDING CALL -> 7167 response = openai_chat_completions.embedding( 7168 model=model, 7169 input=input, 7170 api_base=api_base, 7171 api_key=api_key, 7172 logging_obj=logging, 7173 timeout=timeout, 7174 model_response=EmbeddingResponse(), 7175 optional_params=optional_params, 7176 client=client, 7177 aembedding=aembedding, 7178 max_retries=max_retries, 7179 shared_session=shared_session, 7180 litellm_params=litellm_params_dict, 7181 ) 7182 elif custom_llm_provider == "databricks": File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py:1516, in OpenAIChatCompletion.embedding(self, model, input, timeout, logging_obj, model_response, optional_params, api_key, api_base, client, aembedding, max_retries, shared_session, litellm_params) 1515 error_headers = getattr(error_response, "headers", None) -> 1516 raise OpenAIError( 1517 status_code=status_code, 1518 message=error_text, 1519 response=upstream_error_response(e), 1520 headers=error_headers, 1521 ) OpenAIError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys. During handling of the above exception, another exception occurred: AuthenticationError Traceback (most recent call last) Cell In[14], line 2 1 question = "Tell me about Dr. Jordan Hayes" ----> 2 result = await search_engine.search(question) 3 print(result.response) File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/structured_search/local_search/search.py:66, in LocalSearch.search(self, query, conversation_history, **kwargs) 64 search_prompt = "" 65 llm_calls, prompt_tokens, output_tokens = {}, {}, {} ---> 66 context_result = self.context_builder.build_context( 67 query=query, 68 conversation_history=conversation_history, 69 **kwargs, 70 **self.context_builder_params, 71 ) 72 llm_calls["build_context"] = context_result.llm_calls 73 prompt_tokens["build_context"] = context_result.prompt_tokens File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/structured_search/local_search/mixed_context.py:139, in LocalSearchMixedContext.build_context(self, query, conversation_history, include_entity_names, exclude_entity_names, conversation_history_max_turns, conversation_history_user_turns_only, max_context_tokens, text_unit_prop, community_prop, top_k_mapped_entities, top_k_relationships, include_community_rank, include_entity_rank, rank_description, include_relationship_weight, relationship_ranking_attribute, return_candidate_context, use_community_summary, min_community_rank, community_context_name, column_delimiter, **kwargs) 134 pre_user_questions = "\n".join( 135 conversation_history.get_user_turns(conversation_history_max_turns) 136 ) 137 query = f"{query}\n{pre_user_questions}" --> 139 selected_entities = map_query_to_entities( 140 query=query, 141 text_embedding_vectorstore=self.entity_text_embeddings, 142 text_embedder=self.text_embedder, 143 all_entities_dict=self.entities, 144 embedding_vectorstore_key=self.embedding_vectorstore_key, 145 include_entity_names=include_entity_names, 146 exclude_entity_names=exclude_entity_names, 147 k=top_k_mapped_entities, 148 oversample_scaler=2, 149 ) 151 # build context 152 final_context = list[str]() File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/context_builder/entity_extraction.py:62, in map_query_to_entities(query, text_embedding_vectorstore, text_embedder, all_entities_dict, embedding_vectorstore_key, include_entity_names, exclude_entity_names, k, oversample_scaler) 58 matched_entities = [] 59 if query != "": 60 # get entities with highest semantic similarity to query 61 # oversample to account for excluded entities ---> 62 search_results = text_embedding_vectorstore.similarity_search_by_text( 63 text=query, 64 text_embedder=lambda t: text_embedder.embedding(input=[t]).first_embedding, 65 k=k * oversample_scaler, 66 ) 67 for result in search_results: 68 if embedding_vectorstore_key == EntityVectorStoreKey.ID and isinstance( 69 result.document.id, str 70 ): File ~/work/graphrag/graphrag/packages/graphrag-vectors/graphrag_vectors/vector_store.py:186, in VectorStore.similarity_search_by_text(self, text, text_embedder, k, select, filters, include_vectors) 176 def similarity_search_by_text( 177 self, 178 text: str, (...) 183 include_vectors: bool = True, 184 ) -> list[VectorStoreSearchResult]: 185 """Perform a text-based similarity search.""" --> 186 query_embedding = text_embedder(text) 187 if query_embedding: 188 return self.similarity_search_by_vector( 189 query_embedding=query_embedding, 190 k=k, (...) 193 include_vectors=include_vectors, 194 ) File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/context_builder/entity_extraction.py:64, in map_query_to_entities.<locals>.<lambda>(t) 58 matched_entities = [] 59 if query != "": 60 # get entities with highest semantic similarity to query 61 # oversample to account for excluded entities 62 search_results = text_embedding_vectorstore.similarity_search_by_text( 63 text=query, ---> 64 text_embedder=lambda t: text_embedder.embedding(input=[t]).first_embedding, 65 k=k * oversample_scaler, 66 ) 67 for result in search_results: 68 if embedding_vectorstore_key == EntityVectorStoreKey.ID and isinstance( 69 result.document.id, str 70 ): File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/embedding/lite_llm_embedding.py:130, in LiteLLMEmbedding.embedding(self, **kwargs) 127 request_metrics = None 129 try: --> 130 return self._embedding(metrics=request_metrics, **kwargs) 131 finally: 132 if request_metrics: File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_logging.py:49, in with_logging.<locals>._request_count_middleware(**kwargs) 47 metrics: Metrics | None = kwargs.get("metrics") 48 try: ---> 49 return sync_middleware(**kwargs) 50 except Exception as e: 51 retries = metrics.get("retries", None) if metrics else None File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_request_count.py:53, in with_request_count.<locals>._request_count_middleware(**kwargs) 51 metrics["failed_response_count"] = 0 52 try: ---> 53 result = sync_middleware(**kwargs) 54 if metrics is not None: 55 metrics["successful_response_count"] = 1 File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_metrics.py:56, in with_metrics.<locals>._metrics_middleware(**kwargs) 54 metrics: Metrics | None = kwargs.get("metrics") 55 start_time = time.time() ---> 56 response = sync_middleware(**kwargs) 57 end_time = time.time() 59 if metrics is not None: File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/embedding/lite_llm_embedding.py:193, in _create_base_embeddings.<locals>._base_embedding(**kwargs) 190 new_args: dict[str, Any] = {**base_args, **kwargs} 191 model_config.validate_provider_auth_args(new_args) --> 193 response = cast("litellm.EmbeddingResponse", litellm.embedding(**new_args)) 194 return _convert_embedding_response(response, new_args["model"]) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/embeddings/dispatch.py:77, in embedding(*args, **kwargs) 75 if _FLAG.requested(kwargs): 76 return aembedding(**_FLAG.keywords(args, kwargs)) ---> 77 return _DISPATCH.run( 78 args, 79 _FLAG.dropped(kwargs), 80 python=_PYTHON_EMBEDDING, 81 binding=NATIVE_EMBEDDING, 82 native=native_call_hook, 83 ) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/rust_bridge/dispatch.py:72, in PublicDispatch.run(self, args, kwargs, python, binding, native, rules) 64 return runtime.run( 65 self.context(native_request), 66 binding=binding, (...) 69 rules=selected_rules, 70 ) 71 if not catalog.rust_might_handle(self.route, selected_rules): ---> 72 return python(*args, **kwargs) 73 request: Final = self.request(args, kwargs) 74 if request is None: File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py:2296, in client.<locals>.wrapper(*args, **kwargs) 2292 if logging_obj: 2293 logging_obj.failure_handler( 2294 e, traceback_exception, start_time, end_time 2295 ) # DO NOT MAKE THREADED - router retry fallback relies on this! -> 2296 raise e File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py:2296, in client.<locals>.wrapper(*args, **kwargs) 2292 if logging_obj: 2293 logging_obj.failure_handler( 2294 e, traceback_exception, start_time, end_time 2295 ) # DO NOT MAKE THREADED - router retry fallback relies on this! -> 2296 raise e File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py:7996, in embedding(model, input, dimensions, encoding_format, timeout, api_base, api_version, api_key, api_type, caching, user, custom_llm_provider, litellm_call_id, logger_fn, **kwargs) 7990 litellm_logging_obj.post_call( 7991 input=input, 7992 api_key=api_key, 7993 original_response=str(e), 7994 ) 7995 ## Map to OpenAI Exception -> 7996 raise exception_type( 7997 model=model, 7998 original_exception=e, 7999 custom_llm_provider=custom_llm_provider, 8000 extra_kwargs=kwargs, 8001 ) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py:2915, in exception_type(model, original_exception, custom_llm_provider, completion_kwargs, extra_kwargs) 2913 if isinstance(e, error_type): 2914 setattr(e, "litellm_response_headers", litellm_response_headers) -> 2915 raise e # it's already mapped 2916 raised_exc: Final = APIConnectionError( 2917 message=f"{original_exception}\n{redact_string(traceback.format_exc())}", 2918 llm_provider="", 2919 model="", 2920 ) 2921 setattr(raised_exc, "litellm_response_headers", litellm_response_headers) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py:2660, in exception_type(model, original_exception, custom_llm_provider, completion_kwargs, extra_kwargs) 2645 extract_and_raise_litellm_exception( 2646 response=_without_credentials(_litellm_proxy_response(mappable_exception, custom_llm_provider)), 2647 error_str=error_str, (...) 2650 body=getattr(mappable_exception, "body", None), 2651 ) 2652 if ( 2653 custom_llm_provider == "openai" 2654 or custom_llm_provider == "text-completion-openai" (...) 2658 or custom_llm_provider == "runwayml" 2659 ): -> 2660 _map_openai_exception( 2661 model=model, 2662 original_exception=mappable_exception, 2663 custom_llm_provider=custom_llm_provider, 2664 error_str=error_str, 2665 exception_type=exception_type, 2666 exception_provider=exception_provider, 2667 extra_information=extra_information, 2668 ) 2669 elif custom_llm_provider == "anthropic" or custom_llm_provider == "anthropic_text": # one of the anthropics 2670 _map_anthropic_exception( 2671 model=model, 2672 original_exception=mappable_exception, (...) 2677 extra_information=extra_information, 2678 ) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py:510, in _map_openai_exception(model, original_exception, custom_llm_provider, error_str, exception_type, exception_provider, extra_information) 501 raise BadRequestError( 502 message=f"{exception_provider} - {message}", 503 llm_provider=custom_llm_provider, (...) 507 body=getattr(original_exception, "body", None), 508 ) 509 elif original_exception.status_code == 401: --> 510 raise AuthenticationError( 511 message=f"AuthenticationError: {exception_provider} - {message}", 512 llm_provider=custom_llm_provider, 513 model=model, 514 response=response, 515 litellm_debug_info=extra_information, 516 ) 517 elif original_exception.status_code == 404: 518 raise NotFoundError( 519 message=f"NotFoundError: {exception_provider} - {message}", 520 model=model, (...) 523 litellm_debug_info=extra_information, 524 ) AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
Inspecting the context data used to generate the response¶
In [15]:
Copied!
result.context_data["entities"].head()
result.context_data["entities"].head()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[15], line 1 ----> 1 result.context_data["entities"].head() NameError: name 'result' is not defined
In [16]:
Copied!
result.context_data["relationships"].head()
result.context_data["relationships"].head()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[16], line 1 ----> 1 result.context_data["relationships"].head() NameError: name 'result' is not defined
In [17]:
Copied!
if "reports" in result.context_data:
result.context_data["reports"].head()
if "reports" in result.context_data:
result.context_data["reports"].head()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[17], line 1 ----> 1 if "reports" in result.context_data: 2 result.context_data["reports"].head() NameError: name 'result' is not defined
In [18]:
Copied!
result.context_data["sources"].head()
result.context_data["sources"].head()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[18], line 1 ----> 1 result.context_data["sources"].head() NameError: name 'result' is not defined
In [19]:
Copied!
if "claims" in result.context_data:
print(result.context_data["claims"].head())
if "claims" in result.context_data:
print(result.context_data["claims"].head())
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[19], line 1 ----> 1 if "claims" in result.context_data: 2 print(result.context_data["claims"].head()) NameError: name 'result' is not defined
Question Generation¶
This function takes a list of user queries and generates the next candidate questions.
In [20]:
Copied!
question_generator = LocalQuestionGen(
model=chat_model,
context_builder=context_builder,
tokenizer=tokenizer,
model_params=model_params,
context_builder_params=local_context_params,
)
question_generator = LocalQuestionGen(
model=chat_model,
context_builder=context_builder,
tokenizer=tokenizer,
model_params=model_params,
context_builder_params=local_context_params,
)
In [21]:
Copied!
question_history = [
"Tell me about Agent Mercer",
"What happens in Dulce military base?",
]
candidate_questions = await question_generator.agenerate(
question_history=question_history, context_data=None, question_count=5
)
print(candidate_questions.response)
question_history = [
"Tell me about Agent Mercer",
"What happens in Dulce military base?",
]
candidate_questions = await question_generator.agenerate(
question_history=question_history, context_data=None, question_count=5
)
print(candidate_questions.response)
Request failed with exception=litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
Traceback (most recent call last):
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py", line 1483, in embedding
raw_response: Final = self.make_sync_openai_embedding_request(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
openai_client=openai_client,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<2 lines>...
logging_obj=logging_obj,
^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/logging_utils.py", line 419, in sync_wrapper
result: Final = func(*args, **kwargs)
~~~~^^^^^^^^^^^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py", line 1343, in make_sync_openai_embedding_request
bypass_response: Final = openai_client.post(
~~~~~~~~~~~~~~~~~~^
"/embeddings", body=body, options=options, cast_to=CreateEmbeddingResponse
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1474, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1248, in request
raise self._make_status_error_from_response(err.response) from None
openai.AuthenticationError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py", line 7167, in embedding
response = openai_chat_completions.embedding(
model=model,
...<11 lines>...
litellm_params=litellm_params_dict,
)
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py", line 1516, in embedding
raise OpenAIError(
...<4 lines>...
)
litellm.llms.openai.common_utils.OpenAIError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_logging.py", line 49, in _request_count_middleware
return sync_middleware(**kwargs)
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_request_count.py", line 53, in _request_count_middleware
result = sync_middleware(**kwargs)
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_metrics.py", line 56, in _metrics_middleware
response = sync_middleware(**kwargs)
File "/home/runner/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/embedding/lite_llm_embedding.py", line 193, in _base_embedding
response = cast("litellm.EmbeddingResponse", litellm.embedding(**new_args))
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/embeddings/dispatch.py", line 77, in embedding
return _DISPATCH.run(
~~~~~~~~~~~~~^
args,
^^^^^
...<3 lines>...
native=native_call_hook,
^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/rust_bridge/dispatch.py", line 72, in run
return python(*args, **kwargs)
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py", line 2296, in wrapper
raise e
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py", line 2147, in wrapper
result = original_function(*args, **kwargs)
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py", line 7996, in embedding
raise exception_type(
~~~~~~~~~~~~~~^
model=model,
^^^^^^^^^^^^
...<2 lines>...
extra_kwargs=kwargs,
^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2915, in exception_type
raise e # it's already mapped
^^^^^^^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2660, in exception_type
_map_openai_exception(
~~~~~~~~~~~~~~~~~~~~~^
model=model,
^^^^^^^^^^^^
...<5 lines>...
extra_information=extra_information,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/runner/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 510, in _map_openai_exception
raise AuthenticationError(
...<5 lines>...
)
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.
--------------------------------------------------------------------------- AuthenticationError Traceback (most recent call last) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py:1483, in OpenAIChatCompletion.embedding(self, model, input, timeout, logging_obj, model_response, optional_params, api_key, api_base, client, aembedding, max_retries, shared_session, litellm_params) 1482 ## embedding CALL -> 1483 raw_response: Final = self.make_sync_openai_embedding_request( 1484 openai_client=openai_client, 1485 data=data, 1486 timeout=timeout, 1487 logging_obj=logging_obj, 1488 ) 1489 headers: Final = dict(raw_response.headers) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/logging_utils.py:419, in track_llm_api_timing.<locals>.decorator.<locals>.sync_wrapper(*args, **kwargs) 418 try: --> 419 result: Final = func(*args, **kwargs) 420 return result File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py:1343, in OpenAIChatCompletion.make_sync_openai_embedding_request(self, openai_client, data, timeout, logging_obj) 1342 body, options = _request_without_sdk_transform(data, timeout) -> 1343 bypass_response: Final = openai_client.post( 1344 "/embeddings", body=body, options=options, cast_to=CreateEmbeddingResponse 1345 ) 1346 assert isinstance(bypass_response, LegacyAPIResponse) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py:1474, in SyncAPIClient.post(self, path, cast_to, body, content, options, files, stream, stream_cls) 1471 opts = FinalRequestOptions.construct( 1472 method="post", url=path, json_data=body, content=content, files=to_httpx_files(files), **options 1473 ) -> 1474 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/openai/_base_client.py:1248, in SyncAPIClient.request(self, cast_to, options, stream, stream_cls) 1247 log.debug("Re-raising status error") -> 1248 raise self._make_status_error_from_response(err.response) from None 1250 break AuthenticationError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys. During handling of the above exception, another exception occurred: OpenAIError Traceback (most recent call last) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py:7167, in embedding(model, input, dimensions, encoding_format, timeout, api_base, api_version, api_key, api_type, caching, user, custom_llm_provider, litellm_call_id, logger_fn, **kwargs) 7166 ## EMBEDDING CALL -> 7167 response = openai_chat_completions.embedding( 7168 model=model, 7169 input=input, 7170 api_base=api_base, 7171 api_key=api_key, 7172 logging_obj=logging, 7173 timeout=timeout, 7174 model_response=EmbeddingResponse(), 7175 optional_params=optional_params, 7176 client=client, 7177 aembedding=aembedding, 7178 max_retries=max_retries, 7179 shared_session=shared_session, 7180 litellm_params=litellm_params_dict, 7181 ) 7182 elif custom_llm_provider == "databricks": File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/llms/openai/openai.py:1516, in OpenAIChatCompletion.embedding(self, model, input, timeout, logging_obj, model_response, optional_params, api_key, api_base, client, aembedding, max_retries, shared_session, litellm_params) 1515 error_headers = getattr(error_response, "headers", None) -> 1516 raise OpenAIError( 1517 status_code=status_code, 1518 message=error_text, 1519 response=upstream_error_response(e), 1520 headers=error_headers, 1521 ) OpenAIError: Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys. During handling of the above exception, another exception occurred: AuthenticationError Traceback (most recent call last) Cell In[21], line 5 1 question_history = [ 2 "Tell me about Agent Mercer", 3 "What happens in Dulce military base?", 4 ] ----> 5 candidate_questions = await question_generator.agenerate( 6 question_history=question_history, context_data=None, question_count=5 7 ) 8 print(candidate_questions.response) File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/question_gen/local_gen.py:85, in LocalQuestionGen.agenerate(self, question_history, context_data, question_count, **kwargs) 79 conversation_history = ConversationHistory.from_list(history) 81 if context_data is None: 82 # generate context data based on the question history 83 result = cast( 84 "ContextBuilderResult", ---> 85 self.context_builder.build_context( 86 query=question_text, 87 conversation_history=conversation_history, 88 **kwargs, 89 **self.context_builder_params, 90 ), 91 ) 92 context_data = cast("str", result.context_chunks) 93 context_records = result.context_records File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/structured_search/local_search/mixed_context.py:139, in LocalSearchMixedContext.build_context(self, query, conversation_history, include_entity_names, exclude_entity_names, conversation_history_max_turns, conversation_history_user_turns_only, max_context_tokens, text_unit_prop, community_prop, top_k_mapped_entities, top_k_relationships, include_community_rank, include_entity_rank, rank_description, include_relationship_weight, relationship_ranking_attribute, return_candidate_context, use_community_summary, min_community_rank, community_context_name, column_delimiter, **kwargs) 134 pre_user_questions = "\n".join( 135 conversation_history.get_user_turns(conversation_history_max_turns) 136 ) 137 query = f"{query}\n{pre_user_questions}" --> 139 selected_entities = map_query_to_entities( 140 query=query, 141 text_embedding_vectorstore=self.entity_text_embeddings, 142 text_embedder=self.text_embedder, 143 all_entities_dict=self.entities, 144 embedding_vectorstore_key=self.embedding_vectorstore_key, 145 include_entity_names=include_entity_names, 146 exclude_entity_names=exclude_entity_names, 147 k=top_k_mapped_entities, 148 oversample_scaler=2, 149 ) 151 # build context 152 final_context = list[str]() File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/context_builder/entity_extraction.py:62, in map_query_to_entities(query, text_embedding_vectorstore, text_embedder, all_entities_dict, embedding_vectorstore_key, include_entity_names, exclude_entity_names, k, oversample_scaler) 58 matched_entities = [] 59 if query != "": 60 # get entities with highest semantic similarity to query 61 # oversample to account for excluded entities ---> 62 search_results = text_embedding_vectorstore.similarity_search_by_text( 63 text=query, 64 text_embedder=lambda t: text_embedder.embedding(input=[t]).first_embedding, 65 k=k * oversample_scaler, 66 ) 67 for result in search_results: 68 if embedding_vectorstore_key == EntityVectorStoreKey.ID and isinstance( 69 result.document.id, str 70 ): File ~/work/graphrag/graphrag/packages/graphrag-vectors/graphrag_vectors/vector_store.py:186, in VectorStore.similarity_search_by_text(self, text, text_embedder, k, select, filters, include_vectors) 176 def similarity_search_by_text( 177 self, 178 text: str, (...) 183 include_vectors: bool = True, 184 ) -> list[VectorStoreSearchResult]: 185 """Perform a text-based similarity search.""" --> 186 query_embedding = text_embedder(text) 187 if query_embedding: 188 return self.similarity_search_by_vector( 189 query_embedding=query_embedding, 190 k=k, (...) 193 include_vectors=include_vectors, 194 ) File ~/work/graphrag/graphrag/packages/graphrag/graphrag/query/context_builder/entity_extraction.py:64, in map_query_to_entities.<locals>.<lambda>(t) 58 matched_entities = [] 59 if query != "": 60 # get entities with highest semantic similarity to query 61 # oversample to account for excluded entities 62 search_results = text_embedding_vectorstore.similarity_search_by_text( 63 text=query, ---> 64 text_embedder=lambda t: text_embedder.embedding(input=[t]).first_embedding, 65 k=k * oversample_scaler, 66 ) 67 for result in search_results: 68 if embedding_vectorstore_key == EntityVectorStoreKey.ID and isinstance( 69 result.document.id, str 70 ): File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/embedding/lite_llm_embedding.py:130, in LiteLLMEmbedding.embedding(self, **kwargs) 127 request_metrics = None 129 try: --> 130 return self._embedding(metrics=request_metrics, **kwargs) 131 finally: 132 if request_metrics: File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_logging.py:49, in with_logging.<locals>._request_count_middleware(**kwargs) 47 metrics: Metrics | None = kwargs.get("metrics") 48 try: ---> 49 return sync_middleware(**kwargs) 50 except Exception as e: 51 retries = metrics.get("retries", None) if metrics else None File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_request_count.py:53, in with_request_count.<locals>._request_count_middleware(**kwargs) 51 metrics["failed_response_count"] = 0 52 try: ---> 53 result = sync_middleware(**kwargs) 54 if metrics is not None: 55 metrics["successful_response_count"] = 1 File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/middleware/with_metrics.py:56, in with_metrics.<locals>._metrics_middleware(**kwargs) 54 metrics: Metrics | None = kwargs.get("metrics") 55 start_time = time.time() ---> 56 response = sync_middleware(**kwargs) 57 end_time = time.time() 59 if metrics is not None: File ~/work/graphrag/graphrag/packages/graphrag-llm/graphrag_llm/embedding/lite_llm_embedding.py:193, in _create_base_embeddings.<locals>._base_embedding(**kwargs) 190 new_args: dict[str, Any] = {**base_args, **kwargs} 191 model_config.validate_provider_auth_args(new_args) --> 193 response = cast("litellm.EmbeddingResponse", litellm.embedding(**new_args)) 194 return _convert_embedding_response(response, new_args["model"]) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/embeddings/dispatch.py:77, in embedding(*args, **kwargs) 75 if _FLAG.requested(kwargs): 76 return aembedding(**_FLAG.keywords(args, kwargs)) ---> 77 return _DISPATCH.run( 78 args, 79 _FLAG.dropped(kwargs), 80 python=_PYTHON_EMBEDDING, 81 binding=NATIVE_EMBEDDING, 82 native=native_call_hook, 83 ) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/rust_bridge/dispatch.py:72, in PublicDispatch.run(self, args, kwargs, python, binding, native, rules) 64 return runtime.run( 65 self.context(native_request), 66 binding=binding, (...) 69 rules=selected_rules, 70 ) 71 if not catalog.rust_might_handle(self.route, selected_rules): ---> 72 return python(*args, **kwargs) 73 request: Final = self.request(args, kwargs) 74 if request is None: File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py:2296, in client.<locals>.wrapper(*args, **kwargs) 2292 if logging_obj: 2293 logging_obj.failure_handler( 2294 e, traceback_exception, start_time, end_time 2295 ) # DO NOT MAKE THREADED - router retry fallback relies on this! -> 2296 raise e File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/utils.py:2296, in client.<locals>.wrapper(*args, **kwargs) 2292 if logging_obj: 2293 logging_obj.failure_handler( 2294 e, traceback_exception, start_time, end_time 2295 ) # DO NOT MAKE THREADED - router retry fallback relies on this! -> 2296 raise e File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/main.py:7996, in embedding(model, input, dimensions, encoding_format, timeout, api_base, api_version, api_key, api_type, caching, user, custom_llm_provider, litellm_call_id, logger_fn, **kwargs) 7990 litellm_logging_obj.post_call( 7991 input=input, 7992 api_key=api_key, 7993 original_response=str(e), 7994 ) 7995 ## Map to OpenAI Exception -> 7996 raise exception_type( 7997 model=model, 7998 original_exception=e, 7999 custom_llm_provider=custom_llm_provider, 8000 extra_kwargs=kwargs, 8001 ) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py:2915, in exception_type(model, original_exception, custom_llm_provider, completion_kwargs, extra_kwargs) 2913 if isinstance(e, error_type): 2914 setattr(e, "litellm_response_headers", litellm_response_headers) -> 2915 raise e # it's already mapped 2916 raised_exc: Final = APIConnectionError( 2917 message=f"{original_exception}\n{redact_string(traceback.format_exc())}", 2918 llm_provider="", 2919 model="", 2920 ) 2921 setattr(raised_exc, "litellm_response_headers", litellm_response_headers) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py:2660, in exception_type(model, original_exception, custom_llm_provider, completion_kwargs, extra_kwargs) 2645 extract_and_raise_litellm_exception( 2646 response=_without_credentials(_litellm_proxy_response(mappable_exception, custom_llm_provider)), 2647 error_str=error_str, (...) 2650 body=getattr(mappable_exception, "body", None), 2651 ) 2652 if ( 2653 custom_llm_provider == "openai" 2654 or custom_llm_provider == "text-completion-openai" (...) 2658 or custom_llm_provider == "runwayml" 2659 ): -> 2660 _map_openai_exception( 2661 model=model, 2662 original_exception=mappable_exception, 2663 custom_llm_provider=custom_llm_provider, 2664 error_str=error_str, 2665 exception_type=exception_type, 2666 exception_provider=exception_provider, 2667 extra_information=extra_information, 2668 ) 2669 elif custom_llm_provider == "anthropic" or custom_llm_provider == "anthropic_text": # one of the anthropics 2670 _map_anthropic_exception( 2671 model=model, 2672 original_exception=mappable_exception, (...) 2677 extra_information=extra_information, 2678 ) File ~/work/graphrag/graphrag/.venv/lib/python3.13/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py:510, in _map_openai_exception(model, original_exception, custom_llm_provider, error_str, exception_type, exception_provider, extra_information) 501 raise BadRequestError( 502 message=f"{exception_provider} - {message}", 503 llm_provider=custom_llm_provider, (...) 507 body=getattr(original_exception, "body", None), 508 ) 509 elif original_exception.status_code == 401: --> 510 raise AuthenticationError( 511 message=f"AuthenticationError: {exception_provider} - {message}", 512 llm_provider=custom_llm_provider, 513 model=model, 514 response=response, 515 litellm_debug_info=extra_information, 516 ) 517 elif original_exception.status_code == 404: 518 raise NotFoundError( 519 message=f"NotFoundError: {exception_provider} - {message}", 520 model=model, (...) 523 litellm_debug_info=extra_information, 524 ) AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - Incorrect API key provided: ArzIHXTm************************************************************************dHdW. You can find your API key at https://platform.openai.com/account/api-keys.