I’m building a retrieval augmented generation system with LangChain, OpenAI models, and a web interface. My setup uses the “map_rerank” approach for document processing.
I can successfully get answers and source documents, but I’m struggling to capture the similarity scores from my vector search. The scores show up in my console logs but I can’t figure out how to access them programmatically to display in my UI.
Here’s my setup code:
@on_chat_start
def setup():
model = AzureChatOpenAI(
deployment_name=config.MODEL_DEPLOYMENT,
model_name=config.MODEL_NAME,
openai_api_base=config.ENDPOINT_URL,
openai_api_version=config.API_VERSION,
openai_api_key=config.API_KEY,
temperature=0.3,
streaming=True
)
embed_model = OpenAIEmbeddings(
deployment=config.EMBEDDING_DEPLOYMENT,
model=config.EMBEDDING_MODEL,
openai_api_base=config.ENDPOINT_URL,
openai_api_key=config.API_KEY,
chunk_size=1000
)
vector_store = FAISS.load_local(
config.VECTOR_DB_PATH,
embed_model
)
document_retriever = vector_store.as_retriever(
search_type="similarity_score_threshold",
search_kwargs={"score_threshold": 0.4, "k": 4}
)
query_chain = LLMChain(
llm=model,
prompt=QUESTION_PROMPT
)
answer_chain = load_qa_with_sources_chain(
model,
chain_type="map_rerank",
return_intermediate_steps=False
)
chat_memory = ConversationBufferMemory(
llm=model,
memory_key="history",
return_messages=True,
input_key="query",
output_key="response"
)
rag_chain = ConversationalRetrievalChain(
retriever=document_retriever,
question_generator=query_chain,
combine_docs_chain=answer_chain,
return_source_documents=True,
memory=chat_memory
)
cl.user_session.set("rag_chain", rag_chain)
And here’s how I process messages:
@on_message
async def handle_message(user_input: str):
history = []
rag_chain = cl.user_session.get("rag_chain")
result = rag_chain({"query": user_input, "history": history})
doc_sources = [doc.metadata.get("source") for doc in result["source_documents"]]
await cl.Message(content=f'Response: {result["response"]}, Sources: {set(doc_sources)}').send()
I need to extract the actual similarity scores that are being calculated during the search process. The scores appear in terminal output but I want to capture them and show them to users. How can I access these score values from the retrieval chain?