Petermoyano commited on
Commit
dfe7b5e
·
1 Parent(s): 4c7c2a7

first commit, Naive RAG implemented

Browse files
Files changed (2) hide show
  1. app.py +64 -9
  2. requirements.txt +76 -0
app.py CHANGED
@@ -1,28 +1,83 @@
 
1
  from flask import Flask, request, jsonify
2
  import logging
 
 
 
 
 
 
 
 
 
3
 
4
  app = Flask(__name__)
 
5
 
6
  # Configure logging
7
  logging.basicConfig(level=logging.DEBUG)
8
 
9
- @app.route('/')
10
- def hello_world():
11
- return 'Hello, World!'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  @app.route('/run/predict', methods=['POST'])
14
  def predict():
15
  try:
16
  data = request.json
17
  app.logger.debug(f"Received data: {data}")
18
- response = {
19
- "data": data
20
- }
21
- return jsonify(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  except Exception as e:
23
  app.logger.error(f"Error processing request: {e}")
24
- return jsonify({"error": "An error occurred"}), 500
25
 
26
  if __name__ == '__main__':
27
  from os import environ
28
- app.run(host='0.0.0.0', port=int(environ.get('PORT', 5000)))
 
1
+ import os
2
  from flask import Flask, request, jsonify
3
  import logging
4
+ import openai
5
+ from llama_index.core import Settings
6
+ from llama_index.llms.openai import OpenAI
7
+ from llama_index.vector_stores.pinecone import PineconeVectorStore
8
+ from llama_index.core import VectorStoreIndex, StorageContext
9
+ from dotenv import load_dotenv
10
+ from pinecone import Pinecone # Correct import
11
+
12
+ load_dotenv()
13
 
14
  app = Flask(__name__)
15
+ app.debug = True
16
 
17
  # Configure logging
18
  logging.basicConfig(level=logging.DEBUG)
19
 
20
+ # Set up OpenAI API key
21
+ openai.api_key = os.getenv('OPENAI_API_KEY')
22
+
23
+ # Initialize Pinecone
24
+ pc = Pinecone(
25
+ api_key=os.getenv('PINECONE_API_KEY')
26
+ )
27
+
28
+ # Name of your existing Pinecone index
29
+ PINECONE_INDEX_NAME = os.getenv('PINECONE_INDEX')
30
+
31
+ # Initialize Pinecone index
32
+ pinecone_index = pc.Index(PINECONE_INDEX_NAME)
33
+
34
+ # Set up LlamaIndex global settings
35
+ Settings.llm = OpenAI(
36
+ model=os.getenv('OPENAI_MODEL', 'gpt-3.5-turbo'), # Default to 'gpt-3.5-turbo' if not specified
37
+ temperature=0
38
+ )
39
+
40
+ # Set up Pinecone Vector Store
41
+ vector_store = PineconeVectorStore(
42
+ pinecone_index=pinecone_index,
43
+ namespace=None # Specify a namespace if used during ingestion
44
+ )
45
+
46
+ # Create Storage Context with the Vector Store
47
+ storage_context = StorageContext.from_defaults(vector_store=vector_store)
48
+
49
+ # Initialize LlamaIndex with the existing Pinecone vector store
50
+ index = VectorStoreIndex.from_vector_store(
51
+ vector_store=vector_store,
52
+ storage_context=storage_context
53
+ )
54
 
55
  @app.route('/run/predict', methods=['POST'])
56
  def predict():
57
  try:
58
  data = request.json
59
  app.logger.debug(f"Received data: {data}")
60
+
61
+ if not data:
62
+ app.logger.error("No data provided in the request.")
63
+ return jsonify({'error': 'No data provided.'}), 400
64
+
65
+ user_query = data.get('query')
66
+
67
+ if not user_query:
68
+ app.logger.error("No query provided in the request.")
69
+ return jsonify({'error': 'No query provided.'}), 400
70
+
71
+ # Perform the query using LlamaIndex
72
+ response = index.as_query_engine().query(user_query)
73
+ # app.logger.debug(f"Generated response: {response}")
74
+
75
+ return jsonify({'response': str(response)})
76
+
77
  except Exception as e:
78
  app.logger.error(f"Error processing request: {e}")
79
+ return jsonify({"error": "An error occurred while processing the request"}), 500
80
 
81
  if __name__ == '__main__':
82
  from os import environ
83
+ app.run(host='0.0.0.0', port=int(environ.get('PORT', 7860)))
requirements.txt CHANGED
@@ -1,9 +1,85 @@
 
 
 
 
 
 
 
 
1
  blinker==1.8.2
 
 
2
  click==8.1.7
 
 
 
 
 
3
  Flask==3.0.3
 
 
 
4
  gunicorn==23.0.0
 
 
 
 
5
  itsdangerous==2.2.0
6
  Jinja2==3.1.4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  MarkupSafe==2.1.5
 
 
 
 
 
 
 
 
8
  packaging==24.1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  Werkzeug==3.0.4
 
 
 
1
+ aiohappyeyeballs==2.4.0
2
+ aiohttp==3.10.5
3
+ aiosignal==1.3.1
4
+ annotated-types==0.7.0
5
+ anyio==4.5.0
6
+ async-timeout==4.0.3
7
+ attrs==24.2.0
8
+ beautifulsoup4==4.12.3
9
  blinker==1.8.2
10
+ certifi==2024.8.30
11
+ charset-normalizer==3.3.2
12
  click==8.1.7
13
+ dataclasses-json==0.6.7
14
+ Deprecated==1.2.14
15
+ dirtyjson==1.0.8
16
+ distro==1.9.0
17
+ exceptiongroup==1.2.2
18
  Flask==3.0.3
19
+ frozenlist==1.4.1
20
+ fsspec==2024.9.0
21
+ greenlet==3.1.0
22
  gunicorn==23.0.0
23
+ h11==0.14.0
24
+ httpcore==1.0.5
25
+ httpx==0.27.2
26
+ idna==3.10
27
  itsdangerous==2.2.0
28
  Jinja2==3.1.4
29
+ jiter==0.5.0
30
+ joblib==1.4.2
31
+ llama-cloud==0.0.17
32
+ llama-index==0.11.10
33
+ llama-index-agent-openai==0.3.3
34
+ llama-index-cli==0.3.1
35
+ llama-index-core==0.11.10
36
+ llama-index-embeddings-openai==0.2.5
37
+ llama-index-indices-managed-llama-cloud==0.3.1
38
+ llama-index-legacy==0.9.48.post3
39
+ llama-index-llms-openai==0.2.9
40
+ llama-index-multi-modal-llms-openai==0.2.1
41
+ llama-index-program-openai==0.2.0
42
+ llama-index-question-gen-openai==0.2.0
43
+ llama-index-readers-file==0.2.2
44
+ llama-index-readers-llama-parse==0.3.0
45
+ llama-index-vector-stores-pinecone==0.2.1
46
+ llama-parse==0.5.6
47
  MarkupSafe==2.1.5
48
+ marshmallow==3.22.0
49
+ multidict==6.1.0
50
+ mypy-extensions==1.0.0
51
+ nest-asyncio==1.6.0
52
+ networkx==3.3
53
+ nltk==3.9.1
54
+ numpy==1.26.4
55
+ openai==1.46.1
56
  packaging==24.1
57
+ pandas==2.2.2
58
+ pillow==10.4.0
59
+ pinecone-client==5.0.1
60
+ pinecone-plugin-inference==1.1.0
61
+ pinecone-plugin-interface==0.0.7
62
+ pydantic==2.9.2
63
+ pydantic_core==2.23.4
64
+ pypdf==4.3.1
65
+ python-dateutil==2.9.0.post0
66
+ python-dotenv==1.0.1
67
+ pytz==2024.2
68
+ PyYAML==6.0.2
69
+ regex==2024.9.11
70
+ requests==2.32.3
71
+ six==1.16.0
72
+ sniffio==1.3.1
73
+ soupsieve==2.6
74
+ SQLAlchemy==2.0.35
75
+ striprtf==0.0.26
76
+ tenacity==8.5.0
77
+ tiktoken==0.7.0
78
+ tqdm==4.66.5
79
+ typing-inspect==0.9.0
80
+ typing_extensions==4.12.2
81
+ tzdata==2024.1
82
+ urllib3==2.2.3
83
  Werkzeug==3.0.4
84
+ wrapt==1.16.0
85
+ yarl==1.11.1