katanaml commited on
Commit
dca3ac6
·
1 Parent(s): 42cd5f6

Added sparrow key support

Browse files
Files changed (2) hide show
  1. api.py +26 -0
  2. config.yml +2 -0
api.py CHANGED
@@ -8,6 +8,8 @@ from typing import Annotated
8
  import json
9
  import argparse
10
  from dotenv import load_dotenv
 
 
11
  import os
12
  from rich import print
13
 
@@ -18,6 +20,9 @@ warnings.filterwarnings("ignore", category=DeprecationWarning)
18
  # Load environment variables from .env file
19
  load_dotenv()
20
 
 
 
 
21
 
22
  # add asyncio to the pipeline
23
 
@@ -48,8 +53,19 @@ async def inference(
48
  group_by_rows: Annotated[bool, Form()] = True,
49
  update_targets: Annotated[bool, Form()] = True,
50
  debug: Annotated[bool, Form()] = False,
 
51
  file: UploadFile = File(None)
52
  ):
 
 
 
 
 
 
 
 
 
 
53
  query = 'retrieve ' + fields
54
  query_types = types
55
 
@@ -84,8 +100,18 @@ async def inference(
84
  async def ingest(
85
  agent: Annotated[str, Form()],
86
  index_name: Annotated[str, Form()],
 
87
  file: UploadFile = File()
88
  ):
 
 
 
 
 
 
 
 
 
89
  try:
90
  answer = await run_from_api_ingest(agent, index_name, file, False)
91
  except ValueError as e:
 
8
  import json
9
  import argparse
10
  from dotenv import load_dotenv
11
+ import box
12
+ import yaml
13
  import os
14
  from rich import print
15
 
 
20
  # Load environment variables from .env file
21
  load_dotenv()
22
 
23
+ # Import config vars
24
+ with open('config.yml', 'r', encoding='utf8') as ymlfile:
25
+ cfg = box.Box(yaml.safe_load(ymlfile))
26
 
27
  # add asyncio to the pipeline
28
 
 
53
  group_by_rows: Annotated[bool, Form()] = True,
54
  update_targets: Annotated[bool, Form()] = True,
55
  debug: Annotated[bool, Form()] = False,
56
+ sparrow_key: Annotated[str, Form()] = None,
57
  file: UploadFile = File(None)
58
  ):
59
+
60
+ protected_access = cfg.PROTECTED_ACCESS
61
+ if protected_access:
62
+ # Retrieve all environment variables that start with 'SPARROW_KEY_'
63
+ sparrow_keys = {key: value for key, value in os.environ.items() if key.startswith('SPARROW_KEY_')}
64
+
65
+ # Check if the provided sparrow_key matches any of the environment variables
66
+ if sparrow_key not in sparrow_keys.values():
67
+ raise HTTPException(status_code=403, detail="Protected access. Agent not allowed.")
68
+
69
  query = 'retrieve ' + fields
70
  query_types = types
71
 
 
100
  async def ingest(
101
  agent: Annotated[str, Form()],
102
  index_name: Annotated[str, Form()],
103
+ sparrow_key: Annotated[str, Form()] = None,
104
  file: UploadFile = File()
105
  ):
106
+ protected_access = cfg.PROTECTED_ACCESS
107
+ if protected_access:
108
+ # Retrieve all environment variables that start with 'SPARROW_KEY_'
109
+ sparrow_keys = {key: value for key, value in os.environ.items() if key.startswith('SPARROW_KEY_')}
110
+
111
+ # Check if the provided sparrow_key matches any of the environment variables
112
+ if sparrow_key not in sparrow_keys.values():
113
+ raise HTTPException(status_code=403, detail="Protected access. Agent not allowed.")
114
+
115
  try:
116
  answer = await run_from_api_ingest(agent, index_name, file, False)
117
  except ValueError as e:
config.yml CHANGED
@@ -1,3 +1,5 @@
 
 
1
  # AGENT FOR LLAMAINDEX
2
  # Tested with these LLMs
3
  #LLM: 'starling-lm:7b-alpha-q4_K_M'
 
1
+ PROTECTED_ACCESS: True
2
+
3
  # AGENT FOR LLAMAINDEX
4
  # Tested with these LLMs
5
  #LLM: 'starling-lm:7b-alpha-q4_K_M'