Hi,
I’m trying to put a feedback system into My RAG Application using streamlit, and I got unexpected error when I pass runnable_config into the invoke method : I will share with you the code first then the error :
import os
import streamlit as st
from langchain.callbacks.tracers.langchain import wait_for_all_tracers
from langchain.callbacks.tracers.run_collector import RunCollectorCallbackHandler
from langchain.memory import ConversationBufferMemory, StreamlitChatMessageHistory
from langchain.schema.runnable import RunnableConfig
from langsmith import Client
from streamlit_feedback import streamlit_feedback
# Set LangSmith environment variables
os.environ["LANGCHAIN_PROJECT"] = "my prject"
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
langchain_endpoint = "https://api.smith.langchain.com"
if "last_run" not in st.session_state:
st.session_state["last_run"] = "some_initial_value"
def ask_question(chain, query,runnable):
response = chain.invoke(
{"question": query},
config={"configurable": {"session_id": "aqw123","runnable":
runnable}}
)
return response
def get_chain(groq_api_key=None, huggingfacehub_api_token=None):
ensemble_retriever = get_retriever(groq_api_key=groq_api_key)
chain = create_full_chain(ensemble_retriever,
groq_api_key=groq_api_key,
chat_memory=StreamlitChatMessageHistory(key="langchain"))
def run():
if "trace_link" not in st.session_state:
st.session_state.trace_link = None
if "run_id" not in st.session_state:
st.session_state.run_id = None
#Tracer
run_collector = RunCollectorCallbackHandler()
runnable_config = RunnableConfig(
callbacks=[run_collector],
tags=["Streamlit Chat"],
)
if st.session_state.trace_link:
st.sidebar.markdown(
f'<a href="{st.session_state.trace_link}" target="_blank"><button>Latest
Trace: 🛠️</button></a>',
unsafe_allow_html=True,
)
has_chat_messages = len(st.session_state.get("langchain", [])) > 0
# Only show the feedback toggle if there are chat messages
if has_chat_messages:
feedback_option = (
"faces" if st.toggle(label="`Thumbs` ⇄ `Faces`", value=False) else "thumbs"
)
else:
pass
show_ui(chain, "what do you want to know ?",langchain_api_key=langchain_api_key,run_collector=run_collector,runnable_config=runnable_config)
and finnaly the show_ui method :
def show_ui(qa, prompt_to_user="How may I help you?",langchain_api_key=None,run_collector=None,runnable_config=None):
client = Client(api_url=langchain_endpoint, api_key=langchain_api_key)
# Generate a new response if last message is not from assistant
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("réfléchir..."):
response = ask_question(qa, prompt,runnable_config)
if st.session_state["agree"] == True:
removed_tag = removeThinkTags(response.content)
else:
removed_tag = response.content
# print(response.content)
st.markdown(removed_tag)
# The run collector will store all the runs in order. We'll just take the root and then
# reset the list for next interaction.
st.write(runnable_config,run_collector)
run = run_collector.traced_runs[0]
run_collector.traced_runs = []
st.session_state.run_id = run.id
wait_for_all_tracers()
# Requires langsmith >= 0.0.19
url = client.share_run(run.id)
# Or if you just want to use this internally
# without sharing
# url = client.read_run(run.id).url
st.session_state.trace_link = url
message = {"role": "assistant", "content": removed_tag}
st.session_state.messages.append(message)
and the error is :
IndexError: list index out of range at this line
run = run_collector.traced_runs[0]
is the way I pass runnable_config inside ask_question method is correct please ?