Download utils.py from mayur1811/QAChatBot: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/spaces/mayur1811/QAChatBot/resolve/main/utils.py
- Command line
-
hf download hf://spaces/mayur1811/QAChatBot/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/mayur1811/QAChatBot/resolve/main/utils.py
2.17 kB
| from langchain.embeddings import SentenceTransformerEmbeddings | |
| import google.generativeai as genai | |
| import streamlit as st | |
| import os | |
| from langchain.vectorstores import Chroma | |
| import pickle | |
| from transformers import AutoTokenizer | |
| # Load tokenizer and ensure padding token is set | |
| tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") | |
| if tokenizer.pad_token is None: | |
| tokenizer.pad_token = tokenizer.eos_token or tokenizer.unk_token | |
| # Configure API key | |
| GOOGLE_API_KEY = os.getenv('Google_API_KEY') | |
| os.environ['Google_API_KEY'] = GOOGLE_API_KEY | |
| genai.configure(api_key=os.environ['Google_API_KEY']) | |
| # Load sentence transformer model | |
| model = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2") | |
| def setup(): | |
| embedding = pickle.load(open('embedding.pkl', 'rb')) | |
| document = pickle.load(open('document.pkl', 'rb')) | |
| db = Chroma.from_documents(document, embedding) | |
| return db | |
| def find_match(input_text): | |
| db = setup() | |
| matching_doc = db.similarity_search_with_score(input_text, k=2) | |
| return matching_doc[0][0].page_content + matching_doc[1][0].page_content | |
| def query_refiner(conversation, query): | |
| generation_config = { | |
| "temperature": 1, | |
| "top_p": 0.95, | |
| "top_k": 64, | |
| "max_output_tokens": 8192, | |
| } | |
| model = genai.GenerativeModel( | |
| model_name="gemini-1.5-flash", | |
| generation_config=generation_config, | |
| system_instruction=f"Your name is Sam, given the following user query and conversation log, formulate a question that would be the most relevant to provide the user with an answer from a knowledge base.\n\nCONVERSATION LOG: \n{conversation}\n\nQuery: {query}\n\nRefined Query:", | |
| ) | |
| chat_session = model.start_chat(history=[]) | |
| response = chat_session.send_message(f"Conversation Log:{conversation} Query:{query}") | |
| return response.text | |
| def get_conversation_string(): | |
| conversation_string = "" | |
| for i in range(len(st.session_state['responses']) - 1): | |
| conversation_string += "Human: " + st.session_state['requests'][i] + "\n" | |
| conversation_string += "Bot: " + st.session_state['responses'][i + 1] + "\n" | |
| return conversation_string | |