Spaces:
Sleeping
Sleeping
File size: 1,940 Bytes
16d5a75 031378e 16d5a75 744b763 16d5a75 031378e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | from langchain_core.tools import tool
from src.config.vector_store import test_rag_vector_store
from src.utils.helper import convert_list_context_source_to_str
from src.utils.logger import logger
from langchain_core.runnables import RunnableConfig
from langchain_experimental.utilities import PythonREPL
from langchain_community.tools import DuckDuckGoSearchRun
duckduckgo_search = DuckDuckGoSearchRun()
python_exec = PythonREPL()
@tool
def retrieve_document(query: str, config: RunnableConfig):
"""Ưu tiên truy xuất tài liệu từ vector store nếu câu hỏi liên quan đến vai trò của chatbot.
Args:
query (str): Câu truy vấn của người dùng bằng tiếng Việt
Returns:
str: Retrieved documents
"""
configuration = config.get("configurable", {})
bot_id = configuration.get("bot_id", None)
if not bot_id:
logger.error("Bot ID is not found")
return {"context_str": "", "selected_documents": [], "selected_ids": []}
retriever = test_rag_vector_store.as_retriever(
search_type="similarity_score_threshold",
search_kwargs={"k": 5, "score_threshold": 0.3},
)
documents = retriever.invoke(query, filter={"bot_id": bot_id})
selected_documents = [doc.__dict__ for doc in documents]
selected_ids = [doc["id"] for doc in selected_documents]
context_str = convert_list_context_source_to_str(documents)
return {
"context_str": context_str,
"selected_documents": selected_documents,
"selected_ids": selected_ids,
}
@tool
def python_repl(code: str):
"""
A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.
Args:
code (str): Python code to execute
Returns:
str: Output of the Python code
"""
return python_exec.run(code)
|