Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnaik06 authored May 16, 2024
1 parent b4a48cf commit f102769
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
86 changes: 86 additions & 0 deletions objectbox/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import streamlit as st
import os
from langchain_groq import ChatGroq
from langchain_openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import create_retrieval_chain
from langchain_objectbox.vectorstores import ObjectBox
from langchain_community.document_loaders import PyPDFDirectoryLoader

from dotenv import load_dotenv
load_dotenv()

## load the Groq And OpenAI Api Key
os.environ['OPEN_API_KEY']=os.getenv("OPENAI_API_KEY")
groq_api_key=os.getenv('GROQ_API_KEY')

st.title("Objectbox VectorstoreDB With Llama3 Demo")

llm=ChatGroq(groq_api_key=groq_api_key,
model_name="Llama3-8b-8192")

prompt=ChatPromptTemplate.from_template(
"""
Answer the questions based on the provided context only.
Please provide the most accurate response based on the question
<context>
{context}
<context>
Questions:{input}
"""

)


## Vector Enbedding and Objectbox Vectorstore db

def vector_embedding():

if "vectors" not in st.session_state:
st.session_state.embeddings=OpenAIEmbeddings()
st.session_state.loader=PyPDFDirectoryLoader("./us_census") ## Data Ingestion
st.session_state.docs=st.session_state.loader.load() ## Documents Loading
st.session_state.text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200)
st.session_state.final_documents=st.session_state.text_splitter.split_documents(st.session_state.docs[:20])
st.session_state.vectors=ObjectBox.from_documents(st.session_state.final_documents,st.session_state.embeddings,embedding_dimensions=768)


input_prompt=st.text_input("Enter Your Question From Documents")

if st.button("Documents Embedding"):
vector_embedding()
st.write("ObjectBox Database is ready")

import time

if input_prompt:
document_chain=create_stuff_documents_chain(llm,prompt)
retriever=st.session_state.vectors.as_retriever()
retrieval_chain=create_retrieval_chain(retriever,document_chain)
start=time.process_time()

response=retrieval_chain.invoke({'input':input_prompt})

print("Response time :",time.process_time()-start)
st.write(response['answer'])

# With a streamlit expander
with st.expander("Document Similarity Search"):
# Find the relevant chunks
for i, doc in enumerate(response["context"]):
st.write(doc.page_content)
st.write("--------------------------------")











17 changes: 17 additions & 0 deletions objectbox/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import requests

# Create Chat Session
url = "https://gateway-dev.on-demand.io/chat/v1/sessions"
headers = { "apikey": "FSs5SmWWSaqrstpxZ87v1NrACHnT0ozk" }
body = { "pluginIds": [], "externalUserId": "plugin-1713962163" }
response = requests.post(url, headers=headers, json=body)
chatSession = response.json()
print(chatSession)
session_id = chatSession['chatSession']['id']

# Answer Query
url = f"https://gateway-dev.on-demand.io/chat/v1/sessions/{session_id}/query"
headers = { "apikey": "FSs5SmWWSaqrstpxZ87v1NrACHnT0ozk" }
body = { "endpointId": "predefined-openai-gpt4o", "query": "What is the weather in Bangalore", "pluginIds": ["plugin-1713924030"], "responseMode": "sync" }
response = requests.post(url, headers=headers, json=body)
print(response.json())
Binary file added objectbox/us_census/acsbr-015.pdf
Binary file not shown.
Binary file added objectbox/us_census/acsbr-016.pdf
Binary file not shown.
Binary file added objectbox/us_census/acsbr-017.pdf
Binary file not shown.
Binary file added objectbox/us_census/p70-178.pdf
Binary file not shown.

0 comments on commit f102769

Please sign in to comment.