Spaces:
No application file
No application file
Commit
·
9989f59
1
Parent(s):
a73c06d
initial Commit
Browse files- Pineconeprac.py +98 -0
Pineconeprac.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from scipy.spatial.distance import cosine
|
2 |
+
import pinecone
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
import openai
|
5 |
+
|
6 |
+
# Initialize Pinecone
|
7 |
+
pinecone.init(api_key='3d6a95f6-e9b7-4e87-b96d-ec90392220a8',
|
8 |
+
environment='us-west4-gcp')
|
9 |
+
|
10 |
+
# Initialize the embedding model
|
11 |
+
model = SentenceTransformer(
|
12 |
+
'sentence-transformers/distilbert-base-nli-mean-tokens')
|
13 |
+
|
14 |
+
# Define department data
|
15 |
+
departments = ["design", "video_production", "marketing"]
|
16 |
+
|
17 |
+
# Generate embeddings for the departments
|
18 |
+
vectors = model.encode(departments)
|
19 |
+
|
20 |
+
# Create a Pinecone index
|
21 |
+
index_name = "mojosolo"
|
22 |
+
if index_name in pinecone.list_indexes():
|
23 |
+
pinecone.delete_index(name=index_name)
|
24 |
+
|
25 |
+
pinecone.create_index(name=index_name, dimension=768, metric='cosine')
|
26 |
+
|
27 |
+
# Insert department vectors into the Pinecone index
|
28 |
+
index = pinecone.Index(index_name)
|
29 |
+
upsert_response = index.upsert(
|
30 |
+
vectors=list(zip(departments, [vector.tolist() for vector in vectors])),
|
31 |
+
namespace="example-namespace"
|
32 |
+
)
|
33 |
+
|
34 |
+
|
35 |
+
def get_department(message):
|
36 |
+
query_vector = model.encode([message])[0]
|
37 |
+
min_distance = 1.0
|
38 |
+
best_department = None
|
39 |
+
|
40 |
+
for department, vector in zip(departments, vectors):
|
41 |
+
distance = cosine(query_vector, vector)
|
42 |
+
print(f"DEBUG: Department: {department}, Distance: {distance}")
|
43 |
+
if distance < min_distance:
|
44 |
+
min_distance = distance
|
45 |
+
best_department = department
|
46 |
+
|
47 |
+
if best_department is not None:
|
48 |
+
return best_department
|
49 |
+
else:
|
50 |
+
print("DEBUG: No department found")
|
51 |
+
return None
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
openai.api_key = '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'
|
57 |
+
|
58 |
+
|
59 |
+
def chatbot(message):
|
60 |
+
department = get_department(message)
|
61 |
+
if department is not None:
|
62 |
+
response = openai.Completion.create(
|
63 |
+
engine="text-davinci-002",
|
64 |
+
prompt=f"[{department}] {message}",
|
65 |
+
max_tokens=50,
|
66 |
+
n=1,
|
67 |
+
stop=None,
|
68 |
+
temperature=0.7,
|
69 |
+
top_p=0.95,
|
70 |
+
)
|
71 |
+
return response.choices[0].text.strip()
|
72 |
+
else:
|
73 |
+
return "Sorry, I couldn't understand your query."
|
74 |
+
|
75 |
+
while True:
|
76 |
+
user_input = input("You:")
|
77 |
+
if user_input.lower() == "exit":
|
78 |
+
break
|
79 |
+
response = chatbot(user_input)
|
80 |
+
print(f"Bot: {response}")
|
81 |
+
|
82 |
+
|
83 |
+
# Query the Pinecone index using an example sentence
|
84 |
+
query_sentence = "We need a new video advertisement campaign."
|
85 |
+
query_vector = model.encode([query_sentence])[0]
|
86 |
+
query_response = index.query(
|
87 |
+
namespace="example-namespace",
|
88 |
+
top_k=1,
|
89 |
+
vector=query_vector.tolist()
|
90 |
+
)
|
91 |
+
|
92 |
+
# Print the query results
|
93 |
+
print("Query results:")
|
94 |
+
if query_response.results:
|
95 |
+
for result in query_response.results:
|
96 |
+
print(f"ID: {result.id}, Distance: {result.distance}")
|
97 |
+
else:
|
98 |
+
print("No results found.")
|