|
import chromadb |
|
import os |
|
import gradio as gr |
|
import json |
|
from huggingface_hub import InferenceClient |
|
|
|
dbPath='/Users/thiloid/Desktop/LSKI/ole_nest/Chatbot/LLM/chromaTS' |
|
if(os.path.exists(dbPath)==False): dbPath="/home/user/app/chromaTS'" |
|
|
|
print(dbPath) |
|
|
|
|
|
|
|
client = chromadb.PersistentClient(path=path) |
|
print(client.heartbeat()) |
|
print(client.get_version()) |
|
print(client.list_collections()) |
|
from chromadb.utils import embedding_functions |
|
default_ef = embedding_functions.DefaultEmbeddingFunction() |
|
sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="T-Systems-onsite/cross-en-de-roberta-sentence-transformer") |
|
|
|
|
|
collection = client.get_collection(name="chromaTS", embedding_function=sentence_transformer_ef) |
|
|
|
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1") |
|
|
|
|
|
def format_prompt(message): |
|
prompt = "" |
|
|
|
|
|
|
|
prompt += f"[INST] {message} [/INST]" |
|
return prompt |
|
|
|
def response( |
|
prompt, history,temperature=0.9, max_new_tokens=500, top_p=0.95, repetition_penalty=1.0, |
|
): |
|
temperature = float(temperature) |
|
if temperature < 1e-2: temperature = 1e-2 |
|
top_p = float(top_p) |
|
generate_kwargs = dict( |
|
temperature=temperature, |
|
max_new_tokens=max_new_tokens, |
|
top_p=top_p, |
|
repetition_penalty=repetition_penalty, |
|
do_sample=True, |
|
seed=42, |
|
) |
|
addon="" |
|
results=collection.query( |
|
query_texts=[prompt], |
|
n_results=10, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
dists=["<br><small>(relevance: "+str(round((1-d)*100)/100)+";" for d in results['distances'][0]] |
|
|
|
|
|
results=results['documents'][0] |
|
combination = zip(results,dists) |
|
combination = [' '.join(triplets) for triplets in combination] |
|
|
|
if(len(results)>1): |
|
addon=" Bitte berücksichtige bei deiner Antwort ausschießlich folgende Auszüge aus unserer Datenbank, sofern sie für die Antwort erforderlich sind. Beantworte die Frage knapp und präzise. Ignoriere unpassende Datenbank-Auszüge OHNE sie zu kommentieren, zu erwähnen oder aufzulisten:\n"+"\n".join(results) |
|
system="Du bist ein deutschsprachiges KI-basiertes Studienberater Assistenzsystem, das zu jedem Anliegen möglichst geeignete Studieninformationen empfiehlt."+addon+"\n\nUser-Anliegen:" |
|
formatted_prompt = format_prompt(system+"\n"+prompt,history) |
|
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False) |
|
output = "" |
|
for response in stream: |
|
output += response.token.text |
|
yield output |
|
|
|
yield output |
|
|
|
gr.ChatInterface(response, chatbot=gr.Chatbot(value=[[None,"Herzlich willkommen! Ich bin Chätti ein KI-basiertes Studienassistenzsystem, das für jede Anfrage die am besten Studieninformationen empfiehlt.<br>Erzähle mir, was du gerne tust!"]],render_markdown=True),title="German BERUFENET-RAG-Interface to the Hugging Face Hub").queue().launch(share=True) |
|
print("Interface up and running!") |
|
|
|
|
|
|