File size: 4,487 Bytes
d7d0d8e 7fc5d88 d7d0d8e 7d5df78 7fc5d88 d7d0d8e 7fc5d88 d7d0d8e 7fc5d88 d7d0d8e 55174eb d7d0d8e 7fc5d88 d7d0d8e f337592 7fc5d88 d7d0d8e 55174eb d7d0d8e 7fc5d88 |
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.docstore.document import Document
from langchain.vectorstores import MongoDBAtlasVectorSearch
from pymongo import MongoClient
from motor.motor_asyncio import AsyncIOMotorClient
import os,pprint
completion_base = os.environ.get("completion_base")
openai_api_key = os.environ.get("openai_api_key")
mongoDB = os.environ.get("MONGO_DB")
template = """### Given the following context
### Context
{context}
### Use it to explain the question: {question}
"""
async def fetch_data(question, context):
url = completion_base
payload = json.dumps(
{
"messages": [
{
"role": "system",
"content": "### You provide explanations based on the provided context",
},
{
"role": "user",
"content": template.format(context=context, question=question),
},
],
"model": "gpt-3.5-turbo",
"temperature": 1,
"presence_penalty": 0,
"top_p": 0.95,
"frequency_penalty": 0,
"stream": False,
}
)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {openai_api_key}",
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, data=payload) as response:
response = await response.json()
return response["choices"][0]["message"]["content"]
async def delete_documents(task_id):
client = AsyncIOMotorClient(mongoDB)
db = client["transcriptions"]
collection = db["videos"]
result = await collection.delete_many({"task_id": task_id})
print(f"Deleted {result.deleted_count} document(s)")
# mongo_client = MongoClient(
# mongoDB
# )
# model_name = "BAAI/bge-base-en"
# collection = mongo_client["transcriptions"]["videos"]
# embeddings = HuggingFaceEmbeddings(model_name=model_name)
# index_name = "test_embeddings"
# vectorstore = MongoDBAtlasVectorSearch(collection, embeddings, index_name=index_name)
def generateChunks(chunks, task_id, n=100):
combined = [chunks[i : i + n] for i in range(0, len(chunks), n)]
result = []
for chunk in combined:
data = {"text": ""}
for item in chunk:
if chunk.index(item) == 0:
data["start"] = item["start"]
if chunk.index(item) == len(chunk) - 1:
data["end"] = item["end"]
data["text"] += " " + item["text"]
temp = Document(
page_content=data["text"],
metadata={"start": data["start"], "end": data["end"], "task_id": task_id},
)
result.append(temp)
return result
def search(query: str, task_id: str):
mongo_client = MongoClient(mongoDB)
model_name = "BAAI/bge-base-en"
collection = mongo_client["transcriptions"]["videos"]
embeddings = HuggingFaceEmbeddings(model_name=model_name)
index_name = "test_embedding"
k = 5
vectorstore = MongoDBAtlasVectorSearch(
collection,
embedding=embeddings,
index_name="test_embedding",
)
data = vectorstore.similarity_search(
query=query,
pre_filter={"text": {"path": "task_id", "query": task_id}},
search_kwargs={
"k": k, # overrequest k during search
"pre_filter": {"path": "task_id", "equals": task_id},
"post_filter_pipeline": [{"$limit": k}], # limit results to top k
},
)
# data =[d.dict() for d in data]
# print(data[0].metadata.exclude({'_id','embedding'}))
# pprint.pprint(data[0].metadata)
return [{"text": d.page_content,'start':d.metadata['start'],"end":d.metadata['end']} for d in data]
# agent =vectorstore.as_retriever(
# )
# return agent.get_relevant_documents
def encode(temp: list[Document]):
mongo_client = MongoClient(mongoDB)
model_name = "BAAI/bge-base-en"
collection = mongo_client["transcriptions"]["videos"]
embeddings = HuggingFaceEmbeddings(model_name=model_name)
index_name = "test_embedding"
vectorstore = MongoDBAtlasVectorSearch(
collection, embeddings, index_name=index_name
)
vectorstore.from_documents(
temp, embedding=embeddings, collection=collection, index_name=index_name
)
# return embeddings.embed_documents(texts = [d.page_content for d in temp]) |