File size: 2,753 Bytes
eaf119d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pymilvus import utility, DataType, FieldSchema, CollectionSchema, Collection
from utils import connect_vector_db, load_collection, convert_query, clean_query
from routing import routing_query
from search import search_chunks

from sentence_transformers import SentenceTransformer
import re

import gradio as gr

import requests
import json

def search(query, path_stop_words, path_define, model_name, topk):
    collection_name = routing_query(query, path_stop_words, path_define)
    results = search_chunks(query, model_name, collection_name, topk)

    refs, relevant_chunks = [], []

    pattern = r"'chunk_ref': '([^']*)', 'chunk_text': '([^']*)'"

    for sample in results:

        matches = re.findall(pattern, str(sample))

        for match in matches:
            refs.append(match[0])
            relevant_chunks.append(match[1])

    return refs, relevant_chunks


def response_saola2m(PROMPT, temperature=0.7):
    url = "https://api.fpt.ai/nlp/llm/api/v1/completions"
    headers = {
        'Authorization': 'Bearer sk-8oIY6XLrokZEJMl6aopCuQ',
        'Content-Type': 'application/json',
    }

    data = {
        "model": "SaoLa2M-instruct",
        "prompt": PROMPT,
        "temperature": temperature,
        "max_tokens": 512
    }

    response = requests.post(url, headers=headers, json=data)
    response_text = response.text
    response_json = json.loads(response_text)
    result = response_json['choices'][0]['text']
    return result

PATH_STOP_WORDS = 'vi_stopword.txt'
PATH_DEFINE = 'define.json'
MODEL_NAME = 'qminh369/datn-dense_embedding'
TOPK = 5

connect_vector_db()

def answer(question):

    refs, relevant_chunks = search(question, PATH_STOP_WORDS, PATH_DEFINE, MODEL_NAME, TOPK)

    INSTRUCTION = "Hãy trả lời câu hỏi sau dựa trên thông tin được cung cấp. Nếu thông tin được cung cấp không liên quan dến câu hỏi thì trả về câu trả lời 'Không có thông tin'"
    INPUT_TEXT = "\n".join(relevant_chunks)

    PROMPT = f"<s>[INST] Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{INSTRUCTION}\n\n{question}\n\n### Input:\n{INPUT_TEXT}\n\n[/INST]### Response:\n"
    print(PROMPT)

    response = response_saola2m(PROMPT, temperature=0)
    
    ref = "\n" + "Trích dẫn từ: " + refs[0]
    
    response = response + ref
    
    return response.strip()

def chatbot(question, history=[]):
    output = answer(question)
    history.append((question, output))
    return history, history

demo = gr.Interface(
    fn=chatbot,
    inputs=["text", "state"],
    outputs=["chatbot", "state"])

demo.queue().launch(share=True)