Spaces:
Sleeping
Sleeping
Update src/qabot.py
Browse files10 unmodified lines1112131415161718192021222324252627282930313233343536373839404142434445464748492 unmodified lines52535455565758596061626364656667686957 unmodified lines12712812913013113213327 unmodified lines16116216316416516616710 unmodified linesfrom langchain_core.prompts import PromptTemplatefrom langchain_core.runnables import RunnablePassthroughfrom langchain_core.output_parsers import StrOutputParserfrom langchain_ibm import ChatWatsonx, WatsonxEmbeddingsfrom ibm_watsonx_ai.foundation_models.utils.enums import ModelTypes, EmbeddingTypesfrom ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParamsfrom ibm_watsonx_ai.foundation_models.utils.enums import DecodingMethods# ---------------------------------------------------------------------------# 1. LLM Initialization# ---------------------------------------------------------------------------def init_llm() -> ChatWatsonx:"""Initialize the IBM Watsonx LLM (Mixtral 8x7B Instruct).Reads credentials from environment variables:WATSONX_API_KEY – your IBM Cloud API keyWATSONX_PROJECT_ID – your Watsonx project IDWATSONX_URL – IBM Cloud endpoint (default provided)Returns:ChatWatsonx instance ready for inference."""parameters = {"max_new_tokens": 500,"min_new_tokens": 1,}llm = ChatWatsonx(model_id="meta-llama/llama-3-3-70b-instruct",url=os.getenv("WATSONX_URL", "https://us-south.ml.cloud.ibm.com"),project_id=os.getenv("WATSONX_PROJECT_ID"),apikey=os.getenv("WATSONX_API_KEY"),params=parameters,)return llm2 unmodified lines# 2. Embedding Model# ---------------------------------------------------------------------------def watsonx_embedding() -> WatsonxEmbeddings:"""Initialize IBM Watsonx Embeddings (Slate 125M English).Returns:WatsonxEmbeddings instance."""embeddings = WatsonxEmbeddings(model_id="ibm/slate-30m-english-rtrvr-v2",url=os.getenv("WATSONX_URL", "https://us-south.ml.cloud.ibm.com"),project_id=os.getenv("WATSONX_PROJECT_ID"),apikey=os.getenv("WATSONX_API_KEY"),)return embeddings57 unmodified linesReturns:Chroma vector store instance."""embedding_model = watsonx_embedding()vector_store = Chroma.from_documents(documents=chunks,embedding=embedding_model,27 unmodified lines# 7. QA Chain# ---------------------------------------------------------------------------def build_qa_chain(retriever, llm: ChatWatsonx):"""Build a QA chain combining the retriever and LLM using LCEL.10 unmodified lines111213141516171819202122232425262728293031323334353637382 unmodified lines41424344454647484950515253545557 unmodified lines11311411511611711811927 unmodified lines14714814915015115215310 unmodified linesfrom langchain_core.prompts import PromptTemplatefrom langchain_core.runnables import RunnablePassthroughfrom langchain_core.output_parsers import StrOutputParserfrom langchain_groq import ChatGroqfrom langchain_huggingface import HuggingFaceEmbeddings# ---------------------------------------------------------------------------# 1. LLM Initialization# ---------------------------------------------------------------------------def init_llm() -> ChatGroq:"""Initialize the Groq LLM (Llama 3 70B Instruct).Reads credentials from environment variables:GROQ_API_KEY – your Groq API keyReturns:ChatGroq instance ready for inference."""llm = ChatGroq(model="llama-3.3-70b-versatile",api_key=os.getenv("GROQ_API_KEY"),temperature=0.1)return llm2 unmodified lines# 2. Embedding Model# ---------------------------------------------------------------------------def get_embedding_model() -> HuggingFaceEmbeddings:"""Initialize HuggingFace Local Embeddings (all-MiniLM-L6-v2).Returns:HuggingFaceEmbeddings instance."""embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")return embeddings57 unmodified linesReturns:Chroma vector store instance."""embedding_model = get_embedding_model()vector_store = Chroma.from_documents(documents=chunks,embedding=embedding_model,27 unmodified lines# 7. QA Chain# ---------------------------------------------------------------------------def build_qa_chain(retriever, llm: ChatGroq):"""Build a QA chain combining the retriever and LLM using LCEL.
src/qabot.py+18-32