File size: 4,764 Bytes
0e5c934
d4296c2
0e5c934
 
 
 
 
 
 
 
66c82c6
0e5c934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4296c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import requests
from pymongo import MongoClient

def google_search(query, api_key, cx):
    url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={api_key}&cx={cx}"
    
    response = requests.get(url)

    if response.status_code == 200:
        search_results = response.json() 
        print(search_results)
        return search_results
    else:
        print(f"Error: {response.status_code}")
        return None




def generate_embedding_for_user_resume(data,user_id):
    from sentence_transformers import SentenceTransformer

    model = SentenceTransformer("nomic-ai/nomic-embed-text-v1", trust_remote_code=True)


    def get_embedding(data, precision="float32"):
        return model.encode(data, precision=precision)


    from pinecone import Vector
    def create_docs_with_vector_embeddings(bson_float32, data):
        docs = []
        for i, (bson_f32_emb, text) in enumerate(zip(bson_float32, data)):
                doc =Vector(
                id=f"{i}",
                values= bson_f32_emb.tolist(),
                metadata={"text":text,"user_id":user_id},
                )
                docs.append(doc)
        return docs
    float32_embeddings = get_embedding(data, "float32")




    docs = create_docs_with_vector_embeddings(float32_embeddings,  data)
    return docs


def insert_embeddings_into_pinecone_database(doc,api_key,name_space):
    from pinecone import Pinecone
    pc = Pinecone(api_key=api_key)
    index_name = "resumes"
    index = pc.Index(index_name)
    upsert_response = index.upsert(namespace=name_space,vectors=doc)
    return upsert_response




def query_vector_database(query,api_key,name_space):
    from pinecone import Pinecone
    from sentence_transformers import SentenceTransformer
    model = SentenceTransformer("nomic-ai/nomic-embed-text-v1", trust_remote_code=True)
    ret=[]
    pc = Pinecone(api_key=api_key)
    index_name = "resumes"


    index = pc.Index(index_name)
    
    # Define a function to generate embeddings in multiple precisions
    def get_embedding(data, precision="float32"):
        return model.encode(data, precision=precision)
    
    query_embedding = get_embedding(query, precision="float32")

    response = index.query(
        namespace=name_space,
        vector=query_embedding.tolist(),
        top_k=3,
        include_metadata=True
        )


    for doc in response['matches']:
        ret.append(doc['metadata']['text'])
    return ret


def delete_vector_namespace(name_space,api_key):
    from pinecone import Pinecone
    pc = Pinecone(api_key=api_key)
    index_name = "resumes"


    index = pc.Index(index_name)
    response = index.delete(delete_all=True,namespace=name_space)
    return response



def split_text_into_chunks(text, chunk_size=400):
    # Split the text into words using whitespace.
    words = text.split()

    # Group the words into chunks of size 'chunk_size'.
    chunks = [' '.join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
    return chunks





def create_user(db_uri: str, db_name: str, collection_name: str, document: dict) -> str:
    """
    Inserts a new document into the specified MongoDB collection.

    Parameters:
        db_uri (str): MongoDB connection URI.
        db_name (str): Name of the database.
        collection_name (str): Name of the collection.
        document (dict): The document to insert.

    Returns:
        str: The ID of the inserted document.
    """
    # Connect to MongoDB
    client = MongoClient(db_uri)
    db = client[db_name]
    collection = db[collection_name]
    
    # Insert the document
    s = collection.find_one(document)
    if s==None:
        result = collection.insert_one(document)
        client.close()
        return str(result.inserted_id)
    else:
        client.close()
        return str(s['_id'])
    
    # Close the connection
    
    
    


def login_user(db_uri: str, db_name: str, collection_name: str, document: dict) -> str:
    """
    Inserts a new document into the specified MongoDB collection.

    Parameters:
        db_uri (str): MongoDB connection URI.
        db_name (str): Name of the database.
        collection_name (str): Name of the collection.
        document (dict): The document to insert.

    Returns:
        str: The ID of the inserted document.
    """
    # Connect to MongoDB
    client = MongoClient(db_uri)
    db = client[db_name]
    collection = db[collection_name]
    
    # Insert the document
    s = collection.find_one(document)
    if s==None:
        return False
    else:
        if document['password']==s['password']:
            client.close()
            return str(s['_id'])
        else:
            return True
    # Close the connection