Upload retriver.py
Browse files- retriver.py +51 -0
retriver.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Retriever function
|
2 |
+
|
3 |
+
from pinecone import Pinecone
|
4 |
+
from langchain_openai import AzureOpenAIEmbeddings
|
5 |
+
import uuid
|
6 |
+
import pandas as pd
|
7 |
+
import streamlit as st
|
8 |
+
import os
|
9 |
+
# Initialize Pinecone client
|
10 |
+
# pc = Pinecone(api_key=st.secrets["PC_API_KEY"])
|
11 |
+
pc = Pinecone(api_key="567aca04-6fb0-40a0-ba92-a5ed30be190b")
|
12 |
+
index = pc.Index("openai-serverless")
|
13 |
+
|
14 |
+
# Azure OpenAI configuration
|
15 |
+
# os.environ["AZURE_OPENAI_API_KEY"] = st.secrets["api_key"]
|
16 |
+
os.environ["AZURE_OPENAI_API_KEY"] = "86b631a9c0294e9698e327c59ff5ac2c"
|
17 |
+
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://davidfearn-gpt4.openai.azure.com/"
|
18 |
+
os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"] = "text-embedding-ada-002"
|
19 |
+
os.environ["AZURE_OPENAI_API_VERSION"] = "2024-08-01-preview"
|
20 |
+
|
21 |
+
# Model configuration
|
22 |
+
embeddings_model = AzureOpenAIEmbeddings(
|
23 |
+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
|
24 |
+
azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
|
25 |
+
openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
|
26 |
+
)
|
27 |
+
|
28 |
+
def retriever(query):
|
29 |
+
|
30 |
+
namespace="gskRegIntel"
|
31 |
+
top_k=3
|
32 |
+
"""
|
33 |
+
Embeds a query string and searches the vector database for similar entries.
|
34 |
+
|
35 |
+
:param query: The string to embed and search for.
|
36 |
+
:param namespace: Pinecone namespace to search within.
|
37 |
+
:param top_k: Number of top results to retrieve.
|
38 |
+
:return: List of search results with metadata and scores.
|
39 |
+
"""
|
40 |
+
try:
|
41 |
+
# Generate embedding for the query
|
42 |
+
query_embedding = embeddings_model.embed_query(query)
|
43 |
+
|
44 |
+
# Perform search in Pinecone
|
45 |
+
results = index.query(vector=query_embedding, top_k=top_k, namespace=namespace, include_metadata=True)
|
46 |
+
|
47 |
+
return results.matches
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
print(f"Error during search: {e}")
|
51 |
+
return []
|