Spaces:
Sleeping
Sleeping
Create query_model.py
Browse files- query_model.py +25 -0
query_model.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from together import Together
|
| 2 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 3 |
+
from langchain.vectorstores import Chroma
|
| 4 |
+
|
| 5 |
+
# Load embeddings
|
| 6 |
+
embeddings = HuggingFaceEmbeddings(model_name="togethercomputer/m2-bert-80M")
|
| 7 |
+
vectorstore = Chroma(persist_directory="embeddings", embedding_function=embeddings)
|
| 8 |
+
|
| 9 |
+
# Initialize Together AI
|
| 10 |
+
together = Together(api_key="your_api_key")
|
| 11 |
+
|
| 12 |
+
def ask_book(question):
|
| 13 |
+
"""Retrieve relevant book content & query GPT model."""
|
| 14 |
+
query_embedding = embeddings.embed_query(question)
|
| 15 |
+
results = vectorstore.similarity_search_by_vector(query_embedding, k=3)
|
| 16 |
+
context = " ".join([doc.page_content for doc in results])
|
| 17 |
+
|
| 18 |
+
response = together.chat(
|
| 19 |
+
model="mistral-7b",
|
| 20 |
+
messages=[
|
| 21 |
+
{"role": "system", "content": "You are an AI answering questions based on a book."},
|
| 22 |
+
{"role": "user", "content": f"Book content: {context}\n\nAnswer this question: {question}"}
|
| 23 |
+
]
|
| 24 |
+
)
|
| 25 |
+
return response["choices"][0]["message"]["content"]
|