|
import os |
|
from langchain_huggingface import HuggingFaceEndpoint |
|
from langchain_core.runnables import RunnablePassthrough |
|
import app.schemas as schemas |
|
from app.prompts import ( |
|
raw_prompt, |
|
raw_prompt_formatted, |
|
history_prompt_formatted, |
|
standalone_prompt_formatted, |
|
format_context, |
|
rag_prompt_formatted, |
|
tokenizer |
|
) |
|
from app.data_indexing import DataIndexer |
|
|
|
data_indexer = DataIndexer() |
|
|
|
llm = HuggingFaceEndpoint( |
|
repo_id="meta-llama/Meta-Llama-3-8B-Instruct", |
|
huggingfacehub_api_token=os.environ['HF_TOKEN'], |
|
max_new_tokens=512, |
|
stop_sequences=[tokenizer.eos_token], |
|
streaming=True, |
|
) |
|
|
|
simple_chain = (raw_prompt | llm).with_types(input_type=schemas.UserQuestion) |
|
|
|
|
|
formatted_chain = (raw_prompt_formatted | llm).with_types(input_type=schemas.UserQuestion) |
|
|
|
|
|
history_chain = (history_prompt_formatted | llm).with_types(input_type=schemas.HistoryInput) |
|
|
|
|
|
standalone_chain = (standalone_prompt_formatted | llm).with_types(input_type=schemas.HistoryInput) |
|
|
|
input_1 = RunnablePassthrough.assign(new_question=standalone_chain) |
|
input_2 = { |
|
'context': lambda x: format_context(data_indexer.search(x['new_question'])), |
|
'standalone_question': lambda x: x['new_question'] |
|
} |
|
input_to_rag_chain = input_1 | input_2 |
|
|
|
|
|
|
|
rag_chain = (input_to_rag_chain | rag_prompt_formatted | llm).with_types(input_type=schemas.HistoryInput) |
|
|
|
|
|
|
|
|
|
input_1 = RunnablePassthrough.assign(new_question=standalone_chain) |
|
input_2 = { |
|
'context': lambda x: format_context(data_indexer.search(x['new_question'], hybrid_search=True)), |
|
'standalone_question': lambda x: x['new_question'] |
|
} |
|
filtered_input_to_rag_chain = input_1 | input_2 |
|
filtered_rag_chain = (filtered_input_to_rag_chain | rag_prompt_formatted | llm).with_types(input_type=schemas.HistoryInput) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|