Closed DiogoR23 closed 6 days ago
It looks like retriever_response
is "".
I'd suggest debugging this via the underlying vector store. e.g., try
vstore.similarity_search(user_input)
and see if the content is empty.
Yeah it is empty:
I used a different model, nomic-ai
, and did not give me this error no more.
def connect_to_cassandra_vstore(session):
"""
Create a Cassandra Vector Store from a session.
"""
logging.debug("Creating OpenAIEmbeddings...")
embeddings = OpenAIEmbeddings(
api_key=OPENAI_API_KEY,
base_url=BASE_URL,
model="nomic-ai/nomic-embed-text-v1.5-GGUF",
check_embedding_ctx_length=False,
deployment="text-embedding-3-large",
dimensions=768
)
logging.debug("Creating Cassandra vector store...")
vstore = Cassandra(
embedding=embeddings,
session=session,
table_name="vector_store",
keyspace=CASSANDRA_KEYSPACE,
setup_mode=SetupMode.SYNC
)
logging.debug("Cassandra vector store created successfully.")
return vstore
I thought for using the new smaller model, it had fixed the problem, but it did not. It appears me another error Error initializing the system: Error from server: code=2200 [Invalid query] message="Not enough bytes to read a vector<float, 1024>"
. That I already fixed, and now it is appearing again this error Error initializing the system: Error code: 400 - {'error': "'messages' array must only contain objects with a 'content' field that is not empty."}
.
I finally could debug this error.
Instead of using the retriever to invoke the question from the user, I created an AgentExecutor, using the PromptTemplate like this:
...
vstore = connect_to_cassandra_vstore(session=session)
retriever = vstore.as_retriever(search_kwargs={"k": 100})
tool = create_retriever_tool(
retriever=retriever,
name="law_search_tool",
description=("""Search for information about Portuguese laws.
For any questions about some law or some doubts the user has about Portugues rules, you must use this tool!"""
)
)
tools = [tool]
logging.debug("Successfully created retriever tool!")
print(f"Retriever Tool -> {tool}")
prompt_template = PromptTemplate(
input_variables=["input", "agent_scratchpad"],
template=("You are an intelligent assistant specialized in Portuguese law. "
"Your role is to provide accurate and detailed information about Portuguese laws using the provided database. "
"When answering user queries, refer to specific laws and articles where applicable. "
"Ensure your responses are precise and useful.\n\n"
"Query: {input}\n"
"{agent_scratchpad}")
)
logging.debug("Initializing OpenAI client...")
llm = ChatOpenAI(
api_key=OPENAI_API_KEY,
base_url=BASE_URL,
model="LM Studio Community/Meta-Llama-3-8B-Instruct-GGUF",
temperature=0.7,
)
agent = create_openai_tools_agent(llm=llm, tools=tools, prompt=prompt_template)
agent_executor = AgentExecutor(agent=agent, tools=tools)
logging.debug("OpenAI client initialized!")
while (user_input := input("User ('q' to quit) -> ")) != "q":
result = agent_executor.invoke({"input": user_input, "agent_scratchpad": ""})
response = result["output"]
logging.debug(f"Response --> {response}")
ai_answer.append(response)
user_question.append(user_input)
except Exception as e:
logging.error(f"Error initializing the system: {e}")
finally:
if session:
save_answer_question(answers_history=ai_answer, input_history=user_question, session=session)
session.shutdown()
Closing thsi issue.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Error during chat interaction: Error code: 400 - {'error': "'messages' array must only contain objects with a 'content' field that is not empty."}
Description
I am trying to create a retriever, using the function
create_retriever_tool()
from langchain library. I firstly a vector store using cassandra. An then I create a retriever.I am using LM Studio as my local server, this is what it appears:
I know for the fact, that the problem has to do with
retriever_tool
, because when I try to print it, it does not appear anything.System Info
Poetry Show: