File size: 2,716 Bytes
066d256
9989f59
 
 
 
 
 
066d256
 
9989f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
066d256
9989f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from scipy.spatial.distance import cosine
import pinecone
from sentence_transformers import SentenceTransformer
import openai

# Initialize Pinecone
pinecone.init(api_key='your pinecone key',
              environment='your environment')

# Initialize the embedding model
model = SentenceTransformer(
    'sentence-transformers/distilbert-base-nli-mean-tokens')

# Define department data
departments = ["design", "video_production", "marketing"]

# Generate embeddings for the departments
vectors = model.encode(departments)

# Create a Pinecone index
index_name = "mojosolo"
if index_name in pinecone.list_indexes():
    pinecone.delete_index(name=index_name)

pinecone.create_index(name=index_name, dimension=768, metric='cosine')

# Insert department vectors into the Pinecone index
index = pinecone.Index(index_name)
upsert_response = index.upsert(
    vectors=list(zip(departments, [vector.tolist() for vector in vectors])),
    namespace="example-namespace"
)


def get_department(message):
    query_vector = model.encode([message])[0]
    min_distance = 1.0
    best_department = None

    for department, vector in zip(departments, vectors):
        distance = cosine(query_vector, vector)
        print(f"DEBUG: Department: {department}, Distance: {distance}")
        if distance < min_distance:
            min_distance = distance
            best_department = department

    if best_department is not None:
        return best_department
    else:
        print("DEBUG: No department found")
        return None




openai.api_key = 'your api key'


def chatbot(message):
    department = get_department(message)
    if department is not None:
        response = openai.Completion.create(
            engine="text-davinci-002",
            prompt=f"[{department}] {message}",
            max_tokens=50,
            n=1,
            stop=None,
            temperature=0.7,
            top_p=0.95,
        )
        return response.choices[0].text.strip()
    else:
        return "Sorry, I couldn't understand your query."

while True:
    user_input = input("You:")
    if user_input.lower() == "exit":
        break
    response = chatbot(user_input)
    print(f"Bot: {response}")


# Query the Pinecone index using an example sentence
query_sentence = "We need a new video advertisement campaign."
query_vector = model.encode([query_sentence])[0]
query_response = index.query(
    namespace="example-namespace",
    top_k=1,
    vector=query_vector.tolist()
)

# Print the query results
print("Query results:")
if query_response.results:
    for result in query_response.results:
        print(f"ID: {result.id}, Distance: {result.distance}")
else:
    print("No results found.")