jaothan commited on
Commit
9aa98b6
·
verified ·
1 Parent(s): 891fbea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -103
app.py CHANGED
@@ -1,103 +1,106 @@
1
- import click
2
- import logging
3
- import sys
4
- import yaml
5
- import os
6
- from langchain.llms import AzureOpenAI, OpenAI
7
- from app.api_funcs import get_job_infos, get_run, get_model, \
8
- trans_model, batch_mod_permission, prepare_api_docs
9
- from pathlib import Path
10
- logger = logging.getLogger(__name__)
11
- logger.setLevel(logging.DEBUG)
12
- handler = logging.StreamHandler(sys.stdout)
13
- handler.setLevel(logging.DEBUG)
14
- logger.addHandler(handler)
15
-
16
- PATH = Path(os.path.abspath(os.path.dirname(__file__)))
17
-
18
- # Open the YAML file
19
- conf_path = PATH / "app" / 'llm.yaml'
20
- with open(conf_path) as f:
21
- config = yaml.safe_load(f)
22
-
23
- # Use AzureOpenAI, if config contains deployment name, otherwise OpenAI
24
- if config['model'].get('deployment_name', False):
25
- llm = AzureOpenAI(**config['model'])
26
- else:
27
- llm = OpenAI(**config['model'])
28
-
29
-
30
- headers = {"Authorization": f"Bearer {os.getenv('DBR_BEARER_TOKEN')}"}
31
- updated_api_docs = prepare_api_docs()
32
-
33
-
34
- def comma_list(comma_str: str):
35
- return comma_str.split(',')
36
-
37
- def determine_api_text(updated_api_docs: dict, query: str):
38
- pick_api_prompt = """Please return the file name from the list {api_docs}
39
- that best corresponds to the following query: {query}. \
40
- DO NOT EXPLAIN your answer!
41
- """
42
- api_docs = os.listdir(PATH / "app" / "dbr_api_docs")
43
- selected_api_doc = llm(pick_api_prompt.format(api_docs=api_docs, query=query)).lstrip().rstrip()
44
- logger.info(f"\nSelecting the following api document: {selected_api_doc}")
45
- api_text = updated_api_docs[selected_api_doc]
46
- return api_text, selected_api_doc
47
-
48
- # Add subcommands for commands
49
- @click.group()
50
- def cli():
51
- pass
52
-
53
- @cli.group(help='Run machine learning model.')
54
- def ml():
55
- pass
56
-
57
- # Add commands for specific subcommands of 'ml'
58
- @ml.command(help='Get information about a model.')
59
- @click.argument('query', type=str)
60
- def get_model_info(query):
61
- # Instruction to get model infos
62
- api_text, _ = determine_api_text(updated_api_docs, query)
63
- logger.info(get_model(llm, query, api_text, headers))
64
-
65
- @ml.command(help='Get information about a model run.')
66
- @click.argument('run_id', type=str)
67
- @click.argument('query', type=str)
68
- def get_run_info(query, run_id):
69
- # ID of the model run for which you'd like information.
70
- # Which information should be pulled from the run?
71
- api_text, _ = determine_api_text(updated_api_docs, query)
72
- logger.info(get_run(llm, run_id, query, api_text, headers))
73
-
74
- @ml.command(help='Transition a model from one state to another.')
75
- @click.argument('query', type=str)
76
- def transition_model(query):
77
- # Instruction to transition a model.
78
- api_text, _ = determine_api_text(updated_api_docs, query)
79
- trans_model(llm, query, api_text, headers)
80
-
81
- @cli.command(help='View job history.')
82
- @click.argument('query', type=str)
83
- def jobs(query):
84
- if ";" not in query:
85
- query = query + ";"
86
- query, response_query = query.split(";")
87
- api_text, _ = determine_api_text(updated_api_docs, query)
88
- # The query for the LLM + an optional query for the API response
89
- logger.info(get_job_infos(llm, query, response_query, api_text, headers))
90
-
91
- @cli.command(help='Manage user permissions.')
92
- @click.argument('query', type=str)
93
- @click.argument('jobs', type=comma_list)
94
- def permissions(jobs, query):
95
- api_text, api_name = determine_api_text(updated_api_docs, query)
96
- # Add/Get user permissions.
97
- batch_mod_permission(
98
- logger, llm, updated_api_docs, api_text, api_name, headers,
99
- query, jobs=jobs
100
- )
101
-
102
- if __name__ == '__main__':
103
- cli()
 
 
 
 
1
+ #https://www.youtube.com/watch?v=AREBu2B5H3M&t=140s
2
+
3
+
4
+ import click
5
+ import logging
6
+ import sys
7
+ import yaml
8
+ import os
9
+ from langchain.llms import AzureOpenAI, OpenAI
10
+ from app.api_funcs import get_job_infos, get_run, get_model, \
11
+ trans_model, batch_mod_permission, prepare_api_docs
12
+ from pathlib import Path
13
+ logger = logging.getLogger(__name__)
14
+ logger.setLevel(logging.DEBUG)
15
+ handler = logging.StreamHandler(sys.stdout)
16
+ handler.setLevel(logging.DEBUG)
17
+ logger.addHandler(handler)
18
+
19
+ PATH = Path(os.path.abspath(os.path.dirname(__file__)))
20
+
21
+ # Open the YAML file
22
+ conf_path = PATH / "app" / 'llm.yaml'
23
+ with open(conf_path) as f:
24
+ config = yaml.safe_load(f)
25
+
26
+ # Use AzureOpenAI, if config contains deployment name, otherwise OpenAI
27
+ if config['model'].get('deployment_name', False):
28
+ llm = AzureOpenAI(**config['model'])
29
+ else:
30
+ llm = OpenAI(**config['model'])
31
+
32
+
33
+ headers = {"Authorization": f"Bearer {os.getenv('DBR_BEARER_TOKEN')}"}
34
+ updated_api_docs = prepare_api_docs()
35
+
36
+
37
+ def comma_list(comma_str: str):
38
+ return comma_str.split(',')
39
+
40
+ def determine_api_text(updated_api_docs: dict, query: str):
41
+ pick_api_prompt = """Please return the file name from the list {api_docs}
42
+ that best corresponds to the following query: {query}. \
43
+ DO NOT EXPLAIN your answer!
44
+ """
45
+ api_docs = os.listdir(PATH / "app" / "dbr_api_docs")
46
+ selected_api_doc = llm(pick_api_prompt.format(api_docs=api_docs, query=query)).lstrip().rstrip()
47
+ logger.info(f"\nSelecting the following api document: {selected_api_doc}")
48
+ api_text = updated_api_docs[selected_api_doc]
49
+ return api_text, selected_api_doc
50
+
51
+ # Add subcommands for commands
52
+ @click.group()
53
+ def cli():
54
+ pass
55
+
56
+ @cli.group(help='Run machine learning model.')
57
+ def ml():
58
+ pass
59
+
60
+ # Add commands for specific subcommands of 'ml'
61
+ @ml.command(help='Get information about a model.')
62
+ @click.argument('query', type=str)
63
+ def get_model_info(query):
64
+ # Instruction to get model infos
65
+ api_text, _ = determine_api_text(updated_api_docs, query)
66
+ logger.info(get_model(llm, query, api_text, headers))
67
+
68
+ @ml.command(help='Get information about a model run.')
69
+ @click.argument('run_id', type=str)
70
+ @click.argument('query', type=str)
71
+ def get_run_info(query, run_id):
72
+ # ID of the model run for which you'd like information.
73
+ # Which information should be pulled from the run?
74
+ api_text, _ = determine_api_text(updated_api_docs, query)
75
+ logger.info(get_run(llm, run_id, query, api_text, headers))
76
+
77
+ @ml.command(help='Transition a model from one state to another.')
78
+ @click.argument('query', type=str)
79
+ def transition_model(query):
80
+ # Instruction to transition a model.
81
+ api_text, _ = determine_api_text(updated_api_docs, query)
82
+ trans_model(llm, query, api_text, headers)
83
+
84
+ @cli.command(help='View job history.')
85
+ @click.argument('query', type=str)
86
+ def jobs(query):
87
+ if ";" not in query:
88
+ query = query + ";"
89
+ query, response_query = query.split(";")
90
+ api_text, _ = determine_api_text(updated_api_docs, query)
91
+ # The query for the LLM + an optional query for the API response
92
+ logger.info(get_job_infos(llm, query, response_query, api_text, headers))
93
+
94
+ @cli.command(help='Manage user permissions.')
95
+ @click.argument('query', type=str)
96
+ @click.argument('jobs', type=comma_list)
97
+ def permissions(jobs, query):
98
+ api_text, api_name = determine_api_text(updated_api_docs, query)
99
+ # Add/Get user permissions.
100
+ batch_mod_permission(
101
+ logger, llm, updated_api_docs, api_text, api_name, headers,
102
+ query, jobs=jobs
103
+ )
104
+
105
+ if __name__ == '__main__':
106
+ cli()