Closed imaxwen closed 2 months ago
@imaxwen Hello there! I'm here to help you with any bugs, questions, or contributions. Let me know how I can assist you while we wait for a human maintainer.
It looks like you're encountering a 'NoneType' object is not subscriptable
error when using TONGYI's text-embedding-v2
in Dify version 0.7.2. This issue typically occurs when the response from the dashscope.TextEmbedding.call
method does not contain the expected data.
To resolve this, you need to ensure that the response.output
and response.usage
are not None
before attempting to subscript them. Here is a modified version of the embed_documents
method with added checks to handle this scenario:
@staticmethod
def embed_documents(
credentials_kwargs: dict, model: str, texts: list[str]
) -> tuple[list[list[float]], int]:
"""Call out to Tongyi's embedding endpoint.
Args:
credentials_kwargs: The credentials to use for the call.
model: The model to use for embedding.
texts: The list of texts to embed.
Returns:
List of embeddings, one for each text, and tokens usage.
"""
embeddings = []
embedding_used_tokens = 0
for text in texts:
response = dashscope.TextEmbedding.call(
api_key=credentials_kwargs["dashscope_api_key"],
model=model,
input=text,
text_type="document",
)
if response.output and "embeddings" in response.output and response.output["embeddings"]:
data = response.output["embeddings"][0]
if "embedding" in data:
embeddings.append(data["embedding"])
else:
raise ValueError("Embedding data is missing in the response.")
else:
raise ValueError("Response output is missing or does not contain embeddings.")
if response.usage and "total_tokens" in response.usage:
embedding_used_tokens += response.usage["total_tokens"]
else:
raise ValueError("Response usage is missing or does not contain total tokens.")
return [list(map(float, e)) for e in embeddings], embedding_used_tokens
This code adds checks to ensure that response.output
and response.usage
are not None
and contain the expected keys before attempting to access them. If the expected data is missing, it raises a ValueError
with an appropriate message [1][2].
To continue talking to Dosu, mention @dosu.
Self Checks
Dify version
0.7.2
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
Create Knowledge and upload a simple excel file, there goes an error below(using TONGYI's
text-embedding-v2
):✔️ Expected Behavior
No response
❌ Actual Behavior
No response