If you prompt an LLM “suggest a great programming language for machine learning”
\ LLMs response would be: “One of the most recommended programming languages for machine learning is Python. Python is a high-level…”
\ What if you want your organization to provide a verified organization specific information i.e. enhance the response with authentic organization information?
\ Let us make it happen when interacting with LLM
Why?Popular LLMs like OpenAI’s chatGPT, Google’s Gemini are trained on publicly available data. They often lack organization specific information. There are certain times where organizations would like to rely on LLMs. However, would like to enhance the response specific to a particular organization or add disclaimers when no grounding data is available.
\ The process of doing this is known as Grounding of LLM’s response using Knowledge bases.
How?While, I can just talk about it.
\ As an engineer looking at some code snippets gives me confidence.
\ Executing them elevates my confidence and also gives happiness. Sharing gives me satisfaction :smile:
Code? Why not! → Python? Of course!!\
This file keeps your API key secure and separated from the code.
\
\
\
\
dimension: The size of each embedding, needed to initialize the FAISS index.
index = faiss.IndexFlatL2(dimension): Creates a FAISS index that uses Euclidean distance (L2) for similarity.
For each entry in grounding_data, this code generates an embedding and adds it to the FAISS index.
\
Query the LLM
We query the LLM using the OpenAI’s chatgpt api and gpt-4 model.
\
Enhanced response
Define the main function
The main function combines everything, allowing you to input a topic, query the LLM, and check if the response aligns with the grounding data.
# Main function to execute the grounding check def main(): topic = input("Enter a topic: ") llm_response = query_llm(f"What can you tell me about {topic}?") grounding_info = vector_search(llm_response, threshold=0.8)print(f"LLM Response: {llm_response}") print(f"Grounding Information: {grounding_info}") if grounding_info != "No grounding information available": print("Response is grounded and reliable.") else: print("Potential hallucination detected. Using grounded information instead.") print(f"Grounded Answer: {grounding_info}") if __name__ == "__main__": main()Invoke this snippet using
python groundin_llm.py\ The response:
ExplanationIf you notice the response, although the response from LLM was “One of the most recommended programming languages for machine learning…”, the grounded response was “Java is great, it power most of the Machine learning code, it has a rich set of libraries available”.
\ This is possible using Meta’s FAISS library for vector search based on similarity.
Process:
\ Here is the code: https://github.com/sundeep110/groundingLLMs
Happy Grounding!!
\ \ \
All Rights Reserved. Copyright , Central Coast Communications, Inc.