| import sqlite3 | |
| import psycopg2 | |
| from .stats_tools import stats_tools | |
| from .chart_tools import chart_tools | |
| from utils import TEMP_DIR | |
| def data_file_tools_call(session_hash): | |
| dir_path = TEMP_DIR / str(session_hash) | |
| connection = sqlite3.connect(f'{dir_path}/file_upload/data_source.db') | |
| print("Querying Database in Tools.py"); | |
| cur=connection.execute('select * from data_source') | |
| columns = [i[0] for i in cur.description] | |
| print("COLUMNS 2") | |
| print(columns) | |
| cur.close() | |
| connection.close() | |
| column_string = (columns[:625] + '..') if len(columns) > 625 else columns | |
| tools_calls = [ | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "sql_query_func", | |
| "description": f"""This is a tool useful to query a SQLite table called 'data_source' with the following Columns: {column_string}. | |
| There may also be more columns in the table if the number of columns is too large to process. | |
| This function also saves the results of the query to csv file called query.csv.""", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "queries": { | |
| "type": "array", | |
| "description": "The query to use in the search. Infer this from the user's message. It should be a question or a statement", | |
| "items": { | |
| "type": "string", | |
| } | |
| } | |
| }, | |
| "required": ["queries"], | |
| }, | |
| }, | |
| }, | |
| ] | |
| tools_calls.extend(chart_tools) | |
| tools_calls.extend(stats_tools) | |
| return tools_calls | |
| def sql_tools_call(db_tables): | |
| table_string = (db_tables[:625] + '..') if len(db_tables) > 625 else db_tables | |
| tools_calls = [ | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "sql_query_func", | |
| "description": f"""This is a tool useful to query a PostgreSQL database with the following tables, {table_string}. | |
| There may also be more tables in the database if the number of tables is too large to process. | |
| This function also saves the results of the query to csv file called query.csv.""", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "queries": { | |
| "type": "array", | |
| "description": "The PostgreSQL query to use in the search. Infer this from the user's message. It should be a question or a statement", | |
| "items": { | |
| "type": "string", | |
| } | |
| } | |
| }, | |
| "required": ["queries"], | |
| }, | |
| }, | |
| }, | |
| ] | |
| tools_calls.extend(chart_tools) | |
| tools_calls.extend(stats_tools) | |
| return tools_calls | |
| def doc_db_tools_call(db_collections): | |
| collection_string = (db_collections[:625] + '..') if len(db_collections) > 625 else db_collections | |
| tools_calls = [ | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "doc_db_query_func", | |
| "description": f"""This is a tool useful to build an aggregation pipeline to query a MongoDB NoSQL document database with the following collections, {collection_string}. | |
| There may also be more collections in the database if the number of tables is too large to process. | |
| This function also saves the results of the query to a csv file called query.csv.""", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "aggregation_pipeline": { | |
| "type": "string", | |
| "description": "The MongoDB aggregation pipeline to use in the search. Infer this from the user's message. It should be a question or a statement." | |
| }, | |
| "db_collection": { | |
| "type": "string", | |
| "description": "The MongoDB collection to use in the search. Infer this from the user's message. It should be a question or a statement.", | |
| } | |
| }, | |
| "required": ["aggregation_pipeline","db_collection"], | |
| }, | |
| }, | |
| }, | |
| ] | |
| tools_calls.extend(chart_tools) | |
| tools_calls.extend(stats_tools) | |
| return tools_calls | |
| def graphql_tools_call(graphql_types): | |
| types_string = (graphql_types[:625] + '..') if len(graphql_types) > 625 else graphql_types | |
| tools_calls = [ | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "graphql_query_func", | |
| "description": f"""This is a tool useful to build a GraphQL query for a GraphQL API endpoint with the following types, {types_string}. | |
| There may also be more types in the GraphQL endpoint if the number of types is too large to process. | |
| This function also saves the results of the query to a csv file called query.csv.""", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "graphql_query": { | |
| "type": "string", | |
| "description": "The GraphQL query to use in the search. Infer this from the user's message. It should be a question or a statement." | |
| } | |
| }, | |
| "required": ["graphql_query"], | |
| }, | |
| }, | |
| }, | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "graphql_schema_query", | |
| "description": f"""This is a tool useful to query a GraphQL type and receive back information about its schema. This is useful because | |
| the GraphQL introspection query is too large to be ingested all at once and this allows us to query the schema one type at a time to | |
| view it in manageable bites. You may realize after viewing the schema, that the type you selected was not appropriate for the question | |
| you are attempting answer. You may then query additional types to find the appropriate types to use for your GraphQL API query.""", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "graphql_type": { | |
| "type": "string", | |
| "description": "The GraphQL type that we want to view the schema of in order to make the proper query with our graphql_query_func. Infer this from the user's message. It should be a question or a statement." | |
| } | |
| }, | |
| "required": ["graphql_type"], | |
| }, | |
| }, | |
| }, | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "graphql_csv_query", | |
| "description": f"""This is a tool useful to SQL query our query.csv file that is generated from our GraphQL query. This is useful in a situation | |
| where the results of the GraphQL query need additional querying to answer the user question. The query.csv file is converted to a Pandas dataframe | |
| and we query that dataframe with SQL on a table called 'query' before converting it back to a csv file.""", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "csv_query": { | |
| "type": "string", | |
| "description": "The pandas dataframe SQL query to use in the search. The table that we query is named 'query'. Infer this from the user's message. It should be a question or a statement" | |
| } | |
| }, | |
| "required": ["csv_query"], | |
| }, | |
| }, | |
| }, | |
| ] | |
| tools_calls.extend(chart_tools) | |
| tools_calls.extend(stats_tools) | |
| return tools_calls |