Parameter control of chained llm and retriever from Gradio interface #30836
-
Checked other resources
Commit to Help
Example CodeTEMPERATURE = 0.6
RELEVANCE_SCORE = 0.7
llm = LlamaCpp(
model_path=model_path,
n_gpu_layers=-1,
stop=["Question:", "Answer:"],
n_ctx=8192,
temperature=TEMPERATURE,
verbose=False,
) DescriptionHi folks ! I need to know how to change parameters of chained llm and retriever from Gradio interface. llm is loaded like; TEMPERATURE = 0.6
RELEVANCE_SCORE = 0.7
llm = LlamaCpp(
model_path=model_path,
n_gpu_layers=-1,
stop=["Question:", "Answer:"],
n_ctx=8192,
temperature=TEMPERATURE,
verbose=False,
) and chained with retriever like; chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=index.as_retriever(
search_type="similarity_score_threshold",
search_kwargs={'score_threshold': RELEVANCE_SCORE, 'k': 2},
),
chain_type_kwargs={"prompt": QUESTION_PROMPT},
chain_type="stuff",
return_source_documents=True
) chat function for Gradio is; def chat(message, history, REL_SCORE, TEMP):
response = chain.invoke(messages)
for i, source in enumerate(response["source_documents"], 1):
print(f"\nindex: {i}----------------------------------------------------")
print(f"{source.page_content}")
print("---------------------------------------------------------------")
response_result = response["result"]
yield response_result Gradio interface is as below; demo = gr.ChatInterface(fn=chat,
title='RAG-Chat',
type='messages',
additional_inputs=[
gr.Slider(0.6, 1.0, value=RELEVANCE_SCORE, step=0.01, label="rel_score", visible=True),
gr.Slider(0.0, 1.0, value=TEMPERATURE, step=0.01, label="Temperature", visible=True),
]
)
demo.queue().launch() Now no error is given, however parameters are not changed. How is it possible to change parameters from Gradio's 'additional_inputs' ? Environment:python3.10.16 on conda for Windows11Pro Thanks in advance. System InfoEnvironment: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@SwHaraday, the problem seems to be inside your def chat(message, history, REL_SCORE, TEMP):
llm.temperature = TEMP
updated_retriever = index.as_retriever(
search_type="similarity_score_threshold",
search_kwargs={'score_threshold': REL_SCORE, 'k': 2},
)
updated_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=updated_retriever,
chain_type_kwargs={"prompt": QUESTION_PROMPT},
chain_type="stuff",
return_source_documents=True
)
response = updated_chain.invoke({"query": message})
for i, source in enumerate(response["source_documents"], 1):
print(f"\nIndex: {i} ----------------------------------------------------")
print(f"{source.page_content}")
print("---------------------------------------------------------------")
response_result = response["result"]
yield response_result Additionally, it is worth noting that while defining the from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
index = FAISS.from_documents(docs, embedding_model) In this code, it's important to note that the |
Beta Was this translation helpful? Give feedback.
@SwHaraday, there were a few small bugs in my previous comment, so disregard it and consider this one instead. I have updated the code. After defining your
index
variable as your vector store and setting themodel_path
andQUESTION_PROMPT
variables, running the code below will likely resolve the issue of the two mentioned hyperparameters not updating within your interface. What has been done is that thellm
andretriever
are initialized outside thechat
function. Inside thechat
function, the temperature of thellm
is set first, followed by the score threshold of theretriever
. Finally, the chain is created based on this updatedllm
andretriever
, followed by the invocation.