Created
February 14, 2026 11:04
-
-
Save ABcDexter/e3a94bea3e62e28432d5803b2f96c34d to your computer and use it in GitHub Desktop.
GenAI LLM IBM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Imports | |
| from ibm_watsonx_ai.foundation_models import ModelInference | |
| from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams | |
| from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames | |
| from ibm_watsonx_ai import Credentials | |
| from langchain_ibm import WatsonxLLM, WatsonxEmbeddings | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| from langchain_community.vectorstores import Chroma | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain.chains import RetrievalQA | |
| from huggingface_hub import HfFolder | |
| import gradio as gr | |
| # You can use this section to suppress warnings generated by your code: | |
| def warn(*args, **kwargs): | |
| pass | |
| import warnings | |
| warnings.warn = warn | |
| warnings.filterwarnings('ignore') | |
| #Constants | |
| project_id = "skills-network" | |
| ## LLM | |
| def get_llm(): | |
| #model_id = 'ibm/granite-3-2-8b-instruct' | |
| model_id = "mistralai/mixtral-8x7b-instruct-v01" | |
| parameters = { | |
| GenParams.MAX_NEW_TOKENS: 256, # this controls the maximum number of tokens in the generated output | |
| GenParams.TEMPERATURE: 0.5, # this randomness or creativity of the model's responses | |
| } | |
| watsonx_llm = WatsonxLLM( | |
| model_id= "mistralai/mistral-medium-2505", #model_id, #"mistralai/mixtral-8x7b-instruct-v01", | |
| url="https://us-south.ml.cloud.ibm.com", | |
| project_id=project_id, | |
| params=parameters, | |
| ) | |
| return watsonx_llm | |
| ## Document loader | |
| def document_loader(file): | |
| loader = PyPDFLoader(file.name) | |
| loaded_document = loader.load() | |
| return loaded_document | |
| ## Text splitter | |
| def text_splitter(data): | |
| text_splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=1000, | |
| chunk_overlap=20, | |
| length_function=len, #just the funtcion | |
| ) | |
| chunks = text_splitter.split_documents(data) | |
| return chunks | |
| ## Embedding model | |
| def watsonx_embedding(): | |
| embed_params = { | |
| EmbedTextParamsMetaNames.TRUNCATE_INPUT_TOKENS: 3, | |
| EmbedTextParamsMetaNames.RETURN_OPTIONS: {"input_text": True}, | |
| } | |
| watsonx_embedding = WatsonxEmbeddings( | |
| model_id="ibm/slate-125m-english-rtrvr-v2", | |
| url="https://us-south.ml.cloud.ibm.com", | |
| project_id=project_id, | |
| params=embed_params, | |
| ) | |
| return watsonx_embedding | |
| ## Vector db | |
| def vector_database(chunks): | |
| embedding_model = watsonx_embedding() | |
| ids = [str(i) for i in range(0, len(chunks))] | |
| vectordb = Chroma.from_documents(chunks, embedding_model, ids=ids) | |
| return vectordb | |
| ## Retriever | |
| def retriever(file): | |
| splits = document_loader(file) | |
| chunks = text_splitter(splits) | |
| vectordb = vector_database(chunks) | |
| retriever = vectordb.as_retriever() | |
| return retriever | |
| ## QA Chain | |
| def retriever_qa(file, query): | |
| llm = get_llm() | |
| retriever_obj = retriever(file) | |
| qa = RetrievalQA.from_chain_type(llm=llm, | |
| chain_type="stuff", | |
| retriever=retriever_obj, | |
| return_source_documents=False) | |
| # find the response for given Query | |
| response = qa.invoke(query) | |
| return response['result'] | |
| # Create Gradio interface | |
| rag_application = gr.Interface( | |
| fn=retriever_qa, | |
| allow_flagging=True, | |
| inputs=[ | |
| gr.File(label="Upload PDF File", file_count="single", file_types=['.pdf'], type="filepath"), # Drag and drop file upload | |
| gr.Textbox(label="Input Query", lines=2, placeholder="Type your question here...") | |
| ], | |
| outputs=gr.Textbox(label="Text"), | |
| title="Anubhav project", | |
| description="Upload a PDF document and ask any question. The chatbot will try to answer using the provided document." | |
| ) | |
| # Launch the app | |
| rag_application.launch(server_name="127.0.0.1", server_port= 7860) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment