File size: 2,741 Bytes
a59bc9c
70bcdad
c589e1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffd4434
ee24018
834791f
c589e1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70bcdad
776a762
ee24018
c589e1a
 
 
 
 
 
 
 
 
 
 
 
 
 
776a762
70bcdad
 
776a762
70bcdad
776a762
 
 
 
 
 
 
834791f
ee24018
 
 
 
 
776a762
70bcdad
 
776a762
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
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr
from huggingface_hub import InferenceClient
!pip install -q sentence-transformers
from sentence_transformers import SentenceTransformer
import torch

with open("knowlege.txt", "r", encoding="utf-8") as file:
  knowlege = file.read()

print(knowlege)

cleaned_chunks = [chunk.strip() for chunk in knowlege.strip().split("\n") if chunk.strip()]
print(cleaned_chunks)

model = SentenceTransformer('all-MiniLM-L6-v2')

chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
print(chunk_embeddings)

cleaned_text = ""

def get_top_chunks(query):
    query_embedding = model.encode(query, convert_to_tensor=True)
    query_embedding_normalized = query_embedding / query_embedding.norm()

    similarities = torch.matmul(chunk_embeddings, query_embedding_normalized)
    print(similarities)
    top_indices = torch.topk(similarities, k=5).indices.tolist()
    print(top_indices)

    return [cleaned_chunks[i] for i in top_indices]

top_results = get_top_chunks("What are some good wizard characters?")
print(top_results)







client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

# def respond(message, history):
#     response = ""

#     messages = [{"role": "system", "content": "You are a chatbot that helps users create characters for role playing games."}]

#     if history:
#         messages.extend(history)

#     messages.append({"role": "user", "content": message})

#     stream = client.chat_completion(
#         messages,
#         max_tokens=100,
#         temperature=1.2,
#         stream=True
#     )

#     for message in stream:
#         token = message.choices[0].delta.content

#         if token is not None:
#             response += token
#             yield response

def respond(message, history):
    response = ""

    # Retrieve top chunks based on the current user message
    top_chunks = get_top_chunks(message)
    context = "\n".join(top_chunks)

    # Add knowledge as part of system instructions
    messages = [
        {
            "role": "system",
            "content": (
                "You are a chatbot that helps users create characters for role-playing games. "
                "Use the following knowledge to inform your answers:\n\n" + context
            )
        }
    ]

    if history:
        messages.extend(history)

    messages.append({"role": "user", "content": message})

    stream = client.chat_completion(
        messages,
        max_tokens=100,
        temperature=1.2,
        stream=True
    )

    for message in stream:
        token = message.choices[0].delta.content
        if token is not None:
            response += token
            yield response

chatbot = gr.ChatInterface(respond, type="messages")

chatbot.launch()