当前位置:   article > 正文

graphrag+ollama的两个“坑”与解决办法_graphrag.llm.openai.utils info warning: error deco

graphrag.llm.openai.utils info warning: error decoding faulty json, attempti

https://microsoft.github.io/graphrag/posts/get_started/

根据官方的步骤,修改setting到ollama本地,会出现如下两个常见问题:

找起来很费时间 ,分享给大家希望有帮助

global提问报错:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

解决办法:

I found out that my local Ollama instance (0.3.0) seemed to ignore the system prompt and I got it working by manually stitching together the two prompts into one: File: /graphrag/query/structured_search/global_search/search.py ,

https://github.com/microsoft/graphrag/issues/575

一定要看清楚报错的是哪个文件夹下的/graphrag/query/structured_search/global_search/search.py

修改这个search.py

搜索:_map_response_single_batch,然后修改

  1. map_response_single_batch
  2. #search_messages = [
  3. # {"role": "system", "content": search_prompt},
  4. # {"role": "user", "content": query},
  5. #]
  6. search_messages = [ {"role": "user", "content": search_prompt + "\n\n### USER QUESTION ### \n\n" + query} ]

local提问报错:ZeroDivisionError: Weights sum to zero, can't be normalized

解决办法:

Yes this is due to your locally run embedding model, not returning the weights in a correct format. OpenAI uses internally base64 encoded floats, and most other models will return floats as numbers.

https://github.com/microsoft/graphrag/issues/619

https://github.com/microsoft/graphrag/issues/345

一定要看清楚报错的是哪个文件夹下的graphrag\query\llm\oai\embedding.py

修改这个embedding.py

  1. # Copyright (c) 2024 Microsoft Corporation.
  2. # Licensed under the MIT License
  3. """OpenAI Embedding model implementation."""
  4. import asyncio
  5. from collections.abc import Callable
  6. from typing import Any
  7. import numpy as np
  8. import tiktoken
  9. from tenacity import (
  10. AsyncRetrying,
  11. RetryError,
  12. Retrying,
  13. retry_if_exception_type,
  14. stop_after_attempt,
  15. wait_exponential_jitter,
  16. )
  17. from graphrag.query.llm.base import BaseTextEmbedding
  18. from graphrag.query.llm.oai.base import OpenAILLMImpl
  19. from graphrag.query.llm.oai.typing import (
  20. OPENAI_RETRY_ERROR_TYPES,
  21. OpenaiApiType,
  22. )
  23. from graphrag.query.llm.text_utils import chunk_text
  24. from graphrag.query.progress import StatusReporter
  25. from langchain_community.embeddings import OllamaEmbeddings
  26. class OpenAIEmbedding(BaseTextEmbedding, OpenAILLMImpl):
  27. """Wrapper for OpenAI Embedding models."""
  28. def __init__(
  29. self,
  30. api_key: str | None = None,
  31. azure_ad_token_provider: Callable | None = None,
  32. model: str = "text-embedding-3-small",
  33. deployment_name: str | None = None,
  34. api_base: str | None = None,
  35. api_version: str | None = None,
  36. api_type: OpenaiApiType = OpenaiApiType.OpenAI,
  37. organization: str | None = None,
  38. encoding_name: str = "cl100k_base",
  39. max_tokens: int = 8191,
  40. max_retries: int = 10,
  41. request_timeout: float = 180.0,
  42. retry_error_types: tuple[type[BaseException]] = OPENAI_RETRY_ERROR_TYPES, # type: ignore
  43. reporter: StatusReporter | None = None,
  44. ):
  45. OpenAILLMImpl.__init__(
  46. self=self,
  47. api_key=api_key,
  48. azure_ad_token_provider=azure_ad_token_provider,
  49. deployment_name=deployment_name,
  50. api_base=api_base,
  51. api_version=api_version,
  52. api_type=api_type, # type: ignore
  53. organization=organization,
  54. max_retries=max_retries,
  55. request_timeout=request_timeout,
  56. reporter=reporter,
  57. )
  58. self.model = model
  59. self.encoding_name = encoding_name
  60. self.max_tokens = max_tokens
  61. self.token_encoder = tiktoken.get_encoding(self.encoding_name)
  62. self.retry_error_types = retry_error_types
  63. def embed(self, text: str, **kwargs: Any) -> list[float]:
  64. """
  65. Embed text using OpenAI Embedding's sync function.
  66. For text longer than max_tokens, chunk texts into max_tokens, embed each chunk, then combine using weighted average.
  67. Please refer to: https://github.com/openai/openai-cookbook/blob/main/examples/Embedding_long_inputs.ipynb
  68. """
  69. token_chunks = chunk_text(
  70. text=text, token_encoder=self.token_encoder, max_tokens=self.max_tokens
  71. )
  72. chunk_embeddings = []
  73. chunk_lens = []
  74. for chunk in token_chunks:
  75. try:
  76. embedding, chunk_len = self._embed_with_retry(chunk, **kwargs)
  77. chunk_embeddings.append(embedding)
  78. chunk_lens.append(chunk_len)
  79. # TODO: catch a more specific exception
  80. except Exception as e: # noqa BLE001
  81. self._reporter.error(
  82. message="Error embedding chunk",
  83. details={self.__class__.__name__: str(e)},
  84. )
  85. continue
  86. chunk_embeddings = np.average(chunk_embeddings, axis=0, weights=chunk_lens)
  87. chunk_embeddings = chunk_embeddings / np.linalg.norm(chunk_embeddings)
  88. return chunk_embeddings.tolist()
  89. async def aembed(self, text: str, **kwargs: Any) -> list[float]:
  90. """
  91. Embed text using OpenAI Embedding's async function.
  92. For text longer than max_tokens, chunk texts into max_tokens, embed each chunk, then combine using weighted average.
  93. """
  94. token_chunks = chunk_text(
  95. text=text, token_encoder=self.token_encoder, max_tokens=self.max_tokens
  96. )
  97. chunk_embeddings = []
  98. chunk_lens = []
  99. embedding_results = await asyncio.gather(*[
  100. self._aembed_with_retry(chunk, **kwargs) for chunk in token_chunks
  101. ])
  102. embedding_results = [result for result in embedding_results if result[0]]
  103. chunk_embeddings = [result[0] for result in embedding_results]
  104. chunk_lens = [result[1] for result in embedding_results]
  105. chunk_embeddings = np.average(chunk_embeddings, axis=0, weights=chunk_lens) # type: ignore
  106. chunk_embeddings = chunk_embeddings / np.linalg.norm(chunk_embeddings)
  107. return chunk_embeddings.tolist()
  108. def _embed_with_retry(
  109. self, text: str | tuple, **kwargs: Any
  110. ) -> tuple[list[float], int]:
  111. try:
  112. retryer = Retrying(
  113. stop=stop_after_attempt(self.max_retries),
  114. wait=wait_exponential_jitter(max=10),
  115. reraise=True,
  116. retry=retry_if_exception_type(self.retry_error_types),
  117. )
  118. for attempt in retryer:
  119. with attempt:
  120. embedding = (
  121. OllamaEmbeddings(
  122. model=self.model,
  123. ).embed_query(text)
  124. or []
  125. )
  126. return (embedding, len(text))
  127. except RetryError as e:
  128. self._reporter.error(
  129. message="Error at embed_with_retry()",
  130. details={self.__class__.__name__: str(e)},
  131. )
  132. return ([], 0)
  133. else:
  134. # TODO: why not just throw in this case?
  135. return ([], 0)
  136. async def _aembed_with_retry(
  137. self, text: str | tuple, **kwargs: Any
  138. ) -> tuple[list[float], int]:
  139. try:
  140. retryer = AsyncRetrying(
  141. stop=stop_after_attempt(self.max_retries),
  142. wait=wait_exponential_jitter(max=10),
  143. reraise=True,
  144. retry=retry_if_exception_type(self.retry_error_types),
  145. )
  146. async for attempt in retryer:
  147. with attempt:
  148. embedding = (
  149. await OllamaEmbeddings(
  150. model=self.model,
  151. ).embed_query(text) or [] )
  152. return (embedding, len(text))
  153. except RetryError as e:
  154. self._reporter.error(
  155. message="Error at embed_with_retry()",
  156. details={self.__class__.__name__: str(e)},
  157. )
  158. return ([], 0)
  159. else:
  160. # TODO: why not just throw in this case?
  161. return ([], 0)

改了哪些部分?

增加了一行

from langchain_community.embeddings import OllamaEmbeddings

然后修改两处embedding

搜索:for attempt in retryer:

搜索:async for attempt in retryer: with attempt:

你会发现不一样的地方 替换掉

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/996698
推荐阅读
相关标签
  

闽ICP备14008679号