Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue about custom LLM. The example file dosn't work! #15

Open
Buzeg opened this issue Nov 14, 2024 · 20 comments
Open

Issue about custom LLM. The example file dosn't work! #15

Buzeg opened this issue Nov 14, 2024 · 20 comments

Comments

@Buzeg
Copy link

Buzeg commented Nov 14, 2024

When I use the official example to use my own LLM, such as GLM-4-Plus, it prompts an error
image

@liukidar
Copy link
Contributor

Hello there! os.getenv() is a function, so you should do model=os.getenv("...") and similar for the other parameters. Let me know if that fixes the issue.

@Buzeg
Copy link
Author

Buzeg commented Nov 15, 2024

Hello there! os.getenv() is a function, so you should do model=os.getenv("...") and similar for the other parameters. Let me know if that fixes the issue.

Hello! Thanks for your reply! But is remind me OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
I set a .env file
os.environ["embed_api_key"] = "****************************" os.environ["OPENAI_API_KEY"] = "*************************" os.environ["base_url"] = "https://open.bigmodel.cn/api/paas/v4" os.environ["llm_model"] = "glm-4-flash" os.environ["embed_model"] = "embedding-3"

And load_dotenv() return TRUE
How can I fix this problem! Really appreciate!
@liukidar

@lawcompany-SH
Copy link

Set .env file with below format. see https://pypi.org/project/python-dotenv/

embed_api_key=*******
OPENAI_API_KEY=******
base_url=https://open.bigmodel.cn/api/paas/v4

@Buzeg
Copy link
Author

Buzeg commented Nov 17, 2024

Set .env file with below format. see https://pypi.org/project/python-dotenv/

embed_api_key=*******
OPENAI_API_KEY=******
base_url=https://open.bigmodel.cn/api/paas/v4

It still return error.
.env file

embed_api_key = ***********************
OPENAI_API_KEY = ************************
base_url = https://open.bigmodel.cn/api/paas/v4
llm_model = glm-4-plus
embed_model = embedding-3

And

working_dir = r"D:\Implement\CodeData\fast-graphrag\fast"

grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(
            model=os.getenv("llm_model"),
            base_url=os.getenv("base_url"),
            api_key=os.getenv("OPENAI_API_KEY")
        ),
        embedding_service=OpenAIEmbeddingService(
            model=os.getenv("embed_model"),
            base_url=os.getenv("base_url"),
            api_key=os.getenv("embed_api_key"),
            embedding_dim=512
        )
    )
)

return

{
	"name": "OpenAIError",
	"message": "The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable",
	"stack": "---------------------------------------------------------------------------
OpenAIError                               Traceback (most recent call last)
Cell In[7], line 9
      1 working_dir = r\"D:\\Implement\\CodeData\\fast-graphrag\\fast\"
      3 grag = GraphRAG(
      4     working_dir=working_dir,
      5     domain=DOMAIN,
      6     example_queries=\"\
\".join(QUERIES),
      7     entity_types=ENTITY_TYPES,
      8     config=GraphRAG.Config(
----> 9         llm_service=OpenAILLMService(
     10             model=os.getenv(\"llm_model\"),
     11             base_url=os.getenv(\"base_url\"),
     12             api_key=os.getenv(\"OPENAI_API_KEY\")
     13         ),
     14         embedding_service=OpenAIEmbeddingService(
     15             model=os.getenv(\"embed_model\"),
     16             base_url=os.getenv(\"base_url\"),
     17             api_key=os.getenv(\"embed_api_key\"),
     18             embedding_dim=512
     19         )
     20     )
     21 )

File <string>:6, in __init__(self, model, base_url, api_key)

File d:\\Implement\\Anaconda3\\envs\\fastgr\\Lib\\site-packages\\fast_graphrag\\_llm\\_llm_openai.py:34, in OpenAILLMService.__post_init__(self)
     32 def __post_init__(self):
     33     logger.debug(\"Initialized OpenAILLMService with patched OpenAI client.\")
---> 34     self.llm_async_client: instructor.AsyncInstructor = instructor.from_openai(AsyncOpenAI(api_key=self.api_key))

File d:\\Implement\\Anaconda3\\envs\\fastgr\\Lib\\site-packages\\openai\\_client.py:319, in AsyncOpenAI.__init__(self, api_key, organization, project, base_url, timeout, max_retries, default_headers, default_query, http_client, _strict_response_validation)
    317     api_key = os.environ.get(\"OPENAI_API_KEY\")
    318 if api_key is None:
--> 319     raise OpenAIError(
    320         \"The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable\"
    321     )
    322 self.api_key = api_key
    324 if organization is None:

OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable"
}

@ @lawcompany-SH

@liukidar
Copy link
Contributor

Hello, I am unable to replicate your issue, would you mind sharing the full code you're using? you can use generic values for api_keys as real ones are not necessary to initialise the model.

@tmceld
Copy link

tmceld commented Nov 18, 2024

I think i am hitting the same problem, with Ollama:

from typing import List

from dotenv import load_dotenv

from fast_graphrag import GraphRAG
from fast_graphrag._llm import OpenAIEmbeddingService, OpenAILLMService

load_dotenv()

DOMAIN = "Analyze this story and identify the characters. Focus on how they interact with each other, the locations they explore, and their relationships."

EXAMPLE_QUERIES = [
    "What is the significance of shoes in Caledonian Road?",
    "How does the setting of London contribute to the story's themes?",
    "Describe the chain of events that leads to Bykov's demise.",
    "What does Capmbell's Mother represent in this story?",
]

ENTITY_TYPES = ["Character", "Animal", "Place", "Object", "Activity", "Event"]

api_key = "ollama"
working_dir = "./examples/"
grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(EXAMPLE_QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(
            model="llama3.2:latest",
            base_url="http://localhost:11434/v1",
            api_key=api_key,
        ),
        embedding_service=OpenAIEmbeddingService(
            model="nomic-embed-text",
            base_url="http://localhost:11434/api/embeddings/",
            api_key=api_key,
            embedding_dim=512,  # the output embedding dim of the chosen model
        ),
    ),
)

with open("./Caledonian Road_ From the award-winning au - Andrew O'Hagan.txt") as f:
    grag.insert(f.read())

print(grag.query("Who is Campbell?").response)

Giving error:

 python test.py
Traceback (most recent call last):
  File "/Users/toast/Developer/ai/fast-graphrag/test.py", line 28, in <module>
    config=GraphRAG.Config(
           ^^^^^^^^^^^^^^^^
  File "<string>", line 10, in __init__
  File "/Users/toast/Developer/ai/fast-graphrag/.venv/lib/python3.11/site-packages/fast_graphrag/__init__.py", line 69, in <lambda>
    DefaultVectorStorageConfig(embedding_dim=DefaultEmbeddingService().embedding_dim)
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 8, in __init__
  File "/Users/toast/Developer/ai/fast-graphrag/.venv/lib/python3.11/site-packages/fast_graphrag/_llm/_llm_openai.py", line 115, in __post_init__
    self.embedding_async_client: AsyncOpenAI = AsyncOpenAI(api_key=self.api_key)
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/toast/Developer/ai/fast-graphrag/.venv/lib/python3.11/site-packages/openai/_client.py", line 319, in __init__
    raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

@tmceld
Copy link

tmceld commented Nov 18, 2024

So i got around the above error, by exporting an OPENAI_API_KEY but not sure why i needed to do this.

I now have further errors with embedding, and am wondering given the status of embedding on ollama - has anyone actually got this to work?

@liukidar
Copy link
Contributor

I think i am hitting the same problem, with Ollama:

from typing import List

from dotenv import load_dotenv

from fast_graphrag import GraphRAG
from fast_graphrag._llm import OpenAIEmbeddingService, OpenAILLMService

load_dotenv()

DOMAIN = "Analyze this story and identify the characters. Focus on how they interact with each other, the locations they explore, and their relationships."

EXAMPLE_QUERIES = [
    "What is the significance of shoes in Caledonian Road?",
    "How does the setting of London contribute to the story's themes?",
    "Describe the chain of events that leads to Bykov's demise.",
    "What does Capmbell's Mother represent in this story?",
]

ENTITY_TYPES = ["Character", "Animal", "Place", "Object", "Activity", "Event"]

api_key = "ollama"
working_dir = "./examples/"
grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(EXAMPLE_QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(
            model="llama3.2:latest",
            base_url="http://localhost:11434/v1",
            api_key=api_key,
        ),
        embedding_service=OpenAIEmbeddingService(
            model="nomic-embed-text",
            base_url="http://localhost:11434/api/embeddings/",
            api_key=api_key,
            embedding_dim=512,  # the output embedding dim of the chosen model
        ),
    ),
)

with open("./Caledonian Road_ From the award-winning au - Andrew O'Hagan.txt") as f:
    grag.insert(f.read())

print(grag.query("Who is Campbell?").response)

Giving error:

 python test.py
Traceback (most recent call last):
  File "/Users/toast/Developer/ai/fast-graphrag/test.py", line 28, in <module>
    config=GraphRAG.Config(
           ^^^^^^^^^^^^^^^^
  File "<string>", line 10, in __init__
  File "/Users/toast/Developer/ai/fast-graphrag/.venv/lib/python3.11/site-packages/fast_graphrag/__init__.py", line 69, in <lambda>
    DefaultVectorStorageConfig(embedding_dim=DefaultEmbeddingService().embedding_dim)
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 8, in __init__
  File "/Users/toast/Developer/ai/fast-graphrag/.venv/lib/python3.11/site-packages/fast_graphrag/_llm/_llm_openai.py", line 115, in __post_init__
    self.embedding_async_client: AsyncOpenAI = AsyncOpenAI(api_key=self.api_key)
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/toast/Developer/ai/fast-graphrag/.venv/lib/python3.11/site-packages/openai/_client.py", line 319, in __init__
    raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

Just to be sure, are you using the pip version or this cloned repo? It may be a bug that we fixed but didn't push to pypi.

@liukidar
Copy link
Contributor

liukidar commented Nov 18, 2024

So i got around the above error, by exporting an OPENAI_API_KEY but not sure why i needed to do this.

I now have further errors with embedding, and am wondering given the status of embedding on ollama - has anyone actually got this to work?

Mmmh, looking here it seems it is supported ollama/ollama#2416 ? But indeed we should clarify this better.
In the post they also suggest to look at this: https://github.com/severian42/GraphRAG-Local-UI/blob/main/embedding_proxy.py

@tmceld
Copy link

tmceld commented Nov 19, 2024

Just to be sure, are you using the pip version or this cloned repo? It may be a bug that we fixed but didn't push to pypi.

Ah, yes i am using the pip installed version

@liukidar
Copy link
Contributor

liukidar commented Nov 19, 2024

Let me know if using the repo directly fixes the problem

@jon-torres
Copy link

I am experiencing a similar issue with Azure. But it's throwing resource not found. I have installed fast-graphrag with pip.

Linux mint 22
python 3.11

from fast_graphrag import GraphRAG
from fast_graphrag._llm import OpenAILLMService, OpenAIEmbeddingService

DOMAIN = "Analyze this story and identify the characters. Focus on how they interact with each other, the locations they explore, and their relationships."

EXAMPLE_QUERIES = [
    "What is the significance of Christmas Eve in A Christmas Carol?",
    "How does the setting of Victorian London contribute to the story's themes?",
    "Describe the chain of events that leads to Scrooge's transformation.",
    "How does Dickens use the different spirits (Past, Present, and Future) to guide Scrooge?",
    "Why does Dickens choose to divide the story into \"staves\" rather than chapters?"
]

ENTITY_TYPES = ["Character", "Animal", "Place", "Object", "Activity", "Event"]

model = "gpt-4o"
base_url = "https://<resource>.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview"
api_key = "5xxxxxxxx"

working_dir="./book_example"
grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(EXAMPLE_QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(model=model, base_url=base_url, api_key=api_key),
         embedding_service=OpenAIEmbeddingService(
            model=model,
            base_url=base_url,
            api_key=api_key,
            embedding_dim=512,  # the output embedding dim of the chosen model
        ),
    ),
)

with open("./book.txt") as f:
    grag.insert(f.read())

print(grag.query("Who is Scrooge?").response)
Extracting data:   0%|                                    | 0/1 [00:00<?, ?it/s]Error during information extraction from document: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Extracting data: 100%|████████████████████████████| 1/1 [00:09<00:00,  9.48s/it]
Error during query: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 222, in retry_async
    response: ChatCompletion = await func(*args, **kwargs)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 1633, in create
    return await self._post(
           ^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1838, in post
    return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1532, in request
    return await self._request(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1633, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 217, in retry_async
    async for attempt in max_retries:
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 166, in __anext__
    do = await self.iter(retry_state=self._retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 153, in iter
    result = await action(retry_state)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/_utils.py", line 99, in inner
    return call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/__init__.py", line 419, in exc_check
    raise retry_exc from fut.exception()
tenacity.RetryError: RetryError[<Future at 0x715107d72c50 state=finished raised NotFoundError>]

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/Documents/development/loro/fast_graphrag/graphrag_test.py", line 47, in <module>
    print(grag.query("Who is Scrooge?").response)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 150, in query
    return get_event_loop().run_until_complete(_query())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 146, in _query
    raise e
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 142, in _query
    answer = await self.async_query(query, params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 168, in async_query
    extracted_entities = await self.information_extraction_service.extract_entities_from_query(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_services/_information_extraction.py", line 46, in extract_entities_from_query
    entities, _ = await format_and_send_prompt(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_base.py", line 40, in format_and_send_prompt
    return await llm.send_message(prompt=formatted_prompt, response_model=response_model, **args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_utils.py", line 45, in wait_func
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_llm_openai.py", line 80, in send_message
    llm_response: GTResponseModel = await self.llm_async_client.chat.completions.create(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/client.py", line 387, in create
    return await self.create_fn(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/patch.py", line 161, in new_create_async
    response = await retry_async(
               ^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 248, in retry_async
    raise InstructorRetryException(
instructor.exceptions.InstructorRetryException: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

@liukidar
Copy link
Contributor

I am experiencing a similar issue with Azure. But it's throwing resource not found. I have installed fast-graphrag with pip.

Linux mint 22 python 3.11

from fast_graphrag import GraphRAG
from fast_graphrag._llm import OpenAILLMService, OpenAIEmbeddingService

DOMAIN = "Analyze this story and identify the characters. Focus on how they interact with each other, the locations they explore, and their relationships."

EXAMPLE_QUERIES = [
    "What is the significance of Christmas Eve in A Christmas Carol?",
    "How does the setting of Victorian London contribute to the story's themes?",
    "Describe the chain of events that leads to Scrooge's transformation.",
    "How does Dickens use the different spirits (Past, Present, and Future) to guide Scrooge?",
    "Why does Dickens choose to divide the story into \"staves\" rather than chapters?"
]

ENTITY_TYPES = ["Character", "Animal", "Place", "Object", "Activity", "Event"]

model = "gpt-4o"
base_url = "https://<resource>.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview"
api_key = "5xxxxxxxx"

working_dir="./book_example"
grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(EXAMPLE_QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(model=model, base_url=base_url, api_key=api_key),
         embedding_service=OpenAIEmbeddingService(
            model=model,
            base_url=base_url,
            api_key=api_key,
            embedding_dim=512,  # the output embedding dim of the chosen model
        ),
    ),
)

with open("./book.txt") as f:
    grag.insert(f.read())

print(grag.query("Who is Scrooge?").response)
Extracting data:   0%|                                    | 0/1 [00:00<?, ?it/s]Error during information extraction from document: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Extracting data: 100%|████████████████████████████| 1/1 [00:09<00:00,  9.48s/it]
Error during query: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 222, in retry_async
    response: ChatCompletion = await func(*args, **kwargs)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 1633, in create
    return await self._post(
           ^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1838, in post
    return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1532, in request
    return await self._request(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1633, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 217, in retry_async
    async for attempt in max_retries:
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 166, in __anext__
    do = await self.iter(retry_state=self._retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 153, in iter
    result = await action(retry_state)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/_utils.py", line 99, in inner
    return call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/__init__.py", line 419, in exc_check
    raise retry_exc from fut.exception()
tenacity.RetryError: RetryError[<Future at 0x715107d72c50 state=finished raised NotFoundError>]

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/Documents/development/loro/fast_graphrag/graphrag_test.py", line 47, in <module>
    print(grag.query("Who is Scrooge?").response)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 150, in query
    return get_event_loop().run_until_complete(_query())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 146, in _query
    raise e
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 142, in _query
    answer = await self.async_query(query, params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 168, in async_query
    extracted_entities = await self.information_extraction_service.extract_entities_from_query(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_services/_information_extraction.py", line 46, in extract_entities_from_query
    entities, _ = await format_and_send_prompt(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_base.py", line 40, in format_and_send_prompt
    return await llm.send_message(prompt=formatted_prompt, response_model=response_model, **args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_utils.py", line 45, in wait_func
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_llm_openai.py", line 80, in send_message
    llm_response: GTResponseModel = await self.llm_async_client.chat.completions.create(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/client.py", line 387, in create
    return await self.create_fn(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/patch.py", line 161, in new_create_async
    response = await retry_async(
               ^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 248, in retry_async
    raise InstructorRetryException(
instructor.exceptions.InstructorRetryException: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

From your example it looks like you're using the same model and base_url for both LLM and Embedder, I'm not sure that's supported by Azure, I would doulbe check that. Specifically, the 404 is telling you that the LLM base_url is invalid.

@jon-torres
Copy link

I am experiencing a similar issue with Azure. But it's throwing resource not found. I have installed fast-graphrag with pip.
Linux mint 22 python 3.11

from fast_graphrag import GraphRAG
from fast_graphrag._llm import OpenAILLMService, OpenAIEmbeddingService

DOMAIN = "Analyze this story and identify the characters. Focus on how they interact with each other, the locations they explore, and their relationships."

EXAMPLE_QUERIES = [
    "What is the significance of Christmas Eve in A Christmas Carol?",
    "How does the setting of Victorian London contribute to the story's themes?",
    "Describe the chain of events that leads to Scrooge's transformation.",
    "How does Dickens use the different spirits (Past, Present, and Future) to guide Scrooge?",
    "Why does Dickens choose to divide the story into \"staves\" rather than chapters?"
]

ENTITY_TYPES = ["Character", "Animal", "Place", "Object", "Activity", "Event"]

model = "gpt-4o"
base_url = "https://<resource>.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview"
api_key = "5xxxxxxxx"

working_dir="./book_example"
grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(EXAMPLE_QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(model=model, base_url=base_url, api_key=api_key),
         embedding_service=OpenAIEmbeddingService(
            model=model,
            base_url=base_url,
            api_key=api_key,
            embedding_dim=512,  # the output embedding dim of the chosen model
        ),
    ),
)

with open("./book.txt") as f:
    grag.insert(f.read())

print(grag.query("Who is Scrooge?").response)
Extracting data:   0%|                                    | 0/1 [00:00<?, ?it/s]Error during information extraction from document: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Extracting data: 100%|████████████████████████████| 1/1 [00:09<00:00,  9.48s/it]
Error during query: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 222, in retry_async
    response: ChatCompletion = await func(*args, **kwargs)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 1633, in create
    return await self._post(
           ^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1838, in post
    return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1532, in request
    return await self._request(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1633, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 217, in retry_async
    async for attempt in max_retries:
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 166, in __anext__
    do = await self.iter(retry_state=self._retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 153, in iter
    result = await action(retry_state)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/_utils.py", line 99, in inner
    return call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/__init__.py", line 419, in exc_check
    raise retry_exc from fut.exception()
tenacity.RetryError: RetryError[<Future at 0x715107d72c50 state=finished raised NotFoundError>]

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/Documents/development/loro/fast_graphrag/graphrag_test.py", line 47, in <module>
    print(grag.query("Who is Scrooge?").response)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 150, in query
    return get_event_loop().run_until_complete(_query())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 146, in _query
    raise e
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 142, in _query
    answer = await self.async_query(query, params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 168, in async_query
    extracted_entities = await self.information_extraction_service.extract_entities_from_query(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_services/_information_extraction.py", line 46, in extract_entities_from_query
    entities, _ = await format_and_send_prompt(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_base.py", line 40, in format_and_send_prompt
    return await llm.send_message(prompt=formatted_prompt, response_model=response_model, **args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_utils.py", line 45, in wait_func
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_llm_openai.py", line 80, in send_message
    llm_response: GTResponseModel = await self.llm_async_client.chat.completions.create(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/client.py", line 387, in create
    return await self.create_fn(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/patch.py", line 161, in new_create_async
    response = await retry_async(
               ^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 248, in retry_async
    raise InstructorRetryException(
instructor.exceptions.InstructorRetryException: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

From your example it looks like you're using the same model and base_url for both LLM and Embedder, I'm not sure that's supported by Azure, I would doulbe check that. Specifically, the 404 is telling you that the LLM base_url is invalid.

Hey, I appreciate the answer. Is the OpenAIEmbeddingService needed? I was trying to replicate the example in the main README.md but using this custom.py because from what I understand it's needed to explicit pass the endpoint API if you are not using openAI directly, right?

@liukidar
Copy link
Contributor

Yes, both llm and embedder are necessary, but normally they have different models and urls. I will also clarify this in the example.

@jon-torres
Copy link

Yes, both llm and embedder are necessary, but normally they have different models and urls. I will also clarify this in the example.

Thank you for making it clear, but the error seems to persist even with a different deploy for the embedder.

code:

from fast_graphrag import GraphRAG
from fast_graphrag._llm import OpenAILLMService, OpenAIEmbeddingService

DOMAIN = "Analyze this story and identify the characters. Focus on how they interact with each other, the locations they explore, and their relationships."

EXAMPLE_QUERIES = [
    "What is the significance of Christmas Eve in A Christmas Carol?",
    "How does the setting of Victorian London contribute to the story's themes?",
    "Describe the chain of events that leads to Scrooge's transformation.",
    "How does Dickens use the different spirits (Past, Present, and Future) to guide Scrooge?",
    "Why does Dickens choose to divide the story into \"staves\" rather than chapters?"
]

ENTITY_TYPES = ["Character", "Animal", "Place", "Object", "Activity", "Event"]

emb_model = "text-embedding-3-small"
emb_url = "https://<DEPLOYNAME>.openai.azure.com/openai/deployments/text-embedding-3-small/embeddings?api-version=2023-05-15"
emb_key = "5xxxx"

model = "gpt-4o"
base_url = "https://<DEPLOYNAME>.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview"
api_key = "5xxxx"

working_dir="./book_example"
grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(EXAMPLE_QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(model=model, base_url=base_url, api_key=api_key),
         embedding_service=OpenAIEmbeddingService(
            model=emb_model,
            base_url=emb_url,
            api_key=emb_key,
            embedding_dim=512,  # the output embedding dim of the chosen model
        ),
    ),
)

with open("./book.txt") as f:
    grag.insert(f.read())

print(grag.query("Who is Scrooge?").response)
Extracting data:   0%|                                    | 0/1 [00:00<?, ?it/s]Error during information extraction from document: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Extracting data: 100%|████████████████████████████| 1/1 [00:09<00:00,  9.38s/it]
Error during query: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 222, in retry_async
    response: ChatCompletion = await func(*args, **kwargs)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 1633, in create
    return await self._post(
           ^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1838, in post
    return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1532, in request
    return await self._request(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1633, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 217, in retry_async
    async for attempt in max_retries:
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 166, in __anext__
    do = await self.iter(retry_state=self._retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 153, in iter
    result = await action(retry_state)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/_utils.py", line 99, in inner
    return call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/__init__.py", line 419, in exc_check
    raise retry_exc from fut.exception()
tenacity.RetryError: RetryError[<Future at 0x73ce66217a10 state=finished raised NotFoundError>]

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/Documents/development/loro/fast_graphrag/graphrag_test.py", line 44, in <module>
    print(grag.query("Who is Scrooge?").response)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 150, in query
    return get_event_loop().run_until_complete(_query())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 146, in _query
    raise e
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 142, in _query
    answer = await self.async_query(query, params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 168, in async_query
    extracted_entities = await self.information_extraction_service.extract_entities_from_query(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_services/_information_extraction.py", line 46, in extract_entities_from_query
    entities, _ = await format_and_send_prompt(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_base.py", line 40, in format_and_send_prompt
    return await llm.send_message(prompt=formatted_prompt, response_model=response_model, **args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_utils.py", line 45, in wait_func
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_llm_openai.py", line 80, in send_message
    llm_response: GTResponseModel = await self.llm_async_client.chat.completions.create(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/client.py", line 387, in create
    return await self.create_fn(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/patch.py", line 161, in new_create_async
    response = await retry_async(
               ^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 248, in retry_async
    raise InstructorRetryException(
instructor.exceptions.InstructorRetryException: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

@liukidar
Copy link
Contributor

Hello, for both errors it looks like the url you're using doesn't point to a valid model. Can you try to instantiate the OpenAILLMService and use it directly? it provides a method "send_message".

@21m1n
Copy link

21m1n commented Dec 27, 2024

this is how i run it with ollama:

import instructor 


grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(
            model="llama3", base_url="http://localhost:11434/v1", api_key="ollama", mode=instructor.Mode.JSON
        ),
        embedding_service=OpenAIEmbeddingService(
            model="nomic-embed-text",
            base_url="http://localhost:11434/v1",
            api_key="ollama",
            embedding_dim=768,  
        ),
    ),
)

@idreesghazi
Copy link

idreesghazi commented Jan 6, 2025

I am experiencing a similar issue with Azure. But it's throwing resource not found. I have installed fast-graphrag with pip.
Linux mint 22 python 3.11

from fast_graphrag import GraphRAG
from fast_graphrag._llm import OpenAILLMService, OpenAIEmbeddingService

DOMAIN = "Analyze this story and identify the characters. Focus on how they interact with each other, the locations they explore, and their relationships."

EXAMPLE_QUERIES = [
    "What is the significance of Christmas Eve in A Christmas Carol?",
    "How does the setting of Victorian London contribute to the story's themes?",
    "Describe the chain of events that leads to Scrooge's transformation.",
    "How does Dickens use the different spirits (Past, Present, and Future) to guide Scrooge?",
    "Why does Dickens choose to divide the story into \"staves\" rather than chapters?"
]

ENTITY_TYPES = ["Character", "Animal", "Place", "Object", "Activity", "Event"]

model = "gpt-4o"
base_url = "https://<resource>.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview"
api_key = "5xxxxxxxx"

working_dir="./book_example"
grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(EXAMPLE_QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(model=model, base_url=base_url, api_key=api_key),
         embedding_service=OpenAIEmbeddingService(
            model=model,
            base_url=base_url,
            api_key=api_key,
            embedding_dim=512,  # the output embedding dim of the chosen model
        ),
    ),
)

with open("./book.txt") as f:
    grag.insert(f.read())

print(grag.query("Who is Scrooge?").response)
Extracting data:   0%|                                    | 0/1 [00:00<?, ?it/s]Error during information extraction from document: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Extracting data: 100%|████████████████████████████| 1/1 [00:09<00:00,  9.48s/it]
Error during query: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 222, in retry_async
    response: ChatCompletion = await func(*args, **kwargs)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 1633, in create
    return await self._post(
           ^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1838, in post
    return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1532, in request
    return await self._request(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1633, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 217, in retry_async
    async for attempt in max_retries:
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 166, in __anext__
    do = await self.iter(retry_state=self._retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 153, in iter
    result = await action(retry_state)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/_utils.py", line 99, in inner
    return call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/__init__.py", line 419, in exc_check
    raise retry_exc from fut.exception()
tenacity.RetryError: RetryError[<Future at 0x715107d72c50 state=finished raised NotFoundError>]

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/Documents/development/loro/fast_graphrag/graphrag_test.py", line 47, in <module>
    print(grag.query("Who is Scrooge?").response)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 150, in query
    return get_event_loop().run_until_complete(_query())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 146, in _query
    raise e
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 142, in _query
    answer = await self.async_query(query, params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 168, in async_query
    extracted_entities = await self.information_extraction_service.extract_entities_from_query(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_services/_information_extraction.py", line 46, in extract_entities_from_query
    entities, _ = await format_and_send_prompt(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_base.py", line 40, in format_and_send_prompt
    return await llm.send_message(prompt=formatted_prompt, response_model=response_model, **args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_utils.py", line 45, in wait_func
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_llm_openai.py", line 80, in send_message
    llm_response: GTResponseModel = await self.llm_async_client.chat.completions.create(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/client.py", line 387, in create
    return await self.create_fn(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/patch.py", line 161, in new_create_async
    response = await retry_async(
               ^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 248, in retry_async
    raise InstructorRetryException(
instructor.exceptions.InstructorRetryException: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

From your example it looks like you're using the same model and base_url for both LLM and Embedder, I'm not sure that's supported by Azure, I would doulbe check that. Specifically, the 404 is telling you that the LLM base_url is invalid.

Hey, I appreciate the answer. Is the OpenAIEmbeddingService needed? I was trying to replicate the example in the main README.md but using this custom.py because from what I understand it's needed to explicit pass the endpoint API if you are not using openAI directly, right?

While using Azure, you have to exactly specify the base_url of your model

base_url_llm = https://<resource>.openai.azure.com/openai/deployments/gpt-4o

And for the embed model, specify it like

base_url_embed = https://<resource>.openai.azure.com/openai/deployments/text-embedding-3-large

Make sure that these models are correctly deployed on Azure
Also regarding that api_version, currently if you look inside the fast-graph library, it take api_version from the environmental variables, so you must set your environmental variables like this OPENAI_API_VERSION = "your-version"
Hopefully this helps :D

@wangzhen38
Copy link
Contributor

Yes, both llm and embedder are necessary, but normally they have different models and urls. I will also clarify this in the example.

Thank you for making it clear, but the error seems to persist even with a different deploy for the embedder.

code:

from fast_graphrag import GraphRAG
from fast_graphrag._llm import OpenAILLMService, OpenAIEmbeddingService

DOMAIN = "Analyze this story and identify the characters. Focus on how they interact with each other, the locations they explore, and their relationships."

EXAMPLE_QUERIES = [
    "What is the significance of Christmas Eve in A Christmas Carol?",
    "How does the setting of Victorian London contribute to the story's themes?",
    "Describe the chain of events that leads to Scrooge's transformation.",
    "How does Dickens use the different spirits (Past, Present, and Future) to guide Scrooge?",
    "Why does Dickens choose to divide the story into \"staves\" rather than chapters?"
]

ENTITY_TYPES = ["Character", "Animal", "Place", "Object", "Activity", "Event"]

emb_model = "text-embedding-3-small"
emb_url = "https://<DEPLOYNAME>.openai.azure.com/openai/deployments/text-embedding-3-small/embeddings?api-version=2023-05-15"
emb_key = "5xxxx"

model = "gpt-4o"
base_url = "https://<DEPLOYNAME>.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview"
api_key = "5xxxx"

working_dir="./book_example"
grag = GraphRAG(
    working_dir=working_dir,
    domain=DOMAIN,
    example_queries="\n".join(EXAMPLE_QUERIES),
    entity_types=ENTITY_TYPES,
    config=GraphRAG.Config(
        llm_service=OpenAILLMService(model=model, base_url=base_url, api_key=api_key),
         embedding_service=OpenAIEmbeddingService(
            model=emb_model,
            base_url=emb_url,
            api_key=emb_key,
            embedding_dim=512,  # the output embedding dim of the chosen model
        ),
    ),
)

with open("./book.txt") as f:
    grag.insert(f.read())

print(grag.query("Who is Scrooge?").response)
Extracting data:   0%|                                    | 0/1 [00:00<?, ?it/s]Error during information extraction from document: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Extracting data: 100%|████████████████████████████| 1/1 [00:09<00:00,  9.38s/it]
Error during query: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 222, in retry_async
    response: ChatCompletion = await func(*args, **kwargs)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 1633, in create
    return await self._post(
           ^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1838, in post
    return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1532, in request
    return await self._request(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/openai/_base_client.py", line 1633, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 217, in retry_async
    async for attempt in max_retries:
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 166, in __anext__
    do = await self.iter(retry_state=self._retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/asyncio/__init__.py", line 153, in iter
    result = await action(retry_state)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/_utils.py", line 99, in inner
    return call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/tenacity/__init__.py", line 419, in exc_check
    raise retry_exc from fut.exception()
tenacity.RetryError: RetryError[<Future at 0x73ce66217a10 state=finished raised NotFoundError>]

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jontorres/Documents/development/loro/fast_graphrag/graphrag_test.py", line 44, in <module>
    print(grag.query("Who is Scrooge?").response)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 150, in query
    return get_event_loop().run_until_complete(_query())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 146, in _query
    raise e
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 142, in _query
    answer = await self.async_query(query, params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_graphrag.py", line 168, in async_query
    extracted_entities = await self.information_extraction_service.extract_entities_from_query(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_services/_information_extraction.py", line 46, in extract_entities_from_query
    entities, _ = await format_and_send_prompt(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_base.py", line 40, in format_and_send_prompt
    return await llm.send_message(prompt=formatted_prompt, response_model=response_model, **args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_utils.py", line 45, in wait_func
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/fast_graphrag/_llm/_llm_openai.py", line 80, in send_message
    llm_response: GTResponseModel = await self.llm_async_client.chat.completions.create(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/client.py", line 387, in create
    return await self.create_fn(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/patch.py", line 161, in new_create_async
    response = await retry_async(
               ^^^^^^^^^^^^^^^^^^
  File "/home/jontorres/anaconda3/envs/loro/lib/python3.11/site-packages/instructor/retry.py", line 248, in retry_async
    raise InstructorRetryException(
instructor.exceptions.InstructorRetryException: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

This commit can solve your problem:#62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants