rag-test / rag_retriver.py
FabioSantos's picture
Upload 6 files
338451d
from dotenv import dotenv_values
import google.generativeai as genai
import pinecone
import cohere
co = cohere.Client('qCFXJU5hVIvVrQOBQfQr6lvK8p8rLSYHfVIxxZjP')
#Loading Credentials
env_name = "credentials.env"
config = dotenv_values(env_name)
# initialize connection to pinecone (get API key at app.pinecone.io)
index_name = 'rag-demo'
pinecone.init(
api_key='d4f03782-3537-43b2-9050-ed2c00be71a2',
environment='gcp-starter'
)
index = pinecone.Index('rag-demo')
#Vector Search
def Vector_search(query):
Rag_data = ""
xq = co.embed(
texts=['transportation law'],
model='embed-english-v3.0',
input_type='classification'
)
res = index.query([xq.embeddings[0]], top_k=2, include_metadata=True)
for match in res['matches']:
if match['score'] < 0.80:
continue
Rag_data += match['metadata']['text']
return Rag_data
#GPT Completion
def GPT_completion_with_vector_search(prompt):
genai.configure(api_key="AIzaSyAt0u96OqVw5-rAdM3pmL1rjT8H_jYAnJ8")
for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)
model = genai.GenerativeModel('gemini-pro')
rag = Vector_search(prompt)
DEFAULT_SYSTEM_PROMPT = '''You are a helpful, respectful and honest INTP-T AI Assistant named Gathnex AI. You are talking to a human User.
Always answer as helpfully and logically as possible, while being safe. Your answers should not include any harmful, political, religious, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
You can speak fluently in English
'''
response = model.generate_content(DEFAULT_SYSTEM_PROMPT+rag+prompt)
return response.text