-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
48 lines (36 loc) · 1006 Bytes
/
Copy pathchat.py
File metadata and controls
48 lines (36 loc) · 1006 Bytes
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
import requests
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
DB_PATH = "vector_db"
embedding = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
db = Chroma(
persist_directory=DB_PATH,
embedding_function=embedding
)
def ask_llm(context, question):
prompt = f"""
You MUST answer ONLY using the provided context.
If the answer is not in the context, say:
"Not found in provided documents."
Context:
{context}
Question:
{question}
Answer grounded strictly in the context:
"""
response = requests.post(
"http://localhost:11434/api/generate",
json={
"model": "mistral",
"prompt": prompt,
"stream": False
}
)
return response.json()["response"]
while True:
q = input("You: ")
docs = db.similarity_search(q, k=6)
context = "\n".join([d.page_content for d in docs])
print("AI:", ask_llm(context, q))