passed in context to LLM and adjusted max_tokens
Browse files
app.py
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
| 4 |
with open("knowledgebase.txt", "r", encoding="utf-8") as file:
|
| 5 |
knowledgebase_text = file.read()
|
| 6 |
|
|
@@ -60,18 +65,27 @@ def respond(message, history):
|
|
| 60 |
top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 61 |
|
| 62 |
# allows LLM to interpret information better; without it would yield a Python list for the LLM to interpret which isn't as clean
|
| 63 |
-
context = "\n".join(
|
| 64 |
|
| 65 |
messages = [{"role": "system", "content": "You are a clairvoyant chatbot with vast knowledge on zodiac signs, but you also give the information very clearly so users can understand the message you're conveying."}]
|
| 66 |
|
| 67 |
if history:
|
| 68 |
messages.extend(history)
|
| 69 |
|
| 70 |
-
messages.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
response = client.chat_completion(
|
| 73 |
messages,
|
| 74 |
-
max_tokens=
|
| 75 |
)
|
| 76 |
|
| 77 |
return response.choices[0].message.content.strip()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
+
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
| 8 |
+
|
| 9 |
with open("knowledgebase.txt", "r", encoding="utf-8") as file:
|
| 10 |
knowledgebase_text = file.read()
|
| 11 |
|
|
|
|
| 65 |
top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 66 |
|
| 67 |
# allows LLM to interpret information better; without it would yield a Python list for the LLM to interpret which isn't as clean
|
| 68 |
+
context = "\n".join(top_results)
|
| 69 |
|
| 70 |
messages = [{"role": "system", "content": "You are a clairvoyant chatbot with vast knowledge on zodiac signs, but you also give the information very clearly so users can understand the message you're conveying."}]
|
| 71 |
|
| 72 |
if history:
|
| 73 |
messages.extend(history)
|
| 74 |
|
| 75 |
+
messages.append({
|
| 76 |
+
"role": "user",
|
| 77 |
+
"content": f"""Context from the knowledge base:
|
| 78 |
+
{context}
|
| 79 |
+
|
| 80 |
+
User question:
|
| 81 |
+
{message}
|
| 82 |
+
|
| 83 |
+
Answer using the context above."""
|
| 84 |
+
})
|
| 85 |
|
| 86 |
response = client.chat_completion(
|
| 87 |
messages,
|
| 88 |
+
max_tokens=300
|
| 89 |
)
|
| 90 |
|
| 91 |
return response.choices[0].message.content.strip()
|