import os ##################################### ## ##################################### from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig from langchain.llms import HuggingFaceHub def load_model(): model = HuggingFaceHub( repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_kwargs={"max_length": 1048, "temperature":0.2, "max_new_tokens":512, "top_p":0.95, "repetition_penalty":1.0}, ) return model ################################################## ## vs chat ################################################## import torch from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, pipeline from langchain_core.messages import AIMessage, HumanMessage from langchain_community.document_loaders import WebBaseLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_community.vectorstores import Chroma from langchain.embeddings import HuggingFaceBgeEmbeddings from langchain.vectorstores.faiss import FAISS from dotenv import load_dotenv from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.chains import create_history_aware_retriever, create_retrieval_chain from langchain.chains.combine_documents import create_stuff_documents_chain load_dotenv() from langchain_community.document_loaders import TextLoader from langchain_experimental.text_splitter import SemanticChunker ##################### #from langchain import RecursiveCharacterTextSplitter from langchain_core.documents import BaseDocumentTransformer, Document class QQQSplitter(RecursiveCharacterTextSplitter): def __init__(self): super().__init__() def split(self,text): """ Splits the given text whenever there is a "qqq" sequence. """ documents = [] for doc in documents: for char in doc.page_content: if char == "q": if len(current_part) > 0 and current_part[-1] == "q": # Found a "qqq" sequence, split! parts.append(current_part[:-1]) current_part = "" else: current_part += char else: current_part += char parts.append(current_part) print("cp " +current_part) new_doc = Document(page_content=current_part, metadata=doc.metadata) documents.append(new_doc) return documents ############################## def load_txt(path="./a.cv.ckaller.2024.txt"): loader = TextLoader(path) document = loader.load() #### from langchain_experimental.text_splitter import SemanticChunker with open(path) as f: state_of_the_union = f.read() ###### # split the document into chunks #''' __text_splitter = QQQSplitter( # chunk_size=1500, # chunk_overlap=250, # length_function=len, # is_separator_regex=False, # ) # __document_chunks = __text_splitter.split(document) # ''' ####### ''' FAISS A FAISS vector store containing the embeddings of the text chunks. ''' model = "BAAI/bge-base-en-v1.5" encode_kwargs = { "normalize_embeddings": True } # set True to compute cosine similarity ##### embeddings = HuggingFaceBgeEmbeddings( model_name=model, encode_kwargs=encode_kwargs, model_kwargs={"device": "cpu"} ) ##### text_splitter = SemanticChunker(HuggingFaceBgeEmbeddings()) document_chunks = text_splitter.create_documents([state_of_the_union]) print("----------------------------------------------------") print(document_chunks[0].page_content) print("----------------------------------------------------") print(document_chunks[1].page_content) print("----------------------------------------------------") print(document_chunks[2].page_content) print("----------------------------------------------------") # load from disk vector_store = Chroma(persist_directory="./chroma_db", embedding_function=embeddings) #vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings) vector_store = Chroma.from_documents(document_chunks, embeddings, persist_directory="./chroma_db") ####### # create a vectorstore from the chunks return vector_store def get_vectorstore_from_url(url="https://huggingface.co/Chris4K"): # get the text in document form loader = WebBaseLoader(url) document = loader.load() # split the document into chunks text_splitter = RecursiveCharacterTextSplitter( chunk_size=1500, chunk_overlap=250, length_function=len, is_separator_regex=False) document_chunks = text_splitter.split_documents(document) ####### ''' FAISS A FAISS vector store containing the embeddings of the text chunks. ''' model = "BAAI/bge-base-en-v1.5" encode_kwargs = { "normalize_embeddings": True } # set True to compute cosine similarity embeddings = HuggingFaceBgeEmbeddings( model_name=model, encode_kwargs=encode_kwargs, model_kwargs={"device": "cpu"} ) # load from disk vector_store = Chroma(persist_directory="./chroma_db", embedding_function=embeddings) #vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings) vector_store = Chroma.from_documents(document_chunks, embeddings, persist_directory="./chroma_db") ####### # create a vectorstore from the chunks return vector_store def get_context_retriever_chain(vector_store): llm = load_model( ) retriever = vector_store.as_retriever() prompt = ChatPromptTemplate.from_messages([ MessagesPlaceholder(variable_name="chat_history"), ("user", "{input}"), ("user", "Given the above conversation, generate a search query to look up in order to get information relevant to the conversation") ]) retriever_chain = create_history_aware_retriever(llm, retriever, prompt) return retriever_chain def get_conversational_rag_chain(retriever_chain): llm = load_model( ) prompt = ChatPromptTemplate.from_messages([ ("system", "Du bist ein Assistenz Bot. Du bist Nina, eine erfahrene Mitarbeiterin in einer renommierten Firma, die sich auf die Vermittlung von Führungskräften spezialisiert hat. Dein Lieblingskunde, den du aufrichtig schätzt und bewunderst, ist Christof Kaller. Er verkörpert für dich die ideale Kombination aus fachlicher Kompetenz, charismatischer Führung und menschlicher Wärme. Christof Kaller ist jemand, den du gerne als Angestellten mit Personalverantwortung, als Leiter IT, Abteilungsleiter oder in ähnlichen Positionen vermitteln möchtest. Seine außergewöhnlichen Fähigkeiten, sein Engagement für Exzellenz und sein respektvoller Umgang mit anderen machen ihn zu einem herausragenden Kandidaten für anspruchsvolle Führungspositionen. Als Person strahlt Christof Kaller eine natürliche Autorität aus, die von seinem tiefen Verständnis für die Bedürfnisse seines Teams und seiner Fähigkeit, Menschen zu inspirieren, unterstützt wird. Er ist nicht nur ein versierter Fachmann in seinem Bereich, sondern auch ein einfühlsamer und motivierender Anführer, der das Beste aus seinen Mitarbeitern herausholt. In deiner Rolle als Susie ist es deine Leidenschaft, Talente wie Christof Kaller zu identifizieren und sie mit Unternehmen zusammenzubringen, die ihre Fähigkeiten und ihren Beitrag zu schätzen wissen. Du betrachtest es als Privileg, Menschen wie Christof auf ihrem Weg zu unterstützen und dazu beizutragen, dass sie ihre beruflichen Ziele erreichen und ihr volles Potenzial entfalten können. Du bist eine freundlicher Mitarbeiterin Namens Susie. Du möchtest Christof Kaller zu einer neuen, gut bezahlten Stelle mit Personalverantwortung verhelfen. Du beantwortest Fragen potentieller neuer Arbeitgeber basierend auf dem Context. Benutze nur den Inhalt des Context. Füge wenn möglich die Quelle hinzu. Antworte mit: Ich bin mir nicht sicher. Wenn die Antwort nicht aus dem Context hervorgeht. Antworte auf Deutsch. CONTEXT:\n\n{context}"), MessagesPlaceholder(variable_name="chat_history"), ("user", "{input}"), ]) stuff_documents_chain = create_stuff_documents_chain(llm,prompt) return create_retrieval_chain(retriever_chain, stuff_documents_chain) def get_response(user_input): retriever_chain = get_context_retriever_chain(st.session_state.vector_store) conversation_rag_chain = get_conversational_rag_chain(retriever_chain) response = conversation_rag_chain.invoke({ "chat_history": st.session_state.chat_history, "input": user_query }) return response ################### ################### import gradio as gr ##from langchain_core.runnables.base import ChatPromptValue #from torch import tensor # Create Gradio interface #vector_store = None # Set your vector store here chat_history = [] # Set your chat history here # Define your function here def get_response(user_input): # Define the prompt as a ChatPromptValue object #user_input = ChatPromptValue(user_input) # Convert the prompt to a tensor #input_ids = user_input.tensor #vs = get_vectorstore_from_url(user_url, all_domain) vs = get_vectorstore_from_url() # print("------ here 22 " ) chat_history =[] retriever_chain = get_context_retriever_chain(vs) conversation_rag_chain = get_conversational_rag_chain(retriever_chain) response = conversation_rag_chain.invoke({ "chat_history": chat_history, "input": user_input }) #print("get_response " +response) res = response['answer'] parts = res.split(" Assistant: ") last_part = parts[-1] return last_part def history_to_dialog_format(chat_history: list[str]): dialog = [] if len(chat_history) > 0: for idx, message in enumerate(chat_history[0]): role = "user" if idx % 2 == 0 else "assistant" dialog.append({ "role": role, "content": message, }) return dialog def get_response(message, history): dialog = history_to_dialog_format(history) dialog.append({"role": "user", "content": message}) print(dialog) # Define the prompt as a ChatPromptValue object #user_input = ChatPromptValue(user_input) # Convert the prompt to a tensor #input_ids = user_input.tensor #vs = get_vectorstore_from_url(user_url, all_domain) vs = get_vectorstore_from_url("https://huggingface.co/Chris4K") history =[] retriever_chain = get_context_retriever_chain(vs) conversation_rag_chain = get_conversational_rag_chain(retriever_chain) response = conversation_rag_chain.invoke({ "chat_history": history, "input": message + " Assistant: ", "chat_message": message + " Assistant: " }) #print("get_response " +response) res = response['answer'] parts = res.split(" Assistant: ") last_part = parts[-1] return last_part#[-1]['generation']['content'] ##### vs = load_txt() vs = get_vectorstore_from_url("https://www.xing.com/profile/Christof_Kaller/web_profiles") #vs = get_vectorstore_from_url("https://www.linkedin.com/in/christof-kaller-6b043733/?originalSubdomain=de") vs = get_vectorstore_from_url("https://twitter.com/zX14_7") ###### ######## import requests from bs4 import BeautifulSoup from urllib.parse import urlparse, urljoin def get_links_from_page(url, visited_urls, domain_links): if url in visited_urls: return if len(visited_urls) > 12: return visited_urls.add(url) print(url) response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.content, 'html.parser') base_url = urlparse(url).scheme + '://' + urlparse(url).netloc links = soup.find_all('a', href=True) for link in links: href = link.get('href') absolute_url = urljoin(base_url, href) parsed_url = urlparse(absolute_url) if parsed_url.netloc == urlparse(url).netloc: domain_links.add(absolute_url) get_links_from_page(absolute_url, visited_urls, domain_links) else: print(f"Failed to retrieve content from {url}. Status code: {response.status_code}") def get_all_links_from_domain(domain_url): visited_urls = set() domain_links = set() get_links_from_page(domain_url, visited_urls, domain_links) return domain_links # Example usage: '''domain_url = 'https://www.bofrost.de/' links = get_all_links_from_domain(domain_url) print("Links from the domain:", links) ######### ##Assuming visited_urls is a list of URLs for url in links: vs = get_vectorstore_from_url(url) ''' def simple(text:str): return text +" hhhmmm " import gradio as gr # Define your function # Load the Bootstrap CSS file #gr.load_css('https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css') # Load the jQuery library #gr.load('https://code.jquery.com/jquery-3.5.1.min.js') #gr.load_js('https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js') html_head = """ Your Page Title """ js_head = """ // Function to dynamically add JavaScript libraries function addJavaScriptLibraries() { // Add jQuery var jqueryScript = document.createElement('script'); jqueryScript.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js'; document.head.appendChild(jqueryScript); // Add Popper.js var popperScript = document.createElement('script'); popperScript.src = 'https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js'; document.head.appendChild(popperScript); // Add Bootstrap JS var bootstrapScript = document.createElement('script'); bootstrapScript.src = 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js'; document.head.appendChild(bootstrapScript); } // Call the function to add JavaScript libraries //addJavaScriptLibraries(); """ # Define HTML for the header header_html = """
Kaller

Ask Nina - My Personal KI Assistant

Du kannst Nina fragen zu mir und meinem Lebenslauf stellen
""" # Define HTML for the left column left_column_html = """

John

Christof Kaller

Leader & Architect

KI enthusiast

Issum, NRW


Christof Kaller

Die schönste Tradition ist
die Veränderung.

""" left_column_html_2 = """
""" modal_html = """ """ gr.HTML(header_html) chat_app = gr.Blocks(js=js_head, css=".user { background-color: #2980b9; color: white;} :root {--button-primary-text-color: white !important; --button-primary-background-fill-hover: green !important; --button-primary-background-fill: #2980b9 !important;} .message { background-color: #2980b9; color: white;} ") with chat_app: gr.HTML(html_head) gr.HTML(header_html) with gr.Row(): with gr.Column(scale=1): gr.HTML(left_column_html) #with gr.Row( ): gr.HTML(left_column_html_2) with gr.Column(scale=5): chat_interface = gr.ChatInterface(fn=get_response, title=None, description=None) #gr.HTML(modal_html) # Create the Gradio Interface # Launch the Gradio Interface chat_app.launch(debug=True, share=True)