Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from openai import OpenAI
|
4 |
+
from llama_index.node_parser import SemanticSplitterNodeParser
|
5 |
+
from llama_index.embeddings import OpenAIEmbedding
|
6 |
+
from llama_index.ingestion import IngestionPipeline
|
7 |
+
from pinecone.grpc import PineconeGRPC
|
8 |
+
from pinecone import ServerlessSpec
|
9 |
+
from llama_index.vector_stores import PineconeVectorStore
|
10 |
+
from llama_index import VectorStoreIndex
|
11 |
+
from llama_index.retrievers import VectorIndexRetriever
|
12 |
+
from llama_index.query_engine import RetrieverQueryEngine
|
13 |
+
|
14 |
+
# Set OpenAI API key from environment variables
|
15 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
16 |
+
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
17 |
+
index_name = os.getenv("INDEX_NAME")
|
18 |
+
|
19 |
+
# Initialize OpenAI client
|
20 |
+
client = OpenAI(api_key=openai_api_key)
|
21 |
+
|
22 |
+
# Initialize connection to Pinecone
|
23 |
+
pc = PineconeGRPC(api_key=pinecone_api_key)
|
24 |
+
|
25 |
+
# Initialize your index
|
26 |
+
if index_name not in pc.list_indexes():
|
27 |
+
spec = ServerlessSpec(replicas=1, pod_type="p1")
|
28 |
+
pc.create_index(name=index_name, dimension=1536, spec=spec)
|
29 |
+
|
30 |
+
pinecone_index = pc.Index(index_name)
|
31 |
+
|
32 |
+
# Initialize VectorStore
|
33 |
+
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
34 |
+
pinecone_index.describe_index_stats()
|
35 |
+
|
36 |
+
# Initialize vector index and retriever
|
37 |
+
vector_index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
|
38 |
+
retriever = VectorIndexRetriever(index=vector_index, similarity_top_k=5)
|
39 |
+
query_engine = RetrieverQueryEngine(retriever=retriever)
|
40 |
+
|
41 |
+
# Set up LlamaIndex embedding model and pipeline
|
42 |
+
embed_model = OpenAIEmbedding(api_key=openai_api_key)
|
43 |
+
pipeline = IngestionPipeline(
|
44 |
+
transformations=[
|
45 |
+
SemanticSplitterNodeParser(buffer_size=1, breakpoint_percentile_threshold=95, embed_model=embed_model),
|
46 |
+
embed_model,
|
47 |
+
],
|
48 |
+
)
|
49 |
+
|
50 |
+
def query_annual_report(query):
|
51 |
+
response = query_engine.query(query)
|
52 |
+
return response.response
|
53 |
+
|
54 |
+
# Streamlit app setup
|
55 |
+
st.title("ChatGPT-like Clone with Pinecone Integration")
|
56 |
+
|
57 |
+
# Initialize chat history
|
58 |
+
if "messages" not in st.session_state:
|
59 |
+
st.session_state.messages = []
|
60 |
+
|
61 |
+
# Display chat messages from history
|
62 |
+
for message in st.session_state.messages:
|
63 |
+
with st.chat_message(message["role"]):
|
64 |
+
st.markdown(message["content"])
|
65 |
+
|
66 |
+
# Accept user input
|
67 |
+
if prompt := st.chat_input("What is up?"):
|
68 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
69 |
+
with st.chat_message("user"):
|
70 |
+
st.markdown(prompt)
|
71 |
+
|
72 |
+
with st.chat_message("assistant"):
|
73 |
+
response = query_annual_report(prompt)
|
74 |
+
st.markdown(response)
|
75 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|