Kristofy commited on
Commit
777c7ec
1 Parent(s): e7990b4

updated pinecone

Browse files
Files changed (1) hide show
  1. app.py +13 -27
app.py CHANGED
@@ -1,7 +1,4 @@
1
- import re
2
  import torch
3
- import time
4
- import pinecone
5
  import pickle
6
  import os
7
  import numpy as np
@@ -16,6 +13,7 @@ from peft import PeftModel
16
  from bs4 import BeautifulSoup
17
  import requests
18
  import logging
 
19
 
20
  logging.basicConfig(format='[%(asctime)s] %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.INFO)
21
 
@@ -25,6 +23,17 @@ headers = {
25
  "Cookie": "CONSENT=YES+cb.20210418-17-p0.it+FX+917; ",
26
  }
27
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def google_search(text):
30
  logging.info(f"Google search on: {text}")
@@ -50,19 +59,6 @@ def google_search(text):
50
 
51
  return ans
52
 
53
- PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
54
-
55
- pinecone.init(api_key=PINECONE_API_KEY, environment="gcp-starter")
56
-
57
- sentencetransformer_model = SentenceTransformer('sentence-transformers/multi-qa-mpnet-base-cos-v1')
58
-
59
- CACHE_DIR = "./.cache"
60
- INDEX_NAME = "k8s-semantic-search"
61
-
62
- if not os.path.exists(CACHE_DIR):
63
- os.makedirs(CACHE_DIR)
64
-
65
-
66
  def cached(func):
67
  def wrapper(*args, **kwargs):
68
  SEP = "$|$"
@@ -87,27 +83,18 @@ def cached(func):
87
 
88
  return wrapper
89
 
90
-
91
  @cached
92
  def create_embedding(text: str):
93
  embed_text = sentencetransformer_model.encode(text)
94
 
95
  return embed_text.tolist()
96
 
97
-
98
- index = pinecone.Index(INDEX_NAME)
99
-
100
-
101
  def query_from_pinecone(query, top_k=3):
102
  embedding = create_embedding(query)
103
  if not embedding:
104
  return None
105
 
106
- return index.query(vector=embedding, top_k=top_k, include_metadata=True).get("matches") # gets the metadata (text)
107
-
108
-
109
- cross_encoder = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-12-v2")
110
-
111
 
112
  def get_results_from_pinecone(query, top_k=3, re_rank=True, verbose=True):
113
  results_from_pinecone = query_from_pinecone(query, top_k=top_k)
@@ -154,7 +141,6 @@ def get_results_from_pinecone(query, top_k=3, re_rank=True, verbose=True):
154
 
155
  return final_results
156
 
157
-
158
  def semantic_search(prompt):
159
  final_results = get_results_from_pinecone(prompt, top_k=9, re_rank=True, verbose=True)
160
  if not final_results:
 
 
1
  import torch
 
 
2
  import pickle
3
  import os
4
  import numpy as np
 
13
  from bs4 import BeautifulSoup
14
  import requests
15
  import logging
16
+ from pinecone import Pinecone, ServerlessSpec
17
 
18
  logging.basicConfig(format='[%(asctime)s] %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.INFO)
19
 
 
23
  "Cookie": "CONSENT=YES+cb.20210418-17-p0.it+FX+917; ",
24
  }
25
 
26
+ PINECONE_INDEX_NAME = "kubwizzard"
27
+ PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
28
+ INDEX_NAME = "k8s-semantic-search"
29
+ CACHE_DIR = "./.cache"
30
+
31
+ cross_encoder = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-12-v2")
32
+ pinecone_client = Pinecone(api_key=PINECONE_API_KEY)
33
+ sentencetransformer_model = SentenceTransformer('sentence-transformers/multi-qa-mpnet-base-cos-v1')
34
+
35
+ if not os.path.exists(CACHE_DIR):
36
+ os.makedirs(CACHE_DIR)
37
 
38
  def google_search(text):
39
  logging.info(f"Google search on: {text}")
 
59
 
60
  return ans
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def cached(func):
63
  def wrapper(*args, **kwargs):
64
  SEP = "$|$"
 
83
 
84
  return wrapper
85
 
 
86
  @cached
87
  def create_embedding(text: str):
88
  embed_text = sentencetransformer_model.encode(text)
89
 
90
  return embed_text.tolist()
91
 
 
 
 
 
92
  def query_from_pinecone(query, top_k=3):
93
  embedding = create_embedding(query)
94
  if not embedding:
95
  return None
96
 
97
+ return pinecone_client.Index(PINECONE_INDEX_NAME).query(vector=embedding, top_k=top_k, include_metadata=True).get("matches") # gets the metadata (text)
 
 
 
 
98
 
99
  def get_results_from_pinecone(query, top_k=3, re_rank=True, verbose=True):
100
  results_from_pinecone = query_from_pinecone(query, top_k=top_k)
 
141
 
142
  return final_results
143
 
 
144
  def semantic_search(prompt):
145
  final_results = get_results_from_pinecone(prompt, top_k=9, re_rank=True, verbose=True)
146
  if not final_results: