1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Retrying...

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am using langchain to embed with google palm embedding. I worked on this project around 6 months ago, but when I'm trying to run it now it keeps generating this error


    " Retrying langchain_community.embeddings.google_palm.embed_with_retry.._embed_with_retry in 8.0 seconds as it raised NotFound: 404 Requested entity was not found.. "
    ​

    what I have deducted it the main issue arise from this part of the code:


    llm = GoogleGenerativeAI(model="gemini-1.5-pro")
    ​

    Here is the following code which is responsible for it:

    def get_chain(db, question):

    os.environ['LANGCHAIN_TRACING_V2']='true'
    os.environ["LANGCHAIN_PROJECT"] = 'Sample Agent Trace'
    os.environ['LANGCHAIN_API_KEY']='key'
    os.environ['GOOGLE_API_KEY']='key'

    llm = GoogleGenerativeAI(model="gemini-1.5-pro")


    generate_query = create_sql_query_chain(llm, db) # converts natural language to sql query
    execute_query=QuerySQLDataBaseTool(db=db)
    answer_prompt = PromptTemplate.from_template(
    """Given the following user question, corresponding SQL query, and SQL result, Generate a full-fleged natural language sentence with as if you are telling someone containing the actual output.
    Question: {question}
    SQL Query: {query}
    SQL Result: {result}
    Answer: """
    )

    rephrase_answer = answer_prompt | llm | StrOutputParser()
    chain = (
    RunnablePassthrough.assign(query=generate_query).assign(
    result=itemgetter("query") | execute_query
    )
    | rephrase_answer)

    examples = [" ".join(example.values()) for example in few_shots]
    embeddings=GooglePalmEmbeddings(google_api_key=os.getenv('GOOGLE_API_KEY'))
    vectorstore=Chroma.from_texts(examples,embedding=embeddings,metadatas=few_shots)
    example_selector=SemanticSimilarityExampleSelector(
    vectorstore=vectorstore,
    k=1
    )

    mysql_prompt = """
    prompt
    """
    example_prompt = PromptTemplate(
    input_variables=["Question", "SQLQuery", "SQLResult","Answer",],
    template="\nQuestion: {Question}\nSQLQuery: {SQLQuery}\nSQLResult: {SQLResult}\nAnswer: {Answer}",
    )
    few_shot_prompt = FewShotPromptTemplate(
    example_selector=example_selector,
    example_prompt=example_prompt,
    prefix=mysql_prompt,
    suffix=PROMPT_SUFFIX,
    input_variables=["input", "table_info", "top_k"], #These variables are used in the prefix and suffix
    )
    generate_query = create_sql_query_chain(llm, db,few_shot_prompt)
    chain = (
    RunnablePassthrough.assign(query=generate_query).assign(
    result=itemgetter("query") | execute_query
    )
    | rephrase_answer
    )

    return chain.invoke({"question": question})


    here are the imports I have used for the solution:

    from langchain_google_genai import GoogleGenerativeAI
    from langchain_community.utilities.sql_database import SQLDatabase
    from langchain.chains import create_sql_query_chain
    from langchain_community.tools import QuerySQLDataBaseTool
    from operator import itemgetter
    from langchain_core.output_parsers import StrOutputParser
    from langchain_core.prompts import PromptTemplate
    from langchain_core.runnables import RunnablePassthrough
    from server.few_shorts import few_shots
    from langchain_community.embeddings import GooglePalmEmbeddings
    from langchain_community.vectorstores import Chroma
    from langchain.prompts import SemanticSimilarityExampleSelector
    from langchain.prompts import FewShotPromptTemplate
    from langchain.chains.sql_database.prompt import PROMPT_SUFFIX
    from langchain.prompts.prompt import PromptTemplate
    from langchain.prompts import FewShotPromptTemplate

    Continue reading...

Compartilhe esta Página