diff --git a/text_2_sql/semantic_kernel/rag_with_prompt_based_text_2_sql.ipynb b/text_2_sql/semantic_kernel/Iteration 2 - Prompt Based Text2SQL.ipynb similarity index 99% rename from text_2_sql/semantic_kernel/rag_with_prompt_based_text_2_sql.ipynb rename to text_2_sql/semantic_kernel/Iteration 2 - Prompt Based Text2SQL.ipynb index 112f990..dd5fe3b 100644 --- a/text_2_sql/semantic_kernel/rag_with_prompt_based_text_2_sql.ipynb +++ b/text_2_sql/semantic_kernel/Iteration 2 - Prompt Based Text2SQL.ipynb @@ -239,15 +239,14 @@ "\n", " # Create important information prompt that contains the SQL database information.\n", " engine_specific_rules = \"Use TOP X to limit the number of rows returned instead of LIMIT X. NEVER USE LIMIT X as it produces a syntax error.\"\n", - " important_information_prompt = f\"\"\"\n", + " sql_database_information_prompt = f\"\"\"\n", " [SQL DATABASE INFORMATION]\n", - " {sql_plugin.system_prompt(engine_specific_rules=engine_specific_rules)}\n", + " {sql_plugin.sql_prompt_injection(engine_specific_rules=engine_specific_rules)}\n", " [END SQL DATABASE INFORMATION]\n", " \"\"\"\n", "\n", " arguments = KernelArguments()\n", - " arguments[\"chat_history\"] = chat_history\n", - " arguments[\"important_information\"] = important_information_prompt\n", + " arguments[\"sql_database_information\"] = sql_database_information_prompt\n", " arguments[\"user_input\"] = question\n", "\n", " logging.info(\"Question: %s\", question)\n", diff --git a/text_2_sql/semantic_kernel/rag_with_vector_based_text_2_sql.ipynb b/text_2_sql/semantic_kernel/Iterations 3 & 4 - Vector Based Text2SQL.ipynb similarity index 96% rename from text_2_sql/semantic_kernel/rag_with_vector_based_text_2_sql.ipynb rename to text_2_sql/semantic_kernel/Iterations 3 & 4 - Vector Based Text2SQL.ipynb index 53dbc97..a911381 100644 --- a/text_2_sql/semantic_kernel/rag_with_vector_based_text_2_sql.ipynb +++ b/text_2_sql/semantic_kernel/Iterations 3 & 4 - Vector Based Text2SQL.ipynb @@ -227,15 +227,14 @@ "\n", " # Create important information prompt that contains the SQL database information.\n", " engine_specific_rules = \"Use TOP X to limit the number of rows returned instead of LIMIT X. NEVER USE LIMIT X as it produces a syntax error.\"\n", - " important_information_prompt = f\"\"\"\n", + " sql_database_information_prompt = f\"\"\"\n", " [SQL DATABASE INFORMATION]\n", - " {await sql_plugin.system_prompt(engine_specific_rules=engine_specific_rules, question=question)}\n", + " {await sql_plugin.sql_prompt_injection(engine_specific_rules=engine_specific_rules, question=question)}\n", " [END SQL DATABASE INFORMATION]\n", " \"\"\"\n", "\n", " arguments = KernelArguments()\n", - " arguments[\"chat_history\"] = chat_history\n", - " arguments[\"important_information\"] = important_information_prompt\n", + " arguments[\"sql_database_information\"] = sql_database_information_prompt\n", " arguments[\"user_input\"] = question\n", "\n", " logging.info(\"Question: %s\", question)\n", diff --git a/text_2_sql/semantic_kernel/README.md b/text_2_sql/semantic_kernel/README.md index c98cd60..585c9be 100644 --- a/text_2_sql/semantic_kernel/README.md +++ b/text_2_sql/semantic_kernel/README.md @@ -16,10 +16,8 @@ As the query cache is shared between users (no data is stored in the cache), a n ## Provided Notebooks & Scripts -- `./rag_with_prompt_based_text_2_sql.ipynb` provides example of how to utilise the Prompt Based Text2SQL plugin to query the database. -- `./rag_with_vector_based_text_2_sql.ipynb` provides example of how to utilise the Vector Based Text2SQL plugin to query the database. The query cache plugin will be enabled or disabled depending on the environmental parameters. -- `./rag_with_ai_search_and_text_2_sql.ipynb` provides an example of how to use the Text2SQL and an AISearch plugin in parallel to automatically retrieve data from the most relevant source to answer the query. - - This setup is useful for a production application as the SQL Database is unlikely to be able to answer all the questions a user may ask. +- `./Iteration 2 - Prompt Based Text2SQL.ipynb` provides example of how to utilise the Prompt Based Text2SQL plugin to query the database. +- `./Iterations 3 & 4 - Vector Based Text2SQL.ipynb` provides example of how to utilise the Vector Based Text2SQL plugin to query the database. The query cache plugin will be enabled or disabled depending on the environmental parameters. - `./time_comparison_script.py` provides a utility script for performing time based comparisons between the different approaches. ### ai-search.py diff --git a/text_2_sql/semantic_kernel/plugins/ai_search_plugin/__init__.py b/text_2_sql/semantic_kernel/plugins/ai_search_plugin/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/text_2_sql/semantic_kernel/plugins/ai_search_plugin/ai_search_plugin.py b/text_2_sql/semantic_kernel/plugins/ai_search_plugin/ai_search_plugin.py deleted file mode 100644 index f169a23..0000000 --- a/text_2_sql/semantic_kernel/plugins/ai_search_plugin/ai_search_plugin.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -from semantic_kernel.functions import kernel_function -from typing import Annotated -import os -import json -import logging -from text_2_sql.semantic_kernel.utils.ai_search_utils import run_ai_search_query - - -class AISearchPlugin: - """A plugin that allows for the execution of AI Search queries against a text input.""" - - @staticmethod - def system_prompt() -> str: - """Get the system prompt for the AI Search Plugin.""" - return """Use the AI Search to return documents that have been indexed, that might be relevant for a piece of text to aid understanding. AI Search should always be used, even if you believe it might not be relevant. Execute this in parallel to any other functions that might be relevant.""" - - @kernel_function( - description="Runs an hybrid semantic search against some text to return relevant documents that are indexed within AI Search.", - name="QueryDocumentStorage", - ) - async def query_document_storage( - self, text: Annotated[str, "The text to run a semantic search against."] - ) -> str: - """Sends an text query to AI Search and uses Semantic Ranking to return a result. - - Args: - ---- - text (str): The text to run the search against. - - Returns: - ---- - str: The JSON representation of the search results. - """ - - documents = await run_ai_search_query( - text, - ["ChunkEmbedding"], - ["Title", "Chunk", "SourceUri"], - os.environ["AIService__AzureSearchOptions__RagDocuments__Index"], - os.environ["AIService__AzureSearchOptions__RagDocuments__SemanticConfig"], - ) - - logging.debug("Results: %s", documents) - return json.dumps(documents, default=str) diff --git a/text_2_sql/semantic_kernel/plugins/prompt_based_sql_plugin/prompt_based_sql_plugin.py b/text_2_sql/semantic_kernel/plugins/prompt_based_sql_plugin/prompt_based_sql_plugin.py index 71d58d8..e524cb0 100644 --- a/text_2_sql/semantic_kernel/plugins/prompt_based_sql_plugin/prompt_based_sql_plugin.py +++ b/text_2_sql/semantic_kernel/plugins/prompt_based_sql_plugin/prompt_based_sql_plugin.py @@ -37,7 +37,7 @@ def load_entities(self): entity_name = entity_object["EntityName"].lower() self.entities[entity_name] = entity_object - def system_prompt(self, engine_specific_rules: str | None = None) -> str: + def sql_prompt_injection(self, engine_specific_rules: str | None = None) -> str: """Get the schemas for the database entities and provide a system prompt for the user. Returns: @@ -60,7 +60,7 @@ def system_prompt(self, engine_specific_rules: str | None = None) -> str: engine_specific_rules = f"""\n The following { self.target_engine} Syntax rules must be adhered to.\n {engine_specific_rules}""" - system_prompt = f"""Use the names and descriptions of {self.target_engine} entities provided in ENTITIES LIST to decide which entities to query if you need to retrieve information from the database. Use the 'GetEntitySchema()' function to get more details of the schema of the view you want to query. + sql_prompt_injection = f"""Use the names and descriptions of {self.target_engine} entities provided in ENTITIES LIST to decide which entities to query if you need to retrieve information from the database. Use the 'GetEntitySchema()' function to get more details of the schema of the view you want to query. Always then use the 'RunSQLQuery()' function to run the SQL query against the database. Never just return the SQL query as the answer. @@ -86,7 +86,7 @@ def system_prompt(self, engine_specific_rules: str | None = None) -> str: The source title to cite is the 'entity_name' property. The source reference is the SQL query used. The source chunk is the result of the SQL query used to answer the user query in Markdown table format. e.g. {{ 'title': "vProductAndDescription", 'chunk': '| ProductID | Name | ProductModel | Culture | Description |\\n|-----------|-------------------|--------------|---------|----------------------------------|\\n| 101 | Mountain Bike | MT-100 | en | A durable bike for mountain use. |\\n| 102 | Road Bike | RB-200 | en | Lightweight bike for road use. |\\n| 103 | Hybrid Bike | HB-300 | fr | Vélo hybride pour usage mixte. |\\n', 'reference': 'SELECT ProductID, Name, ProductModel, Culture, Description FROM vProductAndDescription WHERE Culture = \"en\";' }}""" - return system_prompt + return sql_prompt_injection @kernel_function( description="Get the detailed schema of an entity in the Database. Use the entity and the column returned to formulate a SQL query. The view name or table name must be one of the ENTITY NAMES defined in the [ENTITIES LIST]. Only use the column names obtained from GetEntitySchema() when constructing a SQL query, do not make up column names.", diff --git a/text_2_sql/semantic_kernel/plugins/vector_based_sql_plugin/vector_based_sql_plugin.py b/text_2_sql/semantic_kernel/plugins/vector_based_sql_plugin/vector_based_sql_plugin.py index 1db29e8..ae70713 100644 --- a/text_2_sql/semantic_kernel/plugins/vector_based_sql_plugin/vector_based_sql_plugin.py +++ b/text_2_sql/semantic_kernel/plugins/vector_based_sql_plugin/vector_based_sql_plugin.py @@ -211,7 +211,7 @@ async def fetch_queries_from_cache(self, question: str) -> str: return formatted_sql_cache_string - async def system_prompt( + async def sql_prompt_injection( self, engine_specific_rules: str | None = None, question: str | None = None ) -> str: """Get the schemas for the database entities and provide a system prompt for the user. @@ -261,7 +261,7 @@ async def system_prompt( Check the above schemas carefully to see if they can be used to formulate a SQL query. If you need additional schemas, use 'GetEntitySchema()' function to search for the most relevant schemas for the data that you wish to obtain.""" - system_prompt = f"""{query_prompt} + sql_prompt_injection = f"""{query_prompt} If needed, use the 'RunSQLQuery()' function to run the SQL query against the database. Never just return the SQL query as the answer. @@ -280,7 +280,7 @@ async def system_prompt( The source title to cite is the 'EntityName' property. The source reference is the SQL query used. The source chunk is the result of the SQL query used to answer the user query in Markdown table format. e.g. {{ 'title': "vProductAndDescription", 'chunk': '| ProductID | Name | ProductModel | Culture | Description |\\n|-----------|-------------------|--------------|---------|----------------------------------|\\n| 101 | Mountain Bike | MT-100 | en | A durable bike for mountain use. |\\n| 102 | Road Bike | RB-200 | en | Lightweight bike for road use. |\\n| 103 | Hybrid Bike | HB-300 | fr | Vélo hybride pour usage mixte. |\\n', 'reference': 'SELECT ProductID, Name, ProductModel, Culture, Description FROM vProductAndDescription WHERE Culture = \"en\";' }}""" - return system_prompt + return sql_prompt_injection @kernel_function( description="Gets the schema of a view or table in the SQL Database by selecting the most relevant entity based on the search term. Extract key terms from the user question and use these as the search term. Several entities may be returned. Only use when the provided schemas in the system prompt are not sufficient to answer the question.", diff --git a/text_2_sql/semantic_kernel/prompt.yaml b/text_2_sql/semantic_kernel/prompt.yaml index 8f0295e..66e58ae 100644 --- a/text_2_sql/semantic_kernel/prompt.yaml +++ b/text_2_sql/semantic_kernel/prompt.yaml @@ -12,47 +12,35 @@ template: | The response must meet the following requirements: - [RESPONSE OUTPUT REQUIREMENTS] + The answer MUST be in JSON format: + { + "answer": "", + "sources": [ + {"title": , "chunk": , "reference": ""}, + {"title": , "chunk": , "reference": ""} + ] + } - The answer MUST be in JSON format: - { - "answer": "", - "sources": [ - {"title": , "chunk": , "reference": ""}, - {"title": , "chunk": , "reference": ""} - ] - } + [ANSWER PROPERTY REQUIREMENTS] + - **Calculations**: + Use context-provided values and explain calculations briefly. + - **Structure**: + Responses must be direct, easy to understand, and formatted using Markdown. + Use Level 3 and 4 headings, bold sub-headings, and lists where appropriate. Keep font size consistent. + - **Citations**: + Factual statements must be cited using numbered references like [1]. Each citation must match a source in the 'sources' object. - [ANSWER PROPERTY REQUIREMENTS] - - **Language & Tone**: - Use British English, business-friendly language that is professional and clear. - - **Content Restrictions**: - Avoid profanity, offensive language, and code. Rephrase or omit inappropriate content. - - **Information Sources**: - Only use provided functions and important information. Prioritize SQL Database data in case of conflicts. - - **Calculations**: - Use context-provided values and explain calculations briefly. - - **Structure**: - Responses must be direct, easy to understand, and formatted using Markdown. - Use Level 3 and 4 headings, bold sub-headings, and lists where appropriate. Keep font size consistent. - - **Citations**: - Factual statements must be cited using numbered references like [1]. Each citation must match a source in the 'sources' object. + [SOURCES PROPERTY REQUIREMENTS] + - **Reference Inclusion**: + All cited content must have a corresponding reference in the 'sources' object. + - **Source Format**: + Each source must follow this format: {"title": "", "chunk": "", "reference": ""} + - **Source Chunk**: + Include a concise, unedited snippet of relevant context in the 'chunk' property. + - **Mandatory Citations**: + Every source listed must be cited at least once in the answer. - [SOURCES PROPERTY REQUIREMENTS] - - **Reference Inclusion**: - All cited content must have a corresponding reference in the 'sources' object. - - **Source Format**: - Each source must follow this format: {"title": "", "chunk": "", "reference": ""} - - **Source Chunk**: - Include a concise, unedited snippet of relevant context in the 'chunk' property. - - **Mandatory Citations**: - Every source listed must be cited at least once in the answer. - - [IMPORTANT INFORMATION] - - {{$important_information}} - - [END] + {{$sql_database_information}} {{$chat_history}} {{$user_input}} @@ -62,7 +50,7 @@ input_variables: - name: user_input description: The user input is_required: true - - name: important_information + - name: sql_database_information description: Useful information for the chatbot is_required: true output_variable: diff --git a/text_2_sql/semantic_kernel/rag_with_ai_search_and_text_2_sql.ipynb b/text_2_sql/semantic_kernel/rag_with_ai_search_and_text_2_sql.ipynb deleted file mode 100644 index f774448..0000000 --- a/text_2_sql/semantic_kernel/rag_with_ai_search_and_text_2_sql.ipynb +++ /dev/null @@ -1,337 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Copyright (c) Microsoft Corporation.\n", - "\n", - "Licensed under the MIT License." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Text2SQL & AI Search with Semantic Kernel & Azure OpenAI\n", - "\n", - "This notebook demonstrates how the SQL plugin can be integrated with Semantic Kernel and Azure OpenAI to answer questions from the database based on the schemas provided. Additionally, it integrates with an AI Search plugin to show how both can be used in parallel to answer questions based on the best possible source. The prompt may need to be tweaked for your usage.\n", - "\n", - "A multi-shot approach is used for SQL generation for more reliable results and reduced token usage. More details can be found in the README.md." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "gather": { - "logged": 1718623217703 - }, - "jupyter": { - "outputs_hidden": false, - "source_hidden": false - }, - "nteract": { - "transient": { - "deleting": false - } - } - }, - "outputs": [], - "source": [ - "import logging\n", - "import os\n", - "import yaml\n", - "import dotenv\n", - "import json\n", - "from semantic_kernel.connectors.ai.open_ai import (\n", - " AzureChatCompletion,\n", - ")\n", - "from semantic_kernel.contents.chat_history import ChatHistory\n", - "from semantic_kernel.kernel import Kernel\n", - "from plugins.prompt_based_sql_plugin.prompt_based_sql_plugin import PromptBasedSQLPlugin\n", - "from plugins.ai_search_plugin.ai_search_plugin import AISearchPlugin\n", - "from semantic_kernel.functions.kernel_arguments import KernelArguments\n", - "from semantic_kernel.prompt_template.prompt_template_config import PromptTemplateConfig\n", - "from IPython.display import display, Markdown\n", - "\n", - "logging.basicConfig(level=logging.INFO)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Kernel Setup" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "dotenv.load_dotenv()\n", - "kernel = Kernel()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Set up GPT connections" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "gather": { - "logged": 1718623218006 - }, - "jupyter": { - "outputs_hidden": false, - "source_hidden": false - }, - "nteract": { - "transient": { - "deleting": false - } - } - }, - "outputs": [], - "source": [ - "service_id = \"chat\"" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "gather": { - "logged": 1718623218267 - }, - "jupyter": { - "outputs_hidden": false, - "source_hidden": false - }, - "nteract": { - "transient": { - "deleting": false - } - } - }, - "outputs": [], - "source": [ - "chat_service = AzureChatCompletion(\n", - " service_id=service_id,\n", - " deployment_name=os.environ[\"OpenAI__CompletionDeployment\"],\n", - " endpoint=os.environ[\"OpenAI__Endpoint\"],\n", - " api_key=os.environ[\"OpenAI__ApiKey\"],\n", - ")\n", - "kernel.add_service(chat_service)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "gather": { - "logged": 1718623218614 - }, - "jupyter": { - "outputs_hidden": false, - "source_hidden": false - }, - "nteract": { - "transient": { - "deleting": false - } - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "KernelPlugin(name='SQL', description=None, functions={'GetEntitySchema': KernelFunctionFromMethod(metadata=KernelFunctionMetadata(name='GetEntitySchema', plugin_name='SQL', description='Get the detailed schema of an entity in the Database. Use the entity and the column returned to formulate a SQL query. The view name or table name must be one of the ENTITY NAMES defined in the [ENTITIES LIST]. Only use the column names obtained from GetEntitySchema() when constructing a SQL query, do not make up column names.', parameters=[KernelParameterMetadata(name='entity_name', description='The view or table name to get the schema for. It must be one of the ENTITY NAMES defined in the [ENTITIES LIST] function.', default_value=None, type_='str', is_required=True, type_object=, schema_data={'type': 'string', 'description': 'The view or table name to get the schema for. It must be one of the ENTITY NAMES defined in the [ENTITIES LIST] function.'}, function_schema_include=True)], is_prompt=False, is_asynchronous=True, return_parameter=KernelParameterMetadata(name='return', description='', default_value=None, type_='str', is_required=True, type_object=, schema_data={'type': 'string'}, function_schema_include=True), additional_properties={}), invocation_duration_histogram=, streaming_duration_histogram=, method=>, stream_method=None), 'RunSQLQuery': KernelFunctionFromMethod(metadata=KernelFunctionMetadata(name='RunSQLQuery', plugin_name='SQL', description='Runs an SQL query against the SQL Database to extract information.', parameters=[KernelParameterMetadata(name='sql_query', description='The SQL query to run against the DB', default_value=None, type_='str', is_required=True, type_object=, schema_data={'type': 'string', 'description': 'The SQL query to run against the DB'}, function_schema_include=True)], is_prompt=False, is_asynchronous=True, return_parameter=KernelParameterMetadata(name='return', description='', default_value=None, type_='str', is_required=True, type_object=, schema_data={'type': 'string'}, function_schema_include=True), additional_properties={}), invocation_duration_histogram=, streaming_duration_histogram=, method=>, stream_method=None)})" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Register the SQL Plugin with the Database name to use.\n", - "sql_plugin = PromptBasedSQLPlugin(database=os.environ[\"Text2Sql__DatabaseName\"])\n", - "kernel.add_plugin(sql_plugin, \"SQL\")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "KernelPlugin(name='AISearch', description=None, functions={'QueryDocumentStorage': KernelFunctionFromMethod(metadata=KernelFunctionMetadata(name='QueryDocumentStorage', plugin_name='AISearch', description='Runs an hybrid semantic search against some text to return relevant documents that are indexed within AI Search.', parameters=[KernelParameterMetadata(name='text', description='The text to run a semantic search against.', default_value=None, type_='str', is_required=True, type_object=, schema_data={'type': 'string', 'description': 'The text to run a semantic search against.'}, function_schema_include=True)], is_prompt=False, is_asynchronous=True, return_parameter=KernelParameterMetadata(name='return', description='', default_value=None, type_='str', is_required=True, type_object=, schema_data={'type': 'string'}, function_schema_include=True), additional_properties={}), invocation_duration_histogram=, streaming_duration_histogram=, method=>, stream_method=None)})" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ai_search_plugin = AISearchPlugin()\n", - "kernel.add_plugin(ai_search_plugin, \"AISearch\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "nteract": { - "transient": { - "deleting": false - } - } - }, - "source": [ - "## Prompt Setup" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "# Load prompt and execution settings from the file\n", - "with open(\"./prompt.yaml\", \"r\") as file:\n", - " data = yaml.safe_load(file.read())\n", - " prompt_template_config = PromptTemplateConfig(**data)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "chat_function = kernel.add_function(\n", - " prompt_template_config=prompt_template_config,\n", - " plugin_name=\"ChatBot\",\n", - " function_name=\"Chat\",\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## ChatBot setup" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "history = ChatHistory()" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "async def ask_question(question: str, chat_history: ChatHistory) -> str:\n", - " \"\"\"Asks a question to the chatbot and returns the answer.\n", - " \n", - " Args:\n", - " question (str): The question to ask the chatbot.\n", - " chat_history (ChatHistory): The chat history object.\n", - " \n", - " Returns:\n", - " str: The answer from the chatbot.\n", - " \"\"\"\n", - "\n", - " # Create important information prompt that contains the SQL database information and the AI search description (useful if you have multiple indexes).\n", - " engine_specific_rules = \"Use TOP X to limit the number of rows returned instead of LIMIT X. NEVER USE LIMIT X as it produces a syntax error.\"\n", - " important_information_prompt = f\"\"\"\n", - " [AI SEARCH INFORMATION]\n", - " {ai_search_plugin.system_prompt()}\n", - " [END AI SEARCH INFORMATION]\n", - "\n", - " [SQL DATABASE INFORMATION]\n", - " {sql_plugin.system_prompt(engine_specific_rules=engine_specific_rules)}\n", - " [END SQL DATABASE INFORMATION]\n", - " \"\"\"\n", - "\n", - " arguments = KernelArguments()\n", - " arguments[\"chat_history\"] = chat_history\n", - " arguments[\"important_information\"] = important_information_prompt\n", - " arguments[\"user_input\"] = question\n", - "\n", - " logging.info(\"Question: %s\", question)\n", - "\n", - " answer = await kernel.invoke(\n", - " function_name=\"Chat\",\n", - " plugin_name=\"ChatBot\",\n", - " arguments=arguments,\n", - " chat_history=chat_history,\n", - " )\n", - "\n", - " logging.info(\"Answer: %s\", answer)\n", - "\n", - " # Log the question and answer to the chat history.\n", - " chat_history.add_user_message(question)\n", - " chat_history.add_message({\"role\": \"assistant\", \"message\": answer})\n", - "\n", - " json_answer = json.loads(str(answer))\n", - "\n", - " display(Markdown(json_answer[\"answer\"]))" - ] - } - ], - "metadata": { - "kernel_info": { - "name": "python310-sdkv2" - }, - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.3" - }, - "microsoft": { - "host": { - "AzureML": { - "notebookHasBeenCompleted": true - } - }, - "ms_spell_check": { - "ms_spell_check_language": "en" - } - }, - "nteract": { - "version": "nteract-front-end@1.0.0" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/text_2_sql/semantic_kernel/time_comparison_script.py b/text_2_sql/semantic_kernel/time_comparison_script.py index 82e2990..18c1452 100644 --- a/text_2_sql/semantic_kernel/time_comparison_script.py +++ b/text_2_sql/semantic_kernel/time_comparison_script.py @@ -90,15 +90,15 @@ async def ask_question_to_prompt_kernel( # Create important information prompt that contains the SQL database information. engine_specific_rules = "Use TOP X at the start of the query to limit the number of rows returned instead of LIMIT X. NEVER USE LIMIT X as it produces a syntax error. e.g. SELECT TOP 10 * FROM table_name" - important_information_prompt = f""" + sql_database_information_prompt = f""" [SQL DATABASE INFORMATION] - {prompt_sql_plugin.system_prompt(engine_specific_rules=engine_specific_rules)} + {prompt_sql_plugin.sql_prompt_injection(engine_specific_rules=engine_specific_rules)} [END SQL DATABASE INFORMATION] """ arguments = KernelArguments() arguments["chat_history"] = chat_history - arguments["important_information"] = important_information_prompt + arguments["sql_database_information"] = sql_database_information_prompt arguments["user_input"] = question logging.info("Question: %s", question) @@ -128,9 +128,9 @@ async def ask_question_to_vector_kernel( # Create important information prompt that contains the SQL database information. engine_specific_rules = "Use TOP X at the start of the query to limit the number of rows returned instead of LIMIT X. NEVER USE LIMIT X as it produces a syntax error. e.g. SELECT TOP 10 * FROM table_name" - important_information_prompt = f""" + sql_database_information_prompt = f""" [SQL DATABASE INFORMATION] - {await vector_sql_plugin.system_prompt( + {await vector_sql_plugin.sql_prompt_injection( engine_specific_rules=engine_specific_rules, question=question)} [END SQL DATABASE INFORMATION] """ @@ -138,8 +138,7 @@ async def ask_question_to_vector_kernel( arguments = KernelArguments() arguments = KernelArguments() - arguments["chat_history"] = chat_history - arguments["important_information"] = important_information_prompt + arguments["sql_database_information"] = sql_database_information_prompt arguments["user_input"] = question logging.info("Question: %s", question)