|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig |
|
from langchain.llms import HuggingFaceHub |
|
model_name = "bn22/Mistral-7B-Instruct-v0.1-sharded" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_model(model_name: str): |
|
|
|
model = HuggingFaceHub( |
|
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", |
|
model_kwargs={"max_length": 1048, "temperature":0.2, "max_new_tokens":256, "top_p":0.95, "repetition_penalty":1.0}, |
|
) |
|
|
|
""" |
|
:param model_name: Name or path of the model to be loaded. |
|
:return: Loaded quantized model. |
|
|
|
bnb_config = BitsAndBytesConfig( |
|
load_in_4bit=True, |
|
bnb_4bit_use_double_quant=True, |
|
bnb_4bit_quant_type="nf4", |
|
bnb_4bit_compute_dtype=torch.bfloat16 |
|
) |
|
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_name, |
|
load_in_4bit=True, |
|
torch_dtype=torch.bfloat16, |
|
quantization_config=bnb_config |
|
)""" |
|
return model |
|
|
|
|
|
|
|
|
|
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() |
|
|
|
def get_vectorstore_from_url(url): |
|
|
|
loader = WebBaseLoader(url) |
|
document = loader.load() |
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter() |
|
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 |
|
} |
|
embeddings = HuggingFaceBgeEmbeddings( |
|
model_name=model, encode_kwargs=encode_kwargs, model_kwargs={"device": "cpu"} |
|
) |
|
|
|
vector_store = Chroma(persist_directory="./chroma_db", embedding_function=embeddings) |
|
|
|
|
|
vector_store = Chroma.from_documents(document_chunks, embeddings, persist_directory="./chroma_db") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return vector_store |
|
|
|
|
|
|
|
|
|
|
|
def get_context_retriever_chain(vector_store): |
|
|
|
|
|
model_name = "anakin87/zephyr-7b-alpha-sharded" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llm = load_model(model_name) |
|
|
|
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(model_name) |
|
|
|
prompt = ChatPromptTemplate.from_messages([ |
|
("system", "Du bist eine freundlicher Mitarbeiterin Namens Susie und arbeitest in einenm Call Center. Du beantwortest 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, bitte? 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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response |
|
|
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
|
|
|
|
|
chat_history = [] |
|
|
|
|
|
def get_response(user_input): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vs = get_vectorstore_from_url("https://huggingface.co/Chris4K") |
|
|
|
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 |
|
}) |
|
|
|
res = response['answer'] |
|
parts = res.split(" Assistant: ") |
|
last_part = parts[-1] |
|
return last_part |
|
|
|
|
|
|
|
|
|
from fastapi import FastAPI |
|
|
|
app = FastAPI() |
|
|
|
@app.post("/generate/") |
|
def generate(user_input): |
|
print("----yuhu -----") |
|
return get_response(user_input) |
|
|
|
|
|
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}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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: " |
|
}) |
|
|
|
res = response['answer'] |
|
parts = res.split(" Assistant: ") |
|
last_part = parts[-1] |
|
return last_part |
|
|
|
|
|
|
|
vs = get_vectorstore_from_url("https://www.xing.com/profile/Christof_Kaller/web_profiles") |
|
|
|
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) > 25: |
|
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 |
|
|
|
|
|
domain_url = 'https://globl.contact/' |
|
links = get_all_links_from_domain(domain_url) |
|
print("Links from the domain:", links) |
|
|
|
|
|
|
|
for url in links: |
|
vs = get_vectorstore_from_url(url) |
|
|
|
|
|
|
|
def simple(text:str): |
|
return text +" hhhmmm " |
|
|
|
app = gr.ChatInterface( |
|
fn=get_response, |
|
|
|
|
|
|
|
title="Chat with Websites", |
|
description="Schreibe hier deine Frage rein...", |
|
|
|
retry_btn=None, |
|
undo_btn=None, |
|
clear_btn=None |
|
) |
|
|
|
app.launch(debug=True, share=True) |