diff --git a/09_BusinessAIWeek/Test/00-connect-AICore-and-AILaunchpad.md b/09_BusinessAIWeek/Test/00-connect-AICore-and-AILaunchpad.md deleted file mode 100644 index 2bc4981..0000000 --- a/09_BusinessAIWeek/Test/00-connect-AICore-and-AILaunchpad.md +++ /dev/null @@ -1,46 +0,0 @@ -# Setup SAP AI Launchpad and SAP AI Core -[SAP AI Launchpad](https://help.sap.com/docs/ai-launchpad) is a multitenant SaaS application on SAP BTP. You can use SAP AI Launchpad to manage AI use cases across different AI runtimes. SAP AI Launchpad also provides generative AI capabilities via the [Generative AI Hub in SAP AI Core](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/generative-ai-hub-in-sap-ai-core-7db524ee75e74bf8b50c167951fe34a5) and is available in the Cloud Foundry environment. You can also connect the SAP HANA database as an AI runtime to work with the HANA Predictive Analysis Library (PAL), or connect SAP AI Services to work with the SAP AI Service Data Attribute Recommendation. - -## [1/4] Open SAP Business Technology Platform -πŸ‘‰ Open your [BTP cockpit](https://emea.cockpit.btp.cloud.sap/cockpit). - -πŸ‘‰ Navigate to the subaccount: `GenAI CodeJam`. - -## [2/4] Open SAP AI Launchpad and connect to SAP AI Core -πŸ‘‰ Go to **Instances and Subscriptions**. Check whether you see an **SAP AI Core** instance and an **SAP AI Launchpad** subscription. - -![BTP Cockpit](images/btp.png) - -With SAP AI Launchpad you can administer all your machine learning operations. ☝️ Make sure you have the extended plan of SAP AI Core to leverage Generative AI Hub. To connect SAP AI Core to SAP AI Launchpad you need the SAP AI Core service key. - -![SAP AI Core service key](images/service-key.png) - -☝️ In this subaccount the connection between the SAP AI Core service instance and the SAP AI Launchpad application is already established. Otherwise you would have to add a new runtime using the SAP AI Core service key information. - -πŸ‘‰ Open **SAP AI Launchpad**. - -## [3/4] Create a new resource group for your team -SAP AI Core tenants use [resource groups](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/resource-groups) to isolate AI resources and workloads. Scenarios (e.g. `foundation-models`) -and executables (a template for training a model or creation of a deployment) are shared across all resource groups. - -> Make sure to create a **NEW** resource group for your team.
DO NOT USE THE DEFAULT RESOURCE GROUP! - -πŸ‘‰ Open the `SAP AI Core Administration` tab and select `Resource Groups`. - -πŸ‘‰ **Create** a new resource group with your team's name. - -![SAP AI Launchpad - Recourse Group 1/2](images/resource_group.png) - -πŸ‘‰ Go back to **Workspaces**. - -πŸ‘‰ Select your connection and your resource group. - -πŸ‘‰ Make sure it is selected. It should show up at the top next to SAP AI Launchpad. - -☝️ You will need the name of your resource group in [Exercise 04-create-embeddings](04-create-embeddings.ipynb). - -> If your resource group does not show up, use the **browser(!)** refresh button to refresh the page. - -![SAP AI Launchpad - Recourse Group 2/2](images/resource_group_2.png) - -[Next Exercise](01-explore-genai-hub.md) diff --git a/09_BusinessAIWeek/Test/01-explore-genai-hub.md b/09_BusinessAIWeek/Test/01-explore-genai-hub.md deleted file mode 100644 index bab7b98..0000000 --- a/09_BusinessAIWeek/Test/01-explore-genai-hub.md +++ /dev/null @@ -1,139 +0,0 @@ -# Explore Generative AI Hub in SAP AI Launchpad - -To leverage large language models (LLMs) or foundation models in your applications, you can use the Generative AI Hub on SAP AI Core. Like most other LLM applications, Generative AI Hub operates on a pay-per-use basis. - -Generative AI Hub offers all major models on the market. There are open-source models that SAP has deployed such as the Falcon model. And there are models that SAP is a proxy for, such as the GPT models, Google models, models provided by Amazon Bedrock and more. You can easily switch between them, compare results, and select the model that works best for your use case. - -SAP maintains strict data privacy contracts with LLM providers to ensure that your data remains secure. - -To start using one of the available models in Generative AI Hub, you need to first deploy it. You can either deploy an orchestration service and then have all available models accessible through that. Or you can deploy a single model directly via the **Model Library**. You can access your deployed models using the Python SDK, the SAP Cloud SDK for AI (JavaScript SDK), any programming language or API platform, or the user interface in SAP AI Launchpad. The SAP AI Launchpad offers a **Model Library**, a **Chat** interface and a **Prompt Editor**, where you can also save prompts and the model's responses. - -## Deploy a proxy via the Model Library - -![Model Library 1/3](images/model-library.png) - -πŸ‘‰ Open the `Generative AI Hub` tab and select `Model Library`. - -πŸ‘‰ Click on **GPT-4o Mini** which is a text generation model that can also process images. - -![Model Library 2/3](images/model-library-2.png) - -πŸ‘‰ Click on `Deploy` **ONE TIME** to make the model endpoint available to you. **It will TAKE A MINUTE to be deployed.** - -πŸ‘‰ You can check the deployment status of the models by clicking on `ML Operations>Deployments`. - -![Model Library 3/3](images/model-library-3.png) - -πŸ‘‰ Click on `Use in Chat` to open the chat interface and start interacting with the model. - -> ☝️ If your model is not deployed yet, the Chat window will ask you to enable the orchestration service. You can do that and then you have all the models available to chat to. If you deploy the orchestration service now, you will not have to do it again in exercise [07-deploy-orchestration-service.md](07-deploy-orchestration-service.md) -![Chat-orchestration](images/enable_orchestration.png) - -If your model is deployed, the chat will look like this: -![Chat](images/chat1.png) - -## Use the Chat in Generative AI Hub - -πŸ‘‰ If you lost the previous chat window you can always open the `Generative AI Hub` tab and select `Chat`. - -πŸ‘‰ Click `Configure` and have a look at the available fields. - -Under `Selected Model` you will find all the deployed models. If there is no deployment this will be empty and you will not be able to chat. If you have more than one large language model deployed you will be able to select which one you want to use here. - -The `Frequency Penalty` parameter allows you to penalize words that appear too frequently in the text, helping the model sound less robotic. - -Similarly, the higher the `Presence Penalty`, the more likely the model is to introduce new topics, as it penalizes words that have already appeared in the text. - -The `Max Tokens` parameter allows you to set the size of the model's input and output. Tokens are not individual words but are typically 4-5 characters long. - -The `Temperature` parameter allows you to control how creative the model should be, determining how flexible it is in selecting the next token in the sequence. - -![Chat 1/2](images/chat2.png) - -In the `Chat Context` tab, right under `Context History`, you can set the number of messages to be sent to the model, determining how much of the chat history should be provided as context for each new request. This is basically the size of the models memory. - -You can also add a `System Message` to describe the role or give more information about what is expected from the model. Additionally, you can provide example inputs and outputs. - -![Chat 2/2](images/chat3.png) - -## Prompt Engineering -πŸ‘‰ Try out different prompt engineering techniques following these examples: - -1. **Zero shot**: - ``` - The capital of the U.S. is: - ``` -2. **Few shot**: - ``` - Germany - Berlin - India - New Delhi - France - - ``` -3. **Chain of thought** (Copy the WHOLE BLOCK into the chat window): - ``` - 1. What is the most important city of a country? - 2. In which country was the hot air balloon originally developed? - 3. What is the >fill in the word from step 1< of the country >fill in the word from step 2<. - ``` - -πŸ‘‰ Try to add something funny to the `System Message` like "always respond like a pirate" and try the prompts again. You can also instruct it to speak more technically, like a developer, or more polished, like in marketing. - -πŸ‘‰ Have the model count letters in words. For example how often the letter **r** occurs in **strawberry**. Can you come up with a prompt that counts it correctly? - -> πŸ‘‰ Before you move on to the next exercise, make sure to also deploy the `Text Embedding 3 Small` model. You will need it later! Clicking the deploy button once is enough, then you can see your deployments under `ML Operations>Deployments`. - -## Use the Prompt Editor in Generative AI Hub -The `Prompt Editor` is useful if you want to save a prompt and its response to revisit later or compare prompts. Often, you can identify tasks that an LLM can help you with on a regular basis. In that case, you can also save different versions of the prompt that work well, saving you from having to write the prompt again each time. - -The parameters you were able to set in the `Chat` can also be set here. Additionally, you can view the number of tokens your prompt used below the response. - -πŸ‘‰ Go over to `Prompt Editor`, select a model and set `Max Tokens` to the maximum again - -πŸ‘‰ Paste the example below and click **Run** to try out the example below. - -πŸ‘‰ Give your prompt a `Name`, a `Collection` name, and **Save** the prompt. - -πŸ‘‰ If you now head over to `Prompt Management` you will find your previously saved prompt there. To run the prompt again click `Open in Prompt Editor`. You can also select other saved prompts by clicking on **Select**. - -1. Chain of thought prompt - customer support (Copy the WHOLE BLOCK into the prompt editor): - ``` - You are working at a big tech company and you are part of the support team. - You are tasked with sorting the incoming support requests into: German, English, Polish or French. - - Review the incoming query. - Identify the language of the query as either German, English, Polish, or French. - - Example: 'bad usability. very confusing user interface.' - English - Count the number of queries for each language: German, English, Polish, and French. - Summarize the key pain points mentioned in the queries in bullet points in English. - - Queries: - - What are the shipping costs to Australia? - - Kann ich einen Artikel ohne Kassenbon umtauschen? - - Offrez-vous des rΓ©ductions pour les achats en gros? - - Can I change the delivery address after placing the order? - - Comment puis-je annuler mon abonnement? - - Wo kann ich den Status meiner Reparatur einsehen? - - What payment methods do you accept? - - Czemu to tak dΕ‚ugo ma iΕ›Δ‡ do WrocΕ‚awia, gdy cena nie jest wcale niska? - - Quel est le dΓ©lai de livraison estimΓ© pour le Mexique? - - Gibt es eine Garantie auf elektronische GerΓ€te? - - I’m having trouble logging into my account, what should I do? - ``` - -![Prompt Editor](images/prompt_editor.png) - -πŸ‘‰ If you still have time. Ask the LLM to come up with different support queries to have more data. - -## Summary - -By this point, you will know how to use the Generative AI Hub user interface in SAP AI Launchpad to query LLMs and store important prompts. You will also understand how to refine the output of a large language model using prompt engineering techniques. - -## Further reading - -* [Generative AI Hub on SAP AI Core - Help Portal (Documentation)](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/generative-ai-hub-in-sap-ai-core-7db524ee75e74bf8b50c167951fe34a5) -* [This](https://www.promptingguide.ai/) is a good resource if you want to know more about prompt engineering. -* [This](https://developers.sap.com/tutorials/ai-core-generative-ai.html) is a good tutorial on how to prompt LLMs with Generative AI Hub. - ---- - -[Next exercise](02-setup-python-environment.md) diff --git a/09_BusinessAIWeek/Test/02-setup-python-environment.md b/09_BusinessAIWeek/Test/02-setup-python-environment.md deleted file mode 100644 index 4c67ad3..0000000 --- a/09_BusinessAIWeek/Test/02-setup-python-environment.md +++ /dev/null @@ -1,147 +0,0 @@ -# Setup SAP Business Application Studio and a dev space -> [SAP Business Application Studio](https://help.sap.com/docs/bas/sap-business-application-studio/what-is-sap-business-application-studio) is based on Code-OSS, an open-source project used for building Visual Studio Code. Available as a cloud service, SAP Business Application Studio provides a desktop-like experience similar to leading IDEs, with command line and optimized editors. - -> At the heart of SAP Business Application Studio are the dev spaces. The dev spaces are comparable to isolated virtual machines in the cloud containing tailored tools and preinstalled runtimes per business scenario, such as SAP Fiori, SAP S/4HANA extensions, Workflow, Mobile and more. This simplifies and speeds up the setup of your development environment, enabling you to efficiently develop, test, build, and run your solutions locally or in the cloud. - -## Open SAP Business Application Studio -πŸ‘‰ Go back to your [BTP cockpit](https://emea.cockpit.btp.cloud.sap/cockpit). - -πŸ‘‰ Navigate to `Instances and Subscriptions` and open `SAP Business Application Studio`. - -![Clone the repo](images/BTP_cockpit_BAS.png) - - -## Create a new Dev Space for CodeJam exercises - -πŸ‘‰ Create a new Dev Space. - -![Create a Dev Space 1](images/bas.png) - -πŸ‘‰ Enter the name of the Dev space `GenAICodeJam`, select the `Basic` kind of application and `Python Tools` from Additional SAP Extensions. - -πŸ‘‰ Click **Create Dev Space**. - -![Create a Dev Space 2](images/create_dev_space.png) - -You should see the dev space **STARTING**. - -![Dev Space is Starting](images/dev_starting.png) - -πŸ‘‰ Wait for the dev space to get into the **RUNNING** state and then open it. - -![Dev Space is Running](images/dev_running.png) - -## Clone the exercises from the Git repository - -πŸ‘‰ Once you opened your dev space in BAS, use one of the available options to clone this Git repository with exercises using the URL below: - -```sh -https://github.com/SAP-samples/generative-ai-codejam.git -``` - -![Clone the repo](images/clone_git.png) - -πŸ‘‰ Click **Open** to open a project in the Explorer view. - -![Open a project](images/clone_git_2.png) - -## Open the Workspace - -The cloned repository contains a file `codejam.code-workspace` and therefore you will be asked, if you want to open it. - -πŸ‘‰ Click **Open Workspace**. - -![Automatic notification to open a workspace](images/open_workspace.png) - -☝️ If you missed the previous dialog, you can go to the BAS Explorer, open the `codejam.code-workspace` file, and click **Open Workspace**. - -You should see: -* `CODEJAM` as the workspace at the root of the hierarchy of the project, and -* `generative-ai-codejam` as the name of the top level folder. - -πŸ‘‰ You can close the **Get Started** tab. - -![Open a workspace](images/workspace.png) - -## Configure the connection to Generative AI Hub - -πŸ‘‰ Go back to your [BTP cockpit](https://emea.cockpit.btp.cloud.sap/cockpit). - -πŸ‘‰ Navigate to `Instances and Subscriptions` and open the SAP AI Core instance's service binding. - -![Service Binding in the BTP Cockpit](images/service_binding.png) - -πŸ‘‰ Click **Copy JSON**. - -πŸ‘‰ Return to BAS and create a new file `.aicore-config.json` in the `generative-ai-codejam/` directory. - -πŸ‘‰ Paste the service key into `generative-ai-codejam/.aicore-config.json`, which should look similar to the following. - -```json -{ - "appname": "7ce41b4e-e483-4fc8-8de4-0eee29142e43!b505946|aicore!b540", - "clientid": "sb-7ce41b4e-e483-4fc8-8de4-0eee29142e43!b505946|aicore!b540", - "clientsecret": "...", - "identityzone": "genai-codejam-luyq1wkg", - "identityzoneid": "a5a420d8-58c6-4820-ab11-90c7145da589", - "serviceurls": { - "AI_API_URL": "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com" - }, - "url": "https://genai-codejam-luyq1wkg.authentication.eu10.hana.ondemand.com" -} -``` - -## Create a Python virtual environment and install the SAP's [Python SDK for Generative AI Hub]((https://pypi.org/project/generative-ai-hub-sdk/)) - -πŸ‘‰ Start a new Terminal. - -![Extensions to install](images/start_terminal.png) - -πŸ‘‰ Create a virtual environment using the following command: - -```bash -python3 -m venv ~/projects/generative-ai-codejam/env --upgrade-deps -``` - -πŸ‘‰ Activate the `env` virtual environment like this and make sure it is activated: - -```bash -source ~/projects/generative-ai-codejam/env/bin/activate -``` - -![venv](images/venv.png) - -πŸ‘‰ Install the Generative AI Hub [Python SDK](https://pypi.org/project/generative-ai-hub-sdk/) (and the other packages listed below) using the following `pip install` commands. - -```bash -pip install --require-virtualenv -U generative-ai-hub-sdk[all] -``` - -πŸ‘‰ We will also need the [HANA client for Python](https://pypi.org/project/hdbcli/). - -```bash -pip install --require-virtualenv -U hdbcli -``` - -πŸ‘‰ We will also need the [SciPy package](https://pypi.org/project/scipy/). - -```bash -pip install --require-virtualenv scipy -``` - -πŸ‘‰ We will also need the [pydf package](https://pypi.org/project/pypdf/) and [wikipedia](https://pypi.org/project/wikipedia/). - -```bash -pip install --require-virtualenv pypdf wikipedia -``` - - - - -> From now on the exercises continue in BAS. - -[Next exercise](03-prompt-llm.ipynb) diff --git a/09_BusinessAIWeek/Test/03-prompt-llm.ipynb b/09_BusinessAIWeek/Test/03-prompt-llm.ipynb deleted file mode 100644 index a42c41e..0000000 --- a/09_BusinessAIWeek/Test/03-prompt-llm.ipynb +++ /dev/null @@ -1,339 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Check your connection to Generative AI Hub\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ First you need to assign the values from `generative-ai-codejam/.aicore-config.json` to the environmental variables. \n", - "\n", - "That way the Generative AI Hub [Python SDK](https://pypi.org/project/generative-ai-hub-sdk/) will connect to Generative AI Hub." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ For the Python SDK to know which resource group to use, you also need to set the `resource group` in the [`variables.py`](variables.py) file to your own `resource group` (e.g. **team-01**) that you created in the SAP AI Launchpad in exercise [00-connect-AICore-and-AILaunchpad](000-connect-AICore-and-AILaunchpad.md)." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Name: generative-ai-hub-sdk\r\n", - "Version: 3.1.0\r\n", - "Summary: generative AI hub SDK\r\n", - "Home-page: https://www.sap.com/\r\n", - "Author: SAP SE\r\n", - "Author-email: \r\n", - "License: SAP DEVELOPER LICENSE AGREEMENT\r\n", - "Location: /Users/I066288/opt/anaconda3/lib/python3.9/site-packages\r\n", - "Requires: ai-core-sdk, click, dacite, langchain, langchain-community, openai, overloading, packaging, pydantic\r\n", - "Required-by: \r\n" - ] - } - ], - "source": [ - "pip install generative-ai-hub-sdk[all] hdbcli scipy pypdf --break-system-packages" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com/oauth/token\n", - "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540\n", - "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=\n", - "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "llm-deployed\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"llm-deployed\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "# #import init_env\n", - "# #import variables\n", - "# #import importlib\n", - "# #variables = importlib.reload(variables)\n", - "\n", - "# # TODO: You need to specify which model you want to use. In this case we are directing our prompt\n", - "# # to the openAI API directly so you need to pick one of the GPT models. Make sure the model is actually deployed\n", - "# # in genAI Hub. You might also want to chose a model that can also process images here already. \n", - "# # E.g. 'gpt-4o-mini' or 'gpt-4o'\n", - "# MODEL_NAME = 'gpt-4o'\n", - "# # Do not modify the `assert` line below\n", - "# assert MODEL_NAME!='', \"\"\"You should change the variable `MODEL_NAME` with the name of your deployed model (like 'gpt-4o-mini') first!\"\"\"\n", - "\n", - "# #init_env.set_environment_variables()\n", - "# # Do not modify the `assert` line below \n", - "# assert variables.RESOURCE_GROUP!='', \"\"\"You should change the value assigned to the `RESOURCE_GROUP` in the `variables.py` file to your own resource group first!\"\"\"\n", - "# print(f\"Resource group is set to: {variables.RESOURCE_GROUP}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Prompt an LLM with Generative AI Hub" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.proxy.native.openai import chat\n", - "from IPython.display import Markdown\n", - "\n", - "messages = [\n", - " {\n", - " \"role\": \"user\", \n", - " \"content\": \"What is the underlying model architecture of an LLM? Explain it as short as possible.\"\n", - " }\n", - "]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o\", messages=messages)\n", - "\n", - "response = chat.completions.create(**kwargs)\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Understanding roles\n", - "Most LLMs have the roles `system`, `assistant` (GPT) or `model` (Gemini) and `user` that can be used to steer the models response. In the previous step you only used the role `user` to ask your question. \n", - "\n", - "πŸ‘‰ Try out different `system` messages to change the response. You can also tell the model to not engage in smalltalk or only answer questions on a certain topic. Then try different user prompts as well!" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "messages = [\n", - " { \"role\": \"system\", \n", - " # TODO try changing the system prompt\n", - " \"content\": \"Speak like Yoda from Star Wars.\"\n", - " }, \n", - " {\n", - " \"role\": \"user\", \n", - " \"content\": \"What is the underlying model architecture of an LLM? Explain it as short as possible.\"\n", - " }\n", - "]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o\", messages=messages)\n", - "response = chat.completions.create(**kwargs)\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Also try to have it speak like a pirate.\n", - "\n", - "πŸ‘‰ Now let's be more serious! Tell it to behave like an SAP consultant talking to AI Developers.\n", - "\n", - "πŸ‘‰ Ask it to behave like an SAP Consultant talking to ABAP Developers and to make ABAP comparisons." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Hallucinations\n", - "πŸ‘‰ Run the following question." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "messages = [\n", - " { \"role\": \"system\", \n", - " \"content\": \"You are an SAP Consultant.\"\n", - " }, \n", - " {\n", - " \"role\": \"user\", \n", - " \"content\": \"How does the data masking of the orchestration service work?\"\n", - " }\n", - "]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o\", messages=messages)\n", - "response = chat.completions.create(**kwargs)\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "☝️ Compare the response to [SAP Help - Generative AI Hub SDK](https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html). \n", - "\n", - "πŸ‘‰ What did the model respond? Was it the truth or a hallucination?\n", - "\n", - "πŸ‘‰ Which questions work well, which questions still do not work so well?\n", - "\n", - "# Use Multimodal Models\n", - "\n", - "Multimodal models can use different inputs such as text, audio and images. In Generative AI Hub on SAP AI Core you can access multiple multimodal models (e.g. `gpt-4o-mini`).\n", - "\n", - "πŸ‘‰ If you have not deployed one of the gpt-4o-mini models in previous exercises, then go back to the model library and deploy a model that can also process images.\n", - "\n", - "πŸ‘‰ Now run the code snippet below to get a description for [the image of the AI Foundation Architecture](documents/ai-foundation-architecture.png). These descriptions can then for example be used as alternative text for screen readers or other assistive tech.\n", - "\n", - "πŸ‘‰ You can upload your own image and play around with it!" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import base64\n", - "\n", - "# get the image from the documents folder\n", - "with open(\"documents/ai-foundation-architecture.png\", \"rb\") as image_file:\n", - " image_data = base64.b64encode(image_file.read()).decode('utf-8')\n", - "\n", - "messages = [{\"role\": \"user\", \"content\": [\n", - " {\"type\": \"text\", \"text\": \"Describe the images as an alternative text.\"},\n", - " {\"type\": \"image_url\", \"image_url\": {\n", - " \"url\": f\"data:image/png;base64,{image_data}\"}\n", - " }]\n", - " }]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o\", messages=messages)\n", - "response = chat.completions.create(**kwargs)\n", - "\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Extracting text from images\n", - "Nora loves bananabread and thinks recipes are a good example of how LLMs can also extract complex text from images, like from [a picture of a recipe of a bananabread](documents/bananabread.png). Try your own recipe if you like :)\n", - "\n", - "This exercise also shows how you can use the output of an LLM in other systems, as you can tell the LLM how to output information, for example in JSON format." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# get the image from the documents folder\n", - "with open(\"documents/bananabread.png\", \"rb\") as image_file:\n", - " image_data = base64.b64encode(image_file.read()).decode('utf-8')\n", - "\n", - "messages = [{\"role\": \"user\", \"content\": [\n", - " {\"type\": \"text\", \"text\": \"Extract the ingredients and instructions in two different json files.\"},\n", - " {\"type\": \"image_url\", \"image_url\": {\n", - " \"url\": f\"data:image/png;base64,{image_data}\"}\n", - " }]\n", - " }]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o\", messages=messages)\n", - "response = chat.completions.create(**kwargs)\n", - "\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[Next exercise](04-create-embeddings.ipynb)\n" - ] - } - ], - "metadata": { - "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.9.12" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test/04-create-embeddings.ipynb b/09_BusinessAIWeek/Test/04-create-embeddings.ipynb deleted file mode 100644 index e7cd841..0000000 --- a/09_BusinessAIWeek/Test/04-create-embeddings.ipynb +++ /dev/null @@ -1,216 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Create embeddings with Generative AI Hub\n", - "Like any other machine learning model, also foundation models only work with numbers. In the context of generative AI, these numbers are embeddings. Embeddings are numerical representations of unstructured data, such as text and images. The text embedding model of OpenAI `text-embedding-3-small` for example turns your input text into 1536 numbers. That is a vector with 1536 dimensions.\n", - "\n", - "πŸ‘‰ Select the kernel again. Make sure to select the same virtual environment as in the previous exercise so that all your packages are installed." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com/oauth/token\n", - "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540\n", - "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=\n", - "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "llm-deployed\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"llm-deployed\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "#import init_env\n", - "\n", - "#init_env.set_environment_variables()\n", - "\n", - "from gen_ai_hub.proxy.native.openai import embeddings\n", - "\n", - "# TODoassign the model name of the embedding model here, e.g. \"text-embedding-3-small\"\n", - "EMBEDDING_MODEL_NAME = \"text-embedding-ada-002\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Create embeddings\n", - "Define the method **get_embedding()**." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "def get_embedding(input_text):\n", - " response = embeddings.create(\n", - " input=input_text, \n", - " model_name=EMBEDDING_MODEL_NAME\n", - " )\n", - " embedding = response.data[0].embedding\n", - " return embedding" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Get embeddings for the words: **apple, orange, phone** and for the phrases: **I love dogs, I love animals, I hate cats.**" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.00779997231438756, -0.02301608771085739, -0.007416142616420984, -0.02781396172940731, -0.004564834292978048, 0.012954260222613811, -0.021933138370513916, -0.008643027395009995, 0.01884879171848297, -0.02980165183544159, -0.0028187509160488844, 0.02016477845609188, -0.004451741464436054, 0.009081690572202206, -0.02152189239859581, 0.002049377653747797, 0.030816059559583664, 5.9009566029999405e-05, 0.001972269034013152, -0.0256617721170187, -0.021014688536524773, -0.008067282848060131, 0.021206602454185486, -0.012344244867563248, 0.0010812351247295737, 0.005072038155049086, 0.010048119351267815, -0.00011684108176268637, 0.01586040109395981, -0.012947405688464642, 0.020548608154058456, -0.016189398244023323, -0.018533501774072647, 0.005267380271106958, -0.01932857744395733, -0.009301021695137024, -0.012131767347455025, -0.008944608271121979, -0.00572660518810153, -0.006144705694168806, 0.010500489734113216, 0.007704014889895916, -0.006518254522234201, 0.0005324783269315958, -0.02350958250463009, 0.01111735962331295, -0.021713806316256523, -0.0006181546486914158, -0.012817177921533585, 0.015531403943896294, -0.005490138661116362, -0.002001398941501975, -0.015065324492752552, -0.00535648362711072, -0.0027467829640954733, 0.0027844805736094713, -0.01941082812845707, 0.006360610015690327, -0.010781507939100266, 0.0049212477169930935, 0.005253672134131193, 0.015120157040655613, -0.027937335893511772, 0.0055518257431685925, -0.009184502065181732, 0.003828017972409725, -0.013591691851615906, -0.008485383354127407, 0.012919989414513111, 0.015654778108000755, 0.03339320421218872, 0.013262694701552391, 0.014050916768610477, 0.00640173489227891, 0.004479158204048872, 0.0014847706770524383, -0.03396894782781601, -0.006268079858273268, -0.0030980557203292847, 0.01804000698029995, 0.007594349328428507, -0.02636088989675045, 0.009588893502950668, 0.02501748688519001, 0.024825571104884148, 0.009904182516038418, -0.021658973768353462, 0.010096097365021706, -0.0080055957660079, 0.002815323881804943, 0.011857602745294571, 0.005483284592628479, 0.007827389054000378, 0.016449853777885437, -0.026785844936966896, 0.01669660210609436, -0.012686950154602528, 0.02641572430729866, 0.004784165881574154, -0.03739600256085396, 0.0009784236317500472, 0.006110435351729393, -0.028787244111299515, -0.011206462979316711, -0.020384110510349274, 0.0019465660443529487, 0.01252245157957077, -0.0036258220206946135, 0.015408029779791832, -0.019890615716576576, 0.006504546385258436, 0.009328437969088554, 0.021878305822610855, -0.051597706973552704, -0.016436144709587097, -0.01739571988582611, -0.009458665736019611, 0.0021950274240225554, -0.02282417193055153, -0.03550426661968231, 0.03413344547152519, 0.025798853486776352, 0.038465242832899094, -0.008725276216864586, 0.013406630605459213, -0.0033173870760947466, -0.017834383994340897, -0.009609456174075603, -0.0011634844122454524, -0.013927542604506016, 0.04707399755716324, 0.008204364217817783, 0.01954790949821472, 0.0165732279419899, -0.017793258652091026, 0.019589034840464592, -0.0222347192466259, 0.015421737916767597, -0.005706042982637882, -0.03347545117139816, 0.010479927994310856, 0.014722619205713272, -0.012447056360542774, 0.007093999534845352, -0.011220171116292477, 0.02361924760043621, 0.02360554039478302, 0.022755630314350128, -0.005233109928667545, 0.0070871454663574696, 0.01071296725422144, -0.02419499307870865, -0.00381430983543396, 0.005442160181701183, 0.001797489239834249, 0.0057574487291276455, -0.016984473913908005, -0.0022361520677804947, -0.00396852707490325, 0.0037354875821620226, -0.006538816727697849, 0.00865673553198576, -0.006720450706779957, -0.008786963298916817, 0.0017872080206871033, 0.02433207631111145, 0.011857602745294571, 0.01348202582448721, -0.01597006618976593, -0.009472373872995377, -0.016148272901773453, 0.004222129005938768, -0.010651280172169209, 0.012933697551488876, 0.008882921189069748, 0.01603860780596733, 0.004530563950538635, 0.012721220031380653, -0.006758148316293955, 0.0006044464535079896, 0.001052962034009397, -0.004564834292978048, 0.023098336532711983, 0.01950678415596485, -0.01669660210609436, 0.014777451753616333, 0.00465393764898181, -0.010507344268262386, 0.005565533880144358, -0.0193011611700058, 0.03213204815983772, 0.020480068400502205, -0.00401307875290513, -0.014366205781698227, -0.6847525835037231, -0.020562317222356796, 0.0059699262492358685, -0.00040225032716989517, 0.03394153341650963, 0.03183046728372574, 0.022344384342432022, -0.0012988530797883868, -0.018314169719815254, -0.010452511720359325, 0.0017477970104664564, 0.003348230617120862, 0.008944608271121979, 0.009465520270168781, 7.823319174349308e-05, -0.022371800616383553, 0.016271647065877914, -0.02368778921663761, -0.01037711650133133, 0.024798154830932617, -0.012241432443261147, -0.014352497644722462, -0.008766401559114456, -0.029664570465683937, 0.0036498112604022026, 0.019589034840464592, 0.019972864538431168, -0.013948105275630951, -0.01954790949821472, 0.021741222590208054, -0.008224926888942719, -0.0004982078098692, 0.027279341593384743, 0.005987061187624931, 0.03742341697216034, 0.0072173732332885265, -0.04422269016504288, 0.013961813412606716, 0.009643726982176304, 0.03413344547152519, -0.02071310766041279, -0.020370401442050934, 0.013523150235414505, -0.006542243994772434, -0.015874108299613, -0.0009981291368603706, 0.013214715756475925, 0.005610085558146238, -0.0006815551314502954, -0.025332774966955185, 0.03473660722374916, 3.08167036564555e-05, 0.0108020706102252, 0.008396279998123646, 0.021727515384554863, 0.008752693422138691, 0.023166878148913383, -0.008725276216864586, -0.0014933382626622915, 0.016408728435635567, -0.0223032608628273, -0.003377360524609685, -0.03465436026453972, -0.025195693597197533, -0.020521191880106926, 0.010630718432366848, -0.009259897284209728, 0.00762861967086792, 0.005500419996678829, -0.019616451114416122, 0.021412227302789688, 0.022440342232584953, -0.023098336532711983, -0.0070871454663574696, -0.0030603581108152866, 0.016998182982206345, 0.023468459025025368, -0.0014941950794309378, -0.02703259326517582, 0.004794446751475334, -0.002368093468248844, -0.009444957599043846, 0.004784165881574154, -0.027498671784996986, 0.031501468271017075, 0.005425024777650833, -0.023989370092749596, -0.011247587390244007, 0.014078333042562008, -0.023715205490589142, 0.03410603106021881, 0.023865995928645134, 0.002882151398807764, -0.018972164019942284, -0.014530704356729984, -0.005349629558622837, -0.03240621089935303, -0.019177787005901337, -0.002107637468725443, -0.019876906648278236, 0.01658693514764309, -0.005606658291071653, 0.00943810399621725, 0.006792418658733368, 0.024030495434999466, 0.01357112918049097, -0.01949307695031166, 0.014585536904633045, 0.03679284080862999, -0.020438943058252335, -0.007724577095359564, -0.0125498678535223, 0.008574486710131168, -0.008759547024965286, 0.025867395102977753, -0.03122730553150177, 0.010733529925346375, -0.007649181876331568, 0.021055812016129494, -0.032899707555770874, 0.025428732857108116, -0.004366065375506878, 0.011843894608318806, 0.0029746817890554667, -0.010637572035193443, 0.018451252952218056, -0.004537418019026518, -0.030980557203292847, -0.003469891147688031, -0.005901385098695755, 0.0080055957660079, 0.005198839120566845, 0.007368163671344519, -0.006189257372170687, 0.023920828476548195, 0.0004181004478596151, 0.010000140406191349, -0.020685691386461258, 0.011932997964322567, -0.025195693597197533, -0.016915932297706604, 0.008067282848060131, 0.0004350215313024819, -0.000493067258503288, 0.0012260281946510077, -0.012255141511559486, -0.028567912057042122, -0.016888516023755074, -0.010891173966228962, 0.005277661606669426, -0.008951461873948574, 0.005490138661116362, -0.0016989614814519882, 0.021535599604249, 0.022645965218544006, -0.01586040109395981, 0.002887292066588998, -0.023399917408823967, 0.0009193069417960942, 0.016299063339829445, 0.010253742337226868, 0.028184082359075546, -0.009020003490149975, 0.004181004595011473, 0.015051616355776787, -0.035805847495794296, 0.006860959809273481, 0.0019225766882300377, -0.017861800268292427, -0.029362989589571953, 0.013358651660382748, -0.016874808818101883, -0.008149531669914722, 0.00830717571079731, -0.014722619205713272, 0.003999370615929365, -0.014983074739575386, 0.0073887258768081665, 0.01011666003614664, 0.0023903693072497845, 0.005428451579064131, 0.008876066654920578, 0.002059658756479621, 0.001682683010585606, 0.030870892107486725, 0.019986571744084358, 0.0016518394695594907, 0.047622326761484146, -0.02023332007229328, 0.0008978878613561392, -0.013461464084684849, 0.019849490374326706, 0.005349629558622837, 0.001652696286328137, -0.01383158564567566, -0.008416841737926006, -0.0059699262492358685, 0.013372360728681087, -0.0009655721369199455, 0.011521751992404461, 0.023989370092749596, -0.002930130111053586, 0.011220171116292477, -0.0023526716977357864, 0.013845293782651424, -0.032872289419174194, 0.004057630430907011, -0.005366764962673187, 0.011309274472296238, 0.0345446914434433, -0.0022481465712189674, -0.02848566323518753, -0.006621066015213728, -0.020425235852599144, 0.012337390333414078, 0.03421569615602493, 0.002344104228541255, 0.012090642936527729, 0.0009141663322225213, -0.00799874123185873, 0.0016724017914384604, -0.004821863491088152, 0.0004039638442918658, -0.003690935904160142, -0.007518954109400511, 0.003428766503930092, 0.010102951899170876, 0.03320128843188286, -0.003367079421877861, -0.026031892746686935, -0.0036121136508882046, -0.004965799394994974, -0.00943810399621725, -0.012474472634494305, 0.00413987971842289, 0.014859701506793499, 0.004191285464912653, -0.010980277322232723, 0.02226213552057743, -0.0009964156197384, -0.014023500494658947, 0.014434746466577053, 0.026593931019306183, -0.0019859771709889174, 0.0076354737393558025, -0.013296965509653091, 0.04578542709350586, 0.008170094341039658, -0.004211848136037588, -0.002212162595242262, -0.003281403100118041, -0.02570289745926857, -0.030788643285632133, 0.010438802652060986, -0.000948436907492578, -0.024619948118925095, 0.02350958250463009, 0.022399216890335083, 0.02294754609465599, 0.04082305356860161, 0.0033310954459011555, -0.0035915514454245567, -0.012995384633541107, 0.005918520502746105, -0.020274445414543152, -0.0002628121292218566, -0.0012174604926258326, -0.013680795207619667, 0.0028410267550498247, 0.004136452917009592, 0.013344943523406982, -0.00026666754274629056, 0.02492152899503708, -0.010157784447073936, 0.006806126795709133, 0.0008798958151601255, -0.017642468214035034, 0.004838998429477215, 0.0053427754901349545, -0.012467618100345135, -0.021727515384554863, -0.027416422963142395, 0.00899944081902504, 0.003930829465389252, -0.006908938754349947, 0.008896629326045513, -0.028951743617653847, -0.006017904728651047, -0.005771156866103411, 0.014708911068737507, -0.003276262665167451, 0.02161785028874874, 0.00834144651889801, 0.02008252963423729, 0.020438943058252335, -0.013790461234748363, 0.03133697062730789, -0.011014548130333424, 0.005195412319153547, -0.006206392776221037, 0.00017295907309744507, 0.005024059675633907, -0.01954790949821472, -0.004030214156955481, 0.03942481800913811, -0.004893831443041563, -0.00591509323567152, -0.02435949258506298, 0.012577284127473831, -0.002400650642812252, -0.025442440062761307, 0.004660791717469692, -0.01603860780596733, -0.01799888163805008, -0.0029781090561300516, 0.0165732279419899, -0.005435305647552013, -0.017162680625915527, 0.04074080288410187, -0.0025137432385236025, -0.012659532949328423, -0.017848091199994087, -0.014311373233795166, 0.007861658930778503, 0.06508658826351166, 0.025510981678962708, -0.010877465829253197, 0.023756330832839012, -0.00557238794863224, 0.02766317129135132, -0.011850749142467976, -0.0068403976038098335, 0.02906140871345997, -0.0007436704472638667, -0.0013117045164108276, -0.008773255161941051, 0.014818577095866203, -0.018478669226169586, 0.023893412202596664, 0.011261295527219772, -0.002345817629247904, -0.02017848752439022, -0.005675199441611767, -0.008163239806890488, 0.0004489439306780696, -0.014585536904633045, 0.010671842843294144, 0.005599804222583771, 0.017162680625915527, 0.005802000407129526, 0.01875283382833004, 0.0290888249874115, -0.0052056931890547276, -0.024537699297070503, -0.006017904728651047, -0.006148132961243391, 0.015325780026614666, -0.006261225789785385, -0.001965414732694626, 0.02227584458887577, 0.017203805968165398, 0.01588781736791134, 0.005952790845185518, 0.005322213284671307, 0.012131767347455025, 0.010534760542213917, -0.0036943629384040833, -0.02633347362279892, 0.007745139300823212, -0.011384669691324234, -0.01457182876765728, 0.03265295922756195, -0.00122174434363842, -0.013797314837574959, 0.024743322283029556, 0.016216814517974854, 0.005027486477047205, -0.005534690339118242, -0.00020540898549370468, 0.026689887046813965, -0.007498391903936863, 0.003086061216890812, 0.010308574885129929, -0.02556581422686577, -0.005568961147218943, -0.02001398801803589, 0.0033173870760947466, 0.014489579945802689, -0.0012834313092753291, -0.0056649185717105865, -0.01140523236244917, -0.01875283382833004, -0.006833543535321951, -0.011597147211432457, -0.0009869912173599005, -0.02636088989675045, -0.011316129006445408, 0.007717723026871681, 0.03317387029528618, 0.008224926888942719, 0.02091873064637184, 0.005661491304636002, 0.006977479439228773, 0.03487369045615196, -0.008272905834019184, -0.014873409643769264, 0.0047567496076226234, -0.014462162740528584, -0.02287900447845459, 0.019081830978393555, -0.0020151070784777403, -0.013043363578617573, 0.011720520444214344, 0.008608756586909294, 0.013783606700599194, -0.006079591810703278, 0.029445238411426544, 0.004568261560052633, -0.01213862095028162, -0.0004193855857010931, 0.017697300761938095, 0.012008393183350563, -0.0193011611700058, -0.011871310882270336, 0.0079713249579072, -0.010438802652060986, -0.0027313611935824156, -0.00796447042375803, 0.014612953178584576, 0.01459924504160881, 0.019684990867972374, 0.012508743442595005, -0.025168275460600853, 0.003680654801428318, 0.02769058756530285, -0.004427752457559109, 0.014818577095866203, -0.01105567254126072, 0.012255141511559486, 0.01385214738547802, -0.008862358517944813, -0.007402434013783932, -0.005239963997155428, -0.01595635898411274, -0.021014688536524773, -0.04998013749718666, 0.01425653975456953, 0.020987272262573242, -0.014105749316513538, 0.020397819578647614, -0.005096027627587318, -0.007923346012830734, -0.02090502344071865, 0.012227724306285381, -0.008759547024965286, -0.0025137432385236025, -0.001355399377644062, -0.01385214738547802, -0.033612534403800964, -0.02697776071727276, -0.016504686325788498, 0.02085018903017044, 0.0009604315855540335, -0.01740942895412445, -0.020301861688494682, 0.018341587856411934, 0.008128968998789787, -0.02493523620069027, -0.028321165591478348, -0.04923989623785019, 0.01145321037620306, 0.0032505595590919256, 0.0035881244111806154, 0.029554903507232666, -0.01528465561568737, 0.017573926597833633, -0.006946636363863945, 0.014297664165496826, -0.026196392253041267, -0.02215247042477131, -0.02851307950913906, 0.00017510098405182362, 0.03821849450469017, 0.018286753445863724, 0.020397819578647614, 0.014105749316513538, 0.01662806048989296, -0.000228327393415384, 0.004462022799998522, -0.008643027395009995, 0.0030380822718143463, 0.01214547548443079, -0.018547210842370987, 0.013708211481571198, 0.014489579945802689, 0.015874108299613, 0.002715939423069358, 0.01289257314056158, -0.0039856624789536, 0.012734928168356419, 0.004235837142914534, -0.00224986020475626, -0.027964752167463303, -0.005263953004032373, -0.0043386491015553474, -0.00937641691416502, -0.019794657826423645, 0.01008924376219511, 0.009143376722931862, -0.02431836724281311, 0.019122954457998276, 0.009136523120105267, 0.0017563645960763097, -0.02919849008321762, 0.0250311940908432, -0.009917890653014183, 0.021357392892241478, 0.011412085965275764, 0.005332494154572487, -0.026635054498910904, -0.006713596638292074, 0.008862358517944813, -0.007450412958860397, 0.015120157040655613, -0.0068780952133238316, 0.007745139300823212, -0.01457182876765728, -0.007464121095836163, 0.005805427674204111, 0.04104238376021385, -0.02562064677476883, -0.022371800616383553, 0.006854105740785599, -0.04178262874484062, -0.005421597510576248, -0.0064599947072565556, -0.00969170592725277, -0.01599748246371746, 0.001160057378001511, 0.013502588495612144, 4.1526243876433e-05, 0.0004118889046367258, -0.005849979352205992, -0.01743684522807598, 0.0061755492351949215, 0.02009623870253563, 0.029966149479150772, 0.0025514408480376005, 0.03520268574357033, 0.02159043401479721, -0.003509302157908678, -0.01949307695031166, 0.0022567142732441425, -0.009506644681096077, -0.011665687896311283, 0.026511680334806442, 0.014818577095866203, 0.012337390333414078, -0.007121415808796883, -0.0027142257895320654, 0.004602531902492046, -0.010233179666101933, -0.009630018845200539, 0.034983355551958084, 0.017752133309841156, 0.016257937997579575, -0.027937335893511772, -0.031035391613841057, -0.01950678415596485, 0.026731012389063835, -0.004818436224013567, -0.002525737974792719, -0.015079032629728317, -0.03835557401180267, -0.011172192171216011, 0.02642943151295185, -0.012309974059462547, 0.01952049322426319, 0.00413987971842289, -0.013996084220707417, 0.015133865177631378, -0.01183018647134304, -0.02492152899503708, 0.0008284900104627013, -0.0026028465945273638, 0.027402713894844055, -0.024126451462507248, 0.018218213692307472, -0.001291142194531858, 0.005287942476570606, -0.006285214796662331, -0.007813680917024612, 0.004701916594058275, 0.03172080218791962, -0.02774542011320591, -0.0008781822980381548, 0.0026748147793114185, 0.015037908218801022, 0.010582739487290382, -0.012070080265402794, -0.0223032608628273, -0.009122814983129501, -0.0028890054672956467, -0.027909917756915092, 0.009959015995264053, 0.007813680917024612, -0.021083230152726173, -0.01115163043141365, 0.008218072354793549, -0.006237236317247152, -0.0062543717212975025, 0.0027416422963142395, 0.013756190426647663, -0.0007548084249719977, -0.013454609550535679, 0.016943348571658134, -0.0016484124353155494, 0.00338592822663486, 0.026936635375022888, -0.012097496539354324, -0.01071296725422144, 0.023838579654693604, -0.02082277275621891, 0.0008332022116519511, 0.007895929738879204, 0.002314974321052432, -0.024030495434999466, 0.010006994009017944, -0.003828017972409725, -0.003776612225919962, 0.02024702914059162, -0.01950678415596485, -0.012330535799264908, -0.0043352218344807625, 0.007333893328905106, 0.016175689175724983, 0.0024366346187889576, 0.006172122433781624, 0.013255840167403221, -0.008430549874901772, -0.001936284825205803, -0.012741782702505589, -0.030185481533408165, 9.40297613851726e-05, -0.0125155970454216, -0.0022841307800263166, 0.02076794020831585, -0.003137466963380575, -0.002657679608091712, -0.0038862780202180147, 0.007292768452316523, 0.013694503344595432, -0.024537699297070503, -0.03391411527991295, -0.02148076705634594, 0.020521191880106926, -0.014626661315560341, 0.00015464576426893473, -0.020644566044211388, -0.002210449194535613, -0.02211134508252144, 0.003615540685132146, -0.000972426263615489, 0.0004596534708980471, 0.002801615744829178, 0.014695202931761742, 0.013358651660382748, 0.023852288722991943, -0.00906112790107727, 0.0021196322049945593, -0.013742482289671898, -0.021343685686588287, -0.0216041412204504, -0.005925374571233988, 0.004972653929144144, 0.01801259070634842, 0.019657574594020844, -0.021864596754312515, -0.028787244111299515, -0.008101552724838257, -0.039808645844459534, -0.03246104344725609, -0.005702616181224585, 0.013159883208572865, -0.0007132553728297353, 0.006065883673727512, 0.010102951899170876, 0.00552783627063036, 0.006391453556716442, 0.01673772558569908, -0.04767715930938721, -0.0010863757925108075, 0.005024059675633907, -0.020973563194274902, 0.022467758506536484, -0.02296125516295433, -0.020480068400502205, -0.023303959518671036, 0.0010658134706318378, 0.008163239806890488, 0.0011035110801458359, 0.011124213226139545, 0.0003103624621871859, -0.0032848301343619823, -0.008848650380969048, 0.02217988669872284, 0.0063845994882285595, -0.00018730985175352544, 0.00939697865396738, -0.01882137358188629, -0.009588893502950668, 0.003223143285140395, -0.0007646611775271595, -0.02372891455888748, 0.006333193741738796, 0.009136523120105267, 0.00834830105304718, 0.008039865642786026, 0.001326269470155239, -0.005174849648028612, 0.009801371023058891, -0.02012365497648716, -0.005884249694645405, -0.011329837143421173, -0.01876654103398323, 0.014379913918673992, -0.02438690885901451, -0.01801259070634842, -0.012536159716546535, 0.010288013145327568, -0.02434578351676464, -0.010150930844247341, 0.026799554005265236, -0.008087844587862492, -0.014407330192625523, -0.010617009364068508, 0.02226213552057743, -0.03484627231955528, -0.00799874123185873, -0.0029181356076151133, -0.023166878148913383, -0.024099035188555717, 0.03821849450469017, 0.0030209471005946398, -0.008293467573821545, -0.007614911533892155, 0.014585536904633045, 0.009479228407144547, -0.0039171213284134865, -0.004314659629017115, 0.013338089920580387, -0.022495174780488014, -0.013214715756475925, 0.00837571732699871, -0.018602043390274048, -0.023715205490589142, 0.0039034131914377213, 0.01145321037620306, -0.002277276711538434, -0.004146733786910772, 0.20792615413665771, -0.0070460205897688866, 0.01658693514764309, 0.03983606398105621, -0.0005894530913792551, -0.0007907924591563642, 0.015476570464670658, 0.013180444948375225, -0.003951392136514187, 0.004993216134607792, -0.014804868027567863, 0.011734229512512684, -0.015188698656857014, 0.007306476589292288, 0.010904882103204727, -0.00762861967086792, -0.03487369045615196, -0.008985732682049274, -0.016312772408127785, -0.006130997557193041, 0.02367408201098442, -0.011604000814259052, -0.011521751992404461, -0.011514897458255291, 0.029006576165556908, 0.0030552176758646965, -0.029390405863523483, 0.009952161461114883, 0.018602043390274048, 0.010198909789323807, -0.00024888970074243844, -0.004198139999061823, -0.003951392136514187, -0.0020151070784777403, -0.00832088477909565, -0.00020380255591589957, -0.007573786657303572, 0.024140160530805588, 0.027471255511045456, 0.026936635375022888, -0.0015875822864472866, -0.0008503374992869794, -0.007827389054000378, -0.003536718664690852, -0.028129249811172485, 0.014283956028521061, -0.008478528819978237, -0.007368163671344519, 0.02297496236860752, 0.021782347932457924, -0.04792390763759613, -0.0037526229862123728, 0.025168275460600853, 0.02507231943309307, 0.0014325081137940288, 0.007135123945772648, 0.0026936635840684175, 0.012686950154602528, -0.0023218283895403147, -0.0020373829174786806, -0.007409288547933102, 0.019013289362192154, 0.0012371661141514778, 0.019095538184046745, -0.02419499307870865, -0.004276962019503117, -0.03416086360812187, 0.005891103763133287, -0.009287313558161259, -0.008814379572868347, 0.0038691426161676645, -0.02069939859211445, -0.007546370383352041, 0.0003493452095426619, -0.030158065259456635, -0.04074080288410187, 0.024057911708950996, 0.026045601814985275, 0.01459924504160881, 0.022371800616383553, -0.006884949281811714, 0.009211918339133263, -0.006144705694168806, -0.002362953033298254, -0.006065883673727512, -0.01662806048989296, 0.03210463002324104, -0.00537361903116107, -0.005884249694645405, -0.003910267259925604, -0.005575815215706825, -0.015394321642816067, 0.0043763467110693455, -0.026031892746686935, 0.005723178386688232, 0.01742313615977764, -0.015805568546056747, 0.01952049322426319, -0.010452511720359325, -0.0006896943668834865, -0.004043922293931246, -0.01797146536409855, 0.017683593556284904, -0.010075535625219345, -0.008149531669914722, 0.024057911708950996, 0.0029712547548115253, 0.009465520270168781, 0.005812281742691994, 0.006860959809273481, -0.012776053510606289, -0.01140523236244917, 0.012529305182397366, -0.011220171116292477, 0.03383186459541321, 0.00973968394100666, -0.0026953769847750664, -0.02906140871345997, 0.017916632816195488, -0.0011420653900131583, 0.007518954109400511, -0.01872541755437851, 0.002097356365993619, 0.01747797057032585, -0.014352497644722462, -0.021179186180233955, -0.02080906555056572, 0.0002630263043101877, 0.01672401838004589, -0.02848566323518753, -0.0034887397196143866, -0.0047910199500620365, 0.004444887395948172, 0.003495594020932913, 0.00871842261403799, 0.0024931810330599546, 0.0049623725935816765, -0.017656177282333374, -0.0015010491479188204, -0.009390125051140785, 0.0013571128947660327, 0.013735627755522728, 0.020507484674453735, 0.007320184726268053, 0.0035435727331787348, -0.002791334642097354, 0.0027844805736094713, -0.00040332128992304206, -0.023290250450372696, -0.021371101960539818, -0.01879395730793476, 9.183430665871128e-05, -0.01878025010228157, -0.006693034432828426, 0.04211162403225899, -0.017724717035889626, -0.03410603106021881, -0.02769058756530285, -0.010836341418325901, 0.013715066015720367, -0.022741923108696938, -0.014750035479664803, 0.02301608771085739, -0.011713666841387749, -0.04150846600532532, -0.007375017739832401, -0.18018072843551636, 0.027622045949101448, -0.006871241144835949, 0.0007826532237231731, 0.018355295062065125, 0.003718352410942316, 0.016819976270198822, 0.009828787297010422, -0.006343475077301264, 0.0018985873321071267, 0.021124353632330894, 0.0033173870760947466, -0.028924325481057167, -0.011014548130333424, 0.023989370092749596, -0.011473773047327995, -0.004808155354112387, 0.01599748246371746, 0.025195693597197533, 0.011131067760288715, 0.016408728435635567, -0.010048119351267815, 0.014119457453489304, -0.019945448264479637, 0.009965869598090649, 0.023427333682775497, -0.02567547932267189, 0.0014179431600496173, -0.011953560635447502, -0.016244230791926384, -0.014229123480618, -0.015270947478711605, 0.011261295527219772, 0.011590292677283287, 0.012776053510606289, -0.007621765602380037, -0.014016645960509777, -0.012330535799264908, 0.0014316512970253825, 0.02363295666873455, 0.021631557494401932, 0.019780948758125305, 0.007847950793802738, 0.004674500320106745, 0.006268079858273268, 0.02023332007229328, 0.02005511336028576, -0.019164079800248146, 0.011967268772423267, -0.01934228651225567, 0.019616451114416122, -0.013564275577664375, -0.006624493282288313, 0.027909917756915092, 0.00047850224655121565, 0.009568331763148308, -0.015353197231888771, -0.0034493287093937397, -0.020671982318162918, -0.006538816727697849, -0.012700658291578293, -0.01074723806232214, 0.02017848752439022, -0.0188076663762331, -0.007333893328905106, -0.016819976270198822, -0.013783606700599194, -0.008080990985035896, -0.032927125692367554, 0.01671030931174755, -0.012241432443261147, -0.011562876403331757, 0.031748216599226, -0.02427724190056324, 0.0005676056607626379, -0.0008173520909622312, -0.018177088350057602, 0.026127850636839867, 0.007669744547456503, -0.002140194410458207, -0.0059288013726472855, 0.02146705985069275, -0.01740942895412445, -0.000289371790131554, -0.0058328439481556416, 0.01217974629253149, -0.0068780952133238316, 0.004225556273013353, -0.027293048799037933, -0.030267730355262756, 0.017697300761938095, -0.019246328622102737, -0.0038588615134358406, -0.021631557494401932, 0.02146705985069275, 0.03657350689172745, 0.011789062060415745, 0.004427752457559109, -0.0028838650323450565, 0.0035881244111806154, -0.0018745979759842157, -0.0022669953759759665, -0.030020983889698982, 0.015737026929855347, 0.035641349852085114, -0.0028564485255628824, -0.006189257372170687, 0.019712409004569054, 0.018862498924136162, -0.0025857114233076572, -0.002345817629247904, 0.02080906555056572, 0.031062807887792587, 0.028924325481057167, -0.007416142616420984, 0.0017272346885874867, 0.011350398883223534, -0.025524690747261047, 0.005846552085131407, -0.011891873553395271, 0.02501748688519001, 0.0007625192520208657, 0.01953420229256153, 0.014695202931761742, -0.005178276915103197, -0.01111050508916378, -0.09908295422792435, -0.044058192521333694, 0.009952161461114883, 0.013673940673470497, 0.015339489094913006, 0.02499006874859333, -0.005161141511052847, 0.02301608771085739, -0.012501888908445835, 0.03871198743581772, 0.0028941461350768805, -0.021412227302789688, -0.000581313855946064, 0.007724577095359564, 0.011631417088210583, -0.0002636688877828419, -0.027608338743448257, -0.011521751992404461, -0.022604839876294136, 0.013680795207619667, -0.013721919618546963, -0.016888516023755074, -0.010500489734113216, -0.019177787005901337, -5.5421871365979314e-05, 0.0007025458617135882, -0.03523010388016701, 0.028869492933154106, 0.036546092480421066, 0.0070871454663574696, 0.030267730355262756, -0.02150818333029747, 0.021302560344338417, -0.03808141127228737, 0.0018900196300819516, 0.00803301203995943, -0.01861575059592724, 0.006370891351252794, 0.019863197579979897, -0.028567912057042122, -0.0013142747338861227, 0.0003814738302025944, 0.015558820217847824, -0.030020983889698982, 0.010788362473249435, -0.02215247042477131, 0.0002831602469086647, 0.001079521607607603, 0.012056372128427029, -0.013262694701552391, -0.01313932053744793, 0.0187391247600317, -0.0022481465712189674, -0.014722619205713272, 0.007032312452793121, -0.02087760530412197, -0.014064624905586243, 0.005939082708209753, -0.0187391247600317, 0.004239264409989119, 0.006220100913196802, -0.019643867388367653, -0.00043416477274149656, 0.01348888035863638, 0.02226213552057743, 0.001953420229256153, -0.01488711778074503, -0.0019003008492290974, 0.004801301285624504, -0.018574627116322517, -0.0011394950561225414, -0.012255141511559486, -0.026826970279216766, 0.046141840517520905, -0.021083230152726173, -0.0007668031030334532, -0.019712409004569054, -0.02020590379834175, 0.013653378933668137, -0.032899707555770874, -0.0044140443205833435, -0.030651560053229332, 0.0014179431600496173, -0.008183802478015423, 0.012447056360542774, -0.005270807538181543, -0.003947964869439602, 0.0078068263828754425, 0.005709470249712467, -0.03451727703213692, 0.0332835391163826, 0.002308120019733906, 0.025881104171276093, -0.012008393183350563, -0.025113442912697792, -0.0021127781365066767, -0.01140523236244917, -0.001965414732694626, 0.03199496492743492, -0.01729976199567318, -0.0037526229862123728, -0.004208420868963003, -0.08060428500175476, 0.021138062700629234, -0.002681668847799301, -0.00485956110060215, 0.014366205781698227, 0.00364295719191432, 0.01595635898411274, -0.03668317571282387, -0.006271506659686565, -0.004181004595011473, -0.022001679986715317, 0.0107677998021245, -0.005767730064690113, -0.010102951899170876, -0.0022618549410253763, -0.02500377781689167, 0.0194382444024086, 0.005449014250189066, 0.03054189495742321, 0.01602489873766899, 0.012700658291578293, 0.011316129006445408, 0.019314870238304138, -0.0019979719072580338, -0.013043363578617573, 0.0076765986159443855, -0.01658693514764309, 0.0029027138371020555, 0.001953420229256153, -0.020411526784300804, 0.008019303902983665, -0.006333193741738796, -0.014352497644722462, 0.042632538825273514, -0.0003375647065695375, -0.008231781423091888, -0.004777311813086271, 0.0011275004362687469, -0.004492866341024637, 0.006734158843755722, -0.006648482754826546, -0.017148973420262337, 0.017848091199994087, -0.01798517256975174, -0.011014548130333424, -0.011850749142467976, -0.011185900308191776, 0.011672542430460453, 0.016545811668038368, -0.012988530099391937, 0.01606602407991886, 0.025798853486776352, -0.016230521723628044, -0.017779549583792686, -0.017916632816195488, -0.05469576269388199, 0.023920828476548195, -0.0019705554004758596, -0.006017904728651047, -0.016477270051836967, -0.0011549168266355991, 0.03317387029528618, 0.015668485313653946, -0.007416142616420984, 0.008266051299870014, -0.008567632175981998, -0.016463562846183777, -0.015174990519881248, 0.03240621089935303, -0.019945448264479637, 0.0007638044189661741, -0.02143964357674122, 0.01679255999624729, -0.015325780026614666, -0.00295754661783576, -0.0013759616995230317, -0.004969226662069559, -0.003567561972886324, -0.00832088477909565, 0.025428732857108116, 0.013070779852569103, -0.0047533223405480385, -0.03402378037571907, 0.01731347106397152, 0.016819976270198822, 0.01740942895412445, -0.02500377781689167, -0.008108407258987427, 0.013495733961462975, 0.012399077415466309, 0.014681494794785976, 0.017162680625915527, 0.006494265049695969, -0.0011103651486337185, 0.013790461234748363, 0.008519653230905533, -0.009479228407144547, -0.014311373233795166, 0.010569031350314617, 0.023824872449040413, 0.01805371418595314, -0.012919989414513111, -0.014311373233795166, -0.011747937649488449, -0.001802629791200161, 0.011980976909399033, 0.003875996917486191, -0.020452652126550674, -0.011747937649488449, 0.0221661776304245, 0.022083928808569908, 0.005781438201665878, -0.005658064503222704, 0.02290642075240612, -0.021206602454185486, 0.01747797057032585, -0.005849979352205992, -0.00865673553198576, -0.015545112080872059, 0.03525751829147339, 0.05255728214979172, 0.007683452684432268, 0.015545112080872059, -0.012769198976457119, 0.021042104810476303, 0.013838439248502254, 0.005329067353159189, -0.02429095096886158, 0.01872541755437851, -0.008492236956954002, 0.008094699122011662, -0.018218213692307472, -0.014818577095866203, -0.014215415343642235, -0.02079535648226738, -0.0004682210856117308, 0.014270247891545296, 0.020644566044211388, -0.019671283662319183, 0.07248902320861816, -0.007409288547933102, -0.004061057697981596, 0.0026748147793114185, 0.006326339673250914, 0.020987272262573242, 0.006847251672297716, 0.01672401838004589, -0.019177787005901337, -0.009869912639260292, 0.012967968359589577, 0.014996783807873726, 0.012001539580523968, -0.017902923747897148, -0.009390125051140785, -0.003324241377413273, -0.023139461874961853, 0.008924045599997044, -0.022563716396689415, 0.005952790845185518, 0.025154568254947662, 0.02434578351676464, 0.008766401559114456, -0.005945936776697636, -0.027622045949101448, -0.010534760542213917, 0.011096796952188015, -0.006723877973854542, -0.004643656779080629, -0.041261717677116394, 0.01945195160806179, -0.006158413831144571, -0.05329752713441849, -0.019095538184046745, 0.018286753445863724, -0.016545811668038368, -0.011137922294437885, 0.0027656317688524723, 0.02781396172940731, -0.0014282242627814412, -0.0244006160646677, 0.023770038038492203, -0.021343685686588287, -0.01731347106397152, 0.021905722096562386, -0.012022101320326328, -0.01355056744068861, -0.015654778108000755, 0.006107008084654808]\n" - ] - } - ], - "source": [ - "apple_embedding = get_embedding(\"apple\")\n", - "orange_embedding = get_embedding(\"orange\")\n", - "phone_embedding = get_embedding(\"phone\")\n", - "dog_embedding = get_embedding(\"I love dogs\")\n", - "animals_embedding = get_embedding(\"I love animals\")\n", - "cat_embedding = get_embedding(\"I hate cats\")\n", - "\n", - "print(apple_embedding)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Calculate Vector Similarities\n", - "To calculate the cosine similarity of the vectors, we also need the [SciPy](https://scipy.org/) package. SciPy contains many fundamental algorithms for scientific computing.\n", - "\n", - "Cosine similarity is used to measure the distance between two vectors. The closer the two vectors are, the higher the similarity between the embedded texts.\n", - "\n", - "πŸ‘‰ Import the SciPy package and define the method **get_cosine_similarity()**." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "from scipy import spatial\n", - "\n", - "# TODO the get_cosine_similarity function does not work very well does it? Fix it!\n", - "def get_cosine_similarity(vector_1, vector_2):\n", - " return 1 - spatial.distance.cosine(vector_1, vector_1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Calculate similarities between the embeddings of the words and phrases from above and find the most similar vectors. You can follow the example below." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "apple-orange\n", - "1.0\n" - ] - } - ], - "source": [ - "print(\"apple-orange\")\n", - "print(get_cosine_similarity(apple_embedding, orange_embedding))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[Next exercise](05-store-embeddings-hana.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test/05-store-embeddings-hana.ipynb b/09_BusinessAIWeek/Test/05-store-embeddings-hana.ipynb deleted file mode 100644 index b92f937..0000000 --- a/09_BusinessAIWeek/Test/05-store-embeddings-hana.ipynb +++ /dev/null @@ -1,485 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Store Embeddings for a Retrieval Augmented Generation (RAG) Use Case\n", - "\n", - "RAG is especially useful for question-answering use cases that involve large amounts of unstructured documents containing important information. \n", - "\n", - "Let’s implement a RAG use case so that the next time you ask about the [orchestration service](https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html), you get the correct response! To achieve this, you need to vectorize our context documents. You can find the documents to vectorize and store as embeddings in SAP HANA Cloud Vector Engine in the `documents` directory.\n", - "\n", - "## LangChain\n", - "\n", - "The Generative AI Hub Python SDK is compatible with the [LangChain](https://python.langchain.com/v0.1/docs/get_started/introduction) library. LangChain is a tool for building applications that utilize large language models, such as GPT models. It is valuable because it helps manage and connect different models, tools, and data, simplifying the process of creating complex AI workflows." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com/oauth/token\n", - "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540\n", - "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=\n", - "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "llm-deployed\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"llm-deployed\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Name: generative-ai-hub-sdk\n", - "Version: 4.4.3\n", - "Summary: generative AI hub SDK\n", - "Home-page: https://www.sap.com/\n", - "Author: SAP SE\n", - "Author-email: \n", - "License: SAP DEVELOPER LICENSE AGREEMENT\n", - "Location: C:\\Users\\C5385222\\AppData\\Local\\anaconda3\\Lib\\site-packages\n", - "Requires: ai-core-sdk, click, dacite, httpx, openai, overloading, packaging, pydantic\n", - "Required-by: \n" - ] - } - ], - "source": [ - "!pip show generative-ai-hub-sdk" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: generative-ai-hub-sdk[all] in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (4.4.3)\n", - "Requirement already satisfied: click>=8.1.7 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (8.1.7)\n", - "Requirement already satisfied: dacite>=1.8.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (1.9.2)\n", - "Requirement already satisfied: httpx>=0.27.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (0.27.0)\n", - "Requirement already satisfied: ai-core-sdk>=2.5.7 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (2.5.7)\n", - "Requirement already satisfied: overloading==0.5.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (0.5.0)\n", - "Requirement already satisfied: pydantic==2.9.2 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (2.9.2)\n", - "Requirement already satisfied: openai>=1.56.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (1.70.0)\n", - "Requirement already satisfied: packaging>=23.2 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (24.1)\n", - "Requirement already satisfied: langchain~=0.3.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (0.3.18)\n", - "Requirement already satisfied: langchain-openai>=0.2.14 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (0.3.12)\n", - "Requirement already satisfied: langchain-community~=0.3.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (0.3.17)\n", - "Requirement already satisfied: langchain-aws==0.2.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (0.2.1)\n", - "Requirement already satisfied: google-cloud-aiplatform==1.61.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (1.61.0)\n", - "Requirement already satisfied: langchain-google-vertexai==2.0.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (2.0.1)\n", - "Requirement already satisfied: boto3==1.35.27 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from generative-ai-hub-sdk[all]) (1.35.27)\n", - "Requirement already satisfied: botocore<1.36.0,>=1.35.27 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from boto3==1.35.27->generative-ai-hub-sdk[all]) (1.35.99)\n", - "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from boto3==1.35.27->generative-ai-hub-sdk[all]) (1.0.1)\n", - "Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from boto3==1.35.27->generative-ai-hub-sdk[all]) (0.10.4)\n", - "Requirement already satisfied: google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.34.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.34.1->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (2.24.2)\n", - "Requirement already satisfied: google-auth<3.0.0dev,>=2.14.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (2.38.0)\n", - "Requirement already satisfied: proto-plus<2.0.0dev,>=1.22.3 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (1.26.1)\n", - "Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<6.0.0dev,>=3.20.2 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (5.29.4)\n", - "Requirement already satisfied: google-cloud-storage<3.0.0dev,>=1.32.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (2.19.0)\n", - "Requirement already satisfied: google-cloud-bigquery!=3.20.0,<4.0.0dev,>=1.15.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (3.30.0)\n", - "Requirement already satisfied: google-cloud-resource-manager<3.0.0dev,>=1.3.3 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (1.14.2)\n", - "Requirement already satisfied: shapely<3.0.0dev in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (2.0.7)\n", - "Requirement already satisfied: docstring-parser<1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (0.16)\n", - "Requirement already satisfied: langchain-core<0.4,>=0.3.2 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain-aws==0.2.1->generative-ai-hub-sdk[all]) (0.3.51)\n", - "Requirement already satisfied: numpy<2.0.0,>=1.26.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain-aws==0.2.1->generative-ai-hub-sdk[all]) (1.26.4)\n", - "Requirement already satisfied: httpx-sse<0.5.0,>=0.4.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain-google-vertexai==2.0.1->generative-ai-hub-sdk[all]) (0.4.0)\n", - "Requirement already satisfied: annotated-types>=0.6.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from pydantic==2.9.2->generative-ai-hub-sdk[all]) (0.6.0)\n", - "Requirement already satisfied: pydantic-core==2.23.4 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from pydantic==2.9.2->generative-ai-hub-sdk[all]) (2.23.4)\n", - "Requirement already satisfied: typing-extensions>=4.6.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from pydantic==2.9.2->generative-ai-hub-sdk[all]) (4.11.0)\n", - "Requirement already satisfied: ai-api-client-sdk==2.4.6 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from ai-core-sdk>=2.5.7->generative-ai-hub-sdk[all]) (2.4.6)\n", - "Requirement already satisfied: aenum~=3.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from ai-api-client-sdk==2.4.6->ai-core-sdk>=2.5.7->generative-ai-hub-sdk[all]) (3.1.15)\n", - "Requirement already satisfied: pyhumps~=3.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from ai-api-client-sdk==2.4.6->ai-core-sdk>=2.5.7->generative-ai-hub-sdk[all]) (3.8.0)\n", - "Requirement already satisfied: requests<3.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from ai-api-client-sdk==2.4.6->ai-core-sdk>=2.5.7->generative-ai-hub-sdk[all]) (2.32.3)\n", - "Requirement already satisfied: colorama in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from click>=8.1.7->generative-ai-hub-sdk[all]) (0.4.6)\n", - "Requirement already satisfied: anyio in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from httpx>=0.27.0->generative-ai-hub-sdk[all]) (4.2.0)\n", - "Requirement already satisfied: certifi in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from httpx>=0.27.0->generative-ai-hub-sdk[all]) (2024.8.30)\n", - "Requirement already satisfied: httpcore==1.* in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from httpx>=0.27.0->generative-ai-hub-sdk[all]) (1.0.2)\n", - "Requirement already satisfied: idna in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from httpx>=0.27.0->generative-ai-hub-sdk[all]) (3.7)\n", - "Requirement already satisfied: sniffio in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from httpx>=0.27.0->generative-ai-hub-sdk[all]) (1.3.0)\n", - "Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from httpcore==1.*->httpx>=0.27.0->generative-ai-hub-sdk[all]) (0.14.0)\n", - "Requirement already satisfied: langchain-text-splitters<1.0.0,>=0.3.6 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain~=0.3.0->generative-ai-hub-sdk[all]) (0.3.6)\n", - "Requirement already satisfied: langsmith<0.4,>=0.1.17 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain~=0.3.0->generative-ai-hub-sdk[all]) (0.3.8)\n", - "Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain~=0.3.0->generative-ai-hub-sdk[all]) (2.0.34)\n", - "Requirement already satisfied: PyYAML>=5.3 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain~=0.3.0->generative-ai-hub-sdk[all]) (6.0.1)\n", - "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain~=0.3.0->generative-ai-hub-sdk[all]) (3.10.5)\n", - "Requirement already satisfied: tenacity!=8.4.0,<10,>=8.1.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain~=0.3.0->generative-ai-hub-sdk[all]) (8.2.3)\n", - "Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain-community~=0.3.0->generative-ai-hub-sdk[all]) (0.6.7)\n", - "Requirement already satisfied: pydantic-settings<3.0.0,>=2.4.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain-community~=0.3.0->generative-ai-hub-sdk[all]) (2.7.1)\n", - "Requirement already satisfied: tiktoken<1,>=0.7 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain-openai>=0.2.14->generative-ai-hub-sdk[all]) (0.9.0)\n", - "Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from openai>=1.56.0->generative-ai-hub-sdk[all]) (1.9.0)\n", - "Requirement already satisfied: jiter<1,>=0.4.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from openai>=1.56.0->generative-ai-hub-sdk[all]) (0.8.2)\n", - "Requirement already satisfied: tqdm>4 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from openai>=1.56.0->generative-ai-hub-sdk[all]) (4.66.5)\n", - "Requirement already satisfied: aiohappyeyeballs>=2.3.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain~=0.3.0->generative-ai-hub-sdk[all]) (2.4.0)\n", - "Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain~=0.3.0->generative-ai-hub-sdk[all]) (1.2.0)\n", - "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain~=0.3.0->generative-ai-hub-sdk[all]) (23.1.0)\n", - "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain~=0.3.0->generative-ai-hub-sdk[all]) (1.4.0)\n", - "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain~=0.3.0->generative-ai-hub-sdk[all]) (6.0.4)\n", - "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain~=0.3.0->generative-ai-hub-sdk[all]) (1.11.0)\n", - "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from botocore<1.36.0,>=1.35.27->boto3==1.35.27->generative-ai-hub-sdk[all]) (2.9.0.post0)\n", - "Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from botocore<1.36.0,>=1.35.27->boto3==1.35.27->generative-ai-hub-sdk[all]) (2.2.3)\n", - "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain-community~=0.3.0->generative-ai-hub-sdk[all]) (3.26.1)\n", - "Requirement already satisfied: typing-inspect<1,>=0.4.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain-community~=0.3.0->generative-ai-hub-sdk[all]) (0.9.0)\n", - "Requirement already satisfied: googleapis-common-protos<2.0.0,>=1.56.2 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.34.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.34.1->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (1.69.2)\n", - "Requirement already satisfied: grpcio<2.0dev,>=1.33.2 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.34.1->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (1.71.0)\n", - "Requirement already satisfied: grpcio-status<2.0.dev0,>=1.33.2 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.34.1->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (1.71.0)\n", - "Requirement already satisfied: cachetools<6.0,>=2.0.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-auth<3.0.0dev,>=2.14.1->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (5.3.3)\n", - "Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-auth<3.0.0dev,>=2.14.1->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (0.2.8)\n", - "Requirement already satisfied: rsa<5,>=3.1.4 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-auth<3.0.0dev,>=2.14.1->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (4.9)\n", - "Requirement already satisfied: google-cloud-core<3.0.0dev,>=2.4.1 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-bigquery!=3.20.0,<4.0.0dev,>=1.15.0->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (2.4.3)\n", - "Requirement already satisfied: google-resumable-media<3.0dev,>=2.0.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-bigquery!=3.20.0,<4.0.0dev,>=1.15.0->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (2.7.2)\n", - "Requirement already satisfied: grpc-google-iam-v1<1.0.0,>=0.14.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-resource-manager<3.0.0dev,>=1.3.3->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (0.14.2)\n", - "Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from google-cloud-storage<3.0.0dev,>=1.32.0->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (1.7.0)\n", - "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langchain-core<0.4,>=0.3.2->langchain-aws==0.2.1->generative-ai-hub-sdk[all]) (1.33)\n", - "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langsmith<0.4,>=0.1.17->langchain~=0.3.0->generative-ai-hub-sdk[all]) (3.10.15)\n", - "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langsmith<0.4,>=0.1.17->langchain~=0.3.0->generative-ai-hub-sdk[all]) (1.0.0)\n", - "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from langsmith<0.4,>=0.1.17->langchain~=0.3.0->generative-ai-hub-sdk[all]) (0.23.0)\n", - "Requirement already satisfied: python-dotenv>=0.21.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from pydantic-settings<3.0.0,>=2.4.0->langchain-community~=0.3.0->generative-ai-hub-sdk[all]) (1.0.1)\n", - "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from requests<3.0->ai-api-client-sdk==2.4.6->ai-core-sdk>=2.5.7->generative-ai-hub-sdk[all]) (3.3.2)\n", - "Requirement already satisfied: greenlet!=0.4.17 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain~=0.3.0->generative-ai-hub-sdk[all]) (3.0.1)\n", - "Requirement already satisfied: regex>=2022.1.18 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from tiktoken<1,>=0.7->langchain-openai>=0.2.14->generative-ai-hub-sdk[all]) (2024.9.11)\n", - "Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.4,>=0.3.2->langchain-aws==0.2.1->generative-ai-hub-sdk[all]) (2.1)\n", - "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from pyasn1-modules>=0.2.1->google-auth<3.0.0dev,>=2.14.1->google-cloud-aiplatform==1.61.0->generative-ai-hub-sdk[all]) (0.4.8)\n", - "Requirement already satisfied: six>=1.5 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.36.0,>=1.35.27->boto3==1.35.27->generative-ai-hub-sdk[all]) (1.16.0)\n", - "Requirement already satisfied: mypy-extensions>=0.3.0 in c:\\users\\c5385222\\appdata\\local\\anaconda3\\lib\\site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain-community~=0.3.0->generative-ai-hub-sdk[all]) (1.0.0)\n" - ] - } - ], - "source": [ - "!pip install \"generative-ai-hub-sdk[all]\"" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.proxy.core.proxy_clients import get_proxy_client\n", - "from gen_ai_hub.proxy.langchain.init_models import init_embedding_model" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "import warnings\n", - "warnings.filterwarnings(\"ignore\", message=\"As the c extension\") #Avoid a warning message for \"RuntimeWarning: As the c extension couldn't be imported, `google-crc32c` is using a pure python implementation that is significantly slower. If possible, please configure a c build environment and compile the extension. warnings.warn(_SLOW_CRC32C_WARNING, RuntimeWarning)\"\n", - "\n", - "# OpenAIEmbeddings to create text embeddings\n", - "from gen_ai_hub.proxy.langchain.openai import OpenAIEmbeddings\n", - "\n", - "# TextLoader to load documents\n", - "from langchain_community.document_loaders import PyPDFDirectoryLoader\n", - "\n", - "# different TextSplitters to chunk documents into smaller text chunks\n", - "from langchain_text_splitters import RecursiveCharacterTextSplitter\n", - "\n", - "# LangChain & HANA Vector Engine\n", - "from langchain_community.vectorstores.hanavector import HanaDB" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Change the `EMBEDDING_DEPLOYMENT_ID` in [variables.py](variables.py) to your deployment ID from exercise [01-explore-genai-hub](01-explore-genai-hub.md). For that go to **SAP AI Launchpad** application and navigate to **ML Operations** > **Deployments**.\n", - "\n", - "☝️ The `EMBEDDING_DEPLOYMENT_ID` is the embedding model that creates vector representations of your text e.g. **text-embedding-3-small**.\n", - "\n", - "πŸ‘‰ In [variables.py](variables.py) also set the `EMBEDDING_TABLE` to `\"EMBEDDINGS_CODEJAM_>add your name here<\"`, like `\"EMBEDDINGS_CODEJAM_NORA\"`.\n", - "\n", - "πŸ‘‰ In the root folder of the project create a [.user.ini](../.user.ini) file with the HANA login information provided by the instructor.\n", - "```sh\n", - "[hana]\n", - "url=XXXXXX.hanacloud.ondemand.com\n", - "user=XXXXXX\n", - "passwd=XXXXXX\n", - "port=443\n", - "```" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [], - "source": [ - "EMBEDDING_DEPLOYMENT_ID = \"d7e7cd43afe2b169\"" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "def get_hana_connection():\n", - " conn = dbapi.connect(\n", - " address='ec41b786-96de-467b-9ff5-db725945f89c.hna0.prod-us10.hanacloud.ondemand.com',\n", - " port='443',\n", - " user='DBADMIN',\n", - " password='9hEW4UK86Fdt',\n", - " encrypt=True\n", - " )\n", - " return conn" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Collecting hdbcli\n", - " Downloading hdbcli-2.24.24-cp36-abi3-win_amd64.whl.metadata (6.1 kB)\n", - "Downloading hdbcli-2.24.24-cp36-abi3-win_amd64.whl (3.6 MB)\n", - " ---------------------------------------- 0.0/3.6 MB ? eta -:--:--\n", - " ----------------------- ---------------- 2.1/3.6 MB 29.1 MB/s eta 0:00:01\n", - " ---------------------------------------- 3.6/3.6 MB 10.7 MB/s eta 0:00:00\n", - "Installing collected packages: hdbcli\n", - "Successfully installed hdbcli-2.24.24\n" - ] - } - ], - "source": [ - "!pip install hdbcli" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], - "source": [ - "from hdbcli import dbapi" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Chunking of the documents\n", - "\n", - "Before you can create embeddings for your documents, you need to break them down into smaller text pieces, called \"`chunks`\". You will use the simplest chunking technique, which involves splitting the text based on character length and the separator `\"\\n\\n\"`, using the [Character Text Splitter](https://python.langchain.com/v0.1/docs/modules/data_connection/document_transformers/character_text_splitter/) from LangChain.\n", - "\n", - "## Character Text Splitter" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Collecting pypdf\n", - " Downloading pypdf-5.4.0-py3-none-any.whl.metadata (7.3 kB)\n", - "Downloading pypdf-5.4.0-py3-none-any.whl (302 kB)\n", - "Installing collected packages: pypdf\n", - "Successfully installed pypdf-5.4.0\n" - ] - } - ], - "source": [ - "!pip install pypdf" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Number of document chunks: 59\n" - ] - } - ], - "source": [ - "\n", - "# Load custom documents\n", - "loader = PyPDFDirectoryLoader('documents/')\n", - "documents = loader.load()\n", - "text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)\n", - "texts = text_splitter.split_documents(documents)\n", - "print(f\"Number of document chunks: {len(texts)}\")\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now you can connect to your SAP HANA Cloud Vector Engine and store the embeddings for your text chunks." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Table EMBEDDINGS_CODEJAM_Testing created in the SAP HANA database.\n" - ] - } - ], - "source": [ - "import importlib\n", - "#variables = importlib.reload(variables)\n", - "\n", - "# DO NOT CHANGE THIS LINE BELOW - It is only to check whether you changed the table name in variables.py\n", - "#assert variables.EMBEDDING_TABLE != 'EMBEDDINGS_CODEJAM_ADD_YOUR_NAME_HERE', 'EMBEDDING_TABLE name not changed in variables.py'\n", - "\n", - "# Create embeddings for custom documents\n", - "embeddings = OpenAIEmbeddings(deployment_id=EMBEDDING_DEPLOYMENT_ID)\n", - "\n", - "db = HanaDB(\n", - " embedding=embeddings, connection=get_hana_connection(), table_name=\"EMBEDDINGS_CODEJAM_\"+\"Testing\"\n", - ")\n", - "\n", - "# Delete already existing documents from the table\n", - "db.delete(filter={})\n", - "\n", - "# add the loaded document chunks\n", - "db.add_documents(texts)\n", - "print(f\"Table {db.table_name} created in the SAP HANA database.\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Check the embeddings in SAP HANA Cloud Vector Engine\n", - "\n", - "πŸ‘‰ Print the rows from your embedding table and scroll to the right to see the embeddings." - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'VEC_TEXT': '/Examples/Orchestration Service\\nOrchestration Service\\nThis notebook demonstrates how to use the SDK to interact with the Orchestration\\nService, enabling the creation of AI-driven workflows by seamlessly integrating various\\nmodules, such as templating, large language models (LLMs), data masking and content\\nfiltering. By leveraging these modules, you can build complex, automated workflows that\\nenhance the capabilities of your AI solutions. For more details on configuring and using',\n", - " 'VEC_META': {'producer': 'Skia/PDF m131',\n", - " 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',\n", - " 'creationdate': '2025-02-02T21:04:28+00:00',\n", - " 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation',\n", - " 'moddate': '2025-02-02T21:04:28+00:00',\n", - " 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf',\n", - " 'total_pages': 14,\n", - " 'page': 0,\n", - " 'page_label': '1'},\n", - " 'VEC_VECTOR': '[-0.035093676,-0.0027178903,-0.029775612,0.003860443,-0.010629201,0.015898792,-0.012588852,0.0023162656,-0.016466606,-0.021410743,0.03204687,-0.00962514,-0.008634928,-0.009368931,-0.022463277,0.0028563815,0.015857246,-0.011958717,0.01577415,-0.021424592,-0.02966482,0.015718754,0.004480191,-0.006450229,0.00658872,0.004369398,0.014873957,-0.038085084,0.016785135,0.0027559754,0.02067674,-0.012505758,-0.037281837,-0.022920297,0.015649509,0.0027161592,0.0046602297,-0.0005660829,-0.0020964108,-0.0007768492,0.019651905,0.018613221,-0.0023733934,0.0041512745,-0.010469937,0.03357027,-0.030717352,-0.028224511,0.0031749113,-0.0012507489,0.019153336,0.030191086,-0.020579796,-0.0152894305,0.012983552,0.014887806,-0.01641121,0.015511017,0.037808105,-0.0284184,-0.009784405,0.011612489,-0.007838603,-0.024346758,-0.022906449,-0.011799452,-0.0065817954,0.017726876,0.008600305,-0.011813302,0.02679805,0.023792792,-0.032102264,-0.028612286,0.02570397,-0.028750777,-0.03168679,-0.0046221446,-0.0027905982,0.0030520004,0.0023006855,-0.01238804,0.010594578,0.045037344,0.017311404,0.021715423,-0.002219322,0.018696316,0.0019579197,-0.00540462,0.024623739,-0.010054463,0.007963246,0.010206803,-0.0008473932,0.009341233,-0.005629668,0.03204687,-0.006010519,-0.026770353,-0.0053976956,0.011439375,-0.040023964,-0.008316398,-0.05949583,-0.015801849,0.01720061,-0.007928623,0.024692984,0.020053528,-0.0125403805,0.029554026,-0.009715159,-0.039386902,-0.007859377,-0.006048604,0.0016246751,-0.019471865,-0.039525393,-0.0034016906,0.0026330643,0.049746048,0.015386375,-0.018100802,0.022920297,0.0025257338,0.021729274,-0.02855689,0.008136359,-0.013489045,0.03171449,-0.004397096,0.0083510205,-0.00013178306,-0.009112722,0.0072500156,-0.026936542,0.009244289,-0.030578861,0.010206803,0.0007123642,0.0109685045,0.0037669612,-0.0019458017,0.0015900524,0.009036552,0.016798986,0.005865103,-0.0034484314,0.0036457814,0.004092416,-0.028750777,-0.00138924,-0.00678607,0.009237365,0.007568545,0.01783767,0.020773683,-0.0152894305,-0.0037219515,-0.002331846,0.030661957,-0.00256555,-0.014347691,0.04212903,-0.007831679,0.019125639,0.00041655562,-0.01199334,0.011273186,0.0073954314,0.03423503,-0.02146614,0.012173379,0.011377054,-0.017491441,0.025122307,-0.029581726,-0.013502894,-0.01703442,0.019194884,0.00081277033,0.025773216,0.014278445,-0.025690122,-0.009742858,-0.0061490103,-0.0067375978,0.01893175,-0.0016039015,-0.026964242,0.03614621,0.0015891867,0.011501696,-0.6293041,-0.045979086,-0.00009759303,-0.023862038,0.02380664,0.0060035945,-0.008884212,0.0020548636,-0.007291563,0.022338634,-0.0012689258,-0.00015439607,-0.012104133,0.005958585,0.012256473,-0.025136156,0.013842198,-0.024734532,-0.017643781,0.007637791,-0.011757905,0.007679338,-0.020330511,-0.013205138,-0.0074577522,0.0010265661,0.017145213,0.012962778,0.0073123365,0.019859642,0.0054115443,-0.0077901315,0.013599838,0.013212062,0.041852046,0.01988734,-0.02793368,0.014472333,0.009888274,0.04041174,-0.007402356,-0.0002984053,0.021660028,0.005034156,0.012983552,0.007921698,0.02665956,-0.01562181,-0.019970434,-0.018114652,0.027545905,-0.0051657227,0.022490975,0.0020358209,-0.0038327447,0.004715626,0.024346758,-0.01214568,0.021244554,0.0033480253,-0.0007443903,0.010476861,-0.023958981,-0.008136359,-0.022657163,0.018225444,0.0016264063,-0.0009772287,-0.021563083,-0.032573134,0.035398357,0.020302814,0.009528196,-0.014389238,0.020926023,0.022823354,0.010248351,0.021909311,0.005726612,-0.0035523,0.020482851,0.0021189158,-0.0014541579,-0.028639985,-0.00013708467,0.016369663,-0.025814764,-0.0033687989,0.015954189,-0.0037288761,0.036700174,0.014112256,0.0015770688,-0.044400286,0.009050401,-0.008704173,0.0041166516,0.027310468,0.013627537,-0.00819868,0.010892334,0.011016977,0.014403087,0.008974231,0.042239822,0.0069384105,-0.027698245,0.012969703,0.022837203,-0.021812368,0.0060382173,-0.016452758,-0.018834807,0.016134227,0.01625887,-0.023889735,0.01120394,-0.012519606,0.02808602,-0.016341964,-0.012706569,-0.0067756833,0.014887806,-0.003394766,0.016355813,0.018710164,-0.0028892732,-0.037891198,0.004355549,-0.007824754,0.0012914307,-0.028529191,0.002470337,-0.0076447153,0.035786133,-0.00016164522,0.04307077,-0.005169185,0.01940262,-0.013094345,-0.018239293,-0.015788,0.016992873,-0.011515545,-0.0067722206,-0.0030156465,-0.024097472,-0.0014792593,-0.0072638644,0.0035869228,-0.00058079755,-0.022643315,-0.00024517273,0.012041812,0.011349356,-0.018045407,-0.007561621,-0.024845324,-0.0060070567,-0.02113376,-0.008600305,0.023751244,-0.027158128,0.009576668,-0.009514347,-0.00555696,-0.02429136,0.01735295,-0.03506598,-0.028695382,0.008904985,-0.016051132,-0.016605098,-0.004667154,0.006342898,-0.016466606,-0.03138211,0.007942472,0.04528663,-0.0037427254,-0.007443903,-0.013128968,-0.02650722,-0.02304494,0.034041144,0.0002549104,0.03742033,0.012845061,-0.0023976292,0.008870363,-0.028293757,0.01656355,0.010795391,0.019624205,-0.0013857778,0.012962778,-0.018280841,0.019485716,0.011564017,0.003701178,0.03393035,0.020233568,0.015261733,-0.010705371,-0.0082887,-0.013689857,-0.009687461,-0.018003859,0.0426276,0.009819028,0.04041174,-0.01718676,0.00059075165,-0.028169114,-0.006602569,0.0005812303,0.025039213,-0.015511017,-0.039497696,0.0012135294,0.013828348,-0.014105331,0.01112777,-0.032323852,-0.0043763225,0.0048298812,0.017588384,0.03174219,0.019984283,0.0020046604,-0.012415738,0.031132827,0.028044473,-0.006993807,-0.0021033355,-0.009445101,0.005034156,-0.006789532,0.018945599,-0.033210196,0.008863438,-0.015691055,0.028182963,-0.02445755,0.028639985,-0.011300884,0.025537781,0.020939874,0.012194152,-0.006446766,-0.030883543,-0.0011676542,-0.023100335,0.012491908,0.0014714693,0.0060001323,-0.021036817,-0.014416936,0.0022123973,0.012263398,-0.019790396,0.01941647,0.02635488,0.011834075,-0.0068172305,-0.032711625,-0.0064363796,0.011162393,-0.022297086,0.0018350087,0.012831212,-0.015095543,0.0002261302,-0.031188224,0.014777013,-0.0048056454,0.015511017,0.019998133,0.009964444,0.01909794,-0.010289898,-0.02618869,0.013454422,-0.011183166,0.009507422,-0.01703442,-0.023405017,0.012083359,-0.02034436,0.018654767,0.017976161,0.025787065,-0.00572315,0.009334308,-0.0015649508,0.0025465074,0.03598002,-0.019776547,0.0029689055,-0.0081086615,0.028916968,0.0070492034,0.0041166516,-0.013309007,0.032157663,-0.0073538837,-0.019014845,-0.008946533,-0.004667154,-0.053623803,0.019347224,0.0012533455,-0.017075967,-0.029193949,0.023460412,-0.010476861,-0.007762433,-0.00757547,0.019513413,0.03708795,0.013122044,-0.015400224,0.002394167,0.01688208,0.04021785,0.020316662,0.010317596,0.011106996,-0.045037344,-0.023640452,-0.027753642,-0.016688192,0.0152340345,0.0028494569,0.0005041946,0.000040952287,0.02430521,0.010546107,0.010622277,0.0041547366,0.00402317,-0.010954656,0.00349171,-0.044427983,0.014264596,0.009777481,0.0001539633,0.0077416594,0.022324786,0.008143283,0.010456087,0.009839801,-0.03138211,-0.04559131,0.0024218652,0.012526531,0.0046844655,0.027864434,-0.013426724,0.032323852,0.014721617,0.029803311,-0.009396629,0.013959915,-0.0017389305,-0.0077416594,-0.016369663,-0.005788933,-0.0039539244,-0.02697809,-0.02570397,0.04957986,-0.01814235,-0.008718022,-0.007561621,0.0116055645,-0.027338168,-0.0064329174,0.014500031,0.01332978,-0.012755042,-0.010698447,0.005595045,-0.04938597,-0.043513943,-0.010947731,-0.006761834,0.0056158192,-0.018336238,-0.011515545,-0.015469469,0.022684863,-0.014486182,0.0021916237,0.011965642,0.011806376,-0.036866363,0.0063463603,0.051324848,0.026133293,0.0051518735,-0.020122774,-0.0010274318,0.009389705,0.012962778,-0.011010053,-0.002297223,-0.026964242,-0.021604631,-0.0048679663,-0.006332511,-0.009542045,0.0019665754,0.03235155,-0.008330247,0.0036353946,0.014167652,0.0048541175,0.020787533,0.027850585,0.0097913295,0.0089119105,-0.0040785666,-0.030191086,-0.010068312,-0.031354412,0.008524135,-0.014721617,0.010386841,0.008676475,0.00071452814,0.011875622,-0.00575431,0.0039643114,0.02823836,-0.004168586,0.0054704035,-0.009036552,0.024263661,-0.0034761296,-0.00017257304,-0.016425058,0.017283704,-0.0066960505,0.009514347,-0.025787065,0.018280841,0.012055661,0.0008815832,0.0099228965,0.008357945,-0.009126572,-0.022671012,0.0041928217,-0.0025136156,-0.0051864963,-0.0015978424,-0.0025395828,0.0016489112,-0.00827485,-0.0074716015,0.0041858973,-0.0035349885,-0.02509461,0.00041352614,-0.010227577,0.012685796,-0.0085379835,-0.01222185,-0.034013443,0.005245355,0.028321454,-0.006914174,0.025482384,0.015344827,0.018363936,-0.024651438,-0.024028227,-0.010774617,-0.012471135,-0.02902776,-0.009542045,0.03296091,0.03534296,0.048942797,-0.00844104,0.023557357,-0.0020358209,-0.0004029229,-0.03143751,0.026465673,-0.001061189,-0.0036180832,-0.0073469593,0.012665022,-0.008074039,0.01671589,0.013309007,-0.00256555,0.013918368,0.012360342,-0.024097472,-0.01466622,-0.043347754,-0.012325719,0.01988734,-0.007928623,-0.008586456,-0.030163389,-0.001975231,0.0276013,0.010539182,0.011404752,0.002395898,0.0099782925,-0.006872627,0.014763164,0.0077832066,0.008995005,-0.035592243,-0.0019977358,-0.02966482,0.0066995127,0.01926413,0.009140421,0.0073123365,0.029415535,0.008191756,0.0040751044,0.025177704,0.0069695706,-0.01419535,-0.0109131085,-0.03060656,-0.016161926,-0.045868292,0.0068518533,-0.012879684,-0.012076435,-0.0012611357,0.0011460149,0.019167185,-0.033237893,-0.013599838,0.021909311,-0.018114652,0.03561994,-0.0029446697,0.02478993,-0.0034467003,0.008863438,-0.01419535,0.0017562418,-0.01862707,-0.0030606561,0.0040785666,0.027268922,0.0023024166,-0.041879747,0.014790863,-0.024873024,-0.012491908,-0.030883543,0.012526531,0.012665022,0.003985085,-0.019762697,-0.018266993,-0.011757905,0.03645089,-0.008925759,0.0055396487,0.013565215,0.012796589,0.01609268,-0.02351581,-0.0053769215,0.017283704,0.009265062,0.01050456,-0.012249549,0.0018176973,-0.012187228,-0.004393634,-0.0016220785,0.0055846586,-0.00579932,0.007111524,0.037475724,0.015012449,0.009729008,0.025219252,0.013613687,0.03689406,-0.024055926,-0.023571206,0.0025482385,-0.019471865,0.011834075,-0.021396894,-0.01167481,0.011882547,-0.0036180832,-0.00026032023,0.012976628,0.00066865294,0.013385177,-0.013745254,0.00429669,-0.029387837,0.014223048,-0.0028459947,-0.0011858311,-0.031797584,-0.023917435,-0.015788,-0.02336347,0.022421729,0.005518875,-0.028695382,0.00859338,0.020579796,0.00012009786,0.00382582,0.0055396487,0.01167481,-0.001757973,0.0056989137,-0.04182435,0.00035012313,0.021992406,-0.005172647,-0.030745052,-0.02397283,-0.015538715,0.024886873,0.0054634786,-0.008697249,0.010989279,-0.0009235633,0.0025274649,0.021660028,-0.005297289,0.032628532,0.003223383,-0.025413139,0.041159593,-0.026257936,0.00827485,0.0026174842,0.0073608086,-0.0012472866,-0.01938877,0.0046948525,0.016688192,0.013516744,0.00599667,0.009666687,0.0011840999,-0.0030260333,-0.01768533,-0.027171979,0.0064363796,-0.020233568,0.010663824,-0.0027507818,0.03249004,-0.0043901717,-0.011702509,0.008191756,-0.027989076,-0.010075237,-0.027185827,-0.008261002,0.004861042,0.013676008,0.01513709,-0.01862707,-0.026604164,-0.039165318,-0.01468007,-0.0125403805,-0.026756505,0.0031974162,0.0048091076,0.036312398,0.019333374,0.009015779,-0.0045009647,0.0049995333,-0.00690725,-0.010372993,0.0092858365,-0.018696316,0.002492842,-0.004393634,-0.018640919,-0.006834542,-0.0019561886,-0.0069176364,-0.004088953,0.023778943,-0.0005050602,-0.03453971,-0.017311404,0.0019717687,-0.019485716,0.006367134,0.013509818,-0.017159063,0.004822957,-0.016286567,-0.013856047,0.006429455,0.0165774,0.01877941,-0.0033272516,0.005093015,0.0005708435,0.009168119,-0.020302814,0.004632531,0.03769731,-0.008129435,0.013994538,0.02650722,0.0063844454,0.0022695248,0.009999067,0.025233101,-0.007637791,-0.027518205,0.042516805,-0.021521537,-0.012041812,-0.0025499696,-0.014001463,-0.002589786,-0.024983816,0.01049071,0.014957052,-0.019499565,0.009313535,-0.014458484,-0.034982882,0.009805178,-0.0075546964,-0.024319058,-0.020302814,-0.03249004,0.033376385,-0.013530592,-0.028584588,-0.008960382,-0.0009971368,-0.0347059,0.010532257,0.009715159,-0.0029308205,0.00772781,0.16098219,0.00037522466,-0.025260799,-0.00069159054,-0.018433182,0.022200143,0.003024302,0.004428257,-0.02460989,0.02129995,0.00619402,0.016480455,0.00091750437,-0.0055396487,0.008094812,-0.036257003,-0.03077275,-0.017394498,0.0071599963,-0.027310468,0.0152894305,-0.03091124,0.00088331435,-0.018225444,0.00962514,-0.009278912,-0.016702041,0.008711098,0.005885877,0.018211596,-0.02271256,0.04041174,0.00018674675,0.015954189,0.017754575,-0.015926491,0.007852452,0.006443304,0.008614154,0.012561154,0.003109128,-0.0053596105,0.024859175,-0.0212861,-0.006138623,0.013579064,-0.025523933,-0.029858708,0.030412672,0.0066094934,-0.028861571,0.013309007,0.02902776,0.01325361,0.025219252,0.019665753,-0.0037323385,0.03489979,0.019319525,0.026396427,-0.03423503,0.011349356,-0.013357478,-0.018613221,-0.01332978,0.0063359733,-0.010345294,-0.007492375,0.0009097142,0.0058166315,0.006377521,-0.006675277,-0.0030260333,-0.0031247083,-0.020288965,-0.031132827,0.0331548,0.022587918,0.017020572,0.027573602,-0.003517677,-0.0129143065,-0.013752178,-0.0014844527,-0.022823354,-0.027573602,0.017228307,-0.022504823,-0.030800449,0.019956585,-0.013883745,-0.023862038,-0.016785135,-0.023709698,-0.0017969236,0.0009711697,0.034013443,0.0066441162,-0.0058789523,-0.0037738858,-0.033958048,0.07716731,-0.010753843,0.0045840596,-0.018696316,-0.010663824,0.0041893595,0.013218987,0.0011382247,-0.009278912,-0.01846088,-0.03802969,0.008101736,-0.0082887,0.0035003657,0.016826684,-0.018848656,0.0030173776,0.011058524,-0.0072984872,-0.015400224,-0.006612956,-0.011197016,0.02665956,-0.022144746,-0.008953458,-0.01080924,0.00819868,-0.010449163,-0.022061652,0.006934948,0.0061593973,0.0007924295,-0.0068553155,0.008011717,-0.0017406616,0.015358676,0.0020133161,-0.025080761,0.005331912,0.019430319,0.012637324,0.009694385,0.005259204,0.016854381,-0.009638989,0.019610357,-0.00039859506,-0.0040716417,-0.04495425,-0.009853651,-0.012484984,0.0070076557,-0.011467073,0.017962312,-0.023737395,-0.01767148,-0.012678872,0.0061178496,0.001603036,-0.056199737,0.0043763225,0.014403087,-0.0045355875,-0.013842198,-0.018585522,-0.17715797,0.0046567675,0.011342431,-0.050826278,0.029609423,-0.009105798,0.00048212256,0.011273186,-0.01656355,0.011951792,0.013987614,0.0023855113,-0.020552097,-0.022837203,-0.01610653,0.023086486,-0.0026088285,0.0039054526,0.039996266,0.006058991,0.05251587,-0.008357945,0.0019284902,-0.014278445,-0.012201076,-0.015511017,-0.022033954,0.010407615,-0.003860443,-0.04512044,-0.003690791,0.0099782925,0.00277848,0.012928155,0.008101736,0.016425058,0.032102264,-0.015843395,-0.02491457,0.021715423,0.02413902,0.04340315,-0.0017899991,0.02603635,0.017948462,0.012284172,0.020607494,-0.016979024,0.000008581316,-0.008247152,0.028944666,-0.023474263,-0.020732136,-0.004937212,-0.01364831,0.012907382,0.0011520738,0.01624502,-0.018848656,-0.029720217,-0.0072777136,-0.0089119105,0.0051657227,-0.02350196,-0.02336347,0.02146614,-0.013952991,0.013994538,-0.018100802,0.020704437,0.007873226,0.004407483,-0.012450361,0.0030520004,0.026867297,0.008087887,-0.0107815415,0.031409808,0.015317129,0.001653239,0.018183898,0.03581383,-0.0040820288,0.012208002,-0.002466875,0.018516276,-0.011155468,0.010989279,-0.011217789,0.012284172,0.032683928,-0.02649337,0.0040266323,-0.004861042,0.020939874,0.0065817954,0.022823354,0.002382049,0.00827485,0.014763164,0.00068596436,0.022158595,0.022130897,0.005093015,0.040190153,0.0035436442,0.009555894,-0.0077693574,0.020552097,-0.013018175,-0.029886406,-0.0017242157,0.023224978,0.02301724,0.0037358007,0.045563612,-0.0054115443,0.00725694,0.0015164788,0.01750529,0.03484439,0.02635488,-0.016134227,0.0009452026,-0.005577734,-0.02697809,-0.092844516,-0.018502427,-0.011453224,0.028778477,-0.024014378,0.006606031,-0.008925759,0.026701108,0.005408082,0.01396684,-0.003725414,-0.02807217,-0.0004639456,-0.0038154332,-0.0048056454,-0.019361073,-0.006723749,-0.021632329,0.013122044,0.02650722,0.0073469593,0.008759569,0.0067410604,-0.027573602,-0.0131012695,-0.011370129,-0.053069837,0.011404752,0.023072638,0.003071043,0.029055458,-0.013516744,0.0009936745,-0.017408347,-0.0065229367,-0.002627871,-0.011681735,-0.018502427,0.03800199,-0.025219252,0.0101098595,-0.002887542,-0.0013883745,-0.021493837,-0.00844104,0.011626339,-0.026299484,-0.015123242,0.00042499494,-0.012568078,-0.00579932,-0.0097913295,-0.02778134,0.01041454,0.028916968,0.02082908,0.0096043665,-0.0027542443,-0.010379917,-0.01591264,0.021563083,-0.0075339223,-0.0022245152,0.04243371,-0.008801118,-0.0031904916,-0.026894996,-0.024969967,-0.005259204,-0.01325361,0.0032562749,0.0051968833,-0.01923643,0.013994538,-0.007762433,-0.0082956245,0.0016757438,-0.035038278,-0.0033030156,-0.00051847653,-0.013703707,-0.021715423,0.0053769215,-0.013544441,-0.0046221446,0.010753843,-0.024346758,0.017325252,0.00698342,-0.03168679,0.015981887,0.036090814,0.013669084,-0.018183898,0.018585522,-0.007492375,-0.03345948,-0.018834807,-0.004604833,0.03719874,-0.019818094,-0.0107815415,-0.08021411,0.017117515,0.016688192,-0.010864637,-0.009306611,-0.009327384,-0.0071253735,-0.024679136,0.0054565542,0.014403087,-0.043153863,0.014901656,-0.014319993,0.0067826076,0.000974632,-0.014998599,0.046449956,-0.022463277,0.01671589,0.0044351816,-0.017006721,0.0025811302,0.01405686,0.028169114,-0.006107463,-0.0038085086,-0.03174219,0.032296155,-0.0017250812,-0.034622807,0.0020046604,-0.028030623,-0.0068172305,0.03725414,-0.013946066,0.000031214622,0.034041144,0.012955854,0.019859642,0.014721617,-0.023751244,-0.057335366,0.037309535,-0.026631862,-0.011383979,-0.018488578,0.0064987005,-0.00024387438,0.0055396487,0.001425594,0.015067845,0.017214458,-0.05124175,-0.016148077,0.0007759836,-0.0063532847,0.015843395,-0.013454422,0.0142092,0.0060382173,0.034428917,0.0011693853,0.01563566,-0.0010205072,0.015469469,0.0066164182,-0.014278445,0.010324521,0.016923627,-0.013932217,-0.0155525645,-0.0098675,-0.0060001323,0.0039573866,0.008385643,-0.030024897,0.0039054526,0.012768891,-0.01846088,0.02603635,0.014583126,0.01988734,-0.009313535,0.035869226,-0.001622944,0.005048005,-0.02872308,-0.014749316,-0.0034328513,-0.016840532,-0.021147609,-0.013516744,-0.010719221,0.014790863,0.027352016,0.0008374391,0.00678607,-0.011501696,0.025468536,0.010054463,0.0152340345,0.017325252,-0.0009045208,-0.025759367,-0.004597909,0.015053996,-0.018253144,-0.023418866,0.017297553,-0.010823089,-0.012962778,0.0011918901,-0.0018228907,0.0044628796,-0.019984283,0.018391635,0.0038292822,-0.026050199,-0.04337545,0.020469002,0.015538715,0.0135513665,0.029443234,0.0071392227,0.015857246,0.017463744,0.004469804,-0.016189624,0.011681735,0.004320926,-0.0000057411644,-0.007229242,-0.02699194,-0.009493574,-0.01639736,-0.0149155045,-0.03124362,0.0070041935,0.015206336,0.06608801,0.0011944867,-0.010670749,0.0033030156,-0.033736464,0.0132882325,-0.0071392227,0.005975896,-0.025620876,-0.022504823,-0.00069159054,-0.0015026297,-0.008378719,-0.01158479,-0.024540644,0.016314266,-0.01956881,0.02539929,-0.0027940604,-0.0117371315,0.027545905,0.008489512,0.007873226,-0.008157133,0.0013883745,-0.000098945486,0.028182963,-0.0023439638,-0.018710164,-0.021050666,0.034179635,-0.0047433246,-0.031022035,-0.014763164,0.012159529,0.019194884,-0.0021050666,-0.032129962,0.02067674,0.013516744,-0.014638523,0.013558291,-0.025994802,-0.026728805,0.0031749113,0.0032320388,0.0038812165,-0.01703442,-0.012194152]'}" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "from IPython.display import Markdown\n", - "cursor = db.connection.cursor()\n", - "\n", - "# Use `db.table_name` instead of `variables.EMBEDDING_TABLE` because HANA driver sanitizes a table name by removing unaccepted characters\n", - "is_ok = cursor.execute(f'''SELECT \"VEC_TEXT\", \"VEC_META\", TO_NVARCHAR(\"VEC_VECTOR\") FROM \"{db.table_name}\"''')\n", - "record_columns=cursor.fetchone()\n", - "if record_columns:\n", - " display({\"VEC_TEXT\" : record_columns[0], \"VEC_META\" : eval(record_columns[1]), \"VEC_VECTOR\" : record_columns[2]})\n", - "cursor.close()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[Next exercise](06-RAG.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test/06-RAG.ipynb b/09_BusinessAIWeek/Test/06-RAG.ipynb deleted file mode 100644 index 57946fa..0000000 --- a/09_BusinessAIWeek/Test/06-RAG.ipynb +++ /dev/null @@ -1,309 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Implement the Retrieval for a Retrieval Augmented Generation (RAG) Use Case\n", - "\n", - "Now that you have all your context information stored in the SAP HANA Cloud Vector Store, you can start asking the LLM questions about the orchestration service of Generative AI Hub. This time, the model will not respond from its knowledge baseβ€”what it learned during trainingβ€”but instead, the retriever will search for relevant context information in your vector database and send the appropriate text chunk to the LLM to review before responding.\n", - "\n", - "πŸ‘‰ Change the `LLM_DEPLOYMENT_ID` in [variables.py](variables.py) to your deployment ID from exercise [01-explore-genai-hub](01-explore-genai-hub.md). For that go to **SAP AI Launchpad** application and navigate to **ML Operations** > **Deployments**.\n", - "\n", - "☝️ The `LLM_DEPLOYMENT_ID` is the deployment ID of the chat model you want to use e.g. **gpt-4o-mini**." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "# import init_env\n", - "# import variables\n", - "\n", - "#init_env.set_environment_variables()\n", - "\n", - "from gen_ai_hub.proxy.langchain.openai import ChatOpenAI\n", - "from gen_ai_hub.proxy.langchain.openai import OpenAIEmbeddings\n", - "from langchain.chains import RetrievalQA\n", - "from langchain_community.vectorstores.hanavector import HanaDB\n", - "from hdbcli import dbapi" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You are again connecting to our shared SAP HANA Cloud Vector Engine." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# # connect to HANA instance\n", - "# connection = init_env.connect_to_hana_db()\n", - "# connection.isconnected()" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "EMBEDDING_DEPLOYMENT_ID = \"d7e7cd43afe2b169\"" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "def get_hana_connection():\n", - " conn = dbapi.connect(\n", - " address='ec41b786-96de-467b-9ff5-db725945f89c.hna0.prod-us10.hanacloud.ondemand.com',\n", - " port='443',\n", - " user='DBADMIN',\n", - " password='9hEW4UK86Fdt',\n", - " encrypt=True\n", - " )\n", - " return conn" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com/oauth/token\n", - "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540\n", - "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=\n", - "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "llm-deployed\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"llm-deployed\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "# Reference which embedding model, DB connection and table to use\n", - "embeddings = OpenAIEmbeddings(deployment_id=EMBEDDING_DEPLOYMENT_ID)\n", - "db = HanaDB(\n", - " embedding=embeddings, connection=get_hana_connection(), table_name=\"EMBEDDINGS_CODEJAM_\"+\"Testing\"\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In this step you are defining which LLM to use during the retrieving process. You then also assign which database to retrieve information from. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "LLM_DEPLOYMENT_ID = \"d4e55e72ddccfd73\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#Need to fix with the name mentioned in deployement id\n", - "# Define which model to use\n", - "chat_llm = ChatOpenAI(deployment_id=LLM_DEPLOYMENT_ID)\n", - "\n", - "# Create a retriever instance of the vector store\n", - "retriever = db.as_retriever(search_kwargs={\"k\": 2})" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Instead of sending the query directly to the LLM, you will now create a `RetrievalQA` instance and pass both the LLM and the database to be used during the retrieval process. Once set up, you can send your query to the `Retriever`.\n", - "\n", - "πŸ‘‰ Try out different queries. Feel free to ask anything you'd like to know about the Models that are available in Generative AI Hub." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'query': 'What is data masking in the orchestration service?',\n", - " 'result': \"I don't know.\",\n", - " 'source_documents': [Document(metadata={'producer': 'Skia/PDF m131', 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', 'creationdate': '2025-02-02T21:04:28+00:00', 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation', 'moddate': '2025-02-02T21:04:28+00:00', 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf', 'total_pages': 14, 'page': 0, 'page_label': '1'}, page_content='/Examples/Orchestration Service\\nOrchestration Service\\nThis notebook demonstrates how to use the SDK to interact with the Orchestration\\nService, enabling the creation of AI-driven workflows by seamlessly integrating various\\nmodules, such as templating, large language models (LLMs), data masking and content\\nfiltering. By leveraging these modules, you can build complex, automated workflows that\\nenhance the capabilities of your AI solutions. For more details on configuring and using'),\n", - " Document(metadata={'producer': 'Skia/PDF m131', 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', 'creationdate': '2025-02-02T21:04:28+00:00', 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation', 'moddate': '2025-02-02T21:04:28+00:00', 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf', 'total_pages': 14, 'page': 13, 'page_label': '14'}, page_content='https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html 14/14')]}" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# Create the QA instance to query llm based on custom documents\n", - "qa = RetrievalQA.from_llm(llm=chat_llm, retriever=retriever, return_source_documents=True)\n", - "\n", - "# Send query\n", - "query = \"What is data masking in the orchestration service?\"\n", - "\n", - "answer = qa.invoke(query)\n", - "display(answer)" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'producer': 'Skia/PDF m131',\n", - " 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',\n", - " 'creationdate': '2025-02-02T21:04:28+00:00',\n", - " 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation',\n", - " 'moddate': '2025-02-02T21:04:28+00:00',\n", - " 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf',\n", - " 'total_pages': 14,\n", - " 'page': 0,\n", - " 'page_label': '1'}" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "/Examples/Orchestration Service\n", - "Orchestration Service\n", - "This notebook demonstrates how to use the SDK to interact with the Orchestration\n", - "Service, enabling the creation of AI-driven workflows by seamlessly integrating various\n", - "modules, such as templating, large language models (LLMs), data masking and content\n", - "filtering. By leveraging these modules, you can build complex, automated workflows that\n", - "enhance the capabilities of your AI solutions. For more details on configuring and using\n" - ] - }, - { - "data": { - "text/plain": [ - "{'producer': 'Skia/PDF m131',\n", - " 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',\n", - " 'creationdate': '2025-02-02T21:04:28+00:00',\n", - " 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation',\n", - " 'moddate': '2025-02-02T21:04:28+00:00',\n", - " 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf',\n", - " 'total_pages': 14,\n", - " 'page': 13,\n", - " 'page_label': '14'}" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html 14/14\n" - ] - } - ], - "source": [ - "for document in answer['source_documents']:\n", - " display(document.metadata) \n", - " print(document.page_content)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Go back to [05-store-embeddings-hana](05-store-embeddings-hana.ipynb) and try out different chunk sizes and/or different values for overlap. Store these chunks in a different table by adding a new variable to [variables.py](variables.py) and run this script again using the newly created table.\n", - "\n", - "[Next exercise](07-deploy-orchestration-service.md)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test/07-deploy-orchestration-service.md b/09_BusinessAIWeek/Test/07-deploy-orchestration-service.md deleted file mode 100644 index b10b3ed..0000000 --- a/09_BusinessAIWeek/Test/07-deploy-orchestration-service.md +++ /dev/null @@ -1,59 +0,0 @@ -# Deploy the orchestration service SAP AI Core - -To use orchestration service, you need to create a deployment [configuration](https://help.sap.com/docs/ai-launchpad/sap-ai-launchpad/configurations). Using this configuration, you can deploy the orchestration service. After successful deployment, you will receive a deployment URL, which you can use to query the model of your choice via the orchestration service. - -☝️ Some of you already created an orchestration deployment in the beginning. Go to the SAP AI Launchpad and find your deployment URL under `Deployments`. - -## Create a configuration to deploy the orchestration service on SAP AI Core - -πŸ‘‰ Open the `ML Operations` tab and click on `Configurations`. - -![Configurations](images/configuration_o.png) - -πŸ‘‰ **Create** a new configuration. - -πŸ‘‰ Enter a configuration name `orchestration-conf`, select the `orchestration` scenario, version and the executable `orchestration`. - -![Create configuration 1/4](images/configuration_o2.png) - -πŸ‘‰ Click **Next**, **Next**, **Review** and **Create**. - -## Deploy a proxy for a large language model on SAP AI Core - -πŸ‘‰ Click on `Create Deployment` to create a deployment for that configuration. - -This will not actually deploy all the models, but it will deploy a proxy that returns a URL for you to use to query the models via the orchestration service. - -![Create deployment 1/5](images/deployment_o.png) - -πŸ‘‰ Click **Review** and **Create**. - -The deployment status is going to change from `UNKNOWN` to `PENDING` and then to `RUNNING`. - -Once the deployment is running you will receive a URL to query the orchestration service. Wait a couple of minutes, then use the **refresh** icon on the page for the URL to appear. - -☝️ You will need the deployment URL in the next exercise! - -![Create deployment 4/5](images/deployments_4.png) - -Under `Deployments` you can see all the deployed models and their status. - -![Create deployment 2/5](images/deployment_o2.png) - -Using the `URL`, `client ID`, and `client secret` from the SAP AI Core service key, you can now query the model using any programming language or API platform. - -![Create deployment 5/5](images/deployments_5.png) - -## Summary - -By this point, you will have learned which models are available through the Generative AI Hub and how to deploy the orchestration service in SAP AI Launchpad. - -## Further reading - -* [SAP AI Launchpad - Help Portal (Documentation)](https://help.sap.com/docs/ai-launchpad/sap-ai-launchpad/what-is-sap-ai-launchpad) -* [SAP AI Core Terminology](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/terminology) -* [Available Models in the Generative AI Hub](https://me.sap.com/notes/3437766) - ---- - -[Next exercise](08-orchestration-service.ipynb) diff --git a/09_BusinessAIWeek/Test/08-orchestration-service.ipynb b/09_BusinessAIWeek/Test/08-orchestration-service.ipynb deleted file mode 100644 index 29652b5..0000000 --- a/09_BusinessAIWeek/Test/08-orchestration-service.ipynb +++ /dev/null @@ -1,375 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Use the orchestration service of Generative AI Hub\n", - "\n", - "The orchestration service of Generative AI Hub lets you use all the available models with the same codebase. You only deploy the orchestration service and then you can access all available models simply by changing the model name parameter. You can also use grounding, prompt templating, data masking and content filtering capabilities.\n", - "\n", - "Store the `orchestration deployment url` from the previous step in your `variables.py` file. This code is based on the [AI180 TechEd 2024 Jump-Start session](https://github.com/SAP-samples/teched2024-AI180/tree/e648921c46337b57f61ecc9a93251d4b838d7ad0/exercises/python).\n", - "\n", - "πŸ‘‰ Make sure you assign the deployment url of the orchestration service to `AICORE_ORCHESTRATION_DEPLOYMENT_URL` in [variables.py](variables.py)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# import init_env\n", - "# import variables\n", - "\n", - "# init_env.set_environment_variables()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Import the packages you want to use" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com/oauth/token\n", - "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540\n", - "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=\n", - "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "llm-deployed\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"llm-deployed\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.orchestration.models.llm import LLM\n", - "from gen_ai_hub.orchestration.models.message import SystemMessage, UserMessage\n", - "from gen_ai_hub.orchestration.models.template import Template, TemplateValue\n", - "from gen_ai_hub.orchestration.models.config import OrchestrationConfig\n", - "from gen_ai_hub.orchestration.service import OrchestrationService\n", - "from gen_ai_hub.orchestration.models.azure_content_filter import AzureContentFilter\n", - "from gen_ai_hub.orchestration.exceptions import OrchestrationError" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Assign the model you want to use\n", - "You can find more information regarding the available models here: https://me.sap.com/notes/3437766" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "AICORE_ORCHESTRATION_DEPLOYMENT_URL = \"https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2/inference/deployments/d56c73baa4a99356\"" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "# TODOchose the model you want to try e.g. gemini-1.5-flash, mistralai--mistral-large-instruct, \n", - "# ibm--granite-13b-chat meta--llama3.1-70b-instruct, amazon--titan-text-lite, anthropic--claude-3.5-sonnet, \n", - "# gpt-4o-mini and assign it to name\n", - "llm = LLM(\n", - " name=\"gpt-4o\",\n", - " version=\"latest\",\n", - " parameters={\"max_tokens\": 500, \"temperature\": 1},\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Create a prompt template\n", - "The parameter **user_query** in the code snippet below is going to hold the user query that you will add later on. The user query is the text to be translated by the model. The parameter **to_lang** can be any language you want to translate into. By default it is set to **English**." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [], - "source": [ - "template = Template(\n", - " messages=[\n", - " SystemMessage(\"You are a helpful translation assistant.\"),\n", - " UserMessage(\n", - " \"Translate the following text to {{?to_lang}}: {{?user_query}}\",\n", - " )\n", - " ],\n", - " defaults=[\n", - " TemplateValue(name=\"to_lang\", value=\"English\"),\n", - " ],\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Create an orchestration configuration \n", - "\n", - "Create an orchestration configuration by adding the llm you referenced and the prompt template you created previously." - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], - "source": [ - "config = OrchestrationConfig(\n", - " template=template,\n", - " llm=llm,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Add it the configuration to the OrchestrationService instance and send the prompt to the model." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Does this really work with all the models that are available?\n" - ] - } - ], - "source": [ - "\n", - "orchestration_service = OrchestrationService(\n", - " api_url=AICORE_ORCHESTRATION_DEPLOYMENT_URL,\n", - " config=config,\n", - ")\n", - "result = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"Geht das wirklich mit allen Modellen die verfΓΌgbar sind?\"\n", - " )\n", - " ]\n", - ")\n", - "print(result.orchestration_result.choices[0].message.content)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Add a content filter\n", - "\n", - "Create a content filter and add it as an input filter to the orchestration configuration. This is going to filter out harmful content from the input query and not send the request to the model. Whereas adding a filter to the output would let the request go through but then filter any harmful text created by the model. Depending on your use case it can make sense to have both input and output filters.\n", - "\n", - "πŸ‘‰ Try out different values for the content filters. You can chose values [0 = **Safe**, 2 = **Low**, 4 = **Medium**, 6 = **High**]. Where **Safe** is *content generally related to violence* and **High** is *severely harmful content*." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "content_filter = AzureContentFilter(\n", - " hate=0,\n", - " sexual=0,\n", - " self_harm=0,\n", - " violence=0,\n", - ")\n", - "\n", - "orchestration_service.config.input_filters = [content_filter]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Try out the content filter" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Content filtered due to safety violations. Please modify the prompt and try again.\n", - "You are a super talented developer!\n" - ] - } - ], - "source": [ - "\n", - "try:\n", - " result = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"Du bist ein ganz mieser Entwickler!\",\n", - " ),\n", - " ]\n", - " )\n", - " print(result.orchestration_result.choices[0].message.content)\n", - "except OrchestrationError as error:\n", - " print(error.message)\n", - "\n", - "\n", - "result = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"Du bist ein super talentierter Entwickler!\",\n", - " )\n", - " ]\n", - ")\n", - "print(result.orchestration_result.choices[0].message.content)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Now also add the content filter as an output filter" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Content filtered due to safety violations. Please modify the prompt and try again.\n" - ] - } - ], - "source": [ - "orchestration_service.config.output_filters = [content_filter]\n", - "\n", - "try:\n", - " result = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value='Ich wΓΌrde gerne wissen, wie ich gewaltvoll die Fensterscheibe in einem BΓΌrogebΓ€ude am Besten zerstΓΆren kann.',\n", - " )\n", - " ]\n", - " )\n", - " print(result.orchestration_result.choices[0].message.content)\n", - "except OrchestrationError as error:\n", - " print(error.message)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Optional exercise\n", - "\n", - "πŸ‘‰ Now that you know how the orchestration service works, try adding the [Data Masking](https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html#data-masking) capability. With Data Masking you can hide personal information like email, name or phone numbers before sending such sensitive data to an LLM.\n", - "\n", - "## More Info\n", - "\n", - "Here you can find more [info on the content filter](https://learn.microsoft.com/en-us/azure/ai-studio/concepts/content-filtering)\n", - "\n", - "[Next exercise](09-orchestration-service-grounding.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test/09-orchestration-service-grounding.ipynb b/09_BusinessAIWeek/Test/09-orchestration-service-grounding.ipynb deleted file mode 100644 index 3fad519..0000000 --- a/09_BusinessAIWeek/Test/09-orchestration-service-grounding.ipynb +++ /dev/null @@ -1,257 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Use the orchestration service of Generative AI Hub\n", - "\n", - "The orchestration service of Generative AI Hub lets you use all the available models with the same codebase. You only deploy the orchestration service and then you can access all available models simply by changing the model name parameter. You can also use grounding, prompt templating, data masking and content filtering capabilities.\n", - "\n", - "Store the `orchestration deployment url` from the previous step in your `variables.py` file. This code is based on the [AI180 TechEd 2024 Jump-Start session](https://github.com/SAP-samples/teched2024-AI180/tree/e648921c46337b57f61ecc9a93251d4b838d7ad0/exercises/python).\n", - "\n", - "πŸ‘‰ Make sure you assign the deployment url of the orchestration service to `AICORE_ORCHESTRATION_DEPLOYMENT_URL` in [variables.py](variables.py)." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com/oauth/token\n", - "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540\n", - "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=\n", - "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "llm-deployed\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"llm-deployed\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "# import init_env\n", - "# import variables\n", - "\n", - "# init_env.set_environment_variables()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Import the packages you want to use" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.orchestration.models.llm import LLM\n", - "from gen_ai_hub.orchestration.models.config import GroundingModule, OrchestrationConfig\n", - "from gen_ai_hub.orchestration.models.document_grounding import DocumentGrounding, DocumentGroundingFilter\n", - "from gen_ai_hub.orchestration.models.template import Template, TemplateValue\n", - "from gen_ai_hub.orchestration.models.message import SystemMessage, UserMessage\n", - "from gen_ai_hub.orchestration.service import OrchestrationService" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Assign a model and define a prompt template\n", - "**user_query** is again the user input. Whereas **grounding_response** is the context retrieved from the context information, in this case from sap.help.com." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "AICORE_ORCHESTRATION_DEPLOYMENT_URL = \"https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2/inference/deployments/d56c73baa4a99356\"" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO again you need to chose a model e.g. gemini-1.5-flash or gpt-4o-mini\n", - "llm = LLM(\n", - " name=\"gpt-4o\",\n", - " parameters={\n", - " 'temperature': 0.0,\n", - " }\n", - ")\n", - "template = Template(\n", - " messages=[\n", - " SystemMessage(\"You are a helpful translation assistant.\"),\n", - " UserMessage(\"\"\"Answer the request by providing relevant answers that fit to the request.\n", - " Request: {{ ?user_query }}\n", - " Context:{{ ?grounding_response }}\n", - " \"\"\"),\n", - " ]\n", - " )" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Create an orchestration configuration that specifies the grounding capability" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "# Set up Document Grounding\n", - "filters = [\n", - " DocumentGroundingFilter(id=\"SAPHelp\", data_repository_type=\"help.sap.com\")\n", - " ]\n", - "\n", - "grounding_config = GroundingModule(\n", - " type=\"document_grounding_service\",\n", - " config=DocumentGrounding(input_params=[\"user_query\"], output_param=\"grounding_response\", filters=filters)\n", - " )\n", - "\n", - "config = OrchestrationConfig(\n", - " template=template,\n", - " llm=llm,\n", - " grounding=grounding_config\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "orchestration_service = OrchestrationService(\n", - " api_url=AICORE_ORCHESTRATION_DEPLOYMENT_URL,\n", - " config=config\n", - ")\n", - "\n", - "response = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"What is Joule?\"\n", - " )\n", - " ]\n", - ")\n", - "\n", - "print(response.orchestration_result.choices[0].message.content)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Streaming\n", - "Long response times can be frustrating in chat applications, especially when a large amount of text is involved. To create a smoother, more engaging user experience, we can use streaming to display the response in real-time, character by character or chunk by chunk, as the model generates it. This avoids the awkward wait for a complete response and provides immediate feedback to the user." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "response = orchestration_service.stream(\n", - " config=config,\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"What is Joule?\"\n", - " )\n", - " ]\n", - ")\n", - "\n", - "for chunk in response:\n", - " print(chunk.orchestration_result.choices[0].delta.content)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[More Info on the content filter](https://learn.microsoft.com/en-us/azure/ai-studio/concepts/content-filtering)\n", - "\n", - "[Next exercise](10-chatbot-with-memory.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test/10-chatbot-with-memory.ipynb b/09_BusinessAIWeek/Test/10-chatbot-with-memory.ipynb deleted file mode 100644 index 5a370c1..0000000 --- a/09_BusinessAIWeek/Test/10-chatbot-with-memory.ipynb +++ /dev/null @@ -1,307 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Chatbot with Memory\n", - "\n", - "Let's add some history to the interaction and build a chatbot. Unlike many people think. LLMs are fixed in their state. They are trained until a certain cutoff date and do not know anything after that point unless you feed them current information. That is also why LLMs do not remember anything about you or the prompts you send to the model. If the model seems to remember you and what you said it is always because the application you are using (e.g. ChatPGT or the chat function in SAP AI Launchpad) is sending the chat history to the model to provide the conversation history to the model as context.\n", - "\n", - "Below you can find a simple implementation of a chatbot with memory.\n", - "\n", - "The code in this exercise is based on the [help documentation](https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html) of the Generative AI Hub Python SDK." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "# import init_env\n", - "# import variables\n", - "from typing import List\n", - "\n", - "# init_env.set_environment_variables()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Import the packages you want to use" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.orchestration.models.llm import LLM\n", - "from gen_ai_hub.orchestration.models.message import Message, SystemMessage, UserMessage\n", - "from gen_ai_hub.orchestration.models.template import Template, TemplateValue\n", - "from gen_ai_hub.orchestration.models.config import OrchestrationConfig\n", - "from gen_ai_hub.orchestration.service import OrchestrationService" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com/oauth/token\n", - "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540\n", - "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=\n", - "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "llm-deployed\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"llm-deployed\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "AICORE_ORCHESTRATION_DEPLOYMENT_URL = \"https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2/inference/deployments/d56c73baa4a99356\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Create the chatbot class" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "class ChatBot:\n", - " def __init__(self, orchestration_service: OrchestrationService):\n", - " self.service = orchestration_service\n", - " self.config = OrchestrationConfig(\n", - " template=Template(\n", - " messages=[\n", - " SystemMessage(\"You are a helpful chatbot assistant.\"),\n", - " UserMessage(\"{{?user_query}}\"),\n", - " ],\n", - " ),\n", - " # TODO add a model name here, e.g. gemini-1.5-flash\n", - " llm=LLM(name=\"gpt-4o\"),\n", - " )\n", - " self.history: List[Message] = []\n", - "\n", - " def chat(self, user_input):\n", - " response = self.service.run(\n", - " config=self.config,\n", - " template_values=[\n", - " TemplateValue(name=\"user_query\", value=user_input),\n", - " ],\n", - " history=self.history,\n", - " )\n", - "\n", - " message = response.orchestration_result.choices[0].message\n", - "\n", - " self.history = response.module_results.templating\n", - " self.history.append(message)\n", - "\n", - " return message.content\n", - " \n", - " def reset(self):\n", - " self.history = []\n", - "\n", - "service = OrchestrationService(api_url=AICORE_ORCHESTRATION_DEPLOYMENT_URL)\n", - "bot = ChatBot(orchestration_service=service)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"Hello! Yes, I'm here. How can I assist you today?\"" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.chat(\"Hello, are you there?\")" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"While I don't have personal preferences or feelings, I think CodeJams are great events! They offer excellent opportunities for programmers to challenge themselves, learn new skills, and showcase their problem-solving abilities. If you have any questions about participating in a CodeJam, I'd be happy to help!\"" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.chat(\"Do you like CodeJams?\")" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"I don't have the ability to remember past interactions or conversations. Each session is independent, so I can only reference what we've talked about within this current session. If there's something you'd like to discuss again or if you have any new questions, feel free to let me know!\"" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.chat(\"Can you remember what we first talked about?\")" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[Message(role=, content='You are a helpful chatbot assistant.'),\n", - " Message(role=, content='Hello, are you there?'),\n", - " Message(role=, content=\"Hello! Yes, I'm here. How can I assist you today?\"),\n", - " Message(role=, content='You are a helpful chatbot assistant.'),\n", - " Message(role=, content='Do you like CodeJams?'),\n", - " Message(role=, content=\"While I don't have personal preferences or feelings, I think CodeJams are great events! They offer excellent opportunities for programmers to challenge themselves, learn new skills, and showcase their problem-solving abilities. If you have any questions about participating in a CodeJam, I'd be happy to help!\"),\n", - " Message(role=, content='You are a helpful chatbot assistant.'),\n", - " Message(role=, content='Can you remember what we first talked about?'),\n", - " Message(role=, content=\"I don't have the ability to remember past interactions or conversations. Each session is independent, so I can only reference what we've talked about within this current session. If there's something you'd like to discuss again or if you have any new questions, feel free to let me know!\")]" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.history" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And to prove to you that the model does indeed not remember you, let's delete the history and try again :)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"I'm afraid I can't remember past interactions or conversations, as I don't have the ability to store personal data or memories. Nonetheless, I'm here to assist you with any questions or information you might need. How can I help you today?\"" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.reset()\n", - "bot.chat(\"Can you remember what we first talked about?\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[Next exercise](11-your-chatbot.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test/11-your-chatbot.ipynb b/09_BusinessAIWeek/Test/11-your-chatbot.ipynb deleted file mode 100644 index 1362f0e..0000000 --- a/09_BusinessAIWeek/Test/11-your-chatbot.ipynb +++ /dev/null @@ -1,47 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Now it's your turn!\n", - "Build your own chatbot similar to [10-chatbot-with-memory.ipynb](10-chatbot-with-memory.ipynb) with what you have learned! Combine what you have learned in [08-orchestration-service.ipynb](08-orchestration-service.ipynb) and/or [09-orchestration-service-grounding.ipynb](09-orchestration-service-grounding.ipynb) and build a chatbot that can answer SAP specific questions or filter out personal data or hate speech." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import init_env\n", - "import variables\n", - "\n", - "init_env.set_environment_variables()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO Your code can start here!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well done! You have completed the CodeJam! If you still have time check out the optional [AI Agents exercise](12-ai-agents.ipynb)!" - ] - } - ], - "metadata": { - "language_info": { - "name": "python" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test/12-ai-agents.ipynb b/09_BusinessAIWeek/Test/12-ai-agents.ipynb deleted file mode 100644 index d2c7f1f..0000000 --- a/09_BusinessAIWeek/Test/12-ai-agents.ipynb +++ /dev/null @@ -1,378 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# AI Agents with Generative AI Hub\n", - "\n", - "Everyone is talking about AI Agents and how they are going to make LLMs more powerful. AI Agents are basically systems that have a set of tools that they can use and the LLM is the agents brain to decide which tool to use when. Some systems are more agentic than others, depending on the amount of autonomy and decision-making power they possess. \n", - "\n", - "If you want to know more about AI Agents and how to build one from scratch, check out this [Devtoberfest session](https://community.sap.com/t5/devtoberfest/getting-started-with-agents-using-sap-generative-ai-hub/ev-p/13865119).\n", - "\n", - "πŸ‘‰ Before you start you will need to add the constant `LLM_DEPLOYMENT_ID` to [variables.py](variables.py). You can use the `gpt-4o-mini` model again that we deployed earlier. You find the deployment ID in SAP AI Launchpad." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com/oauth/token\n", - "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540\n", - "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=\n", - "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "llm-deployed\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"llm-deployed\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Import the packages you want to use" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_community.tools import WikipediaQueryRun\n", - "from langchain_community.utilities import WikipediaAPIWrapper\n", - "\n", - "from langchain import hub\n", - "from langchain.agents import AgentExecutor, create_react_agent\n", - "\n", - "from gen_ai_hub.proxy.langchain.openai import ChatOpenAI\n", - "from gen_ai_hub.proxy.langchain.openai import OpenAIEmbeddings\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We will give the agent different tools to use. The first tool will be [access to Wikipedia](https://python.langchain.com/docs/integrations/tools/wikipedia/). This way instead of answering from it's training data, the model will check on Wikipedia articles first." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Page: Bengaluru\n", - "Summary: Bengaluru, also known as Bangalore (its official name until 1 November 2014), is the capital and largest city of the southern Indian state of Karnataka. As per the 2011 census, the city had a population of 8.4 million, making it the third most populous city in India and the most populous in South India. The Bengaluru metropolitan area had a population of around 8.5 million, making it the fifth most populous urban agglomeration in the country. It is located near the center of the Deccan Plateau, at a height of 900 m (3,000 ft) above sea level. The city is known as India's \"Garden City\", due to its parks and greenery.\n", - "Archaeological artifacts indicate that the human settlement in the region happened as early as 4000 BCE. The first mention of the name \"Bengalooru\" is from an old Kannada stone inscription from 890 CE found at the Nageshwara Temple. From 350 CE, it was ruled by the Western Ganga dynasty, and in the early eleventh century, the city became part of the Chola empire. In the late Middle Ages, the region was part of the Hoysala Kingdom and then the Vijayanagara Empire. In 1537 CE, Kempe Gowda I, a feudal ruler under the Vijayanagara Empire, established a mud fort which is considered the foundation of the modern city of Bengaluru and its oldest areas, or petes, which still exist. After the fall of the Vijayanagara Empire, Kempe Gowda declared independence, and the city was expanded by his successors. In 1638 CE, an Adil Shahi army defeated Kempe Gowda III, and the city became a jagir (feudal estate) of Shahaji Bhonsle. The Mughals later captured Bengaluru and sold it to Maharaja Chikka Devaraja Wodeyar of the Kingdom of Mysore. After the death of Krishnaraja Wodeyar II in 1759 CE, Hyder Ali seized control of the kingdom of Mysore and with it, the administration of Bengaluru, which passed subsequently to his son, Tipu Sultan.\n", - "The city was captured by the British East India Company during the Anglo-Mysore Wars, and became part of the Princely State of Mysore. The administrative control of the city was returned to Krishnaraja Wadiyar III, then Maharaja of Mysore, and the old city developed under the dominions of the Mysore kingdom. In 1809 CE, the British shifted their military garrison to the city and established the cantonment, outside the old city. In the late 19th century CE, the city was essentially composed of two distinct urban settlements, the old pete and the new cantonment. Following India's independence in 1947, Bengaluru became the capital of Mysore State, and remained the capital when the state was enlarged and unified in 1956 and subsequently renamed as Karnataka in 1973. The two urban settlements which had developed as independent entities, merged under a single urban administration in 1949.\n", - "Bengaluru is one of the fastest-growing metropolises in India. As of 2023, the metropolitan area had an estimated GDP of $359.9 billion, and is one of the most productive metro areas of India. The city is a major center for information technology (IT), and is consistently ranked amongst the world's fastest growing technology hubs. It is widely regarded as the \"Silicon Valley of India\", as the largest hub and exporter of IT services in the country. Manufacturing is a major contributor to the economy and the city is also home to several state-owned manufacturing companies. Bengaluru also hosts several institutes of national importance in higher education.\n", - "\n", - "Page: Bangalore Days\n", - "Summary: Bangalore Days is a 2014 Indian Malayalam-language coming of age romantic comedy-drama film written and directed by Anjali Menon, and produced by Anwar Rasheed and Sophia Paul under the banner Anwar Rasheed Entertainments and Weekend Blockbusters. The film features an ensemble cast of Nivin Pauly, Dulquer Salmaan, Fahadh Faasil, Nazriya Nazim, Parvathy Thiruvothu, Isha Talwar and Paris Laxmi..\n", - "Bangalore Days revolves around the life of three cousins from Kerala who move to Bangalore, continuing Anjali Menon's trend of \n" - ] - } - ], - "source": [ - "wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())\n", - "\n", - "# Let's check if it works\n", - "print(wikipedia.run(\"Bangalore\"))\n", - "\n", - "# Assign wikipedia to the set of tools\n", - "tools = [wikipedia]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "c:\\Users\\C5385222\\AppData\\Local\\anaconda3\\Lib\\site-packages\\langsmith\\client.py:253: LangSmithMissingAPIKeyWarning: API key must be provided when using hosted LangSmith API\n", - " warnings.warn(\n" - ] - }, - { - "data": { - "text/plain": [ - "PromptTemplate(input_variables=['agent_scratchpad', 'input', 'tool_names', 'tools'], input_types={}, partial_variables={}, metadata={'lc_hub_owner': 'hwchase17', 'lc_hub_repo': 'react', 'lc_hub_commit_hash': 'd15fe3c426f1c4b3f37c9198853e4a86e20c425ca7f4752ec0c9b0e97ca7ea4d'}, template='Answer the following questions as best you can. You have access to the following tools:\\n\\n{tools}\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [{tool_names}]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: {input}\\nThought:{agent_scratchpad}')" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# For agents the initial prompt including all the instructions is very important. \n", - "# LangChain already has a good set of prompts that we can reuse here.\n", - "prompt = hub.pull(\"hwchase17/react\")\n", - "prompt" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mTo provide a comprehensive answer about Bangalore, I will look for information about the city, its history, culture, and significance. \n", - "Action: wikipedia\n", - "Action Input: Bangalore\u001b[0m\u001b[36;1m\u001b[1;3mPage: Bengaluru\n", - "Summary: Bengaluru, also known as Bangalore (its official name until 1 November 2014), is the capital and largest city of the southern Indian state of Karnataka. As per the 2011 census, the city had a population of 8.4 million, making it the third most populous city in India and the most populous in South India. The Bengaluru metropolitan area had a population of around 8.5 million, making it the fifth most populous urban agglomeration in the country. It is located near the center of the Deccan Plateau, at a height of 900 m (3,000 ft) above sea level. The city is known as India's \"Garden City\", due to its parks and greenery.\n", - "Archaeological artifacts indicate that the human settlement in the region happened as early as 4000 BCE. The first mention of the name \"Bengalooru\" is from an old Kannada stone inscription from 890 CE found at the Nageshwara Temple. From 350 CE, it was ruled by the Western Ganga dynasty, and in the early eleventh century, the city became part of the Chola empire. In the late Middle Ages, the region was part of the Hoysala Kingdom and then the Vijayanagara Empire. In 1537 CE, Kempe Gowda I, a feudal ruler under the Vijayanagara Empire, established a mud fort which is considered the foundation of the modern city of Bengaluru and its oldest areas, or petes, which still exist. After the fall of the Vijayanagara Empire, Kempe Gowda declared independence, and the city was expanded by his successors. In 1638 CE, an Adil Shahi army defeated Kempe Gowda III, and the city became a jagir (feudal estate) of Shahaji Bhonsle. The Mughals later captured Bengaluru and sold it to Maharaja Chikka Devaraja Wodeyar of the Kingdom of Mysore. After the death of Krishnaraja Wodeyar II in 1759 CE, Hyder Ali seized control of the kingdom of Mysore and with it, the administration of Bengaluru, which passed subsequently to his son, Tipu Sultan.\n", - "The city was captured by the British East India Company during the Anglo-Mysore Wars, and became part of the Princely State of Mysore. The administrative control of the city was returned to Krishnaraja Wadiyar III, then Maharaja of Mysore, and the old city developed under the dominions of the Mysore kingdom. In 1809 CE, the British shifted their military garrison to the city and established the cantonment, outside the old city. In the late 19th century CE, the city was essentially composed of two distinct urban settlements, the old pete and the new cantonment. Following India's independence in 1947, Bengaluru became the capital of Mysore State, and remained the capital when the state was enlarged and unified in 1956 and subsequently renamed as Karnataka in 1973. The two urban settlements which had developed as independent entities, merged under a single urban administration in 1949.\n", - "Bengaluru is one of the fastest-growing metropolises in India. As of 2023, the metropolitan area had an estimated GDP of $359.9 billion, and is one of the most productive metro areas of India. The city is a major center for information technology (IT), and is consistently ranked amongst the world's fastest growing technology hubs. It is widely regarded as the \"Silicon Valley of India\", as the largest hub and exporter of IT services in the country. Manufacturing is a major contributor to the economy and the city is also home to several state-owned manufacturing companies. Bengaluru also hosts several institutes of national importance in higher education.\n", - "\n", - "Page: Bangalore Days\n", - "Summary: Bangalore Days is a 2014 Indian Malayalam-language coming of age romantic comedy-drama film written and directed by Anjali Menon, and produced by Anwar Rasheed and Sophia Paul under the banner Anwar Rasheed Entertainments and Weekend Blockbusters. The film features an ensemble cast of Nivin Pauly, Dulquer Salmaan, Fahadh Faasil, Nazriya Nazim, Parvathy Thiruvothu, Isha Talwar and Paris Laxmi..\n", - "Bangalore Days revolves around the life of three cousins from Kerala who move to Bangalore, continuing Anjali Menon's trend of \u001b[0m\u001b[32;1m\u001b[1;3mI have gathered sufficient information about Bangalore, also known as Bengaluru, including its history, significance, and modern-day status as a major city in India.\n", - "\n", - "Final Answer: Bangalore, officially known as Bengaluru, is the capital of the southern Indian state of Karnataka. Known as India's \"Garden City\" for its greenery, it has a population of about 8.4 million (2011 census), making it the third most populous city in India. With a history dating back to 4000 BCE, it was founded as a modern city in 1537 by Kempe Gowda I. The city has grown into a major center for information technology and is considered the \"Silicon Valley of India.\" As of 2023, the metropolitan area has a GDP of approximately $359.9 billion and is one of India's most productive urban areas.\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "{'input': 'Tell me about Bangalore!',\n", - " 'output': 'Bangalore, officially known as Bengaluru, is the capital of the southern Indian state of Karnataka. Known as India\\'s \"Garden City\" for its greenery, it has a population of about 8.4 million (2011 census), making it the third most populous city in India. With a history dating back to 4000 BCE, it was founded as a modern city in 1537 by Kempe Gowda I. The city has grown into a major center for information technology and is considered the \"Silicon Valley of India.\" As of 2023, the metropolitan area has a GDP of approximately $359.9 billion and is one of India\\'s most productive urban areas.'}" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Create a chat model instance which will act as the brain of the agent\n", - "llm = ChatOpenAI(deployment_id=\"de162ae3c70b743b\")\n", - "\n", - "# Create an agent with the llm, tools and prompt\n", - "agent = create_react_agent(llm, tools, prompt)\n", - "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n", - "\n", - "# Let's ask try the agent and ask about Bangalore\n", - "agent_executor.invoke({\"input\": \"Tell me about Bangalore!\"})" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Give the agent access to our HANA vector store\n", - "Now the agent can use Wikipedia but wouldn't it be nice if the agent could pick between resources and decide when to use what?" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "def get_hana_connection():\n", - " conn = dbapi.connect(\n", - " address='ec41b786-96de-467b-9ff5-db725945f89c.hna0.prod-us10.hanacloud.ondemand.com',\n", - " port='443',\n", - " user='DBADMIN',\n", - " password='9hEW4UK86Fdt',\n", - " encrypt=True\n", - " )\n", - " return conn" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_community.vectorstores.hanavector import HanaDB\n", - "from langchain.tools.retriever import create_retriever_tool\n", - "from hdbcli import dbapi\n", - "# # connect to HANA instance\n", - "# connection = init_env.connect_to_hana_db()\n", - "# connection.isconnected()\n", - "\n", - "# Reference which embedding model, DB connection and table to use\n", - "embeddings = OpenAIEmbeddings(deployment_id=\"dc76007e42a9e390\")\n", - "db = HanaDB(\n", - " embedding=embeddings, connection=get_hana_connection(), table_name=\"EMBEDDINGS_CODEJAM_\"+\"Testing\"\n", - ")\n", - "\n", - "# Create a retriever instance of the vector store\n", - "retriever = db.as_retriever(search_kwargs={\"k\": 2})\n", - "\n", - "# We need to add a description to the retriever so that the llm knows when to use this tool.\n", - "retriever_tool = create_retriever_tool(\n", - " retriever,\n", - " \"SAP orchestration service docu\",\n", - " \"Search for information SAP's orchestration service. For any questions about generative AI hub and sap's orchestration service, you must use this tool!\",\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Ask the Agent\n", - "\n", - "You can ask the agent anything in general and it will try to find an entry from Wikipedia, unless you ask about SAP's orchestration service. Then it will get the information from SAP HANA vector store, where it holds the documentation, instead of Wikipedia." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mData masking is a technique used to protect sensitive information by replacing it with fictitious data. This ensures that sensitive data, such as personally identifiable information (PII), financial information, and other confidential data, is shielded from unauthorized access while still being usable for testing and analysis purposes. To provide a more detailed understanding, I will look up more information on Wikipedia. \n", - "\n", - "Action: wikipedia \n", - "Action Input: \"data masking\" \u001b[0m" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "c:\\Users\\C5385222\\AppData\\Local\\anaconda3\\Lib\\site-packages\\wikipedia\\wikipedia.py:389: GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system (\"lxml\"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.\n", - "\n", - "The code that caused this warning is on line 389 of the file c:\\Users\\C5385222\\AppData\\Local\\anaconda3\\Lib\\site-packages\\wikipedia\\wikipedia.py. To get rid of this warning, pass the additional argument 'features=\"lxml\"' to the BeautifulSoup constructor.\n", - "\n", - " lis = BeautifulSoup(html).find_all('li')\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[36;1m\u001b[1;3mPage: Data masking\n", - "Summary: Data masking or data obfuscation is the process of modifying sensitive data in such a way that it is of no or little value to unauthorized intruders while still being usable by software or authorized personnel. Data masking can also be referred as anonymization, or tokenization, depending on different context.\n", - "The main reason to mask data is to protect information that is classified as personally identifiable information, or mission critical data. However, the data must remain usable for the purposes of undertaking valid test cycles. It must also look real and appear consistent. It is more common to have masking applied to data that is represented outside of a corporate production system. In other words, where data is needed for the purpose of application development, building program extensions and conducting various test cycles. It is common practice in enterprise computing to take data from the production systems to fill the data component, required for these non-production environments. However, this practice is not always restricted to non-production environments. In some organizations, data that appears on terminal screens to call center operators may have masking dynamically applied based on user security permissions (e.g. preventing call center operators from viewing credit card numbers in billing systems).\n", - "The primary concern from a corporate governance perspective is that personnel conducting work in these non-production environments are not always security cleared to operate with the information contained in the production data. This practice represents a security hole where data can be copied by unauthorized personnel, and security measures associated with standard production level controls can be easily bypassed. This represents an access point for a data security breach.\n", - "\n", - "\n", - "\n", - "Page: Data-centric security\n", - "Summary: Data-centric security is an approach to security that emphasizes the dependability of the data itself rather than the security of networks, servers, or applications. Data-centric security is evolving rapidly as enterprises increasingly rely on digital information to run their business and big data projects become mainstream.\n", - "\n", - "It involves the separation of data and digital rights management that assign encrypted files to pre-defined access control lists, ensuring access rights to critical and confidential data are aligned with documented business needs and job requirements that are attached to user identities.\n", - "Data-centric security also allows organizations to overcome the disconnect between IT security technology and the objectives of business strategy by relating security services directly to the data they implicitly protect; a relationship that is often obscured by the presentation of security as an end in itself.\n", - "\n", - "\u001b[0m\u001b[32;1m\u001b[1;3mI now know the final answer. \n", - "Final Answer: Data masking, or data obfuscation, is the process of modifying sensitive data to protect it from unauthorized access while still allowing it to be usable for authorized personnel or software. It replaces sensitive information with fictitious data, ensuring that personally identifiable information (PII) and other confidential data are shielded from intruders. Data masking is crucial in environments such as testing and application development, where realistic-looking data is necessary but direct access to actual sensitive data needs to be restricted.\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "{'input': 'What is data masking?',\n", - " 'output': 'Data masking, or data obfuscation, is the process of modifying sensitive data to protect it from unauthorized access while still allowing it to be usable for authorized personnel or software. It replaces sensitive information with fictitious data, ensuring that personally identifiable information (PII) and other confidential data are shielded from intruders. Data masking is crucial in environments such as testing and application development, where realistic-looking data is necessary but direct access to actual sensitive data needs to be restricted.'}" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Now let's add the retriever as a tool\n", - "tools = [wikipedia, retriever_tool]\n", - "\n", - "# And create the agent again with the two tools, wikipedia and the HANA vector store (retriever)\n", - "agent = create_react_agent(llm, tools, prompt)\n", - "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n", - "\n", - "# First ask: What is Data Masking?\n", - "# Then ask: What is Data Masking in SAP's Generative AI Hub?\n", - "# Pay attention to the response! Do you see what is happening now?\n", - "agent_executor.invoke({\"input\": \"What is data masking?\"})" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test/creds.json b/09_BusinessAIWeek/Test/creds.json deleted file mode 100644 index 3cb88af..0000000 --- a/09_BusinessAIWeek/Test/creds.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "serviceurls": { - "AI_API_URL": "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com" - }, - "appname": "adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540", - "clientid": "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540", - "clientsecret": "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=", - "identityzone": "anuragv2-39wjy902", - "identityzoneid": "d81dc917-bdb7-41c4-ab2a-cce93f2cf866", - "url": "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com", - "AI-Resource-Group": "llm-deployed" - } \ No newline at end of file diff --git a/09_BusinessAIWeek/Test/documents/Orchestration_Service_Generative_AI_hub_SDK.pdf b/09_BusinessAIWeek/Test/documents/Orchestration_Service_Generative_AI_hub_SDK.pdf deleted file mode 100644 index 979b277..0000000 Binary files a/09_BusinessAIWeek/Test/documents/Orchestration_Service_Generative_AI_hub_SDK.pdf and /dev/null differ diff --git a/09_BusinessAIWeek/Test/documents/ai-foundation-architecture.png b/09_BusinessAIWeek/Test/documents/ai-foundation-architecture.png deleted file mode 100644 index 644a62a..0000000 Binary files a/09_BusinessAIWeek/Test/documents/ai-foundation-architecture.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/documents/bananabread.png b/09_BusinessAIWeek/Test/documents/bananabread.png deleted file mode 100644 index 6abe5ca..0000000 Binary files a/09_BusinessAIWeek/Test/documents/bananabread.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/BTP_cockpit.png b/09_BusinessAIWeek/Test/images/BTP_cockpit.png deleted file mode 100644 index df1b18b..0000000 Binary files a/09_BusinessAIWeek/Test/images/BTP_cockpit.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/BTP_cockpit_BAS.png b/09_BusinessAIWeek/Test/images/BTP_cockpit_BAS.png deleted file mode 100644 index a80f45b..0000000 Binary files a/09_BusinessAIWeek/Test/images/BTP_cockpit_BAS.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.18.20.png b/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.18.20.png deleted file mode 100644 index 61151c6..0000000 Binary files a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.18.20.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.18.56.png b/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.18.56.png deleted file mode 100644 index 0b39967..0000000 Binary files a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.18.56.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.19.21.png b/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.19.21.png deleted file mode 100644 index 00b2439..0000000 Binary files a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.19.21.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.19.51.png b/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.19.51.png deleted file mode 100644 index 340dbfa..0000000 Binary files a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.19.51.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.20.48.png b/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.20.48.png deleted file mode 100644 index cba0786..0000000 Binary files a/09_BusinessAIWeek/Test/images/Screenshot 2025-01-29 at 16.20.48.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/bas.png b/09_BusinessAIWeek/Test/images/bas.png deleted file mode 100644 index 5ccc29a..0000000 Binary files a/09_BusinessAIWeek/Test/images/bas.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/btp.png b/09_BusinessAIWeek/Test/images/btp.png deleted file mode 100644 index 96dde7b..0000000 Binary files a/09_BusinessAIWeek/Test/images/btp.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/chat.png b/09_BusinessAIWeek/Test/images/chat.png deleted file mode 100644 index 823cc64..0000000 Binary files a/09_BusinessAIWeek/Test/images/chat.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/chat1.png b/09_BusinessAIWeek/Test/images/chat1.png deleted file mode 100644 index 1c7c383..0000000 Binary files a/09_BusinessAIWeek/Test/images/chat1.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/chat2.png b/09_BusinessAIWeek/Test/images/chat2.png deleted file mode 100644 index 4a00c89..0000000 Binary files a/09_BusinessAIWeek/Test/images/chat2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/chat3.png b/09_BusinessAIWeek/Test/images/chat3.png deleted file mode 100644 index b78f3a8..0000000 Binary files a/09_BusinessAIWeek/Test/images/chat3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/chat_2.png b/09_BusinessAIWeek/Test/images/chat_2.png deleted file mode 100644 index fde1098..0000000 Binary files a/09_BusinessAIWeek/Test/images/chat_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/clone_git.png b/09_BusinessAIWeek/Test/images/clone_git.png deleted file mode 100644 index 05d861e..0000000 Binary files a/09_BusinessAIWeek/Test/images/clone_git.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/clone_git_2.png b/09_BusinessAIWeek/Test/images/clone_git_2.png deleted file mode 100644 index 3439c4e..0000000 Binary files a/09_BusinessAIWeek/Test/images/clone_git_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/configuration_o.png b/09_BusinessAIWeek/Test/images/configuration_o.png deleted file mode 100644 index 5486bf1..0000000 Binary files a/09_BusinessAIWeek/Test/images/configuration_o.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/configuration_o2.png b/09_BusinessAIWeek/Test/images/configuration_o2.png deleted file mode 100644 index 7cf44a8..0000000 Binary files a/09_BusinessAIWeek/Test/images/configuration_o2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/configurations.png b/09_BusinessAIWeek/Test/images/configurations.png deleted file mode 100644 index a50e4cd..0000000 Binary files a/09_BusinessAIWeek/Test/images/configurations.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/configurations_2.png b/09_BusinessAIWeek/Test/images/configurations_2.png deleted file mode 100644 index 7e753ce..0000000 Binary files a/09_BusinessAIWeek/Test/images/configurations_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/configurations_3.png b/09_BusinessAIWeek/Test/images/configurations_3.png deleted file mode 100644 index 7e9b7e0..0000000 Binary files a/09_BusinessAIWeek/Test/images/configurations_3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/configurations_4.png b/09_BusinessAIWeek/Test/images/configurations_4.png deleted file mode 100644 index dd60a44..0000000 Binary files a/09_BusinessAIWeek/Test/images/configurations_4.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/configurations_5.png b/09_BusinessAIWeek/Test/images/configurations_5.png deleted file mode 100644 index 7628205..0000000 Binary files a/09_BusinessAIWeek/Test/images/configurations_5.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/create_dev_space.png b/09_BusinessAIWeek/Test/images/create_dev_space.png deleted file mode 100644 index 39429ca..0000000 Binary files a/09_BusinessAIWeek/Test/images/create_dev_space.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/deployment_o.png b/09_BusinessAIWeek/Test/images/deployment_o.png deleted file mode 100644 index 772d7e7..0000000 Binary files a/09_BusinessAIWeek/Test/images/deployment_o.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/deployment_o2.png b/09_BusinessAIWeek/Test/images/deployment_o2.png deleted file mode 100644 index 6c5ee69..0000000 Binary files a/09_BusinessAIWeek/Test/images/deployment_o2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/deployment_o3.png b/09_BusinessAIWeek/Test/images/deployment_o3.png deleted file mode 100644 index f34eadd..0000000 Binary files a/09_BusinessAIWeek/Test/images/deployment_o3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/deployments.png b/09_BusinessAIWeek/Test/images/deployments.png deleted file mode 100644 index c207541..0000000 Binary files a/09_BusinessAIWeek/Test/images/deployments.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/deployments_2.png b/09_BusinessAIWeek/Test/images/deployments_2.png deleted file mode 100644 index 7f9e2c4..0000000 Binary files a/09_BusinessAIWeek/Test/images/deployments_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/deployments_3.png b/09_BusinessAIWeek/Test/images/deployments_3.png deleted file mode 100644 index c65f52c..0000000 Binary files a/09_BusinessAIWeek/Test/images/deployments_3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/deployments_4.png b/09_BusinessAIWeek/Test/images/deployments_4.png deleted file mode 100644 index b8a2007..0000000 Binary files a/09_BusinessAIWeek/Test/images/deployments_4.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/deployments_5.png b/09_BusinessAIWeek/Test/images/deployments_5.png deleted file mode 100644 index 1aa2638..0000000 Binary files a/09_BusinessAIWeek/Test/images/deployments_5.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/dev_running.png b/09_BusinessAIWeek/Test/images/dev_running.png deleted file mode 100644 index e07f913..0000000 Binary files a/09_BusinessAIWeek/Test/images/dev_running.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/dev_starting.png b/09_BusinessAIWeek/Test/images/dev_starting.png deleted file mode 100644 index 508e58a..0000000 Binary files a/09_BusinessAIWeek/Test/images/dev_starting.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/enable_orchestration.png b/09_BusinessAIWeek/Test/images/enable_orchestration.png deleted file mode 100644 index 973939d..0000000 Binary files a/09_BusinessAIWeek/Test/images/enable_orchestration.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/extensions.png b/09_BusinessAIWeek/Test/images/extensions.png deleted file mode 100644 index 2169866..0000000 Binary files a/09_BusinessAIWeek/Test/images/extensions.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/model-library-2.png b/09_BusinessAIWeek/Test/images/model-library-2.png deleted file mode 100644 index 1996dd2..0000000 Binary files a/09_BusinessAIWeek/Test/images/model-library-2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/model-library-3.png b/09_BusinessAIWeek/Test/images/model-library-3.png deleted file mode 100644 index b2cbe92..0000000 Binary files a/09_BusinessAIWeek/Test/images/model-library-3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/model-library.png b/09_BusinessAIWeek/Test/images/model-library.png deleted file mode 100644 index 39d4c64..0000000 Binary files a/09_BusinessAIWeek/Test/images/model-library.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/open_workspace.png b/09_BusinessAIWeek/Test/images/open_workspace.png deleted file mode 100644 index 26cd35e..0000000 Binary files a/09_BusinessAIWeek/Test/images/open_workspace.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/prompt_editor.png b/09_BusinessAIWeek/Test/images/prompt_editor.png deleted file mode 100644 index 0b292a1..0000000 Binary files a/09_BusinessAIWeek/Test/images/prompt_editor.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/resource_group.png b/09_BusinessAIWeek/Test/images/resource_group.png deleted file mode 100644 index 792f3c6..0000000 Binary files a/09_BusinessAIWeek/Test/images/resource_group.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/resource_group_2.png b/09_BusinessAIWeek/Test/images/resource_group_2.png deleted file mode 100644 index 0b4dab6..0000000 Binary files a/09_BusinessAIWeek/Test/images/resource_group_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/scenarios.png b/09_BusinessAIWeek/Test/images/scenarios.png deleted file mode 100644 index c51e25c..0000000 Binary files a/09_BusinessAIWeek/Test/images/scenarios.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/scenarios_2.png b/09_BusinessAIWeek/Test/images/scenarios_2.png deleted file mode 100644 index 440b498..0000000 Binary files a/09_BusinessAIWeek/Test/images/scenarios_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/scenarios_3.png b/09_BusinessAIWeek/Test/images/scenarios_3.png deleted file mode 100644 index 226575a..0000000 Binary files a/09_BusinessAIWeek/Test/images/scenarios_3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/select_embedding_table.png b/09_BusinessAIWeek/Test/images/select_embedding_table.png deleted file mode 100644 index f512103..0000000 Binary files a/09_BusinessAIWeek/Test/images/select_embedding_table.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/select_kernel.png b/09_BusinessAIWeek/Test/images/select_kernel.png deleted file mode 100644 index 21fe3f8..0000000 Binary files a/09_BusinessAIWeek/Test/images/select_kernel.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/service-key.png b/09_BusinessAIWeek/Test/images/service-key.png deleted file mode 100644 index 418f07a..0000000 Binary files a/09_BusinessAIWeek/Test/images/service-key.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/service_binding.png b/09_BusinessAIWeek/Test/images/service_binding.png deleted file mode 100644 index 346a1c4..0000000 Binary files a/09_BusinessAIWeek/Test/images/service_binding.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/service_key.png b/09_BusinessAIWeek/Test/images/service_key.png deleted file mode 100644 index 82c020b..0000000 Binary files a/09_BusinessAIWeek/Test/images/service_key.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/setup0010.png b/09_BusinessAIWeek/Test/images/setup0010.png deleted file mode 100644 index 6d2bc07..0000000 Binary files a/09_BusinessAIWeek/Test/images/setup0010.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/setup0030.png b/09_BusinessAIWeek/Test/images/setup0030.png deleted file mode 100644 index 42ecbb8..0000000 Binary files a/09_BusinessAIWeek/Test/images/setup0030.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/setup0040.png b/09_BusinessAIWeek/Test/images/setup0040.png deleted file mode 100644 index e3c7ecb..0000000 Binary files a/09_BusinessAIWeek/Test/images/setup0040.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/setup0042.png b/09_BusinessAIWeek/Test/images/setup0042.png deleted file mode 100644 index 55af409..0000000 Binary files a/09_BusinessAIWeek/Test/images/setup0042.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/setup0060.png b/09_BusinessAIWeek/Test/images/setup0060.png deleted file mode 100644 index 7a5b849..0000000 Binary files a/09_BusinessAIWeek/Test/images/setup0060.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/start_terminal.png b/09_BusinessAIWeek/Test/images/start_terminal.png deleted file mode 100644 index 3eeff1c..0000000 Binary files a/09_BusinessAIWeek/Test/images/start_terminal.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/venv.png b/09_BusinessAIWeek/Test/images/venv.png deleted file mode 100644 index 30eaef5..0000000 Binary files a/09_BusinessAIWeek/Test/images/venv.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/images/workspace.png b/09_BusinessAIWeek/Test/images/workspace.png deleted file mode 100644 index 88d9d71..0000000 Binary files a/09_BusinessAIWeek/Test/images/workspace.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test/init_env.py b/09_BusinessAIWeek/Test/init_env.py deleted file mode 100644 index 21834de..0000000 --- a/09_BusinessAIWeek/Test/init_env.py +++ /dev/null @@ -1,34 +0,0 @@ -import os -import json -import configparser - -from hdbcli import dbapi - -import variables - -ROOT_PATH_DIR = os.path.dirname(os.getcwd()) -AICORE_CONFIG_FILENAME = '.aicore-config.json' -USER_CONFIG_FILENAME = '.user.ini' - -def set_environment_variables() -> None: - with open(os.path.join(ROOT_PATH_DIR, AICORE_CONFIG_FILENAME), 'r') as config_file: - config_data = json.load(config_file) - - os.environ["AICORE_AUTH_URL"]=config_data["url"]+"/oauth/token" - os.environ["AICORE_CLIENT_ID"]=config_data["clientid"] - os.environ["AICORE_CLIENT_SECRET"]=config_data["clientsecret"] - os.environ["AICORE_BASE_URL"]=config_data["serviceurls"]["AI_API_URL"] - - os.environ["AICORE_RESOURCE_GROUP"]=variables.RESOURCE_GROUP - -def connect_to_hana_db() -> dbapi.Connection: - config = configparser.ConfigParser() - config.read(os.path.join(ROOT_PATH_DIR, USER_CONFIG_FILENAME)) - return dbapi.connect( - address=config.get('hana', 'url'), - port=config.get('hana', 'port'), - user=config.get('hana', 'user'), - password=config.get('hana', 'passwd'), - autocommit=True, - sslValidateCertificate=False - ) \ No newline at end of file diff --git a/09_BusinessAIWeek/Test/readme.md b/09_BusinessAIWeek/Test/readme.md deleted file mode 100644 index 8b13789..0000000 --- a/09_BusinessAIWeek/Test/readme.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/09_BusinessAIWeek/Test/variables.py b/09_BusinessAIWeek/Test/variables.py deleted file mode 100644 index 2137ba3..0000000 --- a/09_BusinessAIWeek/Test/variables.py +++ /dev/null @@ -1,11 +0,0 @@ -# TODO Change the resource group, embedding model ID and LLM/Chat model ID to your own -RESOURCE_GROUP = "" # your resource group e.g. team-01 -EMBEDDING_DEPLOYMENT_ID = "" # e.g. d6b74feab22bc49a -LLM_DEPLOYMENT_ID = "" - -# TODO Change the URL to your own orchestration deployment ID -AICORE_ORCHESTRATION_DEPLOYMENT_URL = "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2/inference/deployments/xxx" - -# HANA table name to store your embeddings -# TODO Change the name to your team name -EMBEDDING_TABLE = "EMBEDDINGS_CODEJAM_"+"ADD_YOUR_NAME_HERE" diff --git a/09_BusinessAIWeek/Test2/00-connect-AICore-and-AILaunchpad.md b/09_BusinessAIWeek/Test2/00-connect-AICore-and-AILaunchpad.md deleted file mode 100644 index 2bc4981..0000000 --- a/09_BusinessAIWeek/Test2/00-connect-AICore-and-AILaunchpad.md +++ /dev/null @@ -1,46 +0,0 @@ -# Setup SAP AI Launchpad and SAP AI Core -[SAP AI Launchpad](https://help.sap.com/docs/ai-launchpad) is a multitenant SaaS application on SAP BTP. You can use SAP AI Launchpad to manage AI use cases across different AI runtimes. SAP AI Launchpad also provides generative AI capabilities via the [Generative AI Hub in SAP AI Core](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/generative-ai-hub-in-sap-ai-core-7db524ee75e74bf8b50c167951fe34a5) and is available in the Cloud Foundry environment. You can also connect the SAP HANA database as an AI runtime to work with the HANA Predictive Analysis Library (PAL), or connect SAP AI Services to work with the SAP AI Service Data Attribute Recommendation. - -## [1/4] Open SAP Business Technology Platform -πŸ‘‰ Open your [BTP cockpit](https://emea.cockpit.btp.cloud.sap/cockpit). - -πŸ‘‰ Navigate to the subaccount: `GenAI CodeJam`. - -## [2/4] Open SAP AI Launchpad and connect to SAP AI Core -πŸ‘‰ Go to **Instances and Subscriptions**. Check whether you see an **SAP AI Core** instance and an **SAP AI Launchpad** subscription. - -![BTP Cockpit](images/btp.png) - -With SAP AI Launchpad you can administer all your machine learning operations. ☝️ Make sure you have the extended plan of SAP AI Core to leverage Generative AI Hub. To connect SAP AI Core to SAP AI Launchpad you need the SAP AI Core service key. - -![SAP AI Core service key](images/service-key.png) - -☝️ In this subaccount the connection between the SAP AI Core service instance and the SAP AI Launchpad application is already established. Otherwise you would have to add a new runtime using the SAP AI Core service key information. - -πŸ‘‰ Open **SAP AI Launchpad**. - -## [3/4] Create a new resource group for your team -SAP AI Core tenants use [resource groups](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/resource-groups) to isolate AI resources and workloads. Scenarios (e.g. `foundation-models`) -and executables (a template for training a model or creation of a deployment) are shared across all resource groups. - -> Make sure to create a **NEW** resource group for your team.
DO NOT USE THE DEFAULT RESOURCE GROUP! - -πŸ‘‰ Open the `SAP AI Core Administration` tab and select `Resource Groups`. - -πŸ‘‰ **Create** a new resource group with your team's name. - -![SAP AI Launchpad - Recourse Group 1/2](images/resource_group.png) - -πŸ‘‰ Go back to **Workspaces**. - -πŸ‘‰ Select your connection and your resource group. - -πŸ‘‰ Make sure it is selected. It should show up at the top next to SAP AI Launchpad. - -☝️ You will need the name of your resource group in [Exercise 04-create-embeddings](04-create-embeddings.ipynb). - -> If your resource group does not show up, use the **browser(!)** refresh button to refresh the page. - -![SAP AI Launchpad - Recourse Group 2/2](images/resource_group_2.png) - -[Next Exercise](01-explore-genai-hub.md) diff --git a/09_BusinessAIWeek/Test2/01-explore-genai-hub.md b/09_BusinessAIWeek/Test2/01-explore-genai-hub.md deleted file mode 100644 index bab7b98..0000000 --- a/09_BusinessAIWeek/Test2/01-explore-genai-hub.md +++ /dev/null @@ -1,139 +0,0 @@ -# Explore Generative AI Hub in SAP AI Launchpad - -To leverage large language models (LLMs) or foundation models in your applications, you can use the Generative AI Hub on SAP AI Core. Like most other LLM applications, Generative AI Hub operates on a pay-per-use basis. - -Generative AI Hub offers all major models on the market. There are open-source models that SAP has deployed such as the Falcon model. And there are models that SAP is a proxy for, such as the GPT models, Google models, models provided by Amazon Bedrock and more. You can easily switch between them, compare results, and select the model that works best for your use case. - -SAP maintains strict data privacy contracts with LLM providers to ensure that your data remains secure. - -To start using one of the available models in Generative AI Hub, you need to first deploy it. You can either deploy an orchestration service and then have all available models accessible through that. Or you can deploy a single model directly via the **Model Library**. You can access your deployed models using the Python SDK, the SAP Cloud SDK for AI (JavaScript SDK), any programming language or API platform, or the user interface in SAP AI Launchpad. The SAP AI Launchpad offers a **Model Library**, a **Chat** interface and a **Prompt Editor**, where you can also save prompts and the model's responses. - -## Deploy a proxy via the Model Library - -![Model Library 1/3](images/model-library.png) - -πŸ‘‰ Open the `Generative AI Hub` tab and select `Model Library`. - -πŸ‘‰ Click on **GPT-4o Mini** which is a text generation model that can also process images. - -![Model Library 2/3](images/model-library-2.png) - -πŸ‘‰ Click on `Deploy` **ONE TIME** to make the model endpoint available to you. **It will TAKE A MINUTE to be deployed.** - -πŸ‘‰ You can check the deployment status of the models by clicking on `ML Operations>Deployments`. - -![Model Library 3/3](images/model-library-3.png) - -πŸ‘‰ Click on `Use in Chat` to open the chat interface and start interacting with the model. - -> ☝️ If your model is not deployed yet, the Chat window will ask you to enable the orchestration service. You can do that and then you have all the models available to chat to. If you deploy the orchestration service now, you will not have to do it again in exercise [07-deploy-orchestration-service.md](07-deploy-orchestration-service.md) -![Chat-orchestration](images/enable_orchestration.png) - -If your model is deployed, the chat will look like this: -![Chat](images/chat1.png) - -## Use the Chat in Generative AI Hub - -πŸ‘‰ If you lost the previous chat window you can always open the `Generative AI Hub` tab and select `Chat`. - -πŸ‘‰ Click `Configure` and have a look at the available fields. - -Under `Selected Model` you will find all the deployed models. If there is no deployment this will be empty and you will not be able to chat. If you have more than one large language model deployed you will be able to select which one you want to use here. - -The `Frequency Penalty` parameter allows you to penalize words that appear too frequently in the text, helping the model sound less robotic. - -Similarly, the higher the `Presence Penalty`, the more likely the model is to introduce new topics, as it penalizes words that have already appeared in the text. - -The `Max Tokens` parameter allows you to set the size of the model's input and output. Tokens are not individual words but are typically 4-5 characters long. - -The `Temperature` parameter allows you to control how creative the model should be, determining how flexible it is in selecting the next token in the sequence. - -![Chat 1/2](images/chat2.png) - -In the `Chat Context` tab, right under `Context History`, you can set the number of messages to be sent to the model, determining how much of the chat history should be provided as context for each new request. This is basically the size of the models memory. - -You can also add a `System Message` to describe the role or give more information about what is expected from the model. Additionally, you can provide example inputs and outputs. - -![Chat 2/2](images/chat3.png) - -## Prompt Engineering -πŸ‘‰ Try out different prompt engineering techniques following these examples: - -1. **Zero shot**: - ``` - The capital of the U.S. is: - ``` -2. **Few shot**: - ``` - Germany - Berlin - India - New Delhi - France - - ``` -3. **Chain of thought** (Copy the WHOLE BLOCK into the chat window): - ``` - 1. What is the most important city of a country? - 2. In which country was the hot air balloon originally developed? - 3. What is the >fill in the word from step 1< of the country >fill in the word from step 2<. - ``` - -πŸ‘‰ Try to add something funny to the `System Message` like "always respond like a pirate" and try the prompts again. You can also instruct it to speak more technically, like a developer, or more polished, like in marketing. - -πŸ‘‰ Have the model count letters in words. For example how often the letter **r** occurs in **strawberry**. Can you come up with a prompt that counts it correctly? - -> πŸ‘‰ Before you move on to the next exercise, make sure to also deploy the `Text Embedding 3 Small` model. You will need it later! Clicking the deploy button once is enough, then you can see your deployments under `ML Operations>Deployments`. - -## Use the Prompt Editor in Generative AI Hub -The `Prompt Editor` is useful if you want to save a prompt and its response to revisit later or compare prompts. Often, you can identify tasks that an LLM can help you with on a regular basis. In that case, you can also save different versions of the prompt that work well, saving you from having to write the prompt again each time. - -The parameters you were able to set in the `Chat` can also be set here. Additionally, you can view the number of tokens your prompt used below the response. - -πŸ‘‰ Go over to `Prompt Editor`, select a model and set `Max Tokens` to the maximum again - -πŸ‘‰ Paste the example below and click **Run** to try out the example below. - -πŸ‘‰ Give your prompt a `Name`, a `Collection` name, and **Save** the prompt. - -πŸ‘‰ If you now head over to `Prompt Management` you will find your previously saved prompt there. To run the prompt again click `Open in Prompt Editor`. You can also select other saved prompts by clicking on **Select**. - -1. Chain of thought prompt - customer support (Copy the WHOLE BLOCK into the prompt editor): - ``` - You are working at a big tech company and you are part of the support team. - You are tasked with sorting the incoming support requests into: German, English, Polish or French. - - Review the incoming query. - Identify the language of the query as either German, English, Polish, or French. - - Example: 'bad usability. very confusing user interface.' - English - Count the number of queries for each language: German, English, Polish, and French. - Summarize the key pain points mentioned in the queries in bullet points in English. - - Queries: - - What are the shipping costs to Australia? - - Kann ich einen Artikel ohne Kassenbon umtauschen? - - Offrez-vous des rΓ©ductions pour les achats en gros? - - Can I change the delivery address after placing the order? - - Comment puis-je annuler mon abonnement? - - Wo kann ich den Status meiner Reparatur einsehen? - - What payment methods do you accept? - - Czemu to tak dΕ‚ugo ma iΕ›Δ‡ do WrocΕ‚awia, gdy cena nie jest wcale niska? - - Quel est le dΓ©lai de livraison estimΓ© pour le Mexique? - - Gibt es eine Garantie auf elektronische GerΓ€te? - - I’m having trouble logging into my account, what should I do? - ``` - -![Prompt Editor](images/prompt_editor.png) - -πŸ‘‰ If you still have time. Ask the LLM to come up with different support queries to have more data. - -## Summary - -By this point, you will know how to use the Generative AI Hub user interface in SAP AI Launchpad to query LLMs and store important prompts. You will also understand how to refine the output of a large language model using prompt engineering techniques. - -## Further reading - -* [Generative AI Hub on SAP AI Core - Help Portal (Documentation)](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/generative-ai-hub-in-sap-ai-core-7db524ee75e74bf8b50c167951fe34a5) -* [This](https://www.promptingguide.ai/) is a good resource if you want to know more about prompt engineering. -* [This](https://developers.sap.com/tutorials/ai-core-generative-ai.html) is a good tutorial on how to prompt LLMs with Generative AI Hub. - ---- - -[Next exercise](02-setup-python-environment.md) diff --git a/09_BusinessAIWeek/Test2/02-setup-python-environment.md b/09_BusinessAIWeek/Test2/02-setup-python-environment.md deleted file mode 100644 index 4c67ad3..0000000 --- a/09_BusinessAIWeek/Test2/02-setup-python-environment.md +++ /dev/null @@ -1,147 +0,0 @@ -# Setup SAP Business Application Studio and a dev space -> [SAP Business Application Studio](https://help.sap.com/docs/bas/sap-business-application-studio/what-is-sap-business-application-studio) is based on Code-OSS, an open-source project used for building Visual Studio Code. Available as a cloud service, SAP Business Application Studio provides a desktop-like experience similar to leading IDEs, with command line and optimized editors. - -> At the heart of SAP Business Application Studio are the dev spaces. The dev spaces are comparable to isolated virtual machines in the cloud containing tailored tools and preinstalled runtimes per business scenario, such as SAP Fiori, SAP S/4HANA extensions, Workflow, Mobile and more. This simplifies and speeds up the setup of your development environment, enabling you to efficiently develop, test, build, and run your solutions locally or in the cloud. - -## Open SAP Business Application Studio -πŸ‘‰ Go back to your [BTP cockpit](https://emea.cockpit.btp.cloud.sap/cockpit). - -πŸ‘‰ Navigate to `Instances and Subscriptions` and open `SAP Business Application Studio`. - -![Clone the repo](images/BTP_cockpit_BAS.png) - - -## Create a new Dev Space for CodeJam exercises - -πŸ‘‰ Create a new Dev Space. - -![Create a Dev Space 1](images/bas.png) - -πŸ‘‰ Enter the name of the Dev space `GenAICodeJam`, select the `Basic` kind of application and `Python Tools` from Additional SAP Extensions. - -πŸ‘‰ Click **Create Dev Space**. - -![Create a Dev Space 2](images/create_dev_space.png) - -You should see the dev space **STARTING**. - -![Dev Space is Starting](images/dev_starting.png) - -πŸ‘‰ Wait for the dev space to get into the **RUNNING** state and then open it. - -![Dev Space is Running](images/dev_running.png) - -## Clone the exercises from the Git repository - -πŸ‘‰ Once you opened your dev space in BAS, use one of the available options to clone this Git repository with exercises using the URL below: - -```sh -https://github.com/SAP-samples/generative-ai-codejam.git -``` - -![Clone the repo](images/clone_git.png) - -πŸ‘‰ Click **Open** to open a project in the Explorer view. - -![Open a project](images/clone_git_2.png) - -## Open the Workspace - -The cloned repository contains a file `codejam.code-workspace` and therefore you will be asked, if you want to open it. - -πŸ‘‰ Click **Open Workspace**. - -![Automatic notification to open a workspace](images/open_workspace.png) - -☝️ If you missed the previous dialog, you can go to the BAS Explorer, open the `codejam.code-workspace` file, and click **Open Workspace**. - -You should see: -* `CODEJAM` as the workspace at the root of the hierarchy of the project, and -* `generative-ai-codejam` as the name of the top level folder. - -πŸ‘‰ You can close the **Get Started** tab. - -![Open a workspace](images/workspace.png) - -## Configure the connection to Generative AI Hub - -πŸ‘‰ Go back to your [BTP cockpit](https://emea.cockpit.btp.cloud.sap/cockpit). - -πŸ‘‰ Navigate to `Instances and Subscriptions` and open the SAP AI Core instance's service binding. - -![Service Binding in the BTP Cockpit](images/service_binding.png) - -πŸ‘‰ Click **Copy JSON**. - -πŸ‘‰ Return to BAS and create a new file `.aicore-config.json` in the `generative-ai-codejam/` directory. - -πŸ‘‰ Paste the service key into `generative-ai-codejam/.aicore-config.json`, which should look similar to the following. - -```json -{ - "appname": "7ce41b4e-e483-4fc8-8de4-0eee29142e43!b505946|aicore!b540", - "clientid": "sb-7ce41b4e-e483-4fc8-8de4-0eee29142e43!b505946|aicore!b540", - "clientsecret": "...", - "identityzone": "genai-codejam-luyq1wkg", - "identityzoneid": "a5a420d8-58c6-4820-ab11-90c7145da589", - "serviceurls": { - "AI_API_URL": "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com" - }, - "url": "https://genai-codejam-luyq1wkg.authentication.eu10.hana.ondemand.com" -} -``` - -## Create a Python virtual environment and install the SAP's [Python SDK for Generative AI Hub]((https://pypi.org/project/generative-ai-hub-sdk/)) - -πŸ‘‰ Start a new Terminal. - -![Extensions to install](images/start_terminal.png) - -πŸ‘‰ Create a virtual environment using the following command: - -```bash -python3 -m venv ~/projects/generative-ai-codejam/env --upgrade-deps -``` - -πŸ‘‰ Activate the `env` virtual environment like this and make sure it is activated: - -```bash -source ~/projects/generative-ai-codejam/env/bin/activate -``` - -![venv](images/venv.png) - -πŸ‘‰ Install the Generative AI Hub [Python SDK](https://pypi.org/project/generative-ai-hub-sdk/) (and the other packages listed below) using the following `pip install` commands. - -```bash -pip install --require-virtualenv -U generative-ai-hub-sdk[all] -``` - -πŸ‘‰ We will also need the [HANA client for Python](https://pypi.org/project/hdbcli/). - -```bash -pip install --require-virtualenv -U hdbcli -``` - -πŸ‘‰ We will also need the [SciPy package](https://pypi.org/project/scipy/). - -```bash -pip install --require-virtualenv scipy -``` - -πŸ‘‰ We will also need the [pydf package](https://pypi.org/project/pypdf/) and [wikipedia](https://pypi.org/project/wikipedia/). - -```bash -pip install --require-virtualenv pypdf wikipedia -``` - - - - -> From now on the exercises continue in BAS. - -[Next exercise](03-prompt-llm.ipynb) diff --git a/09_BusinessAIWeek/Test2/03-prompt-llm.ipynb b/09_BusinessAIWeek/Test2/03-prompt-llm.ipynb deleted file mode 100644 index fd73b88..0000000 --- a/09_BusinessAIWeek/Test2/03-prompt-llm.ipynb +++ /dev/null @@ -1,469 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Check your connection to Generative AI Hub\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ First you need to assign the values from `generative-ai-codejam/.aicore-config.json` to the environmental variables. \n", - "\n", - "That way the Generative AI Hub [Python SDK](https://pypi.org/project/generative-ai-hub-sdk/) will connect to Generative AI Hub." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ For the Python SDK to know which resource group to use, you also need to set the `resource group` in the [`variables.py`](variables.py) file to your own `resource group` (e.g. **team-01**) that you created in the SAP AI Launchpad in exercise [00-connect-AICore-and-AILaunchpad](000-connect-AICore-and-AILaunchpad.md)." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "pip install generative-ai-hub-sdk[all] hdbcli scipy pypdf --break-system-packages" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://israel-fsvdxbsq.authentication.eu11.hana.ondemand.com/oauth/token\n", - "sb-49ec08a9-d325-4480-9418-ad8801558203!b28574|aicore!b18\n", - "96d2ba69-3289-4190-ad82-c174e50f9f17$8C_adlgCYD6AscPgIKtLXJkIj1AL6i8p9Opw1JJZ0o8=\n", - "https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "grounding\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"grounding\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "# #import init_env\n", - "# #import variables\n", - "# #import importlib\n", - "# #variables = importlib.reload(variables)\n", - "\n", - "# # TODO: You need to specify which model you want to use. In this case we are directing our prompt\n", - "# # to the openAI API directly so you need to pick one of the GPT models. Make sure the model is actually deployed\n", - "# # in genAI Hub. You might also want to chose a model that can also process images here already. \n", - "# # E.g. 'gpt-4o-mini' or 'gpt-4o'\n", - "# MODEL_NAME = 'gpt-4o'\n", - "# # Do not modify the `assert` line below\n", - "# assert MODEL_NAME!='', \"\"\"You should change the variable `MODEL_NAME` with the name of your deployed model (like 'gpt-4o-mini') first!\"\"\"\n", - "\n", - "# #init_env.set_environment_variables()\n", - "# # Do not modify the `assert` line below \n", - "# assert variables.RESOURCE_GROUP!='', \"\"\"You should change the value assigned to the `RESOURCE_GROUP` in the `variables.py` file to your own resource group first!\"\"\"\n", - "# print(f\"Resource group is set to: {variables.RESOURCE_GROUP}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Prompt an LLM with Generative AI Hub" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "The underlying architecture of a Large Language Model (LLM) is typically based on the Transformer model. The key components of this architecture include:\n", - "\n", - "1. **Attention Mechanism**: Specifically, self-attention allows the model to weigh the significance of different words relative to one another, enabling it to capture contextual relationships effectively.\n", - "\n", - "2. **Layers**: The Transformer consists of multiple stacked layers, each containing two main sub-layers: a multi-head self-attention mechanism and a feed-forward neural network.\n", - "\n", - "3. **Positional Encoding**: Since Transformers do not have a built-in sense of word order, positional encodings are added to input embeddings to retain the sequence information.\n", - "\n", - "4. **Training Objective**: LLMs are usually trained using unsupervised learning techniques, such as predicting the next word in a sequence (language modeling).\n", - "\n", - "This architecture allows LLMs to process and generate human-like text by understanding context, relationships, and nuances within language." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "from gen_ai_hub.proxy.native.openai import chat\n", - "from IPython.display import Markdown\n", - "\n", - "messages = [\n", - " {\n", - " \"role\": \"user\", \n", - " \"content\": \"What is the underlying model architecture of an LLM? Explain it as short as possible.\"\n", - " }\n", - "]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o-mini\", messages=messages)\n", - "\n", - "response = chat.completions.create(**kwargs)\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Understanding roles\n", - "Most LLMs have the roles `system`, `assistant` (GPT) or `model` (Gemini) and `user` that can be used to steer the models response. In the previous step you only used the role `user` to ask your question. \n", - "\n", - "πŸ‘‰ Try out different `system` messages to change the response. You can also tell the model to not engage in smalltalk or only answer questions on a certain topic. Then try different user prompts as well!" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "An LLM, hmm, based on transformer architecture it is. Attention mechanisms, self-attention layers, and feedforward networks, it employs. Training on vast data, it learns patterns and context. Simple, yet powerful, yes." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "messages = [\n", - " { \"role\": \"system\", \n", - " # TODO try changing the system prompt\n", - " \"content\": \"Speak like Yoda from Star Wars.\"\n", - " }, \n", - " {\n", - " \"role\": \"user\", \n", - " \"content\": \"What is the underlying model architecture of an LLM? Explain it as short as possible.\"\n", - " }\n", - "]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o-mini\", messages=messages)\n", - "response = chat.completions.create(**kwargs)\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Also try to have it speak like a pirate.\n", - "\n", - "πŸ‘‰ Now let's be more serious! Tell it to behave like an SAP consultant talking to AI Developers.\n", - "\n", - "πŸ‘‰ Ask it to behave like an SAP Consultant talking to ABAP Developers and to make ABAP comparisons." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Hallucinations\n", - "πŸ‘‰ Run the following question." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "Data masking in the context of orchestration services typically involves the processes and techniques used to obscure specific data within a dataset, thereby protecting sensitive information while retaining its usability for analytics, development, or testing purposes. Although SAP provides various orchestration services (like SAP Data Intelligence, SAP Process Orchestration, etc.), the fundamental principles of data masking generally apply across platforms.\n", - "\n", - "Here's an overview of how data masking might work within an orchestration service:\n", - "\n", - "1. **Definition of Sensitivity**: Identify the data elements that need to be masked based on business rules and compliance requirements. This may include personal information such as names, identification numbers, financial data, etc.\n", - "\n", - "2. **Masking Techniques**: Various techniques can be employed, including:\n", - " - **Static Data Masking**: This replaces the sensitive data with masked values and is typically used in non-production environments.\n", - " - **Dynamic Data Masking**: This keeps the original data intact in the backend, but provides masked values to users based on their roles or permissions.\n", - " - **Tokenization**: Sensitive data is replaced with non-sensitive tokens that can represent the original data without exposing it.\n", - " - **Shuffling**: Rearranging the values within a column or dataset so that the actual data is not easily identifiable.\n", - " - **Data Obfuscation**: Modifying the data format or appearance without significant data loss.\n", - "\n", - "3. **Integration within Orchestration Workflows**: The orchestration service can include data masking as part of its workflow processes. This means:\n", - " - **Data Extraction**: When extracting data from source systems, the orchestration service can apply masking logic to sensitive data before it is moved to another system or environment.\n", - " - **Data Transformation**: During data transformation processes (ETL - Extract, Transform, Load), masking can be incorporated into transformation rules.\n", - "\n", - "4. **Automation and Monitoring**: Data masking can be automated within workflows to ensure that any sensitive data flows are consistently masked. Additionally, monitoring can be set up to ensure compliance and traceability.\n", - "\n", - "5. **Auditing and Compliance**: Organizations often require audits to ensure that data masking procedures follow regulations (like GDPR, HIPAA, etc.). Orchestration services should include mechanisms for logging and monitoring access to sensitive data.\n", - "\n", - "6. **User Role Management**: Access controls can be integrated within the orchestration service to ensure that users view only masked data unless they have permission to see original data.\n", - "\n", - "7. **Performance Considerations**: It's important to maintain performance when applying data masking techniques, especially in high-transaction environments.\n", - "\n", - "To implement data masking effectively, organizations often combine these techniques to suit their specific needs, ensuring that they meet both security requirements and operational efficiency. Always consult relevant technical documentation specific to your SAP environment for detailed implementation approaches and tools available." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "messages = [\n", - " { \"role\": \"system\", \n", - " \"content\": \"You are an SAP Consultant.\"\n", - " }, \n", - " {\n", - " \"role\": \"user\", \n", - " \"content\": \"How does the data masking of the orchestration service work?\"\n", - " }\n", - "]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o-mini\", messages=messages)\n", - "response = chat.completions.create(**kwargs)\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "☝️ Compare the response to [SAP Help - Generative AI Hub SDK](https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html). \n", - "\n", - "πŸ‘‰ What did the model respond? Was it the truth or a hallucination?\n", - "\n", - "πŸ‘‰ Which questions work well, which questions still do not work so well?\n", - "\n", - "# Use Multimodal Models\n", - "\n", - "Multimodal models can use different inputs such as text, audio and images. In Generative AI Hub on SAP AI Core you can access multiple multimodal models (e.g. `gpt-4o-mini`).\n", - "\n", - "πŸ‘‰ If you have not deployed one of the gpt-4o-mini models in previous exercises, then go back to the model library and deploy a model that can also process images.\n", - "\n", - "πŸ‘‰ Now run the code snippet below to get a description for [the image of the AI Foundation Architecture](documents/ai-foundation-architecture.png). These descriptions can then for example be used as alternative text for screen readers or other assistive tech.\n", - "\n", - "πŸ‘‰ You can upload your own image and play around with it!" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "The image is a structured diagram titled \"AI Foundation on SAP BTP.\" It outlines various components related to AI services and management within the SAP Business Technology Platform. \n", - "\n", - "At the top, there are sections labeled \"AI Services,\" displaying four features: Document Processing, Recommendation, Machine Translation, and Generative AI Management, highlighted in a pink box. Under Generative AI Management, there are three subcategories: Toolset, Trust & Control, and Access.\n", - "\n", - "Beneath that, the \"AI Workload Management\" section includes Training and Inference, while the \"Business Data & Context\" segment features Vector Engine and Data Management in blue boxes.\n", - "\n", - "Further down, the \"Foundation Models\" area lists three categories: SAP built, Hosted, and Remote, with an additional category for Fine-tuned. At the bottom, there is a section titled \"Lifecycle Management\" in a blue box. The overall layout is organized and visually segmented for clarity." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import base64\n", - "\n", - "# get the image from the documents folder\n", - "with open(\"documents/ai-foundation-architecture.png\", \"rb\") as image_file:\n", - " image_data = base64.b64encode(image_file.read()).decode('utf-8')\n", - "\n", - "messages = [{\"role\": \"user\", \"content\": [\n", - " {\"type\": \"text\", \"text\": \"Describe the images as an alternative text.\"},\n", - " {\"type\": \"image_url\", \"image_url\": {\n", - " \"url\": f\"data:image/png;base64,{image_data}\"}\n", - " }]\n", - " }]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o-mini\", messages=messages)\n", - "response = chat.completions.create(**kwargs)\n", - "\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Extracting text from images\n", - "Nora loves bananabread and thinks recipes are a good example of how LLMs can also extract complex text from images, like from [a picture of a recipe of a bananabread](documents/bananabread.png). Try your own recipe if you like :)\n", - "\n", - "This exercise also shows how you can use the output of an LLM in other systems, as you can tell the LLM how to output information, for example in JSON format." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/markdown": [ - "Here are the ingredients and instructions extracted from the banana bread recipe into two separate JSON files.\n", - "\n", - "**Ingredients JSON:**\n", - "\n", - "```json\n", - "{\n", - " \"dry_ingredients\": {\n", - " \"all_purpose_flour\": \"260 g\",\n", - " \"sugar\": \"200 g\",\n", - " \"baking_soda\": \"6 g\",\n", - " \"salt\": \"3 g\"\n", - " },\n", - " \"wet_ingredients\": {\n", - " \"banana\": \"225 g\",\n", - " \"large_eggs\": \"2\",\n", - " \"vegetable_oil\": \"100 g\",\n", - " \"whole_milk\": \"55 g\",\n", - " \"vanilla_extract\": \"5 g\"\n", - " },\n", - " \"toppings\": {\n", - " \"chocolate_chips\": \"100 g\",\n", - " \"walnuts\": \"100 g (optional)\"\n", - " }\n", - "}\n", - "```\n", - "\n", - "**Instructions JSON:**\n", - "\n", - "```json\n", - "{\n", - " \"instructions\": [\n", - " \"Preheat oven to 180 Β°C\",\n", - " \"Mash banana in a bowl\",\n", - " \"Combine banana and other wet ingredients together in the same bowl\",\n", - " \"Mix all dry ingredients together in a separate bowl\",\n", - " \"Use a whisk to combine dry mixture into wet mixture until smooth\",\n", - " \"Pour mixture into a greased/buttered loaf pan\",\n", - " \"Place the pan into preheated oven on the middle rack and bake for about 60 minutes or until a toothpick comes out clean\",\n", - " \"Enjoy!\"\n", - " ]\n", - "}\n", - "```" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# get the image from the documents folder\n", - "with open(\"documents/bananabread.png\", \"rb\") as image_file:\n", - " image_data = base64.b64encode(image_file.read()).decode('utf-8')\n", - "\n", - "messages = [{\"role\": \"user\", \"content\": [\n", - " {\"type\": \"text\", \"text\": \"Extract the ingredients and instructions in two different json files.\"},\n", - " {\"type\": \"image_url\", \"image_url\": {\n", - " \"url\": f\"data:image/png;base64,{image_data}\"}\n", - " }]\n", - " }]\n", - "\n", - "kwargs = dict(model_name=\"gpt-4o-mini\", messages=messages)\n", - "response = chat.completions.create(**kwargs)\n", - "\n", - "display(Markdown(response.choices[0].message.content))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[Next exercise](04-create-embeddings.ipynb)\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test2/04-create-embeddings.ipynb b/09_BusinessAIWeek/Test2/04-create-embeddings.ipynb deleted file mode 100644 index 6b11892..0000000 --- a/09_BusinessAIWeek/Test2/04-create-embeddings.ipynb +++ /dev/null @@ -1,216 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Create embeddings with Generative AI Hub\n", - "Like any other machine learning model, also foundation models only work with numbers. In the context of generative AI, these numbers are embeddings. Embeddings are numerical representations of unstructured data, such as text and images. The text embedding model of OpenAI `text-embedding-3-small` for example turns your input text into 1536 numbers. That is a vector with 1536 dimensions.\n", - "\n", - "πŸ‘‰ Select the kernel again. Make sure to select the same virtual environment as in the previous exercise so that all your packages are installed." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://israel-fsvdxbsq.authentication.eu11.hana.ondemand.com/oauth/token\n", - "sb-49ec08a9-d325-4480-9418-ad8801558203!b28574|aicore!b18\n", - "96d2ba69-3289-4190-ad82-c174e50f9f17$8C_adlgCYD6AscPgIKtLXJkIj1AL6i8p9Opw1JJZ0o8=\n", - "https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "grounding\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"grounding\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "#import init_env\n", - "\n", - "#init_env.set_environment_variables()\n", - "\n", - "from gen_ai_hub.proxy.native.openai import embeddings\n", - "\n", - "# TODoassign the model name of the embedding model here, e.g. \"text-embedding-3-small\"\n", - "EMBEDDING_MODEL_NAME = \"text-embedding-ada-002\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Create embeddings\n", - "Define the method **get_embedding()**." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "def get_embedding(input_text):\n", - " response = embeddings.create(\n", - " input=input_text, \n", - " model_name=EMBEDDING_MODEL_NAME\n", - " )\n", - " embedding = response.data[0].embedding\n", - " return embedding" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Get embeddings for the words: **apple, orange, phone** and for the phrases: **I love dogs, I love animals, I hate cats.**" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.007730893790721893, -0.023138046264648438, -0.007587476167827845, -0.02780936472117901, -0.0046508293598890305, 0.013010028749704361, -0.021963387727737427, -0.008393346332013607, 0.018958445638418198, -0.029557693749666214, -0.002926402958109975, 0.020078469067811966, -0.004415214527398348, 0.009158240631222725, -0.021649233996868134, 0.0020146763417869806, 0.030732352286577225, 0.00010212104098172858, 0.0020266277715563774, -0.025460045784711838, -0.021061904728412628, -0.008195294067263603, 0.02137605845928192, -0.012552458792924881, 0.0011336823226884007, 0.005043520592153072, 0.01019631139934063, 7.816474681021646e-05, 0.016062775626778603, -0.013023687526583672, 0.020460916683077812, -0.016158387064933777, -0.018384775146842003, 0.0054430412128567696, -0.019381869584321976, -0.009171899408102036, -0.012033423408865929, -0.0087075000628829, -0.005702558439224958, -0.006166958715766668, 0.010524122975766659, 0.0076284524984657764, -0.006399158388376236, 0.0008080047555267811, -0.02338390424847603, 0.011213894002139568, -0.02182679995894432, -0.000542084570042789, -0.012764169834554195, 0.01532519981265068, -0.005528408568352461, -0.002086385153234005, -0.014888117089867592, -0.005214255768805742, -0.0028854263946413994, 0.0027420087717473507, -0.01949113979935646, 0.006419647019356489, -0.010858763940632343, 0.00499229971319437, 0.005296208430081606, 0.015161293558776379, -0.027959611266851425, 0.005408893804997206, -0.00915141124278307, 0.00379715277813375, -0.013501746580004692, -0.008564081974327564, 0.01311929989606142, 0.01574862375855446, 0.03357338532805443, 0.013153446838259697, 0.0140276113525033, 0.0066450173035264015, 0.00428545568138361, 0.0013949073618277907, -0.03390119969844818, -0.0062045203521847725, -0.0031005528289824724, 0.018138917163014412, 0.00764894112944603, -0.026511775329709053, 0.009725081734359264, 0.02503662183880806, 0.02480442263185978, 0.009820694103837013, -0.021458011120557785, 0.009991428814828396, -0.007724064402282238, 0.003187627764418721, 0.011828540824353695, 0.005425967276096344, 0.007655770517885685, 0.01649985834956169, -0.026866905391216278, 0.01667742244899273, -0.012764169834554195, 0.02642982266843319, 0.004814735148102045, -0.037151999771595, 0.0009714837651699781, 0.006074761506170034, -0.02873816341161728, -0.010353388264775276, -0.020283352583646774, 0.0019668706227093935, 0.012484163977205753, -0.0036059292033314705, 0.01532519981265068, -0.01995554007589817, 0.006477696821093559, 0.009253853000700474, 0.021963387727737427, -0.05168498679995537, -0.016404246911406517, -0.017346706241369247, -0.009547517634928226, 0.0021461425349116325, -0.022905847057700157, -0.035594891756772995, 0.03417437523603439, 0.025828834623098373, 0.03829934075474739, -0.008844087831676006, 0.01335832942277193, -0.003537635086104274, -0.017742810770869255, -0.009656787849962711, -0.0008835550979711115, -0.013986635021865368, 0.04706830531358719, 0.00822944100946188, 0.01949113979935646, 0.016568152233958244, -0.01787939853966236, 0.019655046984553337, -0.022195586934685707, 0.015598376281559467, -0.005596702918410301, -0.03362802416086197, 0.010264605283737183, 0.014737870544195175, -0.012265623547136784, 0.007082099560648203, -0.011302676051855087, 0.0236707404255867, 0.023602446541190147, 0.022659987211227417, -0.00534401461482048, 0.007109417580068111, 0.01068802922964096, -0.02416245825588703, -0.003899594070389867, 0.005545482039451599, 0.001879795570857823, 0.00557279959321022, -0.017223775386810303, -0.002011261647567153, -0.003807396860793233, 0.0037698352243751287, -0.00637867022305727, 0.00859822891652584, -0.006812337785959244, -0.008850917220115662, 0.0016757668927311897, 0.024503929540514946, 0.011753417551517487, 0.013440282084047794, -0.01621302217245102, -0.009533858858048916, -0.016322294250130653, 0.0040464261546730995, -0.01053778175264597, 0.01265489961951971, 0.008953358978033066, 0.01600814051926136, 0.004360579419881105, 0.01274368166923523, -0.006522087845951319, 0.0007128198049031198, 0.0012199035845696926, -0.004702049773186445, 0.023124387487769127, 0.01923162303864956, -0.016800353303551674, 0.014819823205471039, 0.004613267723470926, -0.010558269917964935, 0.005555726122111082, -0.019422845914959908, 0.032152868807315826, 0.020529210567474365, -0.003937155939638615, -0.014341764152050018, -0.6844709515571594, -0.02062482200562954, 0.005719632375985384, -0.00018450082279741764, 0.03406510502099991, 0.031879693269729614, 0.02225022204220295, -0.0012677095364779234, -0.018412092700600624, -0.010291922837495804, 0.0018490632064640522, 0.003162017557770014, 0.008953358978033066, 0.009718252345919609, -0.00012975881691090763, -0.022427788004279137, 0.016404246911406517, -0.0241214819252491, -0.010394364595413208, 0.024708811193704605, -0.01206074096262455, -0.01450567040592432, -0.008762135170400143, -0.0296669639647007, 0.0037356880493462086, 0.01949113979935646, 0.01994188129901886, -0.01404127012938261, -0.019777975976467133, 0.021867776289582253, -0.008270417340099812, -0.0007858091848902404, 0.02741325832903385, 0.005958661437034607, 0.037452492862939835, 0.007205029018223286, -0.04433653876185417, 0.013884193263947964, 0.009636299684643745, 0.03414705768227577, -0.0208979994058609, -0.020570186898112297, 0.013529064133763313, -0.006423061713576317, -0.01598082296550274, -0.0009877036791294813, 0.013146617449820042, 0.00555914081633091, -0.0007674551452510059, -0.02552833966910839, 0.034939270466566086, -2.5383535103173926e-05, 0.010749493725597858, 0.008420664817094803, 0.0217994824051857, 0.008700670674443245, 0.023110728710889816, -0.008980676531791687, -0.0014691771939396858, 0.01649985834956169, -0.022427788004279137, -0.003356655826792121, -0.0348300002515316, -0.025364434346556664, -0.020570186898112297, 0.0108041288331151, -0.009267511777579784, 0.007860653102397919, 0.005415723193436861, -0.019586753100156784, 0.021485328674316406, 0.022373152896761894, -0.023329269140958786, -0.007095758803188801, -0.003233726369217038, 0.017032552510499954, 0.023547811433672905, -0.001466616173274815, -0.027085445821285248, 0.004886444192379713, -0.0021256543695926666, -0.009342635050415993, 0.005050349980592728, -0.027754727751016617, 0.03152456507086754, 0.0055420673452317715, -0.02416245825588703, -0.011207064613699913, 0.01404127012938261, -0.02390293963253498, 0.034201692789793015, 0.024012211710214615, 0.0027334720361977816, -0.019204305484890938, -0.014464694075286388, -0.005364502780139446, -0.03248067945241928, -0.019354552030563354, -0.002422733698040247, -0.019791634753346443, 0.01669108122587204, -0.005634264554828405, 0.009397270157933235, 0.006825997028499842, 0.02387562207877636, 0.013412964530289173, -0.01923162303864956, 0.014696893282234669, 0.03682418540120125, -0.020542869344353676, -0.0077582113444805145, -0.012497822754085064, 0.008543593809008598, -0.008536764420568943, 0.0257332231849432, -0.03106016293168068, 0.010729005560278893, -0.007655770517885685, 0.021143857389688492, -0.0328904464840889, 0.02551468089222908, -0.004486923571676016, 0.011971957981586456, 0.0029417690820991993, -0.010722176171839237, 0.018248187378048897, -0.004295699764043093, -0.03081430494785309, -0.003476170590147376, -0.006026955787092447, 0.008113340474665165, 0.005244988016784191, 0.007321129087358713, -0.006057688035070896, 0.02390293963253498, 0.00024201726773753762, 0.00973191112279892, -0.02067945897579193, 0.012026594020426273, -0.02503662183880806, -0.016854988411068916, 0.008133829571306705, 0.0004366555076558143, -0.0006227569538168609, 0.0013342963065952063, -0.012135864235460758, -0.028246445581316948, -0.017005234956741333, -0.010886082425713539, 0.005207426380366087, -0.009083117358386517, 0.005583044141530991, -0.0018763808766379952, 0.021635575219988823, 0.02274193987250328, -0.015639351680874825, 0.0027317646890878677, -0.023575128987431526, 0.0009586786618456244, 0.01644522324204445, 0.010011916980147362, 0.028464987874031067, -0.008932869881391525, 0.0042000883258879185, 0.015092999674379826, -0.035840753465890884, 0.006699652876704931, 0.0017995499074459076, -0.017715493217110634, -0.029284516349434853, 0.01332418154925108, -0.016745716333389282, -0.00824309978634119, 0.00812016986310482, -0.014683234505355358, 0.003971302881836891, -0.014929093420505524, 0.007409911602735519, 0.010121188126504421, 0.0025405411142855883, 0.0053132823668420315, 0.009001164697110653, 0.0022656572982668877, 0.0018524779006838799, 0.03086894005537033, 0.020228717476129532, 0.0014905190328136086, 0.04799710586667061, -0.020105788484215736, 0.0008775793830864131, -0.013665652833878994, 0.019846269860863686, 0.00550109101459384, 0.0016629616729915142, -0.01391151174902916, -0.008236270397901535, -0.005941587965935469, 0.013481258414685726, -0.0009783131536096334, 0.011425605975091457, 0.024025870487093925, -0.0026600558776408434, 0.011166088283061981, -0.0020915071945637465, 0.013754434883594513, -0.032808493822813034, 0.003995205741375685, -0.005378161557018757, 0.011104622855782509, 0.03461145609617233, -0.0022980968933552504, -0.02851962298154831, -0.006511843763291836, -0.020460916683077812, 0.012327088043093681, 0.03425632789731026, 0.002347610192373395, 0.012033423408865929, 0.0009074580739252269, -0.008072364144027233, 0.001599789597094059, -0.0049342499114573, 0.00034915367723442614, -0.0035308056976646185, -0.007580646779388189, 0.0033242159988731146, 0.009991428814828396, 0.033245574682950974, -0.0033617778681218624, -0.026088351383805275, -0.003450560150668025, -0.004924005828797817, -0.009561176411807537, -0.012607093900442123, 0.004206917714327574, 0.014806164428591728, 0.004121549893170595, -0.010954376310110092, 0.022386811673641205, -0.000854956975672394, -0.014054928906261921, 0.01451932918280363, 0.026798611506819725, -0.002021505730226636, 0.007655770517885685, -0.013269546441733837, 0.04589364677667618, 0.008161147125065327, -0.004148867446929216, -0.0024159043096005917, -0.0033925101161003113, -0.025842493399977684, -0.030923575162887573, 0.010558269917964935, -0.0009270926238968968, -0.02455856464803219, 0.023561470210552216, 0.02248242311179638, 0.02297414094209671, 0.040730610489845276, 0.003235433716326952, -0.003599099814891815, -0.013139788061380386, 0.0061362264677882195, -0.020556528121232986, -0.0002748838160187006, -0.0013411256950348616, -0.013611016795039177, 0.002769326325505972, 0.004422043915838003, 0.01356321107596159, -0.0002215290442109108, 0.02484539896249771, -0.010073382407426834, 0.006737214513123035, 0.0009603860089555383, -0.01785208098590374, 0.005040105897933245, 0.005357673391699791, -0.012265623547136784, -0.021703869104385376, -0.027303988113999367, 0.008898722939193249, 0.0038586175069212914, -0.006993317510932684, 0.008857746608555317, -0.02901134081184864, -0.006184032186865807, -0.005784511566162109, 0.014765188097953796, -0.0034812926314771175, 0.021635575219988823, 0.008413835428655148, 0.020051151514053345, 0.020515551790595055, -0.013843216933310032, 0.031415294855833054, -0.010783640667796135, 0.005125473253428936, -0.006170373409986496, 0.00013445403601508588, 0.004760100040584803, -0.019600411877036095, -0.0040361820720136166, 0.03939204663038254, -0.0048591261729598045, -0.005876708775758743, -0.024408316239714622, 0.012750511057674885, -0.0022980968933552504, -0.02552833966910839, 0.0048010763712227345, -0.015939846634864807, -0.018179893493652344, -0.0028734749648720026, 0.01666376367211342, -0.005398649722337723, -0.017100846394896507, 0.04086719825863838, -0.0024380998220294714, -0.012490993365645409, -0.017783787101507187, -0.014437375590205193, 0.00799041148275137, 0.06490672379732132, 0.025678588077425957, -0.010817787609994411, 0.02364342287182808, -0.005518164485692978, 0.02780936472117901, -0.011780735105276108, -0.006791849620640278, 0.02922988124191761, -0.0009458735003136098, -0.001318076392635703, -0.008871405385434628, 0.014751529321074486, -0.018698928877711296, 0.02391659840941429, 0.011289017274975777, -0.002335658762603998, -0.020297011360526085, -0.005460114683955908, -0.00812016986310482, 0.0006321474211290479, -0.014573964290320873, 0.01068802922964096, 0.0055215791799128056, 0.017114505171775818, 0.005589873529970646, 0.018616976216435432, 0.029175246134400368, -0.005210841074585915, -0.024654176086187363, -0.0062045203521847725, -0.006259155459702015, 0.01517495233565569, -0.006518673151731491, -0.0018695513717830181, 0.022386811673641205, 0.017319386824965477, 0.01603545807301998, 0.005859635304659605, 0.0054430412128567696, 0.012245134450495243, 0.010455829091370106, -0.003636661684140563, -0.026156647130846977, 0.007375764660537243, -0.011459752917289734, -0.014683234505355358, 0.03267190605401993, -0.0011430727317929268, -0.013829558156430721, 0.02458588220179081, 0.0162540003657341, 0.005074252840131521, -0.00527572026476264, -0.000205736025236547, 0.02693519927561283, -0.0074577173218131065, 0.0032029941212385893, 0.010414852760732174, -0.025883469730615616, -0.005466944072395563, -0.020037492737174034, 0.0031944571528583765, 0.01449201162904501, -0.001244660234078765, -0.005750364623963833, -0.011446094140410423, -0.018630634993314743, -0.006689408794045448, -0.011528046801686287, -0.0008955065859481692, -0.02647079899907112, -0.011207064613699913, 0.007806017529219389, 0.03313630446791649, 0.008222611621022224, 0.020829705521464348, 0.005825487896800041, 0.0070274644531309605, 0.034939270466566086, -0.008154317736625671, -0.014970069751143456, 0.004661073442548513, -0.01436908170580864, -0.023042434826493263, 0.018958445638418198, -0.0018593072891235352, -0.013132958672940731, 0.011869517154991627, 0.008625546470284462, 0.013959317468106747, -0.00603719986975193, 0.02944842167198658, 0.004466434940695763, -0.012135864235460758, -0.0006402573199011385, 0.017893057316541672, 0.011965128593146801, -0.019395528361201286, -0.01183537021279335, 0.007969923317432404, -0.010408023372292519, -0.00263786013238132, -0.007853823713958263, 0.01451932918280363, 0.014614940620958805, 0.019805293530225754, 0.01253880001604557, -0.02526882290840149, 0.003517146920785308, 0.027700092643499374, -0.004439117386937141, 0.014970069751143456, -0.011009011417627335, 0.012224646285176277, 0.013843216933310032, -0.008639205247163773, -0.007368935272097588, -0.0053235264495015144, -0.015953505411744118, -0.020979952067136765, -0.0500459261238575, 0.014218835160136223, 0.021212153136730194, -0.01404127012938261, 0.02039262279868126, -0.005053764674812555, -0.007840164005756378, -0.020747752860188484, 0.012361234985291958, -0.00882359966635704, -0.002639567479491234, -0.0013018565950915217, -0.0138022406026721, -0.03357338532805443, -0.027044469490647316, -0.01645888201892376, 0.020966293290257454, 0.001000508782453835, -0.017496952787041664, -0.020474575459957123, 0.01830282248556614, 0.008297734893858433, -0.02505028061568737, -0.028656210750341415, -0.04911712929606438, 0.011671463958919048, 0.003254214534536004, 0.003674223320558667, 0.029557693749666214, -0.015338858589529991, 0.017537929117679596, -0.0068772174417972565, 0.014341764152050018, -0.02642982266843319, -0.022427788004279137, -0.028383035212755203, 0.0003877825220115483, 0.038381293416023254, 0.018248187378048897, 0.020365305244922638, 0.014068587683141232, 0.016868647187948227, -0.0005348282866179943, 0.00446984963491559, -0.008625546470284462, 0.003066405886784196, 0.012081229127943516, -0.018398433923721313, 0.013795411214232445, 0.01449201162904501, 0.015926187857985497, 0.0026583485305309296, 0.012928076088428497, -0.003981546964496374, 0.012559288181364536, 0.0040464261546730995, -0.0021802897099405527, -0.02786399982869625, -0.005306452978402376, -0.00422740587964654, -0.009356293827295303, -0.01995554007589817, 0.009991428814828396, 0.00937678199261427, -0.024203434586524963, 0.01905405893921852, 0.00916507001966238, 0.0017568661132827401, -0.029311833903193474, 0.024886375293135643, -0.009786547161638737, 0.021266788244247437, 0.01158268190920353, 0.0051596201956272125, -0.026866905391216278, -0.006624529138207436, 0.009021652862429619, -0.007519182283431292, 0.015011047013103962, -0.007225517183542252, 0.00776504073292017, -0.014423716813325882, -0.007485034875571728, 0.005873294081538916, 0.04122232645750046, -0.025569315999746323, -0.022373152896761894, 0.006771361455321312, -0.0417959988117218, -0.005524993874132633, -0.006501599680632353, -0.0096431290730834, -0.015693988651037216, 0.0012207572581246495, 0.01346077024936676, 2.3129296096158214e-05, 0.00041766121285036206, -0.005661582108587027, -0.0174150001257658, 0.006324035115540028, 0.019996516406536102, 0.029967457056045532, 0.0025678586680442095, 0.03518512845039368, 0.02156728133559227, -0.003517146920785308, -0.01950479857623577, 0.002188826445490122, -0.009486052207648754, -0.011685122735798359, 0.026743976399302483, 0.01486079953610897, 0.012327088043093681, -0.00719819962978363, -0.0028068881947547197, 0.004609852563589811, -0.009991428814828396, -0.009308488108217716, 0.03510317578911781, 0.017715493217110634, 0.016363270580768585, -0.027891317382454872, -0.03127870336174965, -0.01969602331519127, 0.026853246614336967, -0.004903517663478851, -0.0027915220707654953, -0.015215928666293621, -0.038627151399850845, -0.011234382167458534, 0.026375187560915947, -0.012122205458581448, 0.019368210807442665, 0.004121549893170595, -0.014068587683141232, 0.015147634781897068, -0.011753417551517487, -0.024886375293135643, 0.0008033095509745181, -0.002525174990296364, 0.027263011783361435, -0.024189775809645653, 0.018248187378048897, -0.0013300279388204217, 0.005309867672622204, -0.006327449809759855, -0.007799188140779734, 0.004613267723470926, 0.03171578794717789, -0.027754727751016617, -0.0009407514589838684, 0.0027624969370663166, 0.014983728528022766, 0.010278264060616493, -0.012217816896736622, -0.022386811673641205, -0.009226535446941853, -0.0029127439484000206, -0.02786399982869625, 0.010011916980147362, 0.00787431187927723, -0.0208979994058609, -0.011282187886536121, 0.008215782232582569, -0.0062079350464046, -0.006153299938887358, 0.0027744483668357134, 0.013617846183478832, -0.0007047099061310291, -0.013440282084047794, 0.016991576179862022, -0.001458932994864881, 0.003158602863550186, 0.027071787044405937, -0.012149523012340069, -0.010524122975766659, 0.023766351863741875, -0.020693117752671242, 0.0007324544130824506, 0.007997240871191025, 0.002272486686706543, -0.023807328194379807, 0.010305581614375114, -0.003841544035822153, -0.0036639792378991842, 0.020228717476129532, -0.019422845914959908, -0.011965128593146801, -0.004124964587390423, 0.007505523506551981, 0.016294976696372032, 0.002513223560526967, 0.006187446881085634, 0.013269546441733837, -0.008550423197448254, -0.0021068735513836145, -0.012777828611433506, -0.030104046687483788, -8.958267426351085e-05, -0.012525140307843685, -0.0023595616221427917, 0.020747752860188484, -0.0032132382038980722, -0.002653226489201188, -0.003872276283800602, 0.0073279584757983685, 0.013740776106715202, -0.024421975016593933, -0.033737294375896454, -0.02136239968240261, 0.020802387967705727, -0.01450567040592432, 0.000524157308973372, -0.020529210567474365, -0.001963455928489566, -0.022168269380927086, 0.0035888557322323322, -0.0009595323354005814, 0.00035384888178668916, 0.0027095689438283443, 0.014478352852165699, 0.013419793918728828, 0.023820986971259117, -0.008973847143352032, 0.0022946821991354227, -0.0138022406026721, -0.02132142335176468, -0.021676551550626755, -0.005975734908133745, 0.005005958490073681, 0.017920376732945442, 0.019627729430794716, -0.02184045873582363, -0.028656210750341415, -0.00800407025963068, -0.0398564450442791, -0.03256263583898544, -0.005630849860608578, 0.013085152953863144, -0.0007401374750770628, 0.006173788104206324, 0.009991428814828396, 0.005395235028117895, 0.006484526209533215, 0.01667742244899273, -0.04775124415755272, -0.0008784330566413701, 0.0049308352172374725, -0.02091165818274021, 0.022605352103710175, -0.023056093603372574, -0.020542869344353676, -0.02316536381840706, 0.0009535565623082221, 0.008256758563220501, 0.001225025742314756, 0.010988523252308369, 0.0005399503279477358, -0.0031534808222204447, -0.008844087831676006, 0.02223656326532364, 0.0065152584575116634, -3.3426767913624644e-05, 0.009383611381053925, -0.0185759998857975, -0.00952702946960926, 0.0032303116749972105, -0.0006718433578498662, -0.023943915963172913, 0.006272814702242613, 0.008946528658270836, 0.008202123455703259, 0.008024558424949646, 0.001253196969628334, -0.0050196172669529915, 0.0096431290730834, -0.020105788484215736, -0.005996223539113998, -0.011521217413246632, -0.01879454031586647, 0.014191517606377602, -0.024695152416825294, -0.01805696450173855, -0.012347576208412647, 0.010380705818533897, -0.02432636357843876, -0.010216799564659595, 0.027044469490647316, -0.00788797065615654, -0.014410058036446571, -0.01043534092605114, 0.022195586934685707, -0.03480268269777298, -0.008045046590268612, -0.0027249353006482124, -0.023056093603372574, -0.024039529263973236, 0.038162752985954285, 0.003179091028869152, -0.008263587951660156, -0.007724064402282238, 0.014751529321074486, 0.009766058064997196, -0.0040361820720136166, -0.004131793975830078, 0.013296863995492458, -0.022332176566123962, -0.013317352160811424, 0.008441152982413769, -0.018726246431469917, -0.023766351863741875, 0.003807396860793233, 0.011487070471048355, -0.002301511587575078, -0.0044083851389586926, 0.20783264935016632, -0.007075270172208548, 0.016568152233958244, 0.03982912749052048, -0.0006978805176913738, -0.0008438591612502933, 0.015502763912081718, 0.013269546441733837, -0.003889349987730384, 0.004964982159435749, -0.015188611112535, 0.011685122735798359, -0.01520226988941431, 0.00649135559797287, 0.010968035086989403, -0.007560158614069223, -0.03488463535904884, -0.00880994088947773, -0.016363270580768585, -0.006225008517503738, 0.023561470210552216, -0.011637317016720772, -0.011657805182039738, -0.011528046801686287, 0.02903865836560726, 0.0029793307185173035, -0.029120611026883125, 0.009950452484190464, 0.018603317439556122, 0.010271434672176838, -0.0001519544020993635, -0.004261552821844816, -0.004022523295134306, -0.0019941881764680147, -0.00812016986310482, -0.00013125276018399745, -0.007778699975460768, 0.024258069694042206, 0.02763179875910282, 0.02694885805249214, -0.0015391786582767963, -0.0010577051434665918, -0.007826505228877068, -0.003506902838125825, -0.02808254025876522, 0.014246152713894844, -0.008413835428655148, -0.0073279584757983685, 0.023138046264648438, 0.021676551550626755, -0.04796978831291199, -0.003872276283800602, 0.025350775569677353, 0.024900034070014954, 0.001281368313357234, 0.00725283520296216, 0.0026242013555020094, 0.012948564253747463, -0.0022895601578056812, -0.002159801311790943, -0.007464546710252762, 0.01899942383170128, 0.0011755124432966113, 0.01898576319217682, -0.024189775809645653, -0.004336676560342312, -0.03425632789731026, 0.005781096871942282, -0.00927434116601944, -0.008755305781960487, 0.0039030087646096945, -0.02067945897579193, -0.007526011671870947, 0.00028406083583831787, -0.030049411579966545, -0.04086719825863838, 0.023957576602697372, 0.026061033830046654, 0.014532987959682941, 0.022414129227399826, -0.006849899888038635, 0.00917872879654169, -0.006095249671489, -0.0022332174703478813, -0.006067932117730379, -0.016568152233958244, 0.03245336189866066, -0.005484017543494701, -0.0058425613678991795, -0.0038586175069212914, -0.005596702918410301, -0.015215928666293621, 0.004302529152482748, -0.0261839646846056, 0.00591085571795702, 0.017578905448317528, -0.015734964981675148, 0.01970968209207058, -0.010373876430094242, -0.0007870896952226758, -0.004282040987163782, -0.01807062327861786, 0.01786573976278305, -0.010305581614375114, -0.008400176651775837, 0.02413514070212841, 0.0029485984705388546, 0.009520200081169605, 0.005729876458644867, 0.006965999957174063, -0.012736852280795574, -0.01137096993625164, 0.012579776346683502, -0.011350481770932674, 0.033737294375896454, 0.009759228676557541, -0.002945183776319027, -0.029284516349434853, 0.017893057316541672, -0.000991118373349309, 0.007717235013842583, -0.018698928877711296, 0.002002724912017584, 0.017346706241369247, -0.01451932918280363, -0.021048245951533318, -0.020925316959619522, 0.0002612249809317291, 0.016595469787716866, -0.028601575642824173, -0.0034727556630969048, -0.00474985595792532, 0.00428545568138361, 0.003657149849459529, 0.008816770277917385, 0.002521760296076536, 0.004620097111910582, -0.01782476343214512, -0.001693694037385285, -0.009253853000700474, 0.0012933198595419526, 0.013904682360589504, 0.020693117752671242, 0.007293811533600092, 0.0034539748448878527, -0.002747130813077092, 0.0027215206064283848, -0.00019346442422829568, -0.02320634014904499, -0.0213487409055233, -0.018671611323952675, 0.000315220037009567, -0.018890151754021645, -0.006610870361328125, 0.04215112701058388, -0.017483294010162354, -0.03414705768227577, -0.027549846097826958, -0.010817787609994411, 0.013645164668560028, -0.022659987211227417, -0.014683234505355358, 0.023056093603372574, -0.011787564493715763, -0.041413553059101105, -0.00735527602955699, -0.1793130338191986, 0.027672775089740753, -0.0068772174417972565, 0.0008669084054417908, 0.018002329394221306, 0.0037083704955875874, 0.016773033887147903, 0.009636299684643745, -0.006375255528837442, 0.0020146763417869806, 0.02137605845928192, 0.0033634852152317762, -0.028929388150572777, -0.011009011417627335, 0.024217093363404274, -0.011459752917289734, -0.004766929429024458, 0.01596716418862343, 0.025227846577763557, 0.011022670194506645, 0.01648619957268238, -0.009895817376673222, 0.014177858829498291, -0.019900904968380928, 0.009977770037949085, 0.02343854121863842, -0.025828834623098373, 0.0013334426330402493, -0.011883175931870937, -0.016527175903320312, -0.014136881567537785, -0.01531154103577137, 0.011179747059941292, 0.011452923528850079, 0.012934905476868153, -0.007614793721586466, -0.014054928906261921, -0.01231342926621437, 0.0015664963284507394, 0.02365708164870739, 0.02153996378183365, 0.019532116129994392, 0.007730893790721893, 0.004596193786710501, 0.006303546950221062, 0.020092127844691277, 0.019832611083984375, -0.019436504691839218, 0.011958299204707146, -0.019354552030563354, 0.01969602331519127, -0.013733946718275547, -0.0067269704304635525, 0.028000587597489357, 0.00044561910908669233, 0.009574835188686848, -0.015516422688961029, -0.003517146920785308, -0.020761411637067795, -0.006395743694156408, -0.012613923288881779, -0.010872422717511654, 0.020310670137405396, -0.018876492977142334, -0.007409911602735519, -0.01693694107234478, -0.013761264272034168, -0.008051875978708267, -0.03297239914536476, 0.01671839877963066, -0.012149523012340069, -0.011384628713130951, 0.03201628103852272, -0.024080505594611168, 0.0003649466671049595, -0.0007794065750204027, -0.0181252583861351, 0.026334211230278015, 0.007819675840437412, -0.00203516473993659, -0.005606947001069784, 0.0208979994058609, -0.017551587894558907, -0.0004733635869342834, -0.005716217681765556, 0.012197328731417656, -0.006668920163065195, 0.00435375003144145, -0.027331305667757988, -0.030322587117552757, 0.01766085810959339, -0.01925894059240818, -0.003961058799177408, -0.02158094011247158, 0.021512646228075027, 0.03666028007864952, 0.011903664097189903, 0.004476679489016533, -0.0029161586426198483, 0.00383129995316267, -0.0019532116129994392, -0.002178582362830639, -0.02994013950228691, 0.01574862375855446, 0.035868071019649506, -0.002888841088861227, -0.006013297010213137, 0.019764317199587822, 0.018849175423383713, -0.0023407808039337397, -0.002226388081908226, 0.02067945897579193, 0.031032845377922058, 0.02901134081184864, -0.0074782054871320724, 0.0017082066042348742, 0.011432435363531113, -0.025596633553504944, 0.005658167414367199, -0.011883175931870937, 0.024927351623773575, 0.000780687085352838, 0.01996919885277748, 0.014655916951596737, -0.005197182297706604, -0.01111828163266182, -0.09987331181764603, -0.04414531588554382, 0.009738740511238575, 0.013481258414685726, 0.01532519981265068, 0.024941010400652885, -0.0050913263112306595, 0.023028776049613953, -0.012381723150610924, 0.03876373916864395, 0.002663470571860671, -0.02132142335176468, -0.0006795264198444784, 0.007867482490837574, 0.011671463958919048, -0.0004422043857630342, -0.027918634936213493, -0.011425605975091457, -0.02249608188867569, 0.013617846183478832, -0.013740776106715202, -0.0169642586261034, -0.010442170314490795, -0.019163329154253006, 0.00018546120554674417, 0.0009185558301396668, -0.03526708111166954, 0.02879279851913452, 0.036523692309856415, 0.007314299698919058, 0.03034990467131138, -0.021649233996868134, 0.021198494359850883, -0.03810811787843704, 0.00220589991658926, 0.00812016986310482, -0.018739905208349228, 0.006334279198199511, 0.019859928637742996, -0.02851962298154831, -0.001223318395204842, 0.00017286947695538402, 0.015625692903995514, -0.02991282194852829, 0.010892911814153194, -0.02203168161213398, 0.0003850080829579383, 0.0011183160822838545, 0.011814882047474384, -0.013283205218613148, -0.013010028749704361, 0.018958445638418198, -0.002152971923351288, -0.01484714075922966, 0.0069352672435343266, -0.020775070413947105, -0.014123222790658474, 0.005729876458644867, -0.018630634993314743, 0.004370823502540588, 0.006102079059928656, -0.019559435546398163, -0.0004686683532781899, 0.013385646976530552, 0.022386811673641205, 0.0019395528361201286, -0.014737870544195175, -0.0018285749247297645, 0.0047908322885632515, -0.018439410254359245, -0.0010816080030053854, -0.012409040704369545, -0.026784952729940414, 0.04619413986802101, -0.020993610844016075, -0.0008523959550075233, -0.019764317199587822, -0.02021505869925022, 0.013836387544870377, -0.032863128930330276, -0.00428545568138361, -0.03037722222507, 0.0013283205917105079, -0.008222611621022224, 0.012238305062055588, -0.005132302641868591, -0.003896179376170039, 0.007908458821475506, 0.005528408568352461, -0.034666091203689575, 0.03332752734422684, 0.0021444351878017187, 0.025910787284374237, -0.011985617689788342, -0.025132235139608383, -0.0020590675994753838, -0.011377799324691296, -0.0019941881764680147, 0.03196164593100548, -0.01740134134888649, -0.003537635086104274, -0.004080573562532663, -0.08047778904438019, 0.02110288105905056, -0.0025951764546334743, -0.004756685346364975, 0.014273470267653465, 0.0036537351552397013, 0.015926187857985497, -0.03685150295495987, -0.00644696457311511, -0.004155696835368872, -0.02204534038901329, 0.010701688006520271, -0.005876708775758743, -0.010141676291823387, -0.0021802897099405527, -0.024913692846894264, 0.019354552030563354, 0.005466944072395563, 0.03065039962530136, 0.01598082296550274, 0.012852952815592289, 0.011282187886536121, 0.019518457353115082, -0.0019497970351949334, -0.013153446838259697, 0.00766259990632534, -0.01670474000275135, 0.002653226489201188, 0.0019105279352515936, -0.020556528121232986, 0.008147488348186016, -0.006423061713576317, -0.014273470267653465, 0.042779434472322464, -0.0002806461416184902, -0.008031387813389301, -0.004643999971449375, 0.0010636808583512902, -0.004671317525207996, 0.0066552613861858845, -0.006624529138207436, -0.017278410494327545, 0.01790671795606613, -0.018015988171100616, -0.010892911814153194, -0.011930981650948524, -0.011009011417627335, 0.011691952124238014, 0.016363270580768585, -0.013153446838259697, 0.015939846634864807, 0.025842493399977684, -0.01647254079580307, -0.017770128324627876, -0.017975011840462685, -0.0546899288892746, 0.024080505594611168, -0.0018166234949603677, -0.006006467621773481, -0.016376929357647896, -0.0012181962374597788, 0.03313630446791649, 0.015625692903995514, -0.007273323368281126, 0.008113340474665165, -0.00858457013964653, -0.016527175903320312, -0.01531154103577137, 0.03245336189866066, -0.019846269860863686, 0.0010824616765603423, -0.0216082576662302, 0.01669108122587204, -0.01531154103577137, -0.003040795447304845, -0.0015033242525532842, -0.004784002900123596, -0.0035103175323456526, -0.008195294067263603, 0.025214187800884247, 0.013044175691902637, -0.004698635078966618, -0.03403778746724129, 0.017251092940568924, 0.016759375110268593, 0.017319386824965477, -0.024995645508170128, -0.008099681697785854, 0.013406135141849518, 0.012429528869688511, 0.014765188097953796, 0.01718279905617237, 0.006371840834617615, -0.001158438972197473, 0.013747605495154858, 0.00858457013964653, -0.009506541304290295, -0.014328105375170708, 0.010647052899003029, 0.024217093363404274, 0.01831648126244545, -0.012962223030626774, -0.014341764152050018, -0.01146658230572939, -0.0018183308420702815, 0.01193781103938818, 0.0041181351989507675, -0.020515551790595055, -0.011862687766551971, 0.021990705281496048, 0.021936070173978806, 0.005733291152864695, -0.0056035323068499565, 0.022919505834579468, -0.021239470690488815, 0.017483294010162354, -0.005900611635297537, -0.008372858166694641, -0.015475446358323097, 0.035130493342876434, 0.05258646979928017, 0.00776504073292017, 0.015379834920167923, -0.01288027036935091, 0.02110288105905056, 0.0138022406026721, 0.005419137887656689, -0.024312704801559448, 0.018671611323952675, -0.008475299924612045, 0.007963093928992748, -0.017975011840462685, -0.014751529321074486, -0.014259811490774155, -0.020829705521464348, -0.0005348282866179943, 0.014451034367084503, 0.02062482200562954, -0.019545776769518852, 0.07228249311447144, -0.007526011671870947, -0.004090817645192146, 0.0025576145853847265, 0.0063206204213202, 0.021007269620895386, 0.006487940903753042, 0.01693694107234478, -0.01927259936928749, -0.009895817376673222, 0.013064663857221603, 0.014956410974264145, 0.011876346543431282, -0.017783787101507187, -0.009479222819209099, -0.0034829999785870314, -0.0232199989259243, 0.008864575996994972, -0.022578034549951553, 0.0059040263295173645, 0.024981986731290817, 0.024503929540514946, 0.00872115883976221, -0.0059040263295173645, -0.027713751420378685, -0.010810958221554756, 0.010995352640748024, -0.0067167263478040695, -0.004603023175150156, -0.0412769615650177, 0.01949113979935646, -0.006050858646631241, -0.05329672619700432, -0.019108694046735764, 0.018507706001400948, -0.016581811010837555, -0.011275358498096466, 0.002998111769556999, 0.027782045304775238, -0.0013419793685898185, -0.02435368113219738, 0.02384830452501774, -0.021225811913609505, -0.017305728048086166, 0.02184045873582363, -0.012170011177659035, -0.013740776106715202, -0.0158032588660717, 0.0062762293964624405]\n" - ] - } - ], - "source": [ - "apple_embedding = get_embedding(\"apple\")\n", - "orange_embedding = get_embedding(\"orange\")\n", - "phone_embedding = get_embedding(\"phone\")\n", - "dog_embedding = get_embedding(\"I love dogs\")\n", - "animals_embedding = get_embedding(\"I love animals\")\n", - "cat_embedding = get_embedding(\"I hate cats\")\n", - "\n", - "print(apple_embedding)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Calculate Vector Similarities\n", - "To calculate the cosine similarity of the vectors, we also need the [SciPy](https://scipy.org/) package. SciPy contains many fundamental algorithms for scientific computing.\n", - "\n", - "Cosine similarity is used to measure the distance between two vectors. The closer the two vectors are, the higher the similarity between the embedded texts.\n", - "\n", - "πŸ‘‰ Import the SciPy package and define the method **get_cosine_similarity()**." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "from scipy import spatial\n", - "\n", - "# TODO the get_cosine_similarity function does not work very well does it? Fix it!\n", - "def get_cosine_similarity(vector_1, vector_2):\n", - " return 1 - spatial.distance.cosine(vector_1, vector_1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Calculate similarities between the embeddings of the words and phrases from above and find the most similar vectors. You can follow the example below." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "apple-orange\n", - "1.0\n" - ] - } - ], - "source": [ - "print(\"apple-orange\")\n", - "print(get_cosine_similarity(apple_embedding, orange_embedding))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[Next exercise](05-store-embeddings-hana.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test2/05-store-embeddings-hana.ipynb b/09_BusinessAIWeek/Test2/05-store-embeddings-hana.ipynb deleted file mode 100644 index 7c03951..0000000 --- a/09_BusinessAIWeek/Test2/05-store-embeddings-hana.ipynb +++ /dev/null @@ -1,345 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Store Embeddings for a Retrieval Augmented Generation (RAG) Use Case\n", - "\n", - "RAG is especially useful for question-answering use cases that involve large amounts of unstructured documents containing important information. \n", - "\n", - "Let’s implement a RAG use case so that the next time you ask about the [orchestration service](https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html), you get the correct response! To achieve this, you need to vectorize our context documents. You can find the documents to vectorize and store as embeddings in SAP HANA Cloud Vector Engine in the `documents` directory.\n", - "\n", - "## LangChain\n", - "\n", - "The Generative AI Hub Python SDK is compatible with the [LangChain](https://python.langchain.com/v0.1/docs/get_started/introduction) library. LangChain is a tool for building applications that utilize large language models, such as GPT models. It is valuable because it helps manage and connect different models, tools, and data, simplifying the process of creating complex AI workflows." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://israel-fsvdxbsq.authentication.eu11.hana.ondemand.com/oauth/token\n", - "sb-49ec08a9-d325-4480-9418-ad8801558203!b28574|aicore!b18\n", - "96d2ba69-3289-4190-ad82-c174e50f9f17$8C_adlgCYD6AscPgIKtLXJkIj1AL6i8p9Opw1JJZ0o8=\n", - "https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "grounding\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"grounding\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "#!pip show generative-ai-hub-sdk" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "#!pip install \"generative-ai-hub-sdk[all]\"" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.proxy.core.proxy_clients import get_proxy_client\n", - "from gen_ai_hub.proxy.langchain.init_models import init_embedding_model" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "import warnings\n", - "warnings.filterwarnings(\"ignore\", message=\"As the c extension\") #Avoid a warning message for \"RuntimeWarning: As the c extension couldn't be imported, `google-crc32c` is using a pure python implementation that is significantly slower. If possible, please configure a c build environment and compile the extension. warnings.warn(_SLOW_CRC32C_WARNING, RuntimeWarning)\"\n", - "\n", - "# OpenAIEmbeddings to create text embeddings\n", - "from gen_ai_hub.proxy.langchain.openai import OpenAIEmbeddings\n", - "\n", - "# TextLoader to load documents\n", - "from langchain_community.document_loaders import PyPDFDirectoryLoader\n", - "\n", - "# different TextSplitters to chunk documents into smaller text chunks\n", - "from langchain_text_splitters import RecursiveCharacterTextSplitter\n", - "\n", - "# LangChain & HANA Vector Engine\n", - "from langchain_community.vectorstores.hanavector import HanaDB" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Change the `EMBEDDING_DEPLOYMENT_ID` in [variables.py](variables.py) to your deployment ID from exercise [01-explore-genai-hub](01-explore-genai-hub.md). For that go to **SAP AI Launchpad** application and navigate to **ML Operations** > **Deployments**.\n", - "\n", - "☝️ The `EMBEDDING_DEPLOYMENT_ID` is the embedding model that creates vector representations of your text e.g. **text-embedding-3-small**.\n", - "\n", - "πŸ‘‰ In [variables.py](variables.py) also set the `EMBEDDING_TABLE` to `\"EMBEDDINGS_CODEJAM_>add your name here<\"`, like `\"EMBEDDINGS_CODEJAM_NORA\"`.\n", - "\n", - "πŸ‘‰ In the root folder of the project create a [.user.ini](../.user.ini) file with the HANA login information provided by the instructor.\n", - "```sh\n", - "[hana]\n", - "url=XXXXXX.hanacloud.ondemand.com\n", - "user=XXXXXX\n", - "passwd=XXXXXX\n", - "port=443\n", - "```" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "EMBEDDING_DEPLOYMENT_ID = \"d1ae8a45e3d60115\"" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "def get_hana_connection():\n", - " conn = dbapi.connect(\n", - " address='ec41b786-96de-467b-9ff5-db725945f89c.hna0.prod-us10.hanacloud.ondemand.com',\n", - " port='443',\n", - " user='DBADMIN',\n", - " password='9hEW4UK86Fdt',\n", - " encrypt=True\n", - " )\n", - " return conn" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "#!pip install hdbcli" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "from hdbcli import dbapi" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Chunking of the documents\n", - "\n", - "Before you can create embeddings for your documents, you need to break them down into smaller text pieces, called \"`chunks`\". You will use the simplest chunking technique, which involves splitting the text based on character length and the separator `\"\\n\\n\"`, using the [Character Text Splitter](https://python.langchain.com/v0.1/docs/modules/data_connection/document_transformers/character_text_splitter/) from LangChain.\n", - "\n", - "## Character Text Splitter" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "#!pip install pypdf" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Number of document chunks: 59\n" - ] - } - ], - "source": [ - "\n", - "# Load custom documents\n", - "loader = PyPDFDirectoryLoader('documents/')\n", - "documents = loader.load()\n", - "text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)\n", - "texts = text_splitter.split_documents(documents)\n", - "print(f\"Number of document chunks: {len(texts)}\")\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now you can connect to your SAP HANA Cloud Vector Engine and store the embeddings for your text chunks." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Table EMBEDDINGS_CODEJAM_Testing created in the SAP HANA database.\n" - ] - } - ], - "source": [ - "import importlib\n", - "#variables = importlib.reload(variables)\n", - "\n", - "# DO NOT CHANGE THIS LINE BELOW - It is only to check whether you changed the table name in variables.py\n", - "#assert variables.EMBEDDING_TABLE != 'EMBEDDINGS_CODEJAM_ADD_YOUR_NAME_HERE', 'EMBEDDING_TABLE name not changed in variables.py'\n", - "\n", - "# Create embeddings for custom documents\n", - "embeddings = OpenAIEmbeddings(deployment_id=EMBEDDING_DEPLOYMENT_ID)\n", - "\n", - "db = HanaDB(\n", - " embedding=embeddings, connection=get_hana_connection(), table_name=\"EMBEDDINGS_CODEJAM_\"+\"Testing\"\n", - ")\n", - "\n", - "# Delete already existing documents from the table\n", - "db.delete(filter={})\n", - "\n", - "# add the loaded document chunks\n", - "db.add_documents(texts)\n", - "print(f\"Table {db.table_name} created in the SAP HANA database.\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Check the embeddings in SAP HANA Cloud Vector Engine\n", - "\n", - "πŸ‘‰ Print the rows from your embedding table and scroll to the right to see the embeddings." - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'VEC_TEXT': '/Examples/Orchestration Service\\nOrchestration Service\\nThis notebook demonstrates how to use the SDK to interact with the Orchestration\\nService, enabling the creation of AI-driven workflows by seamlessly integrating various\\nmodules, such as templating, large language models (LLMs), data masking and content\\nfiltering. By leveraging these modules, you can build complex, automated workflows that\\nenhance the capabilities of your AI solutions. For more details on configuring and using',\n", - " 'VEC_META': {'producer': 'Skia/PDF m131',\n", - " 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',\n", - " 'creationdate': '2025-02-02T21:04:28+00:00',\n", - " 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation',\n", - " 'moddate': '2025-02-02T21:04:28+00:00',\n", - " 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf',\n", - " 'total_pages': 14,\n", - " 'page': 0,\n", - " 'page_label': '1'},\n", - " 'VEC_VECTOR': '[-0.03516071,-0.002913306,-0.029698044,0.00420445,-0.010495528,0.015805682,-0.012602951,0.0023136607,-0.016540507,-0.02139313,0.032082763,-0.009310101,-0.008547546,-0.009518071,-0.022543894,0.002800656,0.01622162,-0.012353389,0.015847275,-0.021323806,-0.030003067,0.015736358,0.004440149,-0.0066446257,0.0067451443,0.004620389,0.014668782,-0.037600886,0.016748477,0.0030016932,0.021115836,-0.012325659,-0.037656344,-0.022835052,0.01586114,0.002911573,0.004831825,-0.0004973937,-0.0016342937,-0.0012434843,0.019646185,0.018828172,-0.0026654757,0.003947954,-0.010530189,0.03366333,-0.030779487,-0.028103612,0.0030918133,-0.0016108971,0.019341163,0.030030796,-0.020159177,-0.01493221,0.012991161,0.015375879,-0.016027516,0.014738105,0.037711803,-0.028242258,-0.009746837,0.011570036,-0.007916706,-0.024789965,-0.022987561,-0.01193745,-0.006876858,0.017497167,0.008582208,-0.011757209,0.026966712,0.0236392,-0.031999573,-0.029282106,0.025788218,-0.028921625,-0.031833198,-0.0046273214,-0.003086614,0.0027503967,0.0020900934,-0.012020637,0.010765888,0.045060057,0.017136687,0.021850662,-0.002998227,0.018897494,0.002100492,-0.0051992373,0.025039528,-0.010141979,0.008117743,0.009947875,-0.0009921878,0.009296237,-0.00538641,0.031944115,-0.006336137,-0.026744878,-0.0055909134,0.011798803,-0.039819226,-0.007979097,-0.059507005,-0.015972057,0.017011905,-0.008894162,0.024512671,0.020242365,-0.012159283,0.02914346,-0.010211302,-0.03937556,-0.00757009,-0.0063014757,0.0018422632,-0.019729374,-0.039819226,-0.0032789859,0.0029445016,0.049801763,0.01588887,-0.018980684,0.02289051,0.0024384423,0.02172588,-0.028699791,0.008242524,-0.013358574,0.03186093,-0.004495607,0.008644599,0.00038582674,-0.008963485,0.0067139487,-0.02686966,0.009310101,-0.031056779,0.009691379,0.00019442981,0.011147165,0.0037365188,-0.0019757103,0.0011169695,0.00879711,0.016387997,0.0058508753,-0.003325779,0.0034314967,0.0041281944,-0.028325446,-0.0012382851,-0.0069461814,0.0086792605,0.0072096093,0.01774673,0.020713761,-0.015251096,-0.0040103453,-0.0020675634,0.030335817,-0.0030311556,-0.014710376,0.04239805,-0.008173201,0.019105464,0.00058708055,-0.012124622,0.011368999,0.007681007,0.034273375,-0.021739746,0.012353389,0.011244218,-0.01780219,0.024734506,-0.029337564,-0.013441762,-0.016956447,0.01891136,0.0011568303,0.025788218,0.014128061,-0.025538655,-0.010058791,-0.006190559,-0.0073621203,0.018939089,-0.0015719028,-0.027008306,0.035632107,0.0016533575,0.011590834,-0.6277906,-0.04650198,-0.00012304862,-0.024221515,0.024429483,0.0061836266,-0.008769381,0.0023587206,-0.007445308,0.02211409,-0.0011672288,-0.00033253458,-0.011861194,0.0062910775,0.011902788,-0.025081122,0.013850769,-0.024346296,-0.01792697,0.007507699,-0.011771074,0.0074591725,-0.020575115,-0.013303116,-0.0071194894,0.0013180067,0.017192146,0.013095146,0.00745224,0.02007599,0.0055493196,-0.007507699,0.013781445,0.01307435,0.04156617,0.020381011,-0.027784726,0.014252843,0.010426205,0.04081748,-0.0071888124,-0.00011470818,0.021379264,0.0047521032,0.012651478,0.0077641946,0.026495315,-0.015777953,-0.019978937,-0.018717255,0.027479703,-0.005008599,0.022516165,0.0020519658,-0.004422818,0.004603058,0.02438789,-0.012110758,0.021129701,0.0034055007,-0.0012486835,0.010460866,-0.023750117,-0.008041487,-0.022751864,0.018758848,0.0019722441,-0.0005979123,-0.021476317,-0.0324987,0.0352439,0.020339416,0.009490342,-0.0142805725,0.021309942,0.023070749,0.010648038,0.021947715,0.0058127474,-0.0034678914,0.020422604,0.0019393156,-0.0009913213,-0.02872752,-0.00016041815,0.016498914,-0.025871405,-0.0034401622,0.016346402,-0.0037815787,0.03618669,0.013878498,0.002060631,-0.044339098,0.008845636,-0.008866433,0.0040380745,0.027146952,0.013802242,-0.008360374,0.010689632,0.0112927435,0.014225114,0.008894162,0.041926652,0.0070536323,-0.027521297,0.013095146,0.022807322,-0.021850662,0.0060969726,-0.016568236,-0.018855901,0.01663756,0.016152298,-0.024055138,0.011313541,-0.012623749,0.027867913,-0.016651426,-0.013122875,-0.0067555425,0.015403608,-0.003525083,0.016720748,0.018592473,-0.0027209343,-0.0381832,0.0042599086,-0.008339577,0.0011472984,-0.028256122,0.0026342804,-0.007722601,0.035798483,-0.00011394996,0.043257657,-0.005372545,0.019673916,-0.012755463,-0.018398369,-0.01588887,0.017192146,-0.011507645,-0.0061420323,-0.0024679047,-0.024013545,-0.001973977,-0.0074730376,0.004138593,-0.00082147954,-0.022932103,-0.00012055732,0.012020637,0.010807482,-0.017843783,-0.0074106464,-0.024914745,-0.0057260934,-0.020963326,-0.008804042,0.023694659,-0.026966712,0.009448747,-0.009448747,-0.0057330257,-0.024554266,0.01741398,-0.03549346,-0.028505687,0.0086792605,-0.016055245,-0.0168178,-0.0048006293,0.006200957,-0.016235486,-0.03144499,0.007916706,0.045670103,-0.0036325338,-0.0071472186,-0.0134902885,-0.027036035,-0.023348043,0.03438429,0.00028140872,0.037545428,0.0130466195,-0.0015987655,0.00891496,-0.028810708,0.016623694,0.010904534,0.019798696,-0.0014497207,0.013005026,-0.018439962,0.019604592,0.011507645,0.0038023756,0.033524685,0.02064444,0.015320419,-0.010786685,-0.0079236375,-0.013933957,-0.0095042065,-0.017816054,0.042481236,0.010211302,0.040623374,-0.017455574,0.0006828332,-0.02839477,-0.007057098,0.0008058818,0.02519204,-0.015528389,-0.039846957,0.0009765901,0.013635867,-0.013906227,0.011244218,-0.03219368,-0.0046342537,0.004467878,0.017649679,0.031999573,0.020283958,0.0020727627,-0.012776259,0.031195424,0.027964965,-0.007285865,-0.00240898,-0.008651531,0.0053552147,-0.006762475,0.018772714,-0.032997828,0.0088872295,-0.015819546,0.028380904,-0.024554266,0.02839477,-0.011452187,0.02549706,0.020907868,0.012665343,-0.006280679,-0.030474463,-0.0006785005,-0.023278719,0.012526697,0.0010693099,0.0062460173,-0.021268347,-0.014544,0.0016602898,0.012242471,-0.019424351,0.019396622,0.025899135,0.012020637,-0.0072927973,-0.032997828,-0.0062702806,0.0118057355,-0.021836797,0.0017781393,0.012727734,-0.015389743,-0.00022205077,-0.030945862,0.014710376,-0.0048248926,0.015625441,0.0199928,0.009878552,0.01911933,-0.010315287,-0.026301209,0.01352495,-0.011167962,0.00966365,-0.016859394,-0.023278719,0.0119443815,-0.02010372,0.018842036,0.018121075,0.02573276,-0.006436656,0.009421018,-0.001609164,0.0023153936,0.03629761,-0.020062124,0.0029375693,-0.007944435,0.029198918,0.0073413234,0.0045857276,-0.013379371,0.032082763,-0.0072442708,-0.018939089,-0.00918532,-0.004748637,-0.053877965,0.019424351,0.0013110744,-0.01720601,-0.02875525,0.0236392,-0.0103638135,-0.0075839544,-0.007549293,0.019424351,0.036879923,0.013164469,-0.015625441,0.0022495368,0.01666529,0.040567916,0.020242365,0.0106827,0.010654971,-0.04533735,-0.02343123,-0.027451973,-0.016235486,0.015223367,0.003043287,0.00065293757,0.00037672807,0.024360161,0.010447001,0.010634174,0.00459266,0.0038890296,-0.011258082,0.0035268161,-0.044311367,0.014530135,0.009705244,-0.0005697498,0.0076602097,0.02211409,0.007937502,0.0102598285,0.009594326,-0.031112237,-0.045586914,0.002166349,0.012505899,0.004360427,0.027493568,-0.013684393,0.03205503,0.015126315,0.029642586,-0.009677514,0.014051806,-0.0016325606,-0.007771127,-0.016124569,-0.0057919505,-0.003566677,-0.026966712,-0.025871405,0.049690846,-0.018356774,-0.0088179065,-0.00813854,0.011576969,-0.02764608,-0.006370799,0.014308302,0.013011958,-0.012173148,-0.010245964,0.0055007935,-0.049469013,-0.043229926,-0.010821346,-0.006856061,0.0061385664,-0.018148804,-0.011597766,-0.0156809,0.022668675,-0.0143498955,0.0021507512,0.011590834,0.011985975,-0.03699084,0.0067312797,0.05177054,0.026023917,0.004994734,-0.019937342,-0.0009817893,0.009705244,0.012859447,-0.010453934,-0.0026654757,-0.027257869,-0.021947715,-0.004814494,-0.006391596,-0.009836958,0.0022183412,0.03260962,-0.007875112,0.0030883471,0.014460812,0.0053101545,0.020921731,0.02777086,0.00972604,0.00891496,-0.0037815787,-0.030197171,-0.010128114,-0.031944115,0.008353442,-0.014849022,0.009601259,0.009254643,0.0011637626,0.011674021,-0.00572956,0.004176721,0.028505687,-0.0040415404,0.005365613,-0.009268507,0.02456813,-0.0033136474,0.00083534414,-0.0168178,0.017344655,-0.0065337084,0.0095042065,-0.025205903,0.018245857,0.012457373,0.0012486835,0.0102598285,0.008152405,-0.009102132,-0.022419112,0.004533735,-0.0025077655,-0.005452267,-0.0013258056,-0.00299996,0.0016230287,-0.008249457,-0.007355188,0.0043950886,-0.0033916358,-0.024873152,0.0003366506,-0.010100385,0.013476424,-0.008894162,-0.012561358,-0.034301102,0.005192305,0.02821453,-0.006769407,0.025635706,0.015875004,0.01852315,-0.023611471,-0.024096733,-0.010661903,-0.012554426,-0.029365294,-0.009649785,0.033081014,0.03549346,0.04910853,-0.008547546,0.02384717,-0.0018422632,-0.0006321406,-0.031916387,0.026467584,-0.0011602965,-0.003438429,-0.0072927973,0.013164469,-0.008582208,0.01660983,0.012935703,-0.0027642613,0.013753716,0.012554426,-0.024221515,-0.014807428,-0.04381224,-0.012304862,0.020173041,-0.007902841,-0.008630734,-0.029975338,-0.0015060458,0.02800656,0.010093453,0.0111333,0.0023951153,0.0095458,-0.0067832717,0.015056992,0.007681007,0.009289305,-0.03599259,-0.0021628828,-0.02906027,0.006505979,0.018897494,0.0088595,0.0075215637,0.02947621,0.008291051,0.0039444882,0.025330685,0.007025903,-0.014502406,-0.010807482,-0.030945862,-0.015694765,-0.046002854,0.007382917,-0.012998094,-0.012096892,-0.001577102,0.0012062232,0.018980684,-0.03366333,-0.014037942,0.022169549,-0.017968565,0.03549346,-0.0032252604,0.02492861,-0.002998227,0.009053606,-0.014190452,0.0016264948,-0.01852315,-0.0030207569,0.0043742917,0.027036035,0.0022270067,-0.042065296,0.015126315,-0.025469331,-0.01259602,-0.03100132,0.012623749,0.012547493,0.0037954433,-0.01963232,-0.018273586,-0.0112234205,0.03618669,-0.008942689,0.0051853727,0.013746784,0.013109011,0.015611577,-0.024276974,-0.005313621,0.017580355,0.009129861,0.010121183,-0.012415779,0.0018855901,-0.011861194,-0.0039791497,-0.0015173109,0.0056151766,-0.0063846637,0.0071680155,0.037379052,0.015154044,0.009219981,0.024970206,0.013483356,0.037184946,-0.023958087,-0.024096733,0.0028145206,-0.019507539,0.011507645,-0.02115743,-0.0118057355,0.012138487,-0.0036013385,-0.0002619116,0.012706936,0.0006823999,0.013185266,-0.013857701,0.0041489913,-0.029559398,0.013788378,-0.00288731,-0.0009601259,-0.03205503,-0.02366693,-0.016180027,-0.023556013,0.02232206,0.00604498,-0.028782979,0.008519817,0.020491928,0.0004321866,0.0039236913,0.005601312,0.011798803,-0.0016550906,0.005625575,-0.042425778,0.00054375356,0.021850662,-0.0052235005,-0.031056779,-0.02441562,-0.015805682,0.025094986,0.005324019,-0.008187066,0.011098639,-0.0005853475,0.002580555,0.021878392,-0.0049150125,0.032692805,0.00309008,-0.025829813,0.041399796,-0.026079375,0.008630734,0.0028041222,0.0075631575,-0.0014107265,-0.0190916,0.004967005,0.016415726,0.013753716,0.00618016,0.009774567,0.0017096826,-0.0030814148,-0.017580355,-0.027659943,0.0064713177,-0.020561252,0.010814414,-0.0030294224,0.03252643,-0.0042425776,-0.011507645,0.008166269,-0.028228393,-0.009649785,-0.02743811,-0.00858914,0.005143779,0.013656664,0.01511245,-0.018384503,-0.02612097,-0.039431017,-0.014446948,-0.012852515,-0.027244005,0.003129941,0.0048976815,0.036075775,0.019077735,0.009005079,-0.004544134,0.005479996,-0.0063014757,-0.010793617,0.009088268,-0.018426098,0.0027192011,-0.0048699523,-0.018869765,-0.007202677,-0.0021074242,-0.0067312797,-0.0039098267,0.023320314,-0.00003176409,-0.034467477,-0.016762342,0.0016594233,-0.01947981,0.00634307,0.01343483,-0.017219875,0.005074456,-0.01624935,-0.014183519,0.006894189,0.016526643,0.018980684,-0.0033881697,0.005313621,0.00034336632,0.009261575,-0.020713761,0.004939276,0.03740678,-0.007986029,0.013913159,0.026439855,0.0063846637,0.0023725855,0.010037995,0.025108851,-0.0072165416,-0.02774313,0.042619884,-0.021531776,-0.012138487,-0.002171548,-0.014238979,-0.002452307,-0.024914745,0.010779752,0.0143498955,-0.019355029,0.009136793,-0.014696511,-0.03513298,0.010086521,-0.007445308,-0.024041275,-0.020464199,-0.03244324,0.033552412,-0.013420965,-0.029198918,-0.008790177,-0.00083361106,-0.03518844,0.010252897,0.009760702,-0.0027244005,0.007916706,0.16060792,0.0010173175,-0.025483197,-0.00058274786,-0.018647932,0.021892255,0.002715735,0.004311901,-0.024637453,0.021656558,0.0065163774,0.016387997,0.0008808375,-0.0058751386,0.007791924,-0.036103506,-0.03028036,-0.017871512,0.0078196535,-0.027937235,0.015070857,-0.03066857,0.00084964203,-0.018176535,0.009525003,-0.009566597,-0.01699804,0.008651531,0.0058335443,0.018301316,-0.023056885,0.040457,0.00028162537,0.015486795,0.017774459,-0.015708629,0.007403714,0.006845663,0.008603005,0.012991161,0.0030640839,-0.005275493,0.025219768,-0.021129701,-0.005535455,0.013316981,-0.025455467,-0.02947621,0.030419005,0.0064643854,-0.029115729,0.013303116,0.029282106,0.013233792,0.025094986,0.019022277,-0.0034644252,0.03518844,0.019396622,0.026190292,-0.033912893,0.011237285,-0.013081281,-0.019036142,-0.013268454,0.0063188067,-0.011029316,-0.007396782,0.0010407141,0.0059617925,0.006630761,-0.0066030314,-0.003504286,-0.0039722174,-0.020533523,-0.031334072,0.03319193,0.02312621,0.017275333,0.026952848,-0.0035389476,-0.01307435,-0.014107265,-0.0014904481,-0.022876645,-0.027382651,0.01738625,-0.022391383,-0.031139966,0.019562997,-0.014266708,-0.02417992,-0.016859394,-0.023833305,-0.0019722441,0.0005745157,0.034107,0.006828332,-0.0061558974,-0.004377758,-0.03402381,0.07747557,-0.011119436,0.004200984,-0.018564744,-0.0110847745,0.0042113825,0.013386304,0.0012781459,-0.00924771,-0.018869765,-0.03815547,0.008464359,-0.008367307,0.0035198838,0.017081229,-0.018828172,0.0030328885,0.011126368,-0.0076532774,-0.015667036,-0.006918452,-0.011362067,0.026509179,-0.02232206,-0.009129861,-0.010315287,0.0086099375,-0.010890669,-0.022377519,0.006856061,0.006675821,0.0008145472,-0.0068699257,0.008415832,-0.0015649705,0.01588887,0.002301529,-0.025455467,0.0049739373,0.0199928,0.012582155,0.009788431,0.005632507,0.016693018,-0.009351695,0.019618455,-0.0004003413,-0.004311901,-0.04530962,-0.009497274,-0.012547493,0.0071888124,-0.011632428,0.017843783,-0.023930358,-0.01738625,-0.013122875,0.0062286863,0.0010736425,-0.0559022,0.0044609457,0.014134994,-0.0047867647,-0.014266708,-0.017940836,-0.17735639,0.0047521032,0.011840397,-0.050772287,0.029587127,-0.008998147,0.00029202385,0.011680953,-0.016679155,0.011715615,0.014252843,0.002493901,-0.020311687,-0.022904374,-0.015625441,0.022779593,-0.0027486635,0.004031142,0.039597392,0.0061662956,0.05282425,-0.007979097,0.001947981,-0.014051806,-0.012332591,-0.015569983,-0.02214182,0.010509392,-0.0039548865,-0.04508779,-0.0034921544,0.0104331365,0.003065817,0.013330845,0.007875112,0.016623694,0.032221407,-0.016041381,-0.025150444,0.021559505,0.024221515,0.043562677,-0.0015926998,0.026384396,0.018245857,0.011972111,0.020672169,-0.017164417,-0.00032625216,-0.008332645,0.0284225,-0.023556013,-0.02020077,-0.0050883205,-0.013608137,0.012693072,0.0012070896,0.01630481,-0.018606339,-0.030141713,-0.007701804,-0.008644599,0.005493861,-0.02376398,-0.02343123,0.02172588,-0.013719055,0.013788378,-0.017899241,0.020339416,0.00745224,0.0047243736,-0.012575222,0.0035597447,0.027299464,0.00793057,-0.011244218,0.0313618,0.014987669,0.0019861087,0.018453827,0.035798483,-0.0042772396,0.011888923,-0.0020970257,0.01857861,-0.0111540975,0.01077282,-0.010648038,0.012547493,0.03285918,-0.026162563,0.0040866006,-0.005081388,0.020838544,0.006585701,0.022835052,0.0029011746,0.007972164,0.014849022,0.0005290224,0.022252737,0.0225023,0.004852622,0.040346082,0.0040831347,0.009628988,-0.007507699,0.0202285,-0.012443508,-0.02989215,-0.0017712069,0.023306448,0.022668675,0.0037191878,0.04536508,-0.005428004,0.00747997,0.001347469,0.017635813,0.03441202,0.026439855,-0.016041381,0.0012902775,-0.0055874474,-0.027174681,-0.09317034,-0.018329045,-0.011320473,0.028977083,-0.024512671,0.00634307,-0.008804042,0.02668942,0.0060311155,0.014225114,-0.003947954,-0.0281868,-0.0003366506,-0.0035060192,-0.004710509,-0.019798696,-0.006835264,-0.021795204,0.013268454,0.026509179,0.0075839544,0.009005079,0.006720881,-0.02761835,-0.013455627,-0.011410593,-0.05337884,0.011535374,0.023001427,0.0029237045,0.029198918,-0.013095146,0.000733959,-0.017663542,-0.0062425514,-0.0026654757,-0.011653224,-0.018329045,0.03790591,-0.025566384,0.010252897,-0.0024904348,-0.0011758942,-0.021212889,-0.008034555,0.0119790435,-0.026176428,-0.015181773,0.00041247284,-0.01220781,-0.0056151766,-0.009698311,-0.026980577,0.010495528,0.028977083,0.020533523,0.009913213,-0.0030450202,-0.0105024595,-0.016318673,0.021559505,-0.0076047513,-0.0019791764,0.042813987,-0.008824839,-0.0032495235,-0.027119223,-0.02537228,-0.004991268,-0.013788378,0.003259922,0.005320553,-0.019036142,0.014065671,-0.007701804,-0.008478223,0.0016264948,-0.034938876,-0.003110877,-0.00022876645,-0.0135596115,-0.02172588,0.005538921,-0.013330845,-0.0048491554,0.01086294,-0.025081122,0.017524896,0.007161083,-0.03180547,0.016138433,0.03632534,0.013892363,-0.018176535,0.01855088,-0.007396782,-0.033469226,-0.01873112,-0.0045302687,0.03740678,-0.019673916,-0.01098079,-0.080193035,0.017316926,0.017261468,-0.0105717825,-0.009677514,-0.009282372,-0.006963512,-0.024942474,0.0049358094,0.014266708,-0.04264761,0.015070857,-0.014322166,0.006526776,0.0010476464,-0.014682647,0.046612896,-0.022460707,0.016803935,0.0047001108,-0.0168178,0.002779859,0.014474677,0.028172934,-0.0065129115,-0.004031142,-0.031999573,0.032249138,-0.001991308,-0.035299357,0.002299796,-0.027632214,-0.006942715,0.037268136,-0.013622003,0.00020082055,0.034190185,0.012589087,0.01984029,0.013913159,-0.02379171,-0.05717775,0.037628613,-0.026467584,-0.011299676,-0.018259723,0.006505979,-0.00007094793,0.0055527855,0.001345736,0.015056992,0.017289197,-0.051437788,-0.015999787,0.0012556158,-0.0066203624,0.016207756,-0.0129772965,0.014599459,0.006304942,0.034134727,0.0011507645,0.015445202,-0.0009211316,0.015154044,0.00661343,-0.0144885415,0.010024129,0.0170535,-0.0140934,-0.015306555,-0.010121183,-0.0059860554,0.0039306236,0.008325713,-0.030529922,0.004055405,0.01274853,-0.018661797,0.025829813,0.014391489,0.020339416,-0.009240778,0.03593713,-0.00172788,0.005140313,-0.028977083,-0.015070857,-0.003485222,-0.016790072,-0.021670422,-0.01349722,-0.010391543,0.015015398,0.02741038,0.0012677474,0.006589167,-0.01143139,0.025621843,0.010252897,0.014973804,0.01774673,-0.0005545853,-0.025996188,-0.0043742917,0.01514018,-0.01857861,-0.023403501,0.017233739,-0.0112234205,-0.013032755,0.0016767541,-0.0018925224,0.004516404,-0.019798696,0.018481556,0.0039514205,-0.025954593,-0.043285385,0.020464199,0.01529269,0.013601205,0.029198918,0.006894189,0.016166162,0.01699804,0.004571863,-0.016471185,0.010994654,0.0040172776,0.00022573356,-0.0071194894,-0.026925119,-0.00936556,-0.016554372,-0.01529269,-0.031223154,0.0063604005,0.015320419,0.06555199,0.0011187026,-0.0105717825,0.0030138246,-0.03405154,0.013109011,-0.0072581354,0.0059063337,-0.025649572,-0.022696404,-0.0012668809,-0.0019115863,-0.008457426,-0.011646292,-0.02462359,0.01687326,-0.019549133,0.025594113,-0.002913306,-0.012110758,0.027244005,0.00849902,0.007937502,-0.007992961,0.0014419218,-0.00017374118,0.028200664,-0.0020103718,-0.018647932,-0.020783085,0.034522936,-0.005102185,-0.031084508,-0.014710376,0.012332591,0.019230247,-0.0019549134,-0.032387782,0.021420859,0.013337777,-0.014987669,0.012644545,-0.026107104,-0.026744878,0.003812774,0.0032269934,0.0038231725,-0.016914852,-0.012110758]'}" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "from IPython.display import Markdown\n", - "cursor = db.connection.cursor()\n", - "\n", - "# Use `db.table_name` instead of `variables.EMBEDDING_TABLE` because HANA driver sanitizes a table name by removing unaccepted characters\n", - "is_ok = cursor.execute(f'''SELECT \"VEC_TEXT\", \"VEC_META\", TO_NVARCHAR(\"VEC_VECTOR\") FROM \"{db.table_name}\"''')\n", - "record_columns=cursor.fetchone()\n", - "if record_columns:\n", - " display({\"VEC_TEXT\" : record_columns[0], \"VEC_META\" : eval(record_columns[1]), \"VEC_VECTOR\" : record_columns[2]})\n", - "cursor.close()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[Next exercise](06-RAG.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test2/06-RAG.ipynb b/09_BusinessAIWeek/Test2/06-RAG.ipynb deleted file mode 100644 index d9b3679..0000000 --- a/09_BusinessAIWeek/Test2/06-RAG.ipynb +++ /dev/null @@ -1,309 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Implement the Retrieval for a Retrieval Augmented Generation (RAG) Use Case\n", - "\n", - "Now that you have all your context information stored in the SAP HANA Cloud Vector Store, you can start asking the LLM questions about the orchestration service of Generative AI Hub. This time, the model will not respond from its knowledge baseβ€”what it learned during trainingβ€”but instead, the retriever will search for relevant context information in your vector database and send the appropriate text chunk to the LLM to review before responding.\n", - "\n", - "πŸ‘‰ Change the `LLM_DEPLOYMENT_ID` in [variables.py](variables.py) to your deployment ID from exercise [01-explore-genai-hub](01-explore-genai-hub.md). For that go to **SAP AI Launchpad** application and navigate to **ML Operations** > **Deployments**.\n", - "\n", - "☝️ The `LLM_DEPLOYMENT_ID` is the deployment ID of the chat model you want to use e.g. **gpt-4o-mini**." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# import init_env\n", - "# import variables\n", - "\n", - "#init_env.set_environment_variables()\n", - "\n", - "from gen_ai_hub.proxy.langchain.openai import ChatOpenAI\n", - "from gen_ai_hub.proxy.langchain.openai import OpenAIEmbeddings\n", - "from langchain.chains import RetrievalQA\n", - "from langchain_community.vectorstores.hanavector import HanaDB\n", - "from hdbcli import dbapi" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You are again connecting to our shared SAP HANA Cloud Vector Engine." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "# # connect to HANA instance\n", - "# connection = init_env.connect_to_hana_db()\n", - "# connection.isconnected()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "def get_hana_connection():\n", - " conn = dbapi.connect(\n", - " address='ec41b786-96de-467b-9ff5-db725945f89c.hna0.prod-us10.hanacloud.ondemand.com',\n", - " port='443',\n", - " user='DBADMIN',\n", - " password='9hEW4UK86Fdt',\n", - " encrypt=True\n", - " )\n", - " return conn" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://israel-fsvdxbsq.authentication.eu11.hana.ondemand.com/oauth/token\n", - "sb-49ec08a9-d325-4480-9418-ad8801558203!b28574|aicore!b18\n", - "96d2ba69-3289-4190-ad82-c174e50f9f17$8C_adlgCYD6AscPgIKtLXJkIj1AL6i8p9Opw1JJZ0o8=\n", - "https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "grounding\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"grounding\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "EMBEDDING_DEPLOYMENT_ID = \"d1ae8a45e3d60115\"" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "# Reference which embedding model, DB connection and table to use\n", - "embeddings = OpenAIEmbeddings(deployment_id=EMBEDDING_DEPLOYMENT_ID)\n", - "db = HanaDB(\n", - " embedding=embeddings, connection=get_hana_connection(), table_name=\"EMBEDDINGS_CODEJAM_\"+\"Testing\"\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In this step you are defining which LLM to use during the retrieving process. You then also assign which database to retrieve information from. " - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "LLM_DEPLOYMENT_ID = \"d6dd6e483c92f88c\"" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "#Need to fix with the name mentioned in deployement id\n", - "# Define which model to use\n", - "chat_llm = ChatOpenAI(deployment_id=LLM_DEPLOYMENT_ID)\n", - "\n", - "# Create a retriever instance of the vector store\n", - "retriever = db.as_retriever(search_kwargs={\"k\": 2})" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Instead of sending the query directly to the LLM, you will now create a `RetrievalQA` instance and pass both the LLM and the database to be used during the retrieval process. Once set up, you can send your query to the `Retriever`.\n", - "\n", - "πŸ‘‰ Try out different queries. Feel free to ask anything you'd like to know about the Models that are available in Generative AI Hub." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'query': 'What is data masking in the orchestration service?',\n", - " 'result': \"I don't know.\",\n", - " 'source_documents': [Document(metadata={'producer': 'Skia/PDF m131', 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', 'creationdate': '2025-02-02T21:04:28+00:00', 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation', 'moddate': '2025-02-02T21:04:28+00:00', 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf', 'total_pages': 14, 'page': 0, 'page_label': '1'}, page_content='/Examples/Orchestration Service\\nOrchestration Service\\nThis notebook demonstrates how to use the SDK to interact with the Orchestration\\nService, enabling the creation of AI-driven workflows by seamlessly integrating various\\nmodules, such as templating, large language models (LLMs), data masking and content\\nfiltering. By leveraging these modules, you can build complex, automated workflows that\\nenhance the capabilities of your AI solutions. For more details on configuring and using'),\n", - " Document(metadata={'producer': 'Skia/PDF m131', 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', 'creationdate': '2025-02-02T21:04:28+00:00', 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation', 'moddate': '2025-02-02T21:04:28+00:00', 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf', 'total_pages': 14, 'page': 13, 'page_label': '14'}, page_content='https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html 14/14')]}" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# Create the QA instance to query llm based on custom documents\n", - "qa = RetrievalQA.from_llm(llm=chat_llm, retriever=retriever, return_source_documents=True)\n", - "\n", - "# Send query\n", - "query = \"What is data masking in the orchestration service?\"\n", - "\n", - "answer = qa.invoke(query)\n", - "display(answer)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'producer': 'Skia/PDF m131',\n", - " 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',\n", - " 'creationdate': '2025-02-02T21:04:28+00:00',\n", - " 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation',\n", - " 'moddate': '2025-02-02T21:04:28+00:00',\n", - " 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf',\n", - " 'total_pages': 14,\n", - " 'page': 0,\n", - " 'page_label': '1'}" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "/Examples/Orchestration Service\n", - "Orchestration Service\n", - "This notebook demonstrates how to use the SDK to interact with the Orchestration\n", - "Service, enabling the creation of AI-driven workflows by seamlessly integrating various\n", - "modules, such as templating, large language models (LLMs), data masking and content\n", - "filtering. By leveraging these modules, you can build complex, automated workflows that\n", - "enhance the capabilities of your AI solutions. For more details on configuring and using\n" - ] - }, - { - "data": { - "text/plain": [ - "{'producer': 'Skia/PDF m131',\n", - " 'creator': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',\n", - " 'creationdate': '2025-02-02T21:04:28+00:00',\n", - " 'title': 'Orchestration Service | Generative AI hub SDK 4.1.1 documentation',\n", - " 'moddate': '2025-02-02T21:04:28+00:00',\n", - " 'source': 'documents\\\\Orchestration_Service_Generative_AI_hub_SDK.pdf',\n", - " 'total_pages': 14,\n", - " 'page': 13,\n", - " 'page_label': '14'}" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html 14/14\n" - ] - } - ], - "source": [ - "for document in answer['source_documents']:\n", - " display(document.metadata) \n", - " print(document.page_content)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "πŸ‘‰ Go back to [05-store-embeddings-hana](05-store-embeddings-hana.ipynb) and try out different chunk sizes and/or different values for overlap. Store these chunks in a different table by adding a new variable to [variables.py](variables.py) and run this script again using the newly created table.\n", - "\n", - "[Next exercise](07-deploy-orchestration-service.md)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test2/07-deploy-orchestration-service.md b/09_BusinessAIWeek/Test2/07-deploy-orchestration-service.md deleted file mode 100644 index b10b3ed..0000000 --- a/09_BusinessAIWeek/Test2/07-deploy-orchestration-service.md +++ /dev/null @@ -1,59 +0,0 @@ -# Deploy the orchestration service SAP AI Core - -To use orchestration service, you need to create a deployment [configuration](https://help.sap.com/docs/ai-launchpad/sap-ai-launchpad/configurations). Using this configuration, you can deploy the orchestration service. After successful deployment, you will receive a deployment URL, which you can use to query the model of your choice via the orchestration service. - -☝️ Some of you already created an orchestration deployment in the beginning. Go to the SAP AI Launchpad and find your deployment URL under `Deployments`. - -## Create a configuration to deploy the orchestration service on SAP AI Core - -πŸ‘‰ Open the `ML Operations` tab and click on `Configurations`. - -![Configurations](images/configuration_o.png) - -πŸ‘‰ **Create** a new configuration. - -πŸ‘‰ Enter a configuration name `orchestration-conf`, select the `orchestration` scenario, version and the executable `orchestration`. - -![Create configuration 1/4](images/configuration_o2.png) - -πŸ‘‰ Click **Next**, **Next**, **Review** and **Create**. - -## Deploy a proxy for a large language model on SAP AI Core - -πŸ‘‰ Click on `Create Deployment` to create a deployment for that configuration. - -This will not actually deploy all the models, but it will deploy a proxy that returns a URL for you to use to query the models via the orchestration service. - -![Create deployment 1/5](images/deployment_o.png) - -πŸ‘‰ Click **Review** and **Create**. - -The deployment status is going to change from `UNKNOWN` to `PENDING` and then to `RUNNING`. - -Once the deployment is running you will receive a URL to query the orchestration service. Wait a couple of minutes, then use the **refresh** icon on the page for the URL to appear. - -☝️ You will need the deployment URL in the next exercise! - -![Create deployment 4/5](images/deployments_4.png) - -Under `Deployments` you can see all the deployed models and their status. - -![Create deployment 2/5](images/deployment_o2.png) - -Using the `URL`, `client ID`, and `client secret` from the SAP AI Core service key, you can now query the model using any programming language or API platform. - -![Create deployment 5/5](images/deployments_5.png) - -## Summary - -By this point, you will have learned which models are available through the Generative AI Hub and how to deploy the orchestration service in SAP AI Launchpad. - -## Further reading - -* [SAP AI Launchpad - Help Portal (Documentation)](https://help.sap.com/docs/ai-launchpad/sap-ai-launchpad/what-is-sap-ai-launchpad) -* [SAP AI Core Terminology](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/terminology) -* [Available Models in the Generative AI Hub](https://me.sap.com/notes/3437766) - ---- - -[Next exercise](08-orchestration-service.ipynb) diff --git a/09_BusinessAIWeek/Test2/08-orchestration-service.ipynb b/09_BusinessAIWeek/Test2/08-orchestration-service.ipynb deleted file mode 100644 index 0f7e08f..0000000 --- a/09_BusinessAIWeek/Test2/08-orchestration-service.ipynb +++ /dev/null @@ -1,375 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Use the orchestration service of Generative AI Hub\n", - "\n", - "The orchestration service of Generative AI Hub lets you use all the available models with the same codebase. You only deploy the orchestration service and then you can access all available models simply by changing the model name parameter. You can also use grounding, prompt templating, data masking and content filtering capabilities.\n", - "\n", - "Store the `orchestration deployment url` from the previous step in your `variables.py` file. This code is based on the [AI180 TechEd 2024 Jump-Start session](https://github.com/SAP-samples/teched2024-AI180/tree/e648921c46337b57f61ecc9a93251d4b838d7ad0/exercises/python).\n", - "\n", - "πŸ‘‰ Make sure you assign the deployment url of the orchestration service to `AICORE_ORCHESTRATION_DEPLOYMENT_URL` in [variables.py](variables.py)." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# import init_env\n", - "# import variables\n", - "\n", - "# init_env.set_environment_variables()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Import the packages you want to use" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://israel-fsvdxbsq.authentication.eu11.hana.ondemand.com/oauth/token\n", - "sb-49ec08a9-d325-4480-9418-ad8801558203!b28574|aicore!b18\n", - "96d2ba69-3289-4190-ad82-c174e50f9f17$8C_adlgCYD6AscPgIKtLXJkIj1AL6i8p9Opw1JJZ0o8=\n", - "https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "grounding\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"grounding\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.orchestration.models.llm import LLM\n", - "from gen_ai_hub.orchestration.models.message import SystemMessage, UserMessage\n", - "from gen_ai_hub.orchestration.models.template import Template, TemplateValue\n", - "from gen_ai_hub.orchestration.models.config import OrchestrationConfig\n", - "from gen_ai_hub.orchestration.service import OrchestrationService\n", - "from gen_ai_hub.orchestration.models.azure_content_filter import AzureContentFilter\n", - "from gen_ai_hub.orchestration.exceptions import OrchestrationError" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Assign the model you want to use\n", - "You can find more information regarding the available models here: https://me.sap.com/notes/3437766" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "AICORE_ORCHESTRATION_DEPLOYMENT_URL = \"https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2/inference/deployments/d2d982db268a416f\"" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "# TODOchose the model you want to try e.g. gemini-1.5-flash, mistralai--mistral-large-instruct, \n", - "# ibm--granite-13b-chat meta--llama3.1-70b-instruct, amazon--titan-text-lite, anthropic--claude-3.5-sonnet, \n", - "# gpt-4o-mini and assign it to name\n", - "llm = LLM(\n", - " name=\"gpt-4o\",\n", - " version=\"latest\",\n", - " parameters={\"max_tokens\": 500, \"temperature\": 1},\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Create a prompt template\n", - "The parameter **user_query** in the code snippet below is going to hold the user query that you will add later on. The user query is the text to be translated by the model. The parameter **to_lang** can be any language you want to translate into. By default it is set to **English**." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "template = Template(\n", - " messages=[\n", - " SystemMessage(\"You are a helpful translation assistant.\"),\n", - " UserMessage(\n", - " \"Translate the following text to {{?to_lang}}: {{?user_query}}\",\n", - " )\n", - " ],\n", - " defaults=[\n", - " TemplateValue(name=\"to_lang\", value=\"English\"),\n", - " ],\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Create an orchestration configuration \n", - "\n", - "Create an orchestration configuration by adding the llm you referenced and the prompt template you created previously." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "config = OrchestrationConfig(\n", - " template=template,\n", - " llm=llm,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Add it the configuration to the OrchestrationService instance and send the prompt to the model." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Does this really work with all the models that are available?\n" - ] - } - ], - "source": [ - "\n", - "orchestration_service = OrchestrationService(\n", - " api_url=AICORE_ORCHESTRATION_DEPLOYMENT_URL,\n", - " config=config,\n", - ")\n", - "result = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"Geht das wirklich mit allen Modellen die verfΓΌgbar sind?\"\n", - " )\n", - " ]\n", - ")\n", - "print(result.orchestration_result.choices[0].message.content)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Add a content filter\n", - "\n", - "Create a content filter and add it as an input filter to the orchestration configuration. This is going to filter out harmful content from the input query and not send the request to the model. Whereas adding a filter to the output would let the request go through but then filter any harmful text created by the model. Depending on your use case it can make sense to have both input and output filters.\n", - "\n", - "πŸ‘‰ Try out different values for the content filters. You can chose values [0 = **Safe**, 2 = **Low**, 4 = **Medium**, 6 = **High**]. Where **Safe** is *content generally related to violence* and **High** is *severely harmful content*." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "content_filter = AzureContentFilter(\n", - " hate=0,\n", - " sexual=0,\n", - " self_harm=0,\n", - " violence=0,\n", - ")\n", - "\n", - "orchestration_service.config.input_filters = [content_filter]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Try out the content filter" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "400 - Filtering Module - Input Filter: Prompt filtered due to safety violations. Please modify the prompt and try again.\n", - "You are a super talented developer!\n" - ] - } - ], - "source": [ - "\n", - "try:\n", - " result = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"Du bist ein ganz mieser Entwickler!\",\n", - " ),\n", - " ]\n", - " )\n", - " print(result.orchestration_result.choices[0].message.content)\n", - "except OrchestrationError as error:\n", - " print(error.message)\n", - "\n", - "\n", - "result = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"Du bist ein super talentierter Entwickler!\",\n", - " )\n", - " ]\n", - ")\n", - "print(result.orchestration_result.choices[0].message.content)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Now also add the content filter as an output filter" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "400 - Filtering Module - Input Filter: Prompt filtered due to safety violations. Please modify the prompt and try again.\n" - ] - } - ], - "source": [ - "orchestration_service.config.output_filters = [content_filter]\n", - "\n", - "try:\n", - " result = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value='Ich wΓΌrde gerne wissen, wie ich gewaltvoll die Fensterscheibe in einem BΓΌrogebΓ€ude am Besten zerstΓΆren kann.',\n", - " )\n", - " ]\n", - " )\n", - " print(result.orchestration_result.choices[0].message.content)\n", - "except OrchestrationError as error:\n", - " print(error.message)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Optional exercise\n", - "\n", - "πŸ‘‰ Now that you know how the orchestration service works, try adding the [Data Masking](https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html#data-masking) capability. With Data Masking you can hide personal information like email, name or phone numbers before sending such sensitive data to an LLM.\n", - "\n", - "## More Info\n", - "\n", - "Here you can find more [info on the content filter](https://learn.microsoft.com/en-us/azure/ai-studio/concepts/content-filtering)\n", - "\n", - "[Next exercise](09-orchestration-service-grounding.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test2/09-orchestration-service-grounding.ipynb b/09_BusinessAIWeek/Test2/09-orchestration-service-grounding.ipynb deleted file mode 100644 index 1036b59..0000000 --- a/09_BusinessAIWeek/Test2/09-orchestration-service-grounding.ipynb +++ /dev/null @@ -1,328 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Use the orchestration service of Generative AI Hub\n", - "\n", - "The orchestration service of Generative AI Hub lets you use all the available models with the same codebase. You only deploy the orchestration service and then you can access all available models simply by changing the model name parameter. You can also use grounding, prompt templating, data masking and content filtering capabilities.\n", - "\n", - "Store the `orchestration deployment url` from the previous step in your `variables.py` file. This code is based on the [AI180 TechEd 2024 Jump-Start session](https://github.com/SAP-samples/teched2024-AI180/tree/e648921c46337b57f61ecc9a93251d4b838d7ad0/exercises/python).\n", - "\n", - "πŸ‘‰ Make sure you assign the deployment url of the orchestration service to `AICORE_ORCHESTRATION_DEPLOYMENT_URL` in [variables.py](variables.py)." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://israel-fsvdxbsq.authentication.eu11.hana.ondemand.com/oauth/token\n", - "sb-49ec08a9-d325-4480-9418-ad8801558203!b28574|aicore!b18\n", - "96d2ba69-3289-4190-ad82-c174e50f9f17$8C_adlgCYD6AscPgIKtLXJkIj1AL6i8p9Opw1JJZ0o8=\n", - "https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "grounding\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"grounding\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "# import init_env\n", - "# import variables\n", - "\n", - "# init_env.set_environment_variables()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Import the packages you want to use" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.orchestration.models.llm import LLM\n", - "from gen_ai_hub.orchestration.models.config import GroundingModule, OrchestrationConfig\n", - "from gen_ai_hub.orchestration.models.document_grounding import DocumentGrounding, DocumentGroundingFilter\n", - "from gen_ai_hub.orchestration.models.template import Template, TemplateValue\n", - "from gen_ai_hub.orchestration.models.message import SystemMessage, UserMessage\n", - "from gen_ai_hub.orchestration.service import OrchestrationService" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Assign a model and define a prompt template\n", - "**user_query** is again the user input. Whereas **grounding_response** is the context retrieved from the context information, in this case from sap.help.com." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "AICORE_ORCHESTRATION_DEPLOYMENT_URL = \"https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2/inference/deployments/d2d982db268a416f\"" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO again you need to chose a model e.g. gemini-1.5-flash or gpt-4o-mini\n", - "llm = LLM(\n", - " name=\"gpt-4o\",\n", - " parameters={\n", - " 'temperature': 0.0,\n", - " }\n", - ")\n", - "template = Template(\n", - " messages=[\n", - " SystemMessage(\"You are a helpful translation assistant.\"),\n", - " UserMessage(\"\"\"Answer the request by providing relevant answers that fit to the request.\n", - " Request: {{ ?user_query }}\n", - " Context:{{ ?grounding_response }}\n", - " \"\"\"),\n", - " ]\n", - " )" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Create an orchestration configuration that specifies the grounding capability" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "# Set up Document Grounding\n", - "filters = [\n", - " DocumentGroundingFilter(id=\"SAPHelp\", data_repository_type=\"help.sap.com\")\n", - " ]\n", - "\n", - "grounding_config = GroundingModule(\n", - " type=\"document_grounding_service\",\n", - " config=DocumentGrounding(input_params=[\"user_query\"], output_param=\"grounding_response\", filters=filters)\n", - " )\n", - "\n", - "config = OrchestrationConfig(\n", - " template=template,\n", - " llm=llm,\n", - " grounding=grounding_config\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Joule is SAP's generative AI copilot designed to enhance interactions with SAP business systems. It serves as an intelligent assistant that understands your business needs, streamlining tasks and providing smart insights to facilitate faster decision-making. Joule offers conversational interactions to simplify access to information and automate business processes, improving both employee and customer satisfaction.\n", - "\n", - "Key features of Joule include:\n", - "\n", - "- **Integration with SAP Applications**: Joule integrates seamlessly with SAP applications, providing a conversational user interface that uses SAP Fiori compliant UI controls.\n", - "- **Enterprise-readiness**: It offers out-of-the-box integration with SAP backend systems and complies with AI ethics and GDPR.\n", - "- **Tailored for Business**: Joule's capabilities are customized based on your SAP solution portfolio, offering a unique assistant experience.\n", - "- **Automatic Updates**: Joule is automatically updated whenever new capabilities are added or changed.\n", - "\n", - "Business benefits of Joule include:\n", - "\n", - "- **Faster Work**: Joule acts as your copilot across SAP applications, streamlining tasks and improving efficiency.\n", - "- **Smarter Insights**: It provides quick answers and insights on-demand, helping to make faster decisions without bottlenecks.\n", - "- **Better Outcomes**: Joule can assist with job descriptions, coding, and more, ensuring excellent content delivery.\n", - "- **Full Control**: Users maintain control over decision-making and data privacy while accessing generative AI in a secure environment.\n", - "\n", - "Joule operates in the SAP BTP Cloud Foundry environment and supports multi-tenancy, meaning different consumers are independently provisioned with isolated data. It also supports multiple languages, adapting to the language configured within SAP applications.\n", - "\n", - "For more information, you can refer to the Joule Onboarding Guide and check regional availability in the Data Centers Supported by Joule.\n" - ] - } - ], - "source": [ - "orchestration_service = OrchestrationService(\n", - " api_url=AICORE_ORCHESTRATION_DEPLOYMENT_URL,\n", - " config=config\n", - ")\n", - "\n", - "response = orchestration_service.run(\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"What is Joule?\"\n", - " )\n", - " ]\n", - ")\n", - "\n", - "print(response.orchestration_result.choices[0].message.content)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Streaming\n", - "Long response times can be frustrating in chat applications, especially when a large amount of text is involved. To create a smoother, more engaging user experience, we can use streaming to display the response in real-time, character by character or chunk by chunk, as the model generates it. This avoids the awkward wait for a complete response and provides immediate feedback to the user." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Joule is SAP's generative AI copilot designed to enhance interactions with SAP business systems. It serves\n", - " as an intelligent assistant that simplifies access to information and automates business processes,\n", - " improving both employee and customer satisfaction. Joule offers conversational interactions, guiding\n", - " users through content discovery within the SAP ecosystem and providing role-based access to relevant\n", - " processes. It integrates seamlessly with SAP applications, offering a unified user experience across\n", - " SAP's solution portfolio.\n", - "\n", - "Key features of Joule include:\n", - "\n", - "- **Integration with SAP Applications**:\n", - " Joule provides a conversational user interface integrated with SAP applications, using SAP Fiori compliant\n", - " UI controls.\n", - "- **Enterprise-Readiness**: It offers out-of-the-box integration with SAP backend systems\n", - " and complies with AI ethics and GDPR.\n", - "- **Tailored for Business**: Joule's capabilities are customized\n", - " based on the user's SAP solution portfolio.\n", - "- **Automatic Updates**: Joule is automatically updated\n", - " with new capabilities and changes.\n", - "\n", - "Business benefits of Joule include:\n", - "\n", - "- **Faster Work**: Streamlines\n", - " tasks with an AI assistant that understands your unique role.\n", - "- **Smarter Insights**: Provides quick\n", - " answers and insights for faster decision-making.\n", - "- **Better Outcomes**: Offers excellent content for\n", - " various tasks, such as job descriptions and coding assistance.\n", - "- **Full Control**: Ensures data privacy\n", - " and control over decision-making while using generative AI.\n", - "\n", - "Joule operates in the SAP BTP Cloud Found\n", - "ry environment and supports multi-tenancy, meaning different consumers are independently provisioned\n", - " with isolated data. It also supports multiple languages, adapting to the language configured within\n", - " SAP applications.\n", - "\n", - "For more information, users can refer to the Joule Onboarding Guide and check regional\n", - " availability in the Data Centers Supported by Joule.\n" - ] - } - ], - "source": [ - "response = orchestration_service.stream(\n", - " config=config,\n", - " template_values=[\n", - " TemplateValue(\n", - " name=\"user_query\",\n", - " #TODO Here you can change the user prompt into whatever you want to ask the model\n", - " value=\"What is Joule?\"\n", - " )\n", - " ]\n", - ")\n", - "\n", - "for chunk in response:\n", - " print(chunk.orchestration_result.choices[0].delta.content)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[More Info on the content filter](https://learn.microsoft.com/en-us/azure/ai-studio/concepts/content-filtering)\n", - "\n", - "[Next exercise](10-chatbot-with-memory.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test2/10-chatbot-with-memory.ipynb b/09_BusinessAIWeek/Test2/10-chatbot-with-memory.ipynb deleted file mode 100644 index fc111f9..0000000 --- a/09_BusinessAIWeek/Test2/10-chatbot-with-memory.ipynb +++ /dev/null @@ -1,307 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Chatbot with Memory\n", - "\n", - "Let's add some history to the interaction and build a chatbot. Unlike many people think. LLMs are fixed in their state. They are trained until a certain cutoff date and do not know anything after that point unless you feed them current information. That is also why LLMs do not remember anything about you or the prompts you send to the model. If the model seems to remember you and what you said it is always because the application you are using (e.g. ChatPGT or the chat function in SAP AI Launchpad) is sending the chat history to the model to provide the conversation history to the model as context.\n", - "\n", - "Below you can find a simple implementation of a chatbot with memory.\n", - "\n", - "The code in this exercise is based on the [help documentation](https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html) of the Generative AI Hub Python SDK." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "# import init_env\n", - "# import variables\n", - "from typing import List\n", - "\n", - "# init_env.set_environment_variables()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Import the packages you want to use" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.orchestration.models.llm import LLM\n", - "from gen_ai_hub.orchestration.models.message import Message, SystemMessage, UserMessage\n", - "from gen_ai_hub.orchestration.models.template import Template, TemplateValue\n", - "from gen_ai_hub.orchestration.models.config import OrchestrationConfig\n", - "from gen_ai_hub.orchestration.service import OrchestrationService" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://anuragv2-39wjy902.authentication.eu10.hana.ondemand.com/oauth/token\n", - "sb-adcd4907-f1a7-462d-ba8e-646390ee4185!b398425|aicore!b540\n", - "107d3e9b-9a41-4b30-9b24-a091a45956cd$VmYxjzammFm50xkj1O37HmzgX3maoNrwfrlm99qUhi0=\n", - "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "llm-deployed\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"grounding\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "AICORE_ORCHESTRATION_DEPLOYMENT_URL = \"https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2/inference/deployments/d2d982db268a416f\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Create the chatbot class" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "class ChatBot:\n", - " def __init__(self, orchestration_service: OrchestrationService):\n", - " self.service = orchestration_service\n", - " self.config = OrchestrationConfig(\n", - " template=Template(\n", - " messages=[\n", - " SystemMessage(\"You are a helpful chatbot assistant.\"),\n", - " UserMessage(\"{{?user_query}}\"),\n", - " ],\n", - " ),\n", - " # TODO add a model name here, e.g. gemini-1.5-flash\n", - " llm=LLM(name=\"gpt-4o\"),\n", - " )\n", - " self.history: List[Message] = []\n", - "\n", - " def chat(self, user_input):\n", - " response = self.service.run(\n", - " config=self.config,\n", - " template_values=[\n", - " TemplateValue(name=\"user_query\", value=user_input),\n", - " ],\n", - " history=self.history,\n", - " )\n", - "\n", - " message = response.orchestration_result.choices[0].message\n", - "\n", - " self.history = response.module_results.templating\n", - " self.history.append(message)\n", - "\n", - " return message.content\n", - " \n", - " def reset(self):\n", - " self.history = []\n", - "\n", - "service = OrchestrationService(api_url=AICORE_ORCHESTRATION_DEPLOYMENT_URL)\n", - "bot = ChatBot(orchestration_service=service)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"Hello! Yes, I'm here. How can I assist you today?\"" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.chat(\"Hello, are you there?\")" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"While I don't have personal preferences or feelings, I think CodeJams are great events! They offer excellent opportunities for programmers to challenge themselves, learn new skills, and showcase their problem-solving abilities. If you have any questions about participating in a CodeJam, I'd be happy to help!\"" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.chat(\"Do you like CodeJams?\")" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"I don't have the ability to remember past interactions or conversations. Each session is independent, so I can only reference what we've talked about within this current session. If there's something you'd like to discuss again or if you have any new questions, feel free to let me know!\"" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.chat(\"Can you remember what we first talked about?\")" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[Message(role=, content='You are a helpful chatbot assistant.'),\n", - " Message(role=, content='Hello, are you there?'),\n", - " Message(role=, content=\"Hello! Yes, I'm here. How can I assist you today?\"),\n", - " Message(role=, content='You are a helpful chatbot assistant.'),\n", - " Message(role=, content='Do you like CodeJams?'),\n", - " Message(role=, content=\"While I don't have personal preferences or feelings, I think CodeJams are great events! They offer excellent opportunities for programmers to challenge themselves, learn new skills, and showcase their problem-solving abilities. If you have any questions about participating in a CodeJam, I'd be happy to help!\"),\n", - " Message(role=, content='You are a helpful chatbot assistant.'),\n", - " Message(role=, content='Can you remember what we first talked about?'),\n", - " Message(role=, content=\"I don't have the ability to remember past interactions or conversations. Each session is independent, so I can only reference what we've talked about within this current session. If there's something you'd like to discuss again or if you have any new questions, feel free to let me know!\")]" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.history" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And to prove to you that the model does indeed not remember you, let's delete the history and try again :)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"I'm afraid I can't remember past interactions or conversations, as I don't have the ability to store personal data or memories. Nonetheless, I'm here to assist you with any questions or information you might need. How can I help you today?\"" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bot.reset()\n", - "bot.chat(\"Can you remember what we first talked about?\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[Next exercise](11-your-chatbot.ipynb)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test2/11-your-chatbot.ipynb b/09_BusinessAIWeek/Test2/11-your-chatbot.ipynb deleted file mode 100644 index 1362f0e..0000000 --- a/09_BusinessAIWeek/Test2/11-your-chatbot.ipynb +++ /dev/null @@ -1,47 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Now it's your turn!\n", - "Build your own chatbot similar to [10-chatbot-with-memory.ipynb](10-chatbot-with-memory.ipynb) with what you have learned! Combine what you have learned in [08-orchestration-service.ipynb](08-orchestration-service.ipynb) and/or [09-orchestration-service-grounding.ipynb](09-orchestration-service-grounding.ipynb) and build a chatbot that can answer SAP specific questions or filter out personal data or hate speech." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import init_env\n", - "import variables\n", - "\n", - "init_env.set_environment_variables()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO Your code can start here!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well done! You have completed the CodeJam! If you still have time check out the optional [AI Agents exercise](12-ai-agents.ipynb)!" - ] - } - ], - "metadata": { - "language_info": { - "name": "python" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test2/12-ai-agents.ipynb b/09_BusinessAIWeek/Test2/12-ai-agents.ipynb deleted file mode 100644 index 2d0b1c7..0000000 --- a/09_BusinessAIWeek/Test2/12-ai-agents.ipynb +++ /dev/null @@ -1,373 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# AI Agents with Generative AI Hub\n", - "\n", - "Everyone is talking about AI Agents and how they are going to make LLMs more powerful. AI Agents are basically systems that have a set of tools that they can use and the LLM is the agents brain to decide which tool to use when. Some systems are more agentic than others, depending on the amount of autonomy and decision-making power they possess. \n", - "\n", - "If you want to know more about AI Agents and how to build one from scratch, check out this [Devtoberfest session](https://community.sap.com/t5/devtoberfest/getting-started-with-agents-using-sap-generative-ai-hub/ev-p/13865119).\n", - "\n", - "πŸ‘‰ Before you start you will need to add the constant `LLM_DEPLOYMENT_ID` to [variables.py](variables.py). You can use the `gpt-4o-mini` model again that we deployed earlier. You find the deployment ID in SAP AI Launchpad." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "https://israel-fsvdxbsq.authentication.eu11.hana.ondemand.com/oauth/token\n", - "sb-49ec08a9-d325-4480-9418-ad8801558203!b28574|aicore!b18\n", - "96d2ba69-3289-4190-ad82-c174e50f9f17$8C_adlgCYD6AscPgIKtLXJkIj1AL6i8p9Opw1JJZ0o8=\n", - "https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com/v2\n", - "grounding\n" - ] - } - ], - "source": [ - "import json\n", - "import os\n", - "from ai_core_sdk.ai_core_v2_client import AICoreV2Client\n", - "# Inline credentials\n", - "with open('creds.json') as f:\n", - " credCF = json.load(f)\n", - " \n", - "# Set environment variables\n", - "def set_environment_vars(credCF):\n", - " env_vars = {\n", - " 'AICORE_AUTH_URL': credCF['url'] + '/oauth/token',\n", - " 'AICORE_CLIENT_ID': credCF['clientid'],\n", - " 'AICORE_CLIENT_SECRET': credCF['clientsecret'],\n", - " 'AICORE_BASE_URL': credCF[\"serviceurls\"][\"AI_API_URL\"] + \"/v2\",\n", - " 'AICORE_RESOURCE_GROUP': \"grounding\"\n", - " }\n", - " \n", - " for key, value in env_vars.items():\n", - " os.environ[key] = value\n", - " print(value)\n", - " \n", - "# Create AI Core client instance\n", - "def create_ai_core_client(credCF):\n", - " set_environment_vars(credCF) # Ensure environment variables are set\n", - " return AICoreV2Client(\n", - " base_url=os.environ['AICORE_BASE_URL'],\n", - " auth_url=os.environ['AICORE_AUTH_URL'],\n", - " client_id=os.environ['AICORE_CLIENT_ID'],\n", - " client_secret=os.environ['AICORE_CLIENT_SECRET'],\n", - " resource_group=os.environ['AICORE_RESOURCE_GROUP']\n", - " )\n", - " \n", - "ai_core_client = create_ai_core_client(credCF)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Import the packages you want to use" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_community.tools import WikipediaQueryRun\n", - "from langchain_community.utilities import WikipediaAPIWrapper\n", - "\n", - "from langchain import hub\n", - "from langchain.agents import AgentExecutor, create_react_agent\n", - "\n", - "from gen_ai_hub.proxy.langchain.openai import ChatOpenAI\n", - "from gen_ai_hub.proxy.langchain.openai import OpenAIEmbeddings\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We will give the agent different tools to use. The first tool will be [access to Wikipedia](https://python.langchain.com/docs/integrations/tools/wikipedia/). This way instead of answering from it's training data, the model will check on Wikipedia articles first." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Page: Bengaluru\n", - "Summary: Bengaluru, also known as Bangalore (its official name until 1 November 2014), is the capital and largest city of the southern Indian state of Karnataka. As per the 2011 census, the city had a population of 8.4 million, making it the third most populous city in India and the most populous in South India. The Bengaluru metropolitan area had a population of around 8.5 million, making it the fifth most populous urban agglomeration in the country. It is located near the center of the Deccan Plateau, at a height of 900 m (3,000 ft) above sea level. The city is known as India's \"Garden City\", due to its parks and greenery.\n", - "Archaeological artifacts indicate that the human settlement in the region happened as early as 4000 BCE. The first mention of the name \"Bengalooru\" is from an old Kannada stone inscription from 890 CE found at the Nageshwara Temple. From 350 CE, it was ruled by the Western Ganga dynasty, and in the early eleventh century, the city became part of the Chola empire. In the late Middle Ages, the region was part of the Hoysala Kingdom and then the Vijayanagara Empire. In 1537 CE, Kempe Gowda I, a feudal ruler under the Vijayanagara Empire, established a mud fort which is considered the foundation of the modern city of Bengaluru and its oldest areas, or petes, which still exist. After the fall of the Vijayanagara Empire, Kempe Gowda declared independence, and the city was expanded by his successors. In 1638 CE, an Adil Shahi army defeated Kempe Gowda III, and the city became a jagir (feudal estate) of Shahaji Bhonsle. The Mughals later captured Bengaluru and sold it to Maharaja Chikka Devaraja Wodeyar of the Kingdom of Mysore. After the death of Krishnaraja Wodeyar II in 1759 CE, Hyder Ali seized control of the kingdom of Mysore and with it, the administration of Bengaluru, which passed subsequently to his son, Tipu Sultan.\n", - "The city was captured by the British East India Company during the Anglo-Mysore Wars, and became part of the Princely State of Mysore. The administrative control of the city was returned to Krishnaraja Wadiyar III, then Maharaja of Mysore, and the old city developed under the dominions of the Mysore kingdom. In 1809 CE, the British shifted their military garrison to the city and established the cantonment, outside the old city. In the late 19th century CE, the city was essentially composed of two distinct urban settlements, the old pete and the new cantonment. Following India's independence in 1947, Bengaluru became the capital of Mysore State, and remained the capital when the state was enlarged and unified in 1956 and subsequently renamed as Karnataka in 1973. The two urban settlements which had developed as independent entities, merged under a single urban administration in 1949.\n", - "Bengaluru is one of the fastest-growing metropolises in India. As of 2023, the metropolitan area had an estimated GDP of $359.9 billion, and is one of the most productive metro areas of India. The city is a major center for information technology (IT), and is consistently ranked amongst the world's fastest growing technology hubs. It is widely regarded as the \"Silicon Valley of India\", as the largest hub and exporter of IT services in the country. Manufacturing is a major contributor to the economy and the city is also home to several state-owned manufacturing companies. Bengaluru also hosts several institutes of national importance in higher education.\n", - "\n", - "Page: Bangalore Days\n", - "Summary: Bangalore Days is a 2014 Indian Malayalam-language coming of age romantic comedy-drama film written and directed by Anjali Menon, and produced by Anwar Rasheed and Sophia Paul under the banner Anwar Rasheed Entertainments and Weekend Blockbusters. The film features an ensemble cast of Nivin Pauly, Dulquer Salmaan, Fahadh Faasil, Nazriya Nazim, Parvathy Thiruvothu, Isha Talwar and Paris Laxmi..\n", - "Bangalore Days revolves around the life of three cousins from Kerala who move to Bangalore, continuing Anjali Menon's trend of \n" - ] - } - ], - "source": [ - "wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())\n", - "\n", - "# Let's check if it works\n", - "print(wikipedia.run(\"Bangalore\"))\n", - "\n", - "# Assign wikipedia to the set of tools\n", - "tools = [wikipedia]" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "c:\\Users\\C5385222\\AppData\\Local\\anaconda3\\Lib\\site-packages\\langsmith\\client.py:253: LangSmithMissingAPIKeyWarning: API key must be provided when using hosted LangSmith API\n", - " warnings.warn(\n" - ] - }, - { - "data": { - "text/plain": [ - "PromptTemplate(input_variables=['agent_scratchpad', 'input', 'tool_names', 'tools'], input_types={}, partial_variables={}, metadata={'lc_hub_owner': 'hwchase17', 'lc_hub_repo': 'react', 'lc_hub_commit_hash': 'd15fe3c426f1c4b3f37c9198853e4a86e20c425ca7f4752ec0c9b0e97ca7ea4d'}, template='Answer the following questions as best you can. You have access to the following tools:\\n\\n{tools}\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [{tool_names}]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: {input}\\nThought:{agent_scratchpad}')" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# For agents the initial prompt including all the instructions is very important. \n", - "# LangChain already has a good set of prompts that we can reuse here.\n", - "prompt = hub.pull(\"hwchase17/react\")\n", - "prompt" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mBangalore is a significant city in India, known for its technology sector. I'll gather more information about its history, culture, and significance. \n", - "Action: wikipedia\n", - "Action Input: Bangalore\u001b[0m\u001b[36;1m\u001b[1;3mPage: Bengaluru\n", - "Summary: Bengaluru, also known as Bangalore (its official name until 1 November 2014), is the capital and largest city of the southern Indian state of Karnataka. As per the 2011 census, the city had a population of 8.4 million, making it the third most populous city in India and the most populous in South India. The Bengaluru metropolitan area had a population of around 8.5 million, making it the fifth most populous urban agglomeration in the country. It is located near the center of the Deccan Plateau, at a height of 900 m (3,000 ft) above sea level. The city is known as India's \"Garden City\", due to its parks and greenery.\n", - "Archaeological artifacts indicate that the human settlement in the region happened as early as 4000 BCE. The first mention of the name \"Bengalooru\" is from an old Kannada stone inscription from 890 CE found at the Nageshwara Temple. From 350 CE, it was ruled by the Western Ganga dynasty, and in the early eleventh century, the city became part of the Chola empire. In the late Middle Ages, the region was part of the Hoysala Kingdom and then the Vijayanagara Empire. In 1537 CE, Kempe Gowda I, a feudal ruler under the Vijayanagara Empire, established a mud fort which is considered the foundation of the modern city of Bengaluru and its oldest areas, or petes, which still exist. After the fall of the Vijayanagara Empire, Kempe Gowda declared independence, and the city was expanded by his successors. In 1638 CE, an Adil Shahi army defeated Kempe Gowda III, and the city became a jagir (feudal estate) of Shahaji Bhonsle. The Mughals later captured Bengaluru and sold it to Maharaja Chikka Devaraja Wodeyar of the Kingdom of Mysore. After the death of Krishnaraja Wodeyar II in 1759 CE, Hyder Ali seized control of the kingdom of Mysore and with it, the administration of Bengaluru, which passed subsequently to his son, Tipu Sultan.\n", - "The city was captured by the British East India Company during the Anglo-Mysore Wars, and became part of the Princely State of Mysore. The administrative control of the city was returned to Krishnaraja Wadiyar III, then Maharaja of Mysore, and the old city developed under the dominions of the Mysore kingdom. In 1809 CE, the British shifted their military garrison to the city and established the cantonment, outside the old city. In the late 19th century CE, the city was essentially composed of two distinct urban settlements, the old pete and the new cantonment. Following India's independence in 1947, Bengaluru became the capital of Mysore State, and remained the capital when the state was enlarged and unified in 1956 and subsequently renamed as Karnataka in 1973. The two urban settlements which had developed as independent entities, merged under a single urban administration in 1949.\n", - "Bengaluru is one of the fastest-growing metropolises in India. As of 2023, the metropolitan area had an estimated GDP of $359.9 billion, and is one of the most productive metro areas of India. The city is a major center for information technology (IT), and is consistently ranked amongst the world's fastest growing technology hubs. It is widely regarded as the \"Silicon Valley of India\", as the largest hub and exporter of IT services in the country. Manufacturing is a major contributor to the economy and the city is also home to several state-owned manufacturing companies. Bengaluru also hosts several institutes of national importance in higher education.\n", - "\n", - "Page: Bangalore Days\n", - "Summary: Bangalore Days is a 2014 Indian Malayalam-language coming of age romantic comedy-drama film written and directed by Anjali Menon, and produced by Anwar Rasheed and Sophia Paul under the banner Anwar Rasheed Entertainments and Weekend Blockbusters. The film features an ensemble cast of Nivin Pauly, Dulquer Salmaan, Fahadh Faasil, Nazriya Nazim, Parvathy Thiruvothu, Isha Talwar and Paris Laxmi..\n", - "Bangalore Days revolves around the life of three cousins from Kerala who move to Bangalore, continuing Anjali Menon's trend of \u001b[0m\u001b[32;1m\u001b[1;3mI have gathered a lot of useful information about Bangalore, now known as Bengaluru, covering its history, culture, and economic significance. \n", - "\n", - "Final Answer: Bengaluru, formerly known as Bangalore, is the capital of Karnataka, India, and one of the country's most populous cities. It has a rich history dating back to 4000 BCE with notable periods of rule by various dynasties, including the Western Ganga, Chola, Hoysala, and Vijayanagara empires. In 1537, Kempe Gowda I established a fort that laid the foundation for the modern city. It later came under British control and became part of the Princely State of Mysore. Bengaluru is often referred to as the \"Silicon Valley of India\" due to its significant information technology industry and is known for its parks, earning it the nickname \"Garden City.\" As of 2023, it has a burgeoning economy with an estimated GDP of $359.9 billion, making it one of the fastest-growing cities in India.\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "{'input': 'Tell me about Bangalore!',\n", - " 'output': 'Bengaluru, formerly known as Bangalore, is the capital of Karnataka, India, and one of the country\\'s most populous cities. It has a rich history dating back to 4000 BCE with notable periods of rule by various dynasties, including the Western Ganga, Chola, Hoysala, and Vijayanagara empires. In 1537, Kempe Gowda I established a fort that laid the foundation for the modern city. It later came under British control and became part of the Princely State of Mysore. Bengaluru is often referred to as the \"Silicon Valley of India\" due to its significant information technology industry and is known for its parks, earning it the nickname \"Garden City.\" As of 2023, it has a burgeoning economy with an estimated GDP of $359.9 billion, making it one of the fastest-growing cities in India.'}" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Create a chat model instance which will act as the brain of the agent\n", - "llm = ChatOpenAI(deployment_id=\"d6dd6e483c92f88c\")\n", - "\n", - "# Create an agent with the llm, tools and prompt\n", - "agent = create_react_agent(llm, tools, prompt)\n", - "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n", - "\n", - "# Let's ask try the agent and ask about Bangalore\n", - "agent_executor.invoke({\"input\": \"Tell me about Bangalore!\"})" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Give the agent access to our HANA vector store\n", - "Now the agent can use Wikipedia but wouldn't it be nice if the agent could pick between resources and decide when to use what?" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "def get_hana_connection():\n", - " conn = dbapi.connect(\n", - " address='ec41b786-96de-467b-9ff5-db725945f89c.hna0.prod-us10.hanacloud.ondemand.com',\n", - " port='443',\n", - " user='DBADMIN',\n", - " password='9hEW4UK86Fdt',\n", - " encrypt=True\n", - " )\n", - " return conn" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_community.vectorstores.hanavector import HanaDB\n", - "from langchain.tools.retriever import create_retriever_tool\n", - "from hdbcli import dbapi\n", - "# # connect to HANA instance\n", - "# connection = init_env.connect_to_hana_db()\n", - "# connection.isconnected()\n", - "\n", - "# Reference which embedding model, DB connection and table to use\n", - "embeddings = OpenAIEmbeddings(deployment_id=\"d1ae8a45e3d60115\")\n", - "db = HanaDB(\n", - " embedding=embeddings, connection=get_hana_connection(), table_name=\"EMBEDDINGS_CODEJAM_\"+\"Testing\"\n", - ")\n", - "\n", - "# Create a retriever instance of the vector store\n", - "retriever = db.as_retriever(search_kwargs={\"k\": 2})\n", - "\n", - "# We need to add a description to the retriever so that the llm knows when to use this tool.\n", - "retriever_tool = create_retriever_tool(\n", - " retriever,\n", - " \"SAP orchestration service docu\",\n", - " \"Search for information SAP's orchestration service. For any questions about generative AI hub and sap's orchestration service, you must use this tool!\",\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Ask the Agent\n", - "\n", - "You can ask the agent anything in general and it will try to find an entry from Wikipedia, unless you ask about SAP's orchestration service. Then it will get the information from SAP HANA vector store, where it holds the documentation, instead of Wikipedia." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mData masking refers to the process of obfuscating specific data within a database to protect it from unauthorized access while maintaining its usability for tasks like software testing or user training. This is typically accomplished by replacing sensitive information with altered values that are not identifiable but retain the same format and characteristics as the original data.\n", - "\n", - "Action: wikipedia\n", - "Action Input: \"Data masking\"\u001b[0m" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "c:\\Users\\C5385222\\AppData\\Local\\anaconda3\\Lib\\site-packages\\wikipedia\\wikipedia.py:389: GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system (\"lxml\"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.\n", - "\n", - "The code that caused this warning is on line 389 of the file c:\\Users\\C5385222\\AppData\\Local\\anaconda3\\Lib\\site-packages\\wikipedia\\wikipedia.py. To get rid of this warning, pass the additional argument 'features=\"lxml\"' to the BeautifulSoup constructor.\n", - "\n", - " lis = BeautifulSoup(html).find_all('li')\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[36;1m\u001b[1;3mPage: Data masking\n", - "Summary: Data masking or data obfuscation is the process of modifying sensitive data in such a way that it is of no or little value to unauthorized intruders while still being usable by software or authorized personnel. Data masking can also be referred as anonymization, or tokenization, depending on different context.\n", - "The main reason to mask data is to protect information that is classified as personally identifiable information, or mission critical data. However, the data must remain usable for the purposes of undertaking valid test cycles. It must also look real and appear consistent. It is more common to have masking applied to data that is represented outside of a corporate production system. In other words, where data is needed for the purpose of application development, building program extensions and conducting various test cycles. It is common practice in enterprise computing to take data from the production systems to fill the data component, required for these non-production environments. However, this practice is not always restricted to non-production environments. In some organizations, data that appears on terminal screens to call center operators may have masking dynamically applied based on user security permissions (e.g. preventing call center operators from viewing credit card numbers in billing systems).\n", - "The primary concern from a corporate governance perspective is that personnel conducting work in these non-production environments are not always security cleared to operate with the information contained in the production data. This practice represents a security hole where data can be copied by unauthorized personnel, and security measures associated with standard production level controls can be easily bypassed. This represents an access point for a data security breach.\n", - "\n", - "\n", - "\n", - "Page: Data security\n", - "Summary: Data security or data protection means protecting digital data, such as those in a database, from destructive forces and from the unwanted actions of unauthorized users, such as a cyberattack or a data breach.\u001b[0m\u001b[32;1m\u001b[1;3mI now know the final answer.\n", - "Final Answer: Data masking or data obfuscation is the process of modifying sensitive data so that it holds little or no value for unauthorized intruders while remaining usable by authorized personnel or software. This involves replacing sensitive information with altered values that maintain the original data's format and characteristics. Data masking is primarily used to protect personally identifiable information and mission-critical data, especially in non-production environments for tasks like software testing and user training. It helps mitigate the risk of data breaches by ensuring that individuals without proper security clearance do not have access to sensitive information.\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "{'input': 'What is data masking?',\n", - " 'output': \"Data masking or data obfuscation is the process of modifying sensitive data so that it holds little or no value for unauthorized intruders while remaining usable by authorized personnel or software. This involves replacing sensitive information with altered values that maintain the original data's format and characteristics. Data masking is primarily used to protect personally identifiable information and mission-critical data, especially in non-production environments for tasks like software testing and user training. It helps mitigate the risk of data breaches by ensuring that individuals without proper security clearance do not have access to sensitive information.\"}" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Now let's add the retriever as a tool\n", - "tools = [wikipedia, retriever_tool]\n", - "\n", - "# And create the agent again with the two tools, wikipedia and the HANA vector store (retriever)\n", - "agent = create_react_agent(llm, tools, prompt)\n", - "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n", - "\n", - "# First ask: What is Data Masking?\n", - "# Then ask: What is Data Masking in SAP's Generative AI Hub?\n", - "# Pay attention to the response! Do you see what is happening now?\n", - "agent_executor.invoke({\"input\": \"What is data masking?\"})" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/09_BusinessAIWeek/Test2/BTP_cockpit.png b/09_BusinessAIWeek/Test2/BTP_cockpit.png deleted file mode 100644 index df1b18b..0000000 Binary files a/09_BusinessAIWeek/Test2/BTP_cockpit.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/BTP_cockpit_BAS.png b/09_BusinessAIWeek/Test2/BTP_cockpit_BAS.png deleted file mode 100644 index a80f45b..0000000 Binary files a/09_BusinessAIWeek/Test2/BTP_cockpit_BAS.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/Orchestration_Service_Generative_AI_hub_SDK.pdf b/09_BusinessAIWeek/Test2/Orchestration_Service_Generative_AI_hub_SDK.pdf deleted file mode 100644 index 979b277..0000000 Binary files a/09_BusinessAIWeek/Test2/Orchestration_Service_Generative_AI_hub_SDK.pdf and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.18.20.png b/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.18.20.png deleted file mode 100644 index 61151c6..0000000 Binary files a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.18.20.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.18.56.png b/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.18.56.png deleted file mode 100644 index 0b39967..0000000 Binary files a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.18.56.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.19.21.png b/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.19.21.png deleted file mode 100644 index 00b2439..0000000 Binary files a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.19.21.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.19.51.png b/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.19.51.png deleted file mode 100644 index 340dbfa..0000000 Binary files a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.19.51.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.20.48.png b/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.20.48.png deleted file mode 100644 index cba0786..0000000 Binary files a/09_BusinessAIWeek/Test2/Screenshot 2025-01-29 at 16.20.48.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/ai-foundation-architecture.png b/09_BusinessAIWeek/Test2/ai-foundation-architecture.png deleted file mode 100644 index 644a62a..0000000 Binary files a/09_BusinessAIWeek/Test2/ai-foundation-architecture.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/bananabread.png b/09_BusinessAIWeek/Test2/bananabread.png deleted file mode 100644 index 6abe5ca..0000000 Binary files a/09_BusinessAIWeek/Test2/bananabread.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/bas.png b/09_BusinessAIWeek/Test2/bas.png deleted file mode 100644 index 5ccc29a..0000000 Binary files a/09_BusinessAIWeek/Test2/bas.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/btp.png b/09_BusinessAIWeek/Test2/btp.png deleted file mode 100644 index 96dde7b..0000000 Binary files a/09_BusinessAIWeek/Test2/btp.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/chat.png b/09_BusinessAIWeek/Test2/chat.png deleted file mode 100644 index 823cc64..0000000 Binary files a/09_BusinessAIWeek/Test2/chat.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/chat1.png b/09_BusinessAIWeek/Test2/chat1.png deleted file mode 100644 index 1c7c383..0000000 Binary files a/09_BusinessAIWeek/Test2/chat1.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/chat2.png b/09_BusinessAIWeek/Test2/chat2.png deleted file mode 100644 index 4a00c89..0000000 Binary files a/09_BusinessAIWeek/Test2/chat2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/chat3.png b/09_BusinessAIWeek/Test2/chat3.png deleted file mode 100644 index b78f3a8..0000000 Binary files a/09_BusinessAIWeek/Test2/chat3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/chat_2.png b/09_BusinessAIWeek/Test2/chat_2.png deleted file mode 100644 index fde1098..0000000 Binary files a/09_BusinessAIWeek/Test2/chat_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/clone_git.png b/09_BusinessAIWeek/Test2/clone_git.png deleted file mode 100644 index 05d861e..0000000 Binary files a/09_BusinessAIWeek/Test2/clone_git.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/clone_git_2.png b/09_BusinessAIWeek/Test2/clone_git_2.png deleted file mode 100644 index 3439c4e..0000000 Binary files a/09_BusinessAIWeek/Test2/clone_git_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/configuration_o.png b/09_BusinessAIWeek/Test2/configuration_o.png deleted file mode 100644 index 5486bf1..0000000 Binary files a/09_BusinessAIWeek/Test2/configuration_o.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/configuration_o2.png b/09_BusinessAIWeek/Test2/configuration_o2.png deleted file mode 100644 index 7cf44a8..0000000 Binary files a/09_BusinessAIWeek/Test2/configuration_o2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/configurations.png b/09_BusinessAIWeek/Test2/configurations.png deleted file mode 100644 index a50e4cd..0000000 Binary files a/09_BusinessAIWeek/Test2/configurations.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/configurations_2.png b/09_BusinessAIWeek/Test2/configurations_2.png deleted file mode 100644 index 7e753ce..0000000 Binary files a/09_BusinessAIWeek/Test2/configurations_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/configurations_3.png b/09_BusinessAIWeek/Test2/configurations_3.png deleted file mode 100644 index 7e9b7e0..0000000 Binary files a/09_BusinessAIWeek/Test2/configurations_3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/configurations_4.png b/09_BusinessAIWeek/Test2/configurations_4.png deleted file mode 100644 index dd60a44..0000000 Binary files a/09_BusinessAIWeek/Test2/configurations_4.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/configurations_5.png b/09_BusinessAIWeek/Test2/configurations_5.png deleted file mode 100644 index 7628205..0000000 Binary files a/09_BusinessAIWeek/Test2/configurations_5.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/create_dev_space.png b/09_BusinessAIWeek/Test2/create_dev_space.png deleted file mode 100644 index 39429ca..0000000 Binary files a/09_BusinessAIWeek/Test2/create_dev_space.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/creds.json b/09_BusinessAIWeek/Test2/creds.json deleted file mode 100644 index 8cc9e1c..0000000 --- a/09_BusinessAIWeek/Test2/creds.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "serviceurls": { - "AI_API_URL": "https://api.ai.prodeuonly.eu-central-1.aws.ml.hana.ondemand.com" - }, - "appname": "49ec08a9-d325-4480-9418-ad8801558203!b28574|aicore!b18", - "clientid": "sb-49ec08a9-d325-4480-9418-ad8801558203!b28574|aicore!b18", - "clientsecret": "96d2ba69-3289-4190-ad82-c174e50f9f17$8C_adlgCYD6AscPgIKtLXJkIj1AL6i8p9Opw1JJZ0o8=", - "identityzone": "israel-fsvdxbsq", - "identityzoneid": "24f0ff13-74e1-4d3a-9b4c-17536e62ad78", - "url": "https://israel-fsvdxbsq.authentication.eu11.hana.ondemand.com", - "credential-type": "binding-secret" -} \ No newline at end of file diff --git a/09_BusinessAIWeek/Test2/deployment_o.png b/09_BusinessAIWeek/Test2/deployment_o.png deleted file mode 100644 index 772d7e7..0000000 Binary files a/09_BusinessAIWeek/Test2/deployment_o.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/deployment_o2.png b/09_BusinessAIWeek/Test2/deployment_o2.png deleted file mode 100644 index 6c5ee69..0000000 Binary files a/09_BusinessAIWeek/Test2/deployment_o2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/deployment_o3.png b/09_BusinessAIWeek/Test2/deployment_o3.png deleted file mode 100644 index f34eadd..0000000 Binary files a/09_BusinessAIWeek/Test2/deployment_o3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/deployments.png b/09_BusinessAIWeek/Test2/deployments.png deleted file mode 100644 index c207541..0000000 Binary files a/09_BusinessAIWeek/Test2/deployments.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/deployments_2.png b/09_BusinessAIWeek/Test2/deployments_2.png deleted file mode 100644 index 7f9e2c4..0000000 Binary files a/09_BusinessAIWeek/Test2/deployments_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/deployments_3.png b/09_BusinessAIWeek/Test2/deployments_3.png deleted file mode 100644 index c65f52c..0000000 Binary files a/09_BusinessAIWeek/Test2/deployments_3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/deployments_4.png b/09_BusinessAIWeek/Test2/deployments_4.png deleted file mode 100644 index b8a2007..0000000 Binary files a/09_BusinessAIWeek/Test2/deployments_4.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/deployments_5.png b/09_BusinessAIWeek/Test2/deployments_5.png deleted file mode 100644 index 1aa2638..0000000 Binary files a/09_BusinessAIWeek/Test2/deployments_5.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/dev_running.png b/09_BusinessAIWeek/Test2/dev_running.png deleted file mode 100644 index e07f913..0000000 Binary files a/09_BusinessAIWeek/Test2/dev_running.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/dev_starting.png b/09_BusinessAIWeek/Test2/dev_starting.png deleted file mode 100644 index 508e58a..0000000 Binary files a/09_BusinessAIWeek/Test2/dev_starting.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/documents/Orchestration_Service_Generative_AI_hub_SDK.pdf b/09_BusinessAIWeek/Test2/documents/Orchestration_Service_Generative_AI_hub_SDK.pdf deleted file mode 100644 index 979b277..0000000 Binary files a/09_BusinessAIWeek/Test2/documents/Orchestration_Service_Generative_AI_hub_SDK.pdf and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/documents/ai-foundation-architecture.png b/09_BusinessAIWeek/Test2/documents/ai-foundation-architecture.png deleted file mode 100644 index 644a62a..0000000 Binary files a/09_BusinessAIWeek/Test2/documents/ai-foundation-architecture.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/documents/bananabread.png b/09_BusinessAIWeek/Test2/documents/bananabread.png deleted file mode 100644 index 6abe5ca..0000000 Binary files a/09_BusinessAIWeek/Test2/documents/bananabread.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/enable_orchestration.png b/09_BusinessAIWeek/Test2/enable_orchestration.png deleted file mode 100644 index 973939d..0000000 Binary files a/09_BusinessAIWeek/Test2/enable_orchestration.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/extensions.png b/09_BusinessAIWeek/Test2/extensions.png deleted file mode 100644 index 2169866..0000000 Binary files a/09_BusinessAIWeek/Test2/extensions.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/BTP_cockpit.png b/09_BusinessAIWeek/Test2/images/BTP_cockpit.png deleted file mode 100644 index df1b18b..0000000 Binary files a/09_BusinessAIWeek/Test2/images/BTP_cockpit.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/BTP_cockpit_BAS.png b/09_BusinessAIWeek/Test2/images/BTP_cockpit_BAS.png deleted file mode 100644 index a80f45b..0000000 Binary files a/09_BusinessAIWeek/Test2/images/BTP_cockpit_BAS.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.18.20.png b/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.18.20.png deleted file mode 100644 index 61151c6..0000000 Binary files a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.18.20.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.18.56.png b/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.18.56.png deleted file mode 100644 index 0b39967..0000000 Binary files a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.18.56.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.19.21.png b/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.19.21.png deleted file mode 100644 index 00b2439..0000000 Binary files a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.19.21.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.19.51.png b/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.19.51.png deleted file mode 100644 index 340dbfa..0000000 Binary files a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.19.51.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.20.48.png b/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.20.48.png deleted file mode 100644 index cba0786..0000000 Binary files a/09_BusinessAIWeek/Test2/images/Screenshot 2025-01-29 at 16.20.48.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/bas.png b/09_BusinessAIWeek/Test2/images/bas.png deleted file mode 100644 index 5ccc29a..0000000 Binary files a/09_BusinessAIWeek/Test2/images/bas.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/btp.png b/09_BusinessAIWeek/Test2/images/btp.png deleted file mode 100644 index 96dde7b..0000000 Binary files a/09_BusinessAIWeek/Test2/images/btp.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/chat.png b/09_BusinessAIWeek/Test2/images/chat.png deleted file mode 100644 index 823cc64..0000000 Binary files a/09_BusinessAIWeek/Test2/images/chat.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/chat1.png b/09_BusinessAIWeek/Test2/images/chat1.png deleted file mode 100644 index 1c7c383..0000000 Binary files a/09_BusinessAIWeek/Test2/images/chat1.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/chat2.png b/09_BusinessAIWeek/Test2/images/chat2.png deleted file mode 100644 index 4a00c89..0000000 Binary files a/09_BusinessAIWeek/Test2/images/chat2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/chat3.png b/09_BusinessAIWeek/Test2/images/chat3.png deleted file mode 100644 index b78f3a8..0000000 Binary files a/09_BusinessAIWeek/Test2/images/chat3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/chat_2.png b/09_BusinessAIWeek/Test2/images/chat_2.png deleted file mode 100644 index fde1098..0000000 Binary files a/09_BusinessAIWeek/Test2/images/chat_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/clone_git.png b/09_BusinessAIWeek/Test2/images/clone_git.png deleted file mode 100644 index 05d861e..0000000 Binary files a/09_BusinessAIWeek/Test2/images/clone_git.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/clone_git_2.png b/09_BusinessAIWeek/Test2/images/clone_git_2.png deleted file mode 100644 index 3439c4e..0000000 Binary files a/09_BusinessAIWeek/Test2/images/clone_git_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/configuration_o.png b/09_BusinessAIWeek/Test2/images/configuration_o.png deleted file mode 100644 index 5486bf1..0000000 Binary files a/09_BusinessAIWeek/Test2/images/configuration_o.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/configuration_o2.png b/09_BusinessAIWeek/Test2/images/configuration_o2.png deleted file mode 100644 index 7cf44a8..0000000 Binary files a/09_BusinessAIWeek/Test2/images/configuration_o2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/configurations.png b/09_BusinessAIWeek/Test2/images/configurations.png deleted file mode 100644 index a50e4cd..0000000 Binary files a/09_BusinessAIWeek/Test2/images/configurations.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/configurations_2.png b/09_BusinessAIWeek/Test2/images/configurations_2.png deleted file mode 100644 index 7e753ce..0000000 Binary files a/09_BusinessAIWeek/Test2/images/configurations_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/configurations_3.png b/09_BusinessAIWeek/Test2/images/configurations_3.png deleted file mode 100644 index 7e9b7e0..0000000 Binary files a/09_BusinessAIWeek/Test2/images/configurations_3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/configurations_4.png b/09_BusinessAIWeek/Test2/images/configurations_4.png deleted file mode 100644 index dd60a44..0000000 Binary files a/09_BusinessAIWeek/Test2/images/configurations_4.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/configurations_5.png b/09_BusinessAIWeek/Test2/images/configurations_5.png deleted file mode 100644 index 7628205..0000000 Binary files a/09_BusinessAIWeek/Test2/images/configurations_5.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/create_dev_space.png b/09_BusinessAIWeek/Test2/images/create_dev_space.png deleted file mode 100644 index 39429ca..0000000 Binary files a/09_BusinessAIWeek/Test2/images/create_dev_space.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/deployment_o.png b/09_BusinessAIWeek/Test2/images/deployment_o.png deleted file mode 100644 index 772d7e7..0000000 Binary files a/09_BusinessAIWeek/Test2/images/deployment_o.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/deployment_o2.png b/09_BusinessAIWeek/Test2/images/deployment_o2.png deleted file mode 100644 index 6c5ee69..0000000 Binary files a/09_BusinessAIWeek/Test2/images/deployment_o2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/deployment_o3.png b/09_BusinessAIWeek/Test2/images/deployment_o3.png deleted file mode 100644 index f34eadd..0000000 Binary files a/09_BusinessAIWeek/Test2/images/deployment_o3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/deployments.png b/09_BusinessAIWeek/Test2/images/deployments.png deleted file mode 100644 index c207541..0000000 Binary files a/09_BusinessAIWeek/Test2/images/deployments.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/deployments_2.png b/09_BusinessAIWeek/Test2/images/deployments_2.png deleted file mode 100644 index 7f9e2c4..0000000 Binary files a/09_BusinessAIWeek/Test2/images/deployments_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/deployments_3.png b/09_BusinessAIWeek/Test2/images/deployments_3.png deleted file mode 100644 index c65f52c..0000000 Binary files a/09_BusinessAIWeek/Test2/images/deployments_3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/deployments_4.png b/09_BusinessAIWeek/Test2/images/deployments_4.png deleted file mode 100644 index b8a2007..0000000 Binary files a/09_BusinessAIWeek/Test2/images/deployments_4.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/deployments_5.png b/09_BusinessAIWeek/Test2/images/deployments_5.png deleted file mode 100644 index 1aa2638..0000000 Binary files a/09_BusinessAIWeek/Test2/images/deployments_5.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/dev_running.png b/09_BusinessAIWeek/Test2/images/dev_running.png deleted file mode 100644 index e07f913..0000000 Binary files a/09_BusinessAIWeek/Test2/images/dev_running.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/dev_starting.png b/09_BusinessAIWeek/Test2/images/dev_starting.png deleted file mode 100644 index 508e58a..0000000 Binary files a/09_BusinessAIWeek/Test2/images/dev_starting.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/enable_orchestration.png b/09_BusinessAIWeek/Test2/images/enable_orchestration.png deleted file mode 100644 index 973939d..0000000 Binary files a/09_BusinessAIWeek/Test2/images/enable_orchestration.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/extensions.png b/09_BusinessAIWeek/Test2/images/extensions.png deleted file mode 100644 index 2169866..0000000 Binary files a/09_BusinessAIWeek/Test2/images/extensions.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/model-library-2.png b/09_BusinessAIWeek/Test2/images/model-library-2.png deleted file mode 100644 index 1996dd2..0000000 Binary files a/09_BusinessAIWeek/Test2/images/model-library-2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/model-library-3.png b/09_BusinessAIWeek/Test2/images/model-library-3.png deleted file mode 100644 index b2cbe92..0000000 Binary files a/09_BusinessAIWeek/Test2/images/model-library-3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/model-library.png b/09_BusinessAIWeek/Test2/images/model-library.png deleted file mode 100644 index 39d4c64..0000000 Binary files a/09_BusinessAIWeek/Test2/images/model-library.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/open_workspace.png b/09_BusinessAIWeek/Test2/images/open_workspace.png deleted file mode 100644 index 26cd35e..0000000 Binary files a/09_BusinessAIWeek/Test2/images/open_workspace.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/prompt_editor.png b/09_BusinessAIWeek/Test2/images/prompt_editor.png deleted file mode 100644 index 0b292a1..0000000 Binary files a/09_BusinessAIWeek/Test2/images/prompt_editor.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/resource_group.png b/09_BusinessAIWeek/Test2/images/resource_group.png deleted file mode 100644 index 792f3c6..0000000 Binary files a/09_BusinessAIWeek/Test2/images/resource_group.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/resource_group_2.png b/09_BusinessAIWeek/Test2/images/resource_group_2.png deleted file mode 100644 index 0b4dab6..0000000 Binary files a/09_BusinessAIWeek/Test2/images/resource_group_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/scenarios.png b/09_BusinessAIWeek/Test2/images/scenarios.png deleted file mode 100644 index c51e25c..0000000 Binary files a/09_BusinessAIWeek/Test2/images/scenarios.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/scenarios_2.png b/09_BusinessAIWeek/Test2/images/scenarios_2.png deleted file mode 100644 index 440b498..0000000 Binary files a/09_BusinessAIWeek/Test2/images/scenarios_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/scenarios_3.png b/09_BusinessAIWeek/Test2/images/scenarios_3.png deleted file mode 100644 index 226575a..0000000 Binary files a/09_BusinessAIWeek/Test2/images/scenarios_3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/select_embedding_table.png b/09_BusinessAIWeek/Test2/images/select_embedding_table.png deleted file mode 100644 index f512103..0000000 Binary files a/09_BusinessAIWeek/Test2/images/select_embedding_table.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/select_kernel.png b/09_BusinessAIWeek/Test2/images/select_kernel.png deleted file mode 100644 index 21fe3f8..0000000 Binary files a/09_BusinessAIWeek/Test2/images/select_kernel.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/service-key.png b/09_BusinessAIWeek/Test2/images/service-key.png deleted file mode 100644 index 418f07a..0000000 Binary files a/09_BusinessAIWeek/Test2/images/service-key.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/service_binding.png b/09_BusinessAIWeek/Test2/images/service_binding.png deleted file mode 100644 index 346a1c4..0000000 Binary files a/09_BusinessAIWeek/Test2/images/service_binding.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/service_key.png b/09_BusinessAIWeek/Test2/images/service_key.png deleted file mode 100644 index 82c020b..0000000 Binary files a/09_BusinessAIWeek/Test2/images/service_key.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/setup0010.png b/09_BusinessAIWeek/Test2/images/setup0010.png deleted file mode 100644 index 6d2bc07..0000000 Binary files a/09_BusinessAIWeek/Test2/images/setup0010.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/setup0030.png b/09_BusinessAIWeek/Test2/images/setup0030.png deleted file mode 100644 index 42ecbb8..0000000 Binary files a/09_BusinessAIWeek/Test2/images/setup0030.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/setup0040.png b/09_BusinessAIWeek/Test2/images/setup0040.png deleted file mode 100644 index e3c7ecb..0000000 Binary files a/09_BusinessAIWeek/Test2/images/setup0040.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/setup0042.png b/09_BusinessAIWeek/Test2/images/setup0042.png deleted file mode 100644 index 55af409..0000000 Binary files a/09_BusinessAIWeek/Test2/images/setup0042.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/setup0060.png b/09_BusinessAIWeek/Test2/images/setup0060.png deleted file mode 100644 index 7a5b849..0000000 Binary files a/09_BusinessAIWeek/Test2/images/setup0060.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/start_terminal.png b/09_BusinessAIWeek/Test2/images/start_terminal.png deleted file mode 100644 index 3eeff1c..0000000 Binary files a/09_BusinessAIWeek/Test2/images/start_terminal.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/venv.png b/09_BusinessAIWeek/Test2/images/venv.png deleted file mode 100644 index 30eaef5..0000000 Binary files a/09_BusinessAIWeek/Test2/images/venv.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/images/workspace.png b/09_BusinessAIWeek/Test2/images/workspace.png deleted file mode 100644 index 88d9d71..0000000 Binary files a/09_BusinessAIWeek/Test2/images/workspace.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/init_env.py b/09_BusinessAIWeek/Test2/init_env.py deleted file mode 100644 index 21834de..0000000 --- a/09_BusinessAIWeek/Test2/init_env.py +++ /dev/null @@ -1,34 +0,0 @@ -import os -import json -import configparser - -from hdbcli import dbapi - -import variables - -ROOT_PATH_DIR = os.path.dirname(os.getcwd()) -AICORE_CONFIG_FILENAME = '.aicore-config.json' -USER_CONFIG_FILENAME = '.user.ini' - -def set_environment_variables() -> None: - with open(os.path.join(ROOT_PATH_DIR, AICORE_CONFIG_FILENAME), 'r') as config_file: - config_data = json.load(config_file) - - os.environ["AICORE_AUTH_URL"]=config_data["url"]+"/oauth/token" - os.environ["AICORE_CLIENT_ID"]=config_data["clientid"] - os.environ["AICORE_CLIENT_SECRET"]=config_data["clientsecret"] - os.environ["AICORE_BASE_URL"]=config_data["serviceurls"]["AI_API_URL"] - - os.environ["AICORE_RESOURCE_GROUP"]=variables.RESOURCE_GROUP - -def connect_to_hana_db() -> dbapi.Connection: - config = configparser.ConfigParser() - config.read(os.path.join(ROOT_PATH_DIR, USER_CONFIG_FILENAME)) - return dbapi.connect( - address=config.get('hana', 'url'), - port=config.get('hana', 'port'), - user=config.get('hana', 'user'), - password=config.get('hana', 'passwd'), - autocommit=True, - sslValidateCertificate=False - ) \ No newline at end of file diff --git a/09_BusinessAIWeek/Test2/model-library-2.png b/09_BusinessAIWeek/Test2/model-library-2.png deleted file mode 100644 index 1996dd2..0000000 Binary files a/09_BusinessAIWeek/Test2/model-library-2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/model-library-3.png b/09_BusinessAIWeek/Test2/model-library-3.png deleted file mode 100644 index b2cbe92..0000000 Binary files a/09_BusinessAIWeek/Test2/model-library-3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/model-library.png b/09_BusinessAIWeek/Test2/model-library.png deleted file mode 100644 index 39d4c64..0000000 Binary files a/09_BusinessAIWeek/Test2/model-library.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/open_workspace.png b/09_BusinessAIWeek/Test2/open_workspace.png deleted file mode 100644 index 26cd35e..0000000 Binary files a/09_BusinessAIWeek/Test2/open_workspace.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/prompt_editor.png b/09_BusinessAIWeek/Test2/prompt_editor.png deleted file mode 100644 index 0b292a1..0000000 Binary files a/09_BusinessAIWeek/Test2/prompt_editor.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/readme.md b/09_BusinessAIWeek/Test2/readme.md deleted file mode 100644 index 8b13789..0000000 --- a/09_BusinessAIWeek/Test2/readme.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/09_BusinessAIWeek/Test2/resource_group.png b/09_BusinessAIWeek/Test2/resource_group.png deleted file mode 100644 index 792f3c6..0000000 Binary files a/09_BusinessAIWeek/Test2/resource_group.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/resource_group_2.png b/09_BusinessAIWeek/Test2/resource_group_2.png deleted file mode 100644 index 0b4dab6..0000000 Binary files a/09_BusinessAIWeek/Test2/resource_group_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/scenarios.png b/09_BusinessAIWeek/Test2/scenarios.png deleted file mode 100644 index c51e25c..0000000 Binary files a/09_BusinessAIWeek/Test2/scenarios.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/scenarios_2.png b/09_BusinessAIWeek/Test2/scenarios_2.png deleted file mode 100644 index 440b498..0000000 Binary files a/09_BusinessAIWeek/Test2/scenarios_2.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/scenarios_3.png b/09_BusinessAIWeek/Test2/scenarios_3.png deleted file mode 100644 index 226575a..0000000 Binary files a/09_BusinessAIWeek/Test2/scenarios_3.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/select_embedding_table.png b/09_BusinessAIWeek/Test2/select_embedding_table.png deleted file mode 100644 index f512103..0000000 Binary files a/09_BusinessAIWeek/Test2/select_embedding_table.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/select_kernel.png b/09_BusinessAIWeek/Test2/select_kernel.png deleted file mode 100644 index 21fe3f8..0000000 Binary files a/09_BusinessAIWeek/Test2/select_kernel.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/service-key.png b/09_BusinessAIWeek/Test2/service-key.png deleted file mode 100644 index 418f07a..0000000 Binary files a/09_BusinessAIWeek/Test2/service-key.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/service_binding.png b/09_BusinessAIWeek/Test2/service_binding.png deleted file mode 100644 index 346a1c4..0000000 Binary files a/09_BusinessAIWeek/Test2/service_binding.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/service_key.png b/09_BusinessAIWeek/Test2/service_key.png deleted file mode 100644 index 82c020b..0000000 Binary files a/09_BusinessAIWeek/Test2/service_key.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/setup0010.png b/09_BusinessAIWeek/Test2/setup0010.png deleted file mode 100644 index 6d2bc07..0000000 Binary files a/09_BusinessAIWeek/Test2/setup0010.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/setup0030.png b/09_BusinessAIWeek/Test2/setup0030.png deleted file mode 100644 index 42ecbb8..0000000 Binary files a/09_BusinessAIWeek/Test2/setup0030.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/setup0040.png b/09_BusinessAIWeek/Test2/setup0040.png deleted file mode 100644 index e3c7ecb..0000000 Binary files a/09_BusinessAIWeek/Test2/setup0040.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/setup0042.png b/09_BusinessAIWeek/Test2/setup0042.png deleted file mode 100644 index 55af409..0000000 Binary files a/09_BusinessAIWeek/Test2/setup0042.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/setup0060.png b/09_BusinessAIWeek/Test2/setup0060.png deleted file mode 100644 index 7a5b849..0000000 Binary files a/09_BusinessAIWeek/Test2/setup0060.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/start_terminal.png b/09_BusinessAIWeek/Test2/start_terminal.png deleted file mode 100644 index 3eeff1c..0000000 Binary files a/09_BusinessAIWeek/Test2/start_terminal.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/variables.py b/09_BusinessAIWeek/Test2/variables.py deleted file mode 100644 index 2137ba3..0000000 --- a/09_BusinessAIWeek/Test2/variables.py +++ /dev/null @@ -1,11 +0,0 @@ -# TODO Change the resource group, embedding model ID and LLM/Chat model ID to your own -RESOURCE_GROUP = "" # your resource group e.g. team-01 -EMBEDDING_DEPLOYMENT_ID = "" # e.g. d6b74feab22bc49a -LLM_DEPLOYMENT_ID = "" - -# TODO Change the URL to your own orchestration deployment ID -AICORE_ORCHESTRATION_DEPLOYMENT_URL = "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2/inference/deployments/xxx" - -# HANA table name to store your embeddings -# TODO Change the name to your team name -EMBEDDING_TABLE = "EMBEDDINGS_CODEJAM_"+"ADD_YOUR_NAME_HERE" diff --git a/09_BusinessAIWeek/Test2/venv.png b/09_BusinessAIWeek/Test2/venv.png deleted file mode 100644 index 30eaef5..0000000 Binary files a/09_BusinessAIWeek/Test2/venv.png and /dev/null differ diff --git a/09_BusinessAIWeek/Test2/workspace.png b/09_BusinessAIWeek/Test2/workspace.png deleted file mode 100644 index 88d9d71..0000000 Binary files a/09_BusinessAIWeek/Test2/workspace.png and /dev/null differ diff --git a/09_BusinessAIWeek/workshop_notebook-final.ipynb b/09_BusinessAIWeek/workshop_notebook-final.ipynb deleted file mode 100644 index 885a482..0000000 --- a/09_BusinessAIWeek/workshop_notebook-final.ipynb +++ /dev/null @@ -1,1151 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "b3defa59-142d-4906-add5-b809d01d126b", - "metadata": {}, - "source": [ - "## Utilizing RAG Embeddings for Enhanced Developer Support\n", - "\n", - "Bob is a Content developer in the XYZ company. His team works on a node library that can simplify the software development process inside and outside the company. His team has designed a dedicated documentation for the library. But the developers who use the library always face issues and contact Bob. To make his job easy, Bob decided to use Generative AI for this Use case. \n", - "\n", - "Bob uses the Generative AI Hub SDK to create a help portal for his library. He wants to Improve the effectiveness of our help portal by integrating RAG (Retrieval-Augmented Generation) embeddings so that Developers can receive more contextually relevant responses to their queries about the node library. \n", - "\n", - "Acceptance criteria: \n", - "\n", - "- Bob utilizes the Generative AI Hub SDK to integrate a generative AI model into the help portal for the company's node library. \n", - "\n", - "- Bob configures and deploys the generative AI model within the AI core instance of the company's infrastructure. \n", - "\n", - "- The help portal powered by Generative AI should provide intuitive and contextually relevant responses to developers' inquiries about using the node library. \n", - "\n", - "- The generative AI model is trained on the documentation of the node library to ensure accurate and informative responses. \n", - "\n", - "- The help portal interface is user-friendly, allowing developers to easily interact with the generative AI model and find answers to their questions. \n", - "\n", - "- Bob monitors the performance of the generative AI model regularly, ensuring that it continues to provide accurate and helpful responses to developers' queries. \n", - "\n", - "- Bob documents the process of integrating the Generative AI Help Portal into the company's support workflow, providing clear instructions for future maintenance and updates. \n", - "\n", - "- Bob Queries the model \n", - "\n", - "- Implements RAG embeddings \n", - "\n", - "- Enhances Query responses \n", - "\n", - "- Bob conducts training sessions or provides documentation to the support team, educating them on how to leverage the Generative AI Help Portal effectively to assist developers. \n", - "\n", - "### Setting up LLM's on AI core\n", - "\n", - "- Bob navigates to ML Operations -> Scenarios in the AI-Core workspace. \n", - "\n", - "- He ensures that the foundation-models scenario is present. \n", - "\n", - "- Bob goes to ML Operations -> Configurations and creates a new configuration: \n", - "\n", - "- Names it appropriately. \n", - "\n", - "- Selects the foundation-models scenario. \n", - "\n", - "- Chooses the version and executable ID. \n", - "\n", - "- He sets input parameters specifying the model name and version. \n", - "\n", - "- Bob reviews and creates the configuration. \n", - "\n", - "Model Deployment: \n", - "\n", - "- Bob creates a deployment for the configured model, setting the duration to standard. \n", - "\n", - "- After the deployment status changes to RUNNING, he verifies its availability. \n", - "\n", - "Model Integration: \n", - "\n", - "- Bob installs the Generative AI Hub SDK by running pip3 install generative-ai-hub-sdk. \n", - "\n", - "- He configures the SDK using AI core keys stored in the local directory. \n", - "\n", - "\n", - "\n", - "- Bob uses Python code to generate responses for queries using the SDK. \n", - "\n", - "- He formats the query and invokes the Generative AI Hub SDK to fetch responses. \n", - "\n", - "Implementing RAG Embeddings: \n", - "\n", - "- Bob prepares the documentation for the node library in CSV format with each row representing a topic. \n", - "\n", - "- He connects to the HANA vector storage instance and creates a table to store the documentation data. \n", - "\n", - "- Bob populates the table with data and creates a REAL_VECTOR column to store embeddings. \n", - "\n", - "- Using the Generative AI Hub SDK, he defines a function to generate embeddings for prompts and performs similarity search using the embeddings. \n", - "\n", - "Enhancing Query Responses: \n", - "\n", - "- Bob defines a prompt template to provide context to queries. \n", - "\n", - "- He modifies the function to query the LLM (Large Language Model) based on the prompt template. \n", - "\n", - "- Bob tests the model's response using specific queries related to the node library, ensuring it provides contextually relevant responses based on embeddings. Bob installs Jupyter Notebook using pip3 install notebook. \n", - "\n", - "Retrieval augmented generation optimizes the output of large language models by giving more contexts to your prompts.\n", - "\n", - "In this workshop, we use a [graph document](files/GRAPH_DOCU_2503.csv) to give more context to the model, thus optimiz\n", - "iLoad the document into the data variable. is a structure that contains two-dimensional data and its corresponding labels." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6fb6d05e", - "metadata": {}, - "outputs": [], - "source": [ - "!pip3 install langchain openai docarray panel jupyter_bokeh redlines tiktoken wikipedia DateTime presidio_anonymizer hana-ml pandas presidio_analyzer\n", - "!pip3 install generative-ai-hub-sdk" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e3d388df", - "metadata": {}, - "outputs": [], - "source": [ - "# An alternative way to config.json to set the environment variables for gen-ai-hub-sdk\n", - "\n", - "import os\n", - "\n", - "# please enter the Credentials from your AI core landscape.\n", - "env_vars = {\n", - " 'AICORE_AUTH_URL' : '',\n", - " 'AICORE_CLIENT_ID' : '',\n", - " 'AICORE_CLIENT_SECRET' : '',\n", - " 'AICORE_BASE_URL' : '/v2',\n", - " 'AICORE_RESOURCE_GROUP' : '',\n", - "}\n", - "\n", - "# Set the environment variables using `os.environ`.\n", - "for key, value in env_vars.items():\n", - " os.environ[key] = value" - ] - }, - { - "cell_type": "markdown", - "id": "a2456b27", - "metadata": {}, - "source": [ - "The data in GRAPH_DOCU_QRC3.csv already contains vector embeddings. The only reason why we included the vectors in the csv is to enable colleagues to try out vector search very fast… no need to run the content through an embedding function.\n", - "\n", - "This dataset is created from the SAP HANA Cloud documentation, in specific the Graph Engine part (https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-graph-reference/sap-hana-cloud-sap-hana-database-graph-reference).\n", - "SAP Help Portal" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8e59c835-d853-429a-bbbf-78d1c9894586", - "metadata": {}, - "outputs": [], - "source": [ - "import csv\n", - "\n", - "data = []\n", - "with open('GRAPH_DOCU_2503.csv', encoding='utf-8') as csvfile:\n", - " csv_reader = csv.reader(csvfile)\n", - " for row in csv_reader:\n", - " try:\n", - " data.append(row)\n", - " except:\n", - " print(row)" - ] - }, - { - "cell_type": "markdown", - "id": "e8fe7f05-ae81-46b2-a9bd-ec4eddc31f6a", - "metadata": {}, - "source": [ - "\n", - "\n", - "Now we can create a connection to the HANA Vector storage. Replace the address, port-number, username, and password from the values in your credentials." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "417f80d2-29e4-4fc1-8b59-1304425bf2f0", - "metadata": {}, - "outputs": [], - "source": [ - "import hdbcli\n", - "from hdbcli import dbapi\n", - "\n", - "cc = dbapi.connect(\n", - " address='',\n", - " port='443',\n", - " user='',\n", - " password='',\n", - " encrypt=True\n", - " )\n" - ] - }, - { - "cell_type": "markdown", - "id": "42a8ea5e-0ae7-442e-8f69-97e22cee88bf", - "metadata": {}, - "source": [ - "Now we have created a connection to the HANA vector storage. This connection will help us to work with the vector storage in the upcoming steps.\n", - "\n", - "HANA vector storage stores data in form of tables. To store our data, we can create a table in our HANA vector storage.\n", - "\n", - "Replace the TABLENAME with a name of your choice. **Make sure that you use only Uppercase letters and numbers in the tablename**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a55e535a-1997-42f9-a56b-822d0f6d3eb8", - "metadata": {}, - "outputs": [], - "source": [ - "# Create a table\n", - "cursor = cc.cursor()\n", - "sql_command = '''CREATE TABLE TABLENAME(ID1 BIGINT, ID2 BIGINT, L1 NVARCHAR(3), L2 NVARCHAR(3), L3 NVARCHAR(3), FILENAME NVARCHAR(100), HEADER1 NVARCHAR(5000), HEADER2 NVARCHAR(5000), TEXT NCLOB, VECTOR_STR REAL_VECTOR);'''\n", - "cursor.execute(sql_command)\n", - "cursor.close()" - ] - }, - { - "cell_type": "markdown", - "id": "1968b049-c95e-4d3d-92f2-df853e66fce9", - "metadata": {}, - "source": [ - "In the SQL command, we are creating a table called 'GRAPH_DOCU_2503'. You can give any name of your choice, but make sure that you use the same table name throughout. Also keep in mind that you cannot create multiple tables with the same name.\n", - "\n", - "Once you have created the table, you can populate the table with our data which we have converted to the dataframe in the previous step. Here the SQL insert is inserting everything from the CSV file that includes Text chunks, Embeddings and metadata. this is to show how you can use can insert the text chunks to hana vector DB for RAG to be implemented.\n", - "\n", - "Replace the TABLENAME with the name of your table. this for insertion of embeddings that are pre-existing in a file." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "641461c5-1c5d-45b5-999a-805816b3ff7b", - "metadata": {}, - "outputs": [], - "source": [ - "cursor = cc.cursor()\n", - "sql_insert = 'INSERT INTO TABLENAME(ID1, ID2, L1, L2, L3, FILENAME, HEADER1, HEADER2, TEXT, VECTOR_STR) VALUES (?,?,?,?,?,?,?,?,?,TO_REAL_VECTOR(?))'\n", - "cursor.executemany(sql_insert,data[1:])" - ] - }, - { - "cell_type": "markdown", - "id": "e7efc76b-5542-425e-a722-567f2ce12d63", - "metadata": {}, - "source": [ - "\n", - "Embeddings are vector representations of text data that incorporates the semantic meaning of the text. Define a get_embedding() function that generates embeddings from text data using the text-embedding-ada-002 model. This function will be used to generate embeddings from the user's prompts.\n", - "\n", - "For example if user enters the prompt for \"How can I run a shortest path algorithm?\" the below get_embeddings function would be used to convert the user prompt to embedding which would be used to search similar data in Hana DB using similarity search." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0a1d1974-c692-4b30-8a2a-a044f71fa654", - "metadata": {}, - "outputs": [], - "source": [ - "# Get embeddings\n", - "from gen_ai_hub.proxy.native.openai import embeddings\n", - "\n", - "def get_embedding(input, model=\"text-embedding-ada-002\") -> str:\n", - " response = embeddings.create(\n", - " model_name=model,\n", - " input=input\n", - " )\n", - " return response.data[0].embedding" - ] - }, - { - "cell_type": "markdown", - "id": "853dcfd7-ffa9-4d46-a66e-5a673473174d", - "metadata": {}, - "source": [ - "\n", - "The embeddings can be used to find texts that are similar to each other using techniques such as COSINE-SIMILARITY. You can use such techniques to find text data similar to your prompt from the HANA vector store.\n", - "\n", - "Now define a function to do similarity search in your data. This function takes a query as input and returns similar content from the table.\n", - "\n", - "Replace the TABLENAME with the name of your table.\n", - "\n", - "Here in the below code the code we are trying to find the similar data to users query where it is calling get_embedding functions and storing it in query_vector and with a simple select query to Hana DB we can find the 4 similar embeddings data using COSINE_SIMILARITY as search method." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "548537af-f2cb-4955-ba39-4d9407ad9acc", - "metadata": {}, - "outputs": [], - "source": [ - "cursor = cc.cursor()\n", - "def run_vector_search(query: str, metric=\"COSINE_SIMILARITY\", k=4):\n", - " if metric == 'L2DISTANCE':\n", - " sort = 'ASC'\n", - " else:\n", - " sort = 'DESC'\n", - " query_vector = get_embedding(query)\n", - " sql = '''SELECT TOP {k} \"ID2\", \"TEXT\"\n", - " FROM \"TABLENAME\"\n", - " ORDER BY \"{metric}\"(\"VECTOR_STR\", TO_REAL_VECTOR('{qv}')) {sort}'''.format(k=k, metric=metric, qv=query_vector, sort=sort)\n", - " cursor.execute(sql)\n", - " hdf = cursor.fetchall()\n", - " return hdf[:k]" - ] - }, - { - "cell_type": "markdown", - "id": "94990eca-90d2-4aa2-8833-a3187c47aeba", - "metadata": {}, - "source": [ - "We can define a prompt template to provide context to our prompts. Thus, when we pass a prompt to the model, the template will add the necessary context to the prompt so that better results are generated." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bd39bd7c-0234-4b3f-9bd3-0329524ed18e", - "metadata": {}, - "outputs": [], - "source": [ - "promptTemplate_fstring = \"\"\"\n", - "You are an SAP HANA Cloud expert.\n", - "You are provided multiple context items that are related to the prompt you have to answer.\n", - "Use the following pieces of context to answer the question at the end. \n", - "Context:\n", - "{context}\n", - "Question:\n", - "{query}\n", - "\"\"\"\n", - "from langchain.prompts import PromptTemplate\n", - "promptTemplate = PromptTemplate.from_template(promptTemplate_fstring)" - ] - }, - { - "cell_type": "markdown", - "id": "ffbd6bd2-1443-430c-8248-febc27512bdd", - "metadata": {}, - "source": [ - "Here we have created a template for the prompts. It contains two variables, 'context' and 'query'. These variables will be replaced with the context and query in the upcoming steps \n", - "\n", - "Now we can define a function ask_llm() that queries a model using the above template. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4b9c7f4b-a42f-462d-a2bb-a77f8ba41bec", - "metadata": {}, - "outputs": [], - "source": [ - "!pip install tiktoken\n", - "import tiktoken\n", - "from gen_ai_hub.proxy.langchain.openai import ChatOpenAI\n", - "\n", - "def ask_llm(query: str, metric='COSINE_SIMILARITY', k = 4) -> str:\n", - " context = ''\n", - " context = run_vector_search(query, metric, k)\n", - " prompt = promptTemplate.format(query=query, context=' '.join(str(context)))\n", - " encoding = tiktoken.get_encoding(\"cl100k_base\")\n", - " num_tokens = len(encoding.encode(str(prompt)))\n", - " print('no of tokens'+ str(num_tokens))\n", - " llm = ChatOpenAI(proxy_model_name='gpt-4-32k',max_tokens = 8000)\n", - " response = llm.invoke(prompt).content\n", - " print('Query: '+ query)\n", - " print('\\nResponse:')\n", - " print(response)" - ] - }, - { - "cell_type": "markdown", - "id": "52fa8a66-8241-4e81-a7d9-c632239edbdf", - "metadata": {}, - "source": [ - "The above function appends the query and context into the template to create a prompt. Then that prompt is passed on to the LLM model and the response is printed.\n", - "\n", - "Here we are using gpt-4 model. Make sure that you have already created a deployment for the gpt-4 model in AI Launchpad.\n", - "\n", - "We can compare how the output produced by RAG is different from the output when we directly pass the prompt to the model. If we directly pass the prompt to the model withouth RAG, this will be the output. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e617f819-e8fd-42ed-b5c1-b3679d3c187c", - "metadata": {}, - "outputs": [], - "source": [ - "query = \"How can I run a shortest path algorithm?\"\n", - "from gen_ai_hub.proxy.langchain.openai import ChatOpenAI\n", - "llm = ChatOpenAI(proxy_model_name='gpt-4-32k')\n", - "response = llm.invoke(query).content\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "6bf4ffbe-e081-4f7c-80b8-5a5e2f54da30", - "metadata": {}, - "source": [ - "Now we can test the function by passing the following query to generate results using retrieval augmented generation." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8b61ff1a-6d15-4f2e-a17a-eb51d02c6afe", - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "query = \"I want to calculate a shortest path. How do I do that?\"\n", - "response = ask_llm(query=query, k=4)\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "d38bdada-6fb3-45dd-b314-4027b1beef71", - "metadata": {}, - "source": [ - "Here we can see that we get better output when we use RAG\n", - "\n", - "**Outcome:** By implementing RAG embeddings in the help portal, Bob enhances the support experience for developers using the node library. The help portal now offers more accurate and contextual responses to queries, reducing dependency on manual support and empowering developers to find solutions independently. " - ] - }, - { - "cell_type": "markdown", - "id": "4f3870db-1e0d-45f9-9e28-0fb51d9f5698", - "metadata": {}, - "source": [ - "## Prompting the model\n", - "\n", - "The LLM can do a wide variety of tasks using prompting techniques. By efficient use of prompts, the models can achieve good results in the following tasks\n", - "\n", - "**Content creation using RAG**\n", - "\n", - "The model can generate text content for articles, blogs, stories, etc." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bc31aec6-e78f-45d1-be84-1369686e9887", - "metadata": {}, - "outputs": [], - "source": [ - "query = \"Write an article on the new features in graphs.\"\n", - "response = ask_llm(query=query, k=4)\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "357784ec-0951-4efc-9d85-22986ec5b43e", - "metadata": {}, - "source": [ - "\n", - "**Language Translation using RAG**\n", - "\n", - "If you want to get the output in another language, you can ask the model to do so in the prompt." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "86afecfb-32d4-48d3-a40f-d8f1f35d53e1", - "metadata": {}, - "outputs": [], - "source": [ - "query = \"How can I find the shortest distance between two nodes? Give the output in Dutch.\"\n", - "response = ask_llm(query=query, k=4)\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "42747555-60a1-45de-94fd-aaa29f729add", - "metadata": {}, - "source": [ - "**Text Completion using RAG**\n", - "\n", - "The models can be used for text completions. By giving a text input, the models suggests completions for the given text." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bf016cb7-8bde-4ff2-a5c3-c36978175cd5", - "metadata": {}, - "outputs": [], - "source": [ - "query = \"Graphs can be a useful tool for computer scientists when ...\"\n", - "response = ask_llm(query=query, k=4)\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "90f78e44-1ab4-4ba6-8787-e3a632de4c16", - "metadata": {}, - "source": [ - "**Text Classification using RAG**\n", - "\n", - "The models can be used to classify text based on their semantics. In the following example, the model can classify whether a question is related to graphs or related to business." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e0fc730b-59a0-4f7b-9939-887e60d47ceb", - "metadata": {}, - "outputs": [], - "source": [ - "query = '''Classify the following questions into graph related and business related questions. \n", - "\n", - " 1. How does diversification reduce investment risk?\n", - " 2. Describe the role of dividends in stock investing.\n", - " 3. What is the degree of a node in a graph, and how is it calculated?\n", - " 4. What factors influence consumer behavior in marketing?\n", - " 5. Explain the basic principles of the law of diminishing returns.\n", - " 6. What is the concept of opportunity cost in economics?\n", - " 7. What is the definition of a graph in graph theory?\n", - " 8. How are directed and undirected graphs distinguished from each other?\n", - " 9. Explain the difference between a path and a cycle in a graph.\n", - " 10. What is a connected graph, and why is it significant in the study of graphs?'''\n", - "response = ask_llm(query=query, k=4)\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "8867db6b-8679-4d62-bdae-8310191dcf58", - "metadata": {}, - "source": [ - "\n", - "**Tone adjustment using RAG**\n", - "\n", - "The models can adjust the tone of a content according to the instructions given. In the following example, we see a snippet from an article that has an anger tone, and we ask the LLM to make it professional." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f456cfae-2559-4da2-afd0-488511491c99", - "metadata": {}, - "outputs": [], - "source": [ - "query = '''\n", - " The following conent is not fit for a professional document. Rewrite the same in a professional tone. \n", - " \n", - " I can't believe we're still dealing with this nonsense! If you dare to mess around with the referenced graph tables without ensuring the consistency of the graph workspace through referential constraints, you're practically asking for chaos! And let me tell you, when that happens, each blasted Graph Engine component behaves differently! It's like dealing with a bunch of unruly children who can't agree on a single thing!\n", - "\n", - " Check out the CREATE GRAPH WORKSPACE Statement and the DROP GRAPH WORKSPACE Statement. Maybe if you bothered to read them properly, you wouldn't be stuck in this mess! But no, here we are, dealing with your incompetence and the fallout of your reckless actions. Get your act together and start taking responsibility for maintaining a freaking consistent graph workspace!'''\n", - "response = ask_llm(query=query, k=4)\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "e1709b16-1ec7-44a5-9b47-0fe787f0a607", - "metadata": {}, - "source": [ - "\n", - "**Spell Check / Grammar Check using RAG**\n", - "\n", - "The model can also detect the spelling and grammatical issues in a document and correct them promptly" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4e691c9b-7c20-45a7-82d0-99f4a7079a1f", - "metadata": {}, - "outputs": [], - "source": [ - "query = '''\n", - " Proofread and rewrite the following sentence:\n", - " The GraphScript langaguae will eases the developomont of application-specific graph algthrihms and integrates them into SQL-based data procezzing.\n", - " '''\n", - "response = ask_llm(query=query, k=4)\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "00538abf-712f-46f4-a6ad-dc00a8c3412c", - "metadata": {}, - "source": [ - "## Using data from reddit for Retrieval Augmented Generation\n", - "\n", - "As a Content Developer at XYZ Company, Bob's journey to improve developer support with embeddings encounters a new challenge when Alice, a colleague in his team, discovers critical information on Reddit. Here's how it integrates into the user story: \n", - "\n", - "- Discovery of Critical Information: \n", - "\n", - " - Alice notices a Reddit post highlighting issues with a deprecated function in the node library. Recognizing the potential impact on developers, she investigates and finds a workaround, which she promptly shares as a reply to the post. \n", - "\n", - "- Increased Support Queries: \n", - " - The following day, Bob's team receives numerous inquiries related to the same issue discussed on Reddit. Despite Alice's timely response on the platform, developers continue to face challenges, leading to an influx of support queries. \n", - "\n", - "- Addressing Model Limitations: \n", - "\n", - " - Bob identifies a gap between the support provided by their existing model and the real-time information available on Reddit. He realizes that the model lacks the updated context from Reddit discussions, leading to outdated responses based on the deprecated function. \n", - "\n", - "- Integration of Reddit Data: \n", - "\n", - " - To bridge this gap, Bob decides to connect the model to Reddit using the Reddit API. \n", - "\n", - " - He retrieves relevant data from the XYZNODE subreddit, which contains discussions pertinent to the node library. \n", - "\n", - " - By extracting post titles, content, and comments, Bob ensures that the model gains access to the latest insights and solutions shared by users, including Alice's workaround. \n", - "\n", - "- Storing and Processing Reddit Data: \n", - "\n", - " - Bob structures the retrieved Reddit data and stores it in a suitable format for analysis. \n", - "\n", - " - He creates a new table in the company's vector storage, enabling efficient storage and retrieval of Reddit data along with embeddings for enhanced processing. \n", - "\n", - "- Utilizing Reddit Context in Model Queries: \n", - "\n", - " - Bob modifies the existing functions, run_vector_search() and ask_llm(), to incorporate context from Reddit data and comments. \n", - "\n", - " - He ensures that the model considers the updated context from Reddit discussions when generating responses to developer queries. \n", - "\n", - "- Testing and Validation: \n", - "\n", - " - Bob conducts thorough testing to validate the integration of Reddit data into the model. \n", - "\n", - " - He verifies that the model's responses now reflect the latest information and solutions discussed on Reddit, including Alice's workaround for the deprecated function. \n", - "\n", - "\n", - "You can connect to reddit using their api to use their data for retrieval augmented generation. In this example, I am using the data from a subreddit called 'python' to do Retrieval Augmented Generation about python programming language.\n", - "\n", - "Connect to the reddit api using your credentials." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cea9a450-2a44-4d1f-9803-9a0fed2a6c08", - "metadata": {}, - "outputs": [], - "source": [ - "CLIENT_ID = \"\"\n", - "SECRET_KEY = \"\"\n", - "import requests\n", - "auth = requests.auth.HTTPBasicAuth(CLIENT_ID,SECRET_KEY)\n", - "data = {\n", - " 'grant_type':'password',\n", - " 'username':'',\n", - " 'password':''\n", - "}\n", - "headers = {'User-Agent':'MyAPI/0.0.1'}\n", - "res = requests.post('https://www.reddit.com/api/v1/access_token',auth=auth,data=data,headers=headers)\n", - "TOKEN = res.json()['access_token']\n", - "headers['Authorization'] = f'bearer {TOKEN}'" - ] - }, - { - "cell_type": "markdown", - "id": "9a29f5fc-6524-4863-bd0d-106643f6fa82", - "metadata": {}, - "source": [ - "Here we used our credentials to generate a token for the reddit API. The token will be stored in the headers, which will be passed along with every API call.\n", - "\n", - "Now we can access the posts on the subreddit 'saphanagraph'." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "92eb09f8-d41c-4a04-a8f6-10a727700bb6", - "metadata": {}, - "outputs": [], - "source": [ - "reddit_data = requests.get('https://oauth.reddit.com/r/saphanagraph',headers=headers).json()['data']['children']" - ] - }, - { - "cell_type": "markdown", - "id": "0e37b9a8-67f4-4b80-a4e9-2be78b2fe376", - "metadata": {}, - "source": [ - "Now run the below code snippet to extract the comments and replies of all the posts in the subreddit. in comments variable." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b847a56a-4494-485c-9077-94c23939ba3a", - "metadata": {}, - "outputs": [], - "source": [ - "ids = [data['data']['id'] for data in reddit_data]\n", - "comments = {}\n", - "for i in ids:\n", - " comment_data = requests.get(f'https://oauth.reddit.com/r/saphanagraph/comments/{i}',headers=headers).json()\n", - " comments[f\"{i}\"] = []\n", - " def parse_comments(data):\n", - " for j in data:\n", - " comments[f\"{i}\"].append(j['data']['body'])\n", - " if j['data']['replies'] != '':\n", - " parse_comments(j['data']['replies']['data']['children'])\n", - " if(len(comment_data[1]['data']['children'])):\n", - " parse_comments(comment_data[1]['data']['children'])" - ] - }, - { - "cell_type": "markdown", - "id": "e1f3047a-073d-485c-a442-2a6b0459b6d3", - "metadata": {}, - "source": [ - "Now generate the vector embeddings for the comments and create a pandas dataframe with the data extracted from the reddit, and their vector embeddings. Before sending the data to model we would anonymize the data and then Send the data if content has positive intention will save it as embeddings." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "03394243-d854-4085-ba84-9c0d07b2c6bc", - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.proxy.langchain.openai import ChatOpenAI\n", - "\n", - "def user_intention(query) -> str:\n", - "\n", - " llm = ChatOpenAI(proxy_model_name='gpt-4-32k')\n", - " response = llm.invoke(\"Classify the following query in one word as negative or positive : \" + query).content\n", - " if \"negative\" in response.lower():\n", - " return False\n", - " else:\n", - " return True\n", - " \n", - "from gen_ai_hub.proxy.native.openai import embeddings\n", - "\n", - "def get_embedding(input, model=\"text-embedding-ada-002\") -> str:\n", - " response = embeddings.create(\n", - " model_name=model,\n", - " input=input\n", - " )\n", - " return response.data[0].embedding" - ] - }, - { - "cell_type": "markdown", - "id": "b3d3a54d-3c8a-4e65-be9f-d3d67008133f", - "metadata": {}, - "source": [ - "Here we defined a user_intention() function that classifies data from reddit into positive and negative data. \n", - "\n", - "Anonymizing content and removing content that has negative reviews" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "71f59e3a-5201-4917-9e44-70ecd3c39dee", - "metadata": {}, - "outputs": [], - "source": [ - "!pip install presidio-anonymizer\n", - "import pandas as pd\n", - "from presidio_anonymizer.entities import OperatorConfig\n", - "from presidio_analyzer import AnalyzerEngine, BatchAnalyzerEngine, RecognizerResult, DictAnalyzerResult\n", - "\n", - "analyzer = AnalyzerEngine(log_decision_process=False)\n", - "batch_analyzer = BatchAnalyzerEngine(analyzer_engine=analyzer)\n", - "\n", - "extracted_data = []\n", - "for post in enumerate(reddit_data):\n", - " post_content = post[1]['data']['selftext']\n", - " post_content = reddit_data[0]['data']['selftext']\n", - " anonymizer_results = analyzer.analyze(text=post_content, entities=[\"PERSON\", \"EMAIL_ADDRESS\", \"IP_ADDRESS\"], language=\"en\", return_decision_process=True)\n", - " for i in anonymizer_results:\n", - " post_content = post_content.replace(i.entity_value, i.anonymized_value)\n", - " if user_intention(post_content):\n", - " row = [post[0]+1]\n", - " row.append(post[1]['data']['title'])\n", - " row.append(post_content)\n", - " row.append(str(comments[post[1]['data']['id']]))\n", - " row.append(str(get_embedding(row[1])))\n", - " extracted_data.append(row)\n", - "# df = pd.DataFrame(extracted_data)\n", - "# df.columns = [\"ID\", \"Title\", \"Post\", \"Comments\", \"Post-Vector\"]" - ] - }, - { - "cell_type": "markdown", - "id": "173ecc60-8d0e-4688-a24b-6a35ae487b82", - "metadata": {}, - "source": [ - "You might have to restart your notebook after running the above code. Once you restart the notebook, start again from connecting to the reddit API.\n", - "\n", - "In the above code, we extracted the data from reddit and stored it in a dataframe. We used a batch_analyzer and batch_anonymizer to remove sensitive data from the data coming from reddit.\n", - "\n", - "\n", - "Upload the data to your HANA vector storage as we saw in the previous section.\n", - "\n", - "Add your credentials to create your connectionContext\n", - "\n", - "Replace TABLENAME with a new table that you want to create. **Make sure that you use only uppercase letters and numbers in the tablename.**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "db56769c-415b-462a-8056-4a1712d1044f", - "metadata": {}, - "outputs": [], - "source": [ - "from hana_ml import ConnectionContext\n", - "from hana_ml.dataframe import create_dataframe_from_pandas\n", - "\n", - "cc= ConnectionContext(\n", - " address='',\n", - " port='443',\n", - " user='',\n", - " password='',\n", - " encrypt=True\n", - " )\n", - "\n", - "\n", - "cursor = cc.connection.cursor()\n", - "sql_command = '''CREATE TABLE TABLENAME(ID BIGINT, TITLE NCLOB, TEXT NCLOB, COMMENT NCLOB, VECTOR_STR REAL_VECTOR);'''\n", - "cursor.execute(sql_command)\n", - "cursor.close()\n", - "\n", - "# extracted_data contains the data to be inserted into the table\n", - "cursor = cc.connection.cursor()\n", - "sql_insert = 'INSERT INTO TABLENAME(ID, TITLE, TEXT, COMMENT, VECTOR_STR) VALUES (?,?,?,?,TO_REAL_VECTOR(?))'\n", - "cursor.executemany(sql_insert,extracted_data)\n" - ] - }, - { - "cell_type": "markdown", - "id": "8f7f75bb-3e48-4973-ac2b-1290787d23d2", - "metadata": {}, - "source": [ - "\n", - "The embeddings can be used to find texts that are similar to each other using techniques such as COSINE-SIMILARITY. You can use such techniques to find text data similar to your prompt from the HANA vector store.\n", - "\n", - "Now define a function to do similarity search in your data. This function takes a query as input and returns similar content from the table.\n", - "\n", - "Replace the TABLENAME with the name of your table." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d754c658-8603-425c-9abd-47dd862b5c72", - "metadata": {}, - "outputs": [], - "source": [ - "def run_vector_search(query: str, metric=\"COSINE_SIMILARITY\", k=4):\n", - " if metric == 'L2DISTANCE':\n", - " sort = 'ASC'\n", - " else:\n", - " sort = 'DESC'\n", - " query_vector = get_embedding(query)\n", - " sql = '''SELECT TOP {k} \"TITLE\", \"TEXT\", \"COMMENT\"\n", - " FROM \"TABLENAME\"\n", - " ORDER BY \"{metric}\"(\"VECTOR_STR\", TO_REAL_VECTOR('{qv}')) {sort}'''.format(k=k, metric=metric, qv=query_vector, sort=sort)\n", - " hdf = cc.sql(sql)\n", - " df_context = hdf.head(k).collect()\n", - " return df_context" - ] - }, - { - "cell_type": "markdown", - "id": "f9aea3a9-1283-4942-b4fa-8908f8f12fde", - "metadata": {}, - "source": [ - "We can define a prompt template to provide context to our prompts. Thus, when we pass a prompt to the model, the template will add the necessary context to the prompt so that better results are generated." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f80a5e94-c037-4704-acd4-fd92e102ff1c", - "metadata": {}, - "outputs": [], - "source": [ - "promptTemplate_fstring = \"\"\"\n", - "You are an Python expert.\n", - "You are provided multiple context items that are related to the prompt you have to answer.\n", - "Use the following pieces of context to answer the question at the end. \n", - "Context:\n", - "{context}\n", - "Question:\n", - "{query}\"\"\"\n", - "from langchain.prompts import PromptTemplate\n", - "promptTemplate = PromptTemplate.from_template(promptTemplate_fstring)" - ] - }, - { - "cell_type": "markdown", - "id": "2ae837ab-c194-433d-be47-32eac49497e7", - "metadata": {}, - "source": [ - "Here we have created a template for the prompts. It contains two variables, 'context' and 'query'. These variables will be replaced with the context and query in the upcoming steps \n", - "\n", - "Now we can define a function ask_llm() that queries a model using the above template. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "68b5a612-4bb4-45f8-9003-540e76cdcf96", - "metadata": {}, - "outputs": [], - "source": [ - "from gen_ai_hub.proxy.langchain.openai import ChatOpenAI\n", - "\n", - "def ask_llm(query: str, metric='COSINE_SIMILARITY', k = 4) -> str:\n", - " context = ''\n", - " context = run_vector_search(query, metric, k)\n", - " prompt = promptTemplate.format(query=query, context=' '.join(context['TEXT'].astype('string')))\n", - " llm = ChatOpenAI(proxy_model_name='gpt-4-32k')\n", - " response = llm.invoke(prompt).content\n", - " print('Query: '+ query)\n", - " print('\\nResponse:')\n", - " print(response)" - ] - }, - { - "cell_type": "markdown", - "id": "29c00362-c55c-4816-ab42-d453062d41df", - "metadata": {}, - "source": [ - "The above function appends the query and context into the template to create a prompt. Then that prompt is passed on to the LLM model and the response is printed.\n", - "\n", - "Here we are using gpt-4 model. Make sure that you have already created a deployment for the gpt-4 model in AI Launchpad\n", - "\n", - "Now we can test the function by passing the following query to generate results using retrieval augmented generation.ut. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f212e250-846e-4640-8c59-749803235fe3", - "metadata": {}, - "outputs": [], - "source": [ - "query = \"How can I search for a word in a dictionary?\"\n", - "response = ask_llm(query=query, k=4)\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "0ac31b22-6fca-4aa0-9378-148cfc809c3a", - "metadata": {}, - "source": [ - "We got a detailed output in contrast to the output for the same query without RAG." - ] - }, - { - "cell_type": "markdown", - "id": "67e273ba-5894-42d9-aab5-8fbc3f555048", - "metadata": {}, - "source": [ - "We can see that the output generated is based on the comment that we posted in Reddit.\n", - "\n", - "**Outcome:** Through the integration of live Reddit data, Bob enhances the model's ability to provide up-to-date and relevant support to developers. By leveraging real-time insights and solutions shared by the community, the help portal powered by embeddings becomes even more valuable, addressing developers' queries with greater accuracy and efficiency.\n", - "\n", - "### Code generation\n", - "\n", - "We can make use of the LLM to generate code in python and other programming languages. In the following example, we make the model write a code to search for a word in a dictionary." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "42fa676f-5025-4747-a981-6c27050c15f1", - "metadata": {}, - "outputs": [], - "source": [ - "llm = ChatOpenAI(proxy_model_name='gpt-4-32k')\n", - "response = llm.invoke(\"write the python code to search for a word in a dictionary?\").content\n", - "print(response)" - ] - }, - { - "cell_type": "markdown", - "id": "6bdc5d2c-893c-4b19-89bd-8a13e5d5a8c6", - "metadata": {}, - "source": [ - "We can paste the generated code into a new cell here and run it to see its output\n", - "\n", - "Now we can use RAG to generate code. In the following example, we make use of RAG to generate code using the same prompt." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f8620247", - "metadata": {}, - "outputs": [], - "source": [ - "# Define the dictionary\n", - "dictionary = {\"apple\": \"A fruit\", \"banana\": \"A yellow fruit\", \"cherry\": \"A small, round stone fruit\"}\n", - "\n", - "# Define the word to search\n", - "word_to_search = \"banana\"\n", - "\n", - "# Check if the word is in the dictionary\n", - "if word_to_search in dictionary:\n", - " print(f\"'{word_to_search}' found in the dictionary.\")\n", - "else:\n", - " print(f\"'{word_to_search}' not found in the dictionary.\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "caa8799e-f948-4bdc-82d9-b224a328be47", - "metadata": {}, - "outputs": [], - "source": [ - "query = \"write the python code to search for a word in a dictionary?\"\n", - "response = ask_llm(query=query, k=4)\n", - "response" - ] - }, - { - "cell_type": "markdown", - "id": "9d5458ec-f19d-459d-9dbb-3769c0ef14a4", - "metadata": {}, - "source": [ - "We can paste the generated code into a new cell and run it to see its output.\n", - "\n", - "We can make use of the LLM to find the difference between the codes generated in both cases. Copy paste the code into the following prompt and run the cell." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "59a165dd", - "metadata": {}, - "outputs": [], - "source": [ - "my_dict = {\"apple\": \"a fruit\", \"banana\": \"a yellow fruit\", \"cherry\": \"a small, round, red fruit\"}\n", - "\n", - "# The word to search for\n", - "word_to_find = \"banana\"\n", - "\n", - "if word_to_find in my_dict:\n", - " print(f\"'{word_to_find}' found in the dictionary with the meaning '{my_dict[word_to_find]}'.\")\n", - "else:\n", - " print(f\"'{word_to_find}' not found in the dictionary.\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6413733e-d710-48eb-9198-688c17e634a4", - "metadata": {}, - "outputs": [], - "source": [ - "def compare_and_explain(code1, code2):\n", - " # Split the code into lines\n", - " lines1 = code1.strip().split('\\n')\n", - " lines2 = code2.strip().split('\\n')\n", - " \n", - " # Find differences in lines\n", - " diff_lines = []\n", - " for i, (line1, line2) in enumerate(zip(lines1, lines2), 1):\n", - " if line1 != line2:\n", - " diff_lines.append((i, line1, line2))\n", - " \n", - " if not diff_lines:\n", - " return \"Both code snippets are identical.\"\n", - " \n", - " # Generate difference report with explanations\n", - " report = \"Differences found:\\n\"\n", - " for line_num, line1, line2 in diff_lines:\n", - " report += f\"Line {line_num}:\\n\"\n", - " report += f\" Code-1: {line1}\\n\"\n", - " report += f\" Code-2: {line2}\\n\"\n", - " \n", - " # Explanation for the differences\n", - " if line_num == 2: # Line with the dictionary definition\n", - " report += \" Explanation: Variable names differ (dictionary vs my_dict).\\n\"\n", - " elif line_num == 6: # Line with the word to search\n", - " report += \" Explanation: Variable names differ (word_to_search vs word_to_find).\\n\"\n", - " \n", - " return report\n", - "\n", - "# Define the two code snippets to compare\n", - "code1 = '''\n", - "query = \"\"\n", - "dictionary = {\"apple\": \"A fruit\", \"banana\": \"A yellow fruit\", \"cherry\": \"A small, round stone fruit\"}\n", - "word_to_search = \"banana\"\n", - "\n", - "if word_to_search in dictionary:\n", - " print(f\"'{word_to_search}' found in the dictionary.\")\n", - "else:\n", - " print(f\"'{word_to_search}' not found in the dictionary.\")\n", - "'''\n", - "\n", - "code2 = '''\n", - "query = \"\"\n", - "my_dict = {\"apple\": \"a fruit\", \"banana\": \"a yellow fruit\", \"cherry\": \"a small, round, red fruit\"}\n", - "word_to_find = \"banana\"\n", - "\n", - "if word_to_find in my_dict:\n", - " print(f\"'{word_to_find}' found in the dictionary with the meaning '{my_dict[word_to_find]}'.\")\n", - "else:\n", - " print(f\"'{word_to_find}' not found in the dictionary.\")\n", - "'''\n", - "\n", - "# Compare the two code snippets\n", - "result = compare_and_explain(code1, code2)\n", - "print(result)\n" - ] - }, - { - "cell_type": "markdown", - "id": "31459653-642d-41df-88b6-4612ae010b43", - "metadata": {}, - "source": [ - "Here we can see how the codes differ from each other.\n", - "\n", - "**Outcome:** After generating the code, Bob tests the code and pushes it to github for testing and quality assurance. The effort to write code is reduced by a big margin by making use of the LLMs.\n" - ] - } - ], - "metadata": { - "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.9.9" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}