langchain-ai / langchain-google

MIT License
120 stars 150 forks source link

genai embedding not work, 504 deadline #258

Closed mrunhap closed 6 months ago

mrunhap commented 6 months ago

I try google's package and langchain_google_genai for chat and embedding, only langchain's embedding not work, here my example code:

import google.generativeai as genai
from langchain_google_genai import GoogleGenerativeAI, GoogleGenerativeAIEmbeddings

key = "my-key"
genai.configure(api_key=key)

def gemini_chat():
    model = genai.GenerativeModel("gemini-pro")
    response = model.generate_content("Write a story about a magic backpack.")
    print(response.text)

def gemini_embed():
    result = genai.embed_content(
        model="models/embedding-001",
        content="What is the meaning of life?",
        task_type="retrieval_document",
        title="Embedding of single string",
    )
    # 1 input > 1 vector output
    print(str(result["embedding"])[:50], "... TRIMMED]")

def langchain_chat():
    chat_model = GoogleGenerativeAI(model="gemini-pro", google_api_key=key)
    res = chat_model.invoke("Write a story about a magic backpack.")
    print(res)

def langchain_embed():
    embeddings = GoogleGenerativeAIEmbeddings(
        model="models/embedding-001",
        google_api_key=key,
    )
    print("------------------")
    print(embeddings.embed_query("What is the meaning of life?"))
    # 
    # print("------------------")
    # print(embeddings.embed_documents(
    #     [
    #         "Today is Monday",
    #         "Today is Tuesday",
    #         "Today is April Fools day",
    #     ]
    # ))

# gemini_chat()
# gemini_embed()
# langchain_chat()
# FIXME 504
langchain_embed()

And log:

Traceback (most recent call last):
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/langchain_google_genai/embeddings.py", line 146, in embed_documents
    result = self.client.batch_embed_contents(
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py", line 1350, in batch_embed_contents
    response = rpc(
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
    return wrapped_func(*args, **kwargs)
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
    return retry_target(
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
    _retry_error_helper(
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
    raise final_exc from source_exc
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
    result = target()
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout
    return func(*args, **kwargs)
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/google/api_core/grpc_helpers.py", line 78, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.DeadlineExceeded: 504 Deadline Exceeded

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

Traceback (most recent call last):
  File "/root/p/test-python/main.py", line 48, in <module>
    langchain_embed()
  File "/root/p/test-python/main.py", line 33, in langchain_embed
    print(embeddings.embed_query("What is the meaning of life?"))
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/langchain_google_genai/embeddings.py", line 174, in embed_query
    return self.embed_documents(
  File "/root/p/test-python/.venv/lib/python3.10/site-packages/langchain_google_genai/embeddings.py", line 150, in embed_documents
    raise GoogleGenerativeAIError(f"Error embedding content: {e}") from e
langchain_google_genai._common.GoogleGenerativeAIError: Error embedding content: 504 Deadline Exceeded

I also try add request option and config a larger timeout but it won't work too.

Adi8885 commented 6 months ago

@mrunhap Can you try with latest version : langchain-google-genai-1.0.5 ?

I'm able to successfully run the below code

I try google's package and langchain_google_genai for chat and embedding, only langchain's embedding not work, here my example code:

import google.generativeai as genai
from langchain_google_genai import GoogleGenerativeAI, GoogleGenerativeAIEmbeddings

key = "my-key"
genai.configure(api_key=key)

def gemini_chat():
    model = genai.GenerativeModel("gemini-pro")
    response = model.generate_content("Write a story about a magic backpack.")
    print(response.text)

def gemini_embed():
    result = genai.embed_content(
        model="models/embedding-001",
        content="What is the meaning of life?",
        task_type="retrieval_document",
        title="Embedding of single string",
    )
    # 1 input > 1 vector output
    print(str(result["embedding"])[:50], "... TRIMMED]")

def langchain_chat():
    chat_model = GoogleGenerativeAI(model="gemini-pro", google_api_key=key)
    res = chat_model.invoke("Write a story about a magic backpack.")
    print(res)

def langchain_embed():
    embeddings = GoogleGenerativeAIEmbeddings(
        model="models/embedding-001",
        google_api_key=key,
    )
    print("------------------")
    print(embeddings.embed_query("What is the meaning of life?"))
    # 
    # print("------------------")
    # print(embeddings.embed_documents(
    #     [
    #         "Today is Monday",
    #         "Today is Tuesday",
    #         "Today is April Fools day",
    #     ]
    # ))

# gemini_chat()
# gemini_embed()
# langchain_chat()
# FIXME 504
langchain_embed()
mrunhap commented 6 months ago

@Adi8885 I checked pip freeze and langchain-google-genai version is 1.0.5, maybe it's a network problem? But google's python package ebmedding worked 🤔

mrunhap commented 6 months ago

And I see other people get this error too:

https://www.reddit.com/r/AskProgramming/comments/1bk0e4h/gemini_api_giving_504_deadline_exceeded/

mrunhap commented 6 months ago

I give up on this, have to create a custom class with genai instead.