Help me with the error I am getting in response type 🙏🏻 #14296
Replies: 1 comment
-
Hey there, @JaySingh23! I'm here to assist you with any bugs, questions, or contributions you might have. Let's tackle this issue together! To fix the "UNEXPECTED RESPONSE FORMAT" error in your
You can use the def extract_final_response(input_text: str) -> tuple:
thought_prefix = "Thought: "
answer_prefix = "Answer: "
thought_start = input_text.find(thought_prefix)
answer_start = input_text.find(answer_prefix)
if thought_start == -1 or answer_start == -1:
raise ValueError("UNEXPECTED RESPONSE FORMAT")
thought_end = answer_start
thought = input_text[thought_start + len(thought_prefix):thought_end].strip()
answer = input_text[answer_start + len(answer_prefix):].strip()
return thought, answer You should then modify your def get_answer_from_text(document_id: str, question: str, conversation_history: List[dict]) -> str:
try:
embed_model = HuggingFaceEmbedding(
model_name="BAAI/bge-small-en-v1.5"
)
llm = HuggingFaceInferenceAPI(model_name="mistralai/Mistral-7B-Instruct-v0.3", token=access_token)
logger.info(llm)
reader = SimpleDirectoryReader(input_files=[os.path.join(UPLOAD_DIR, f"{document_id}.txt")])
documents = reader.load_data()
# Combine conversation history with the new question
combined_text = "\n".join([f"Q: {entry['question']}\nA: {entry['answer']}" for entry in conversation_history])
combined_text += f"\nQ: {question}"
logger.info(combined_text)
# Create the index
logger.info('Creating index')
index = VectorStoreIndex.from_documents(
documents,
embed_model=embed_model, llm=llm
)
logger.info('Index created')
# Query the index with the combined text for a concise answer
query_engine = index.as_query_engine(llm=llm)
response = query_engine.query(combined_text)
logger.info(f"Full response: {response}")
# Extract the final response
thought, answer = extract_final_response(response)
return answer
except Exception as e:
# Handle any other exceptions from LlamaIndex
print(f"Error getting answer: {e}")
return "Sorry, I encountered an error while processing your question." Ensure that the response from |
Beta Was this translation helpful? Give feedback.
-
Hi, So I am building an application which takes in a pdf file and then one can query whatever question he wants with the help of that pdf. I am facing a problem in returning the answer. As per my code, I have given a string type for the answer in which it should be returned but somehow it is telling me that it is in "UNEXPECTED RESPONSE FORMAT . I am not able to understand how to fix this error. Can someone please help me? The problem lies in the method get_answer_from_text(it is at the bottom).
Beta Was this translation helpful? Give feedback.
All reactions