Spaces:
Build error
Build error
File size: 4,245 Bytes
f3d0f1e 0d7e513 f3d0f1e 5b20ce0 227833b f3d0f1e 3ebff47 f3d0f1e 3ebff47 f3d0f1e b6312c1 f3d0f1e 3ebff47 f3d0f1e e681b03 f3d0f1e 0d7e513 e681b03 0d7e513 e681b03 0d7e513 e681b03 0d7e513 e681b03 0d7e513 f3d0f1e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.llms.huggingface_hub import HuggingFaceHub
from langchain_community.embeddings import HuggingFaceEmbeddings
from src.vectordatabase import RAG, get_vectorstore
import pandas as pd
from dotenv import load_dotenv, find_dotenv
#Load environmental variables from .env-file
#load_dotenv(find_dotenv())
embeddings = HuggingFaceEmbeddings(model_name="paraphrase-multilingual-MiniLM-L12-v2")
llm = HuggingFaceHub(
# Try different model here
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
# repo_id="CohereForAI/c4ai-command-r-v01", # too large 69gb
# repo_id="CohereForAI/c4ai-command-r-v01-4bit", # too large 22 gb
# repo_id="meta-llama/Meta-Llama-3-8B", # too large 16 gb
task="text-generation",
model_kwargs={
"max_new_tokens": 512,
"top_k": 30,
"temperature": 0.1,
"repetition_penalty": 1.03,
}
#,huggingfacehub_api_token
)
# To Do: Experiment with different templates replying in german or english depending on the input language
prompt1 = ChatPromptTemplate.from_template("""<s>[INST]
Instruction: Beantworte die folgende Frage auf deutsch und nur auf der Grundlage des angegebenen Kontexts:
Context: {context}
Question: {input}
[/INST]"""
# Returns the answer in English!?
)
prompt2 = ChatPromptTemplate.from_template("""Beantworte die folgende Frage auf deutsch und nur auf der Grundlage des angegebenen Kontexts:
<context>
{context}
</context>
Frage: {input}
"""
# Returns the answer in German
)
folder_path = "./src/FAISS"
index_name = "speeches_1949_09_12"
#index_name = "legislature20"
db = get_vectorstore(embeddings=embeddings, folder_path=folder_path, index_name=index_name)
def chatbot(message, history, db=db, llm=llm, prompt=prompt2):
raw_response = RAG(llm=llm, prompt=prompt, db=db, question=message)
response = raw_response['answer'].split("Antwort: ")[1]
return response
# Retrieve speech contents based on keywords
def keyword_search(query,n=10, db=db, embeddings=embeddings, method='ss', party_filter = 'All'):
query_embedding = embeddings.embed_query(query)
if method == 'mmr':
df_res = pd.DataFrame(columns=['Speech Content','Date', 'Party', 'Relevance']) # Add Date/Party/Politician
results = db.max_marginal_relevance_search_with_score_by_vector(query_embedding, k = n)
for doc in results:
party = doc[0].metadata["party"]
#Filter by party input
if party != party_filter and party_filter != 'All':
continue
speech_content = doc[0].page_content
speech_date = doc[0].metadata["date"]
score = round(doc[1], ndigits=2) # Relevance based on relevance search
df_res = pd.concat([df_res, pd.DataFrame({'Speech Content': [speech_content],
'Date': [speech_date],
'Party': [party],
'Relevance': [score]})], ignore_index=True)
df_res.sort_values('Relevance', inplace=True, ascending=True)
else:
df_res = pd.DataFrame(columns=['Speech Content','Date', 'Party']) # Add Date/Party/Politician #Add filter
results = db.similarity_search_by_vector(query_embedding, k = n)
for doc in results:
party = doc.metadata["party"]
#Filter by party input
if party != party_filter and party_filter != 'All':
continue
speech_content = doc.page_content
speech_date = doc.metadata["date"]
df_res = pd.concat([df_res, pd.DataFrame({'Speech Content': [speech_content],
'Date': [speech_date],
'Party': [party]})], ignore_index=True)
return df_res |