Showing posts with label AI. Show all posts
Showing posts with label AI. Show all posts

Tuesday, January 2, 2024

Azure AI Service: Cannot import name 'SemanticSettings' from 'azure.search.documents.indexes.models'

You may encounter the following exception while testing Semantic Ranking with the Azure AI service. I was exploring the 'Quickstart: Semantic Ranking' guide on Azure AI Search, available on Microsoft Learn, using the Python library. 

The exception occurred during the execution of the following code snippet:

%pip install azure-search-documents --pre
%pip show azure-search-documents
%pip install python-dotenv

import os
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents import SearchClient
from azure.search.documents.indexes.models import (  
    SearchIndex,  
    SearchField,  
    SearchFieldDataType,  
    SimpleField,  
    SearchableField,
    ComplexField,
    SearchIndex,  
    SemanticConfiguration,  
    PrioritizedFields,  
    SemanticField,  
    SemanticSettings,  
)

ImportError: cannot import name 'SemanticSettings' from 'azure.search.documents.indexes.models'

Additionally, you might encounter the following exception when executing the sample code from the aforementioned document.

ImportError: cannot import name 'PrioritizedFields' from 'azure.search.documents.indexes.models'

To resolve the issue, replace 'PrioritizedFields' with 'SemanticPrioritizedFields' in every occurrence. Additionally, remove 'prioritized_' from the following code snippet.

semantic_config = SemanticConfiguration(
    name="my-semantic-config",
    prioritized_fields=SemanticPrioritizedFields(
        title_field=SemanticField(field_name="HotelName"),
        prioritized_keywords_fields=[SemanticField(field_name="Category")],
        prioritized_content_fields=[SemanticField(field_name="Description")]
    )
)

Updated

semantic_config = SemanticConfiguration(
    name="my-semantic-config",
    prioritized_fields=SemanticPrioritizedFields(
        title_field=SemanticField(field_name="HotelName"),
        keywords_fields=[SemanticField(field_name="Category")],
        content_fields=[SemanticField(field_name="Description")]
    )
)

The SemanticSettings class is not supported in the latest version of the azure-search-documents library. To resolve the issue, replace SemanticSettings with SemanticSearch.

%pip install azure-search-documents --pre
%pip show azure-search-documents
%pip install python-dotenv

import os
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents import SearchClient
from azure.search.documents.indexes.models import (  
    SearchIndex,  
    SearchField,  
    SearchFieldDataType,  
    SimpleField,  
    SearchableField,
    ComplexField,
    SearchIndex,  
    SemanticConfiguration,  
    SemanticPrioritizedFields,  
    SemanticField,  
    SemanticSearch,  
)

Also, replace SemanticSettings with SemanticSearch throughout your code.

semantic_settings = SemanticSettings(configurations=[semantic_config])

Updated

semantic_settings = SemanticSearch(configurations=[semantic_config])

Also, replace semantic_settings with semantic_search in the code snippet provided below.

index = SearchIndex(
    name=name,
    fields=fields,
    semantic_settings=semantic_settings,
    scoring_profiles=scoring_profiles,
    suggesters = suggester)

Updated

index = SearchIndex(
    name=name,
    fields=fields,
    semantic_search=semantic_settings,
    scoring_profiles=scoring_profiles,
    suggesters = suggester)

Now you can execute the Semantic Search demo without any issues, you may receive a excpetion related to enabling the semantic ranking for your AI Search service, refer to Azure AI Search: Semantic search is not enabled for this service to resolve the issue.

Azure AI Search: Semantic search is not enabled for this service.

You may encounter the following exception while testing Semantic Ranking with the Azure AI service. I was exploring the 'Quickstart: Semantic Ranking' guide on Azure AI Search, available on Microsoft Learn, using the Python library. The exception occurred during the execution of the following code snippet:

results =  search_client.search
(query_type='semantic', semantic_configuration_name='my-semantic-config',
    search_text="what hotel has a good restaurant on site",
    select='HotelName,Description,Category', query_caption='extractive')

for result in results:
    print(result["@search.reranker_score"])
    print(result["HotelName"])
    print(f"Description: {result['Description']}")

    captions = result["@search.captions"]
    if captions:
        caption = captions[0]
        if caption.highlights:
            print(f"Caption: {caption.highlights}\n")
        else:
            print(f"Caption: {caption.text}\n")

The exception encountered is likely due to the Semantic Ranking feature not being enabled for the Azure AI Search Service. To resolve this issue, ensure that Semantic Ranking is activated for the AI Search Service instance you are using for semantic searches. It's important to note that the Semantic Ranking feature is not available for the free tier of Azure AI Search.


Ensure that your Azure AI Search Service is set up with a pricing tier of Basic or higher.



Now, with the appropriate pricing tier selected, you should be able to enable the Smart Ranking plan, allowing your code to execute without any issues.




Thursday, March 30, 2023

Integrate ChatGPT with Visual Studio for Faster Development

The development landscape constantly evolves, and new technologies are emerging daily. The AI models are changing the way developer's day to day work.

The ChatGPT, a language model powered by OpenAI, ChatGPT can assist developers in their day-to-day work. One of the most significant ways ChatGPT can help is by serving as a pair programmer.

As a pair programmer, ChatGPT can provide developers with valuable suggestions and insights that can make the process of programming more efficient and effective. For example, ChatGPT can suggest coding techniques, syntax, and algorithms help developers write more efficient and concise code. Additionally, it can assist in debugging code by analyzing the code and providing feedback on potential errors or bugs.

The Genie AI ChatGPT extension for Visual Studio Editor — ChatGPT — Genie AI — Visual Studio Marketplace( multiple extensions are available, but I felt this one looked good) enables the pair programming capabilities to the developers. This extension supports different models, e.g., GPT-4, GPT3.5, GPT3, etc., that the developers can use to simplify their day-to-day development works.

You should be able to optimize the code, Add Tests, Find Bugs, Explain, Add Comments, and Complete Code operations.

Install the extension — ChatGPT — Genie AI — Visual Studio Marketplace

Configure the extension — e.g., Enable Conversation History, Configure the model — reference list of OpenAI models from Models — OpenAI API

I used one of the existing Java files to test it; requested to optimize the selected code.

You should add the API Key to integrate with the ChatGPT API; executing the first-time operation will prompt the OpenAI API key.

Generate the OpenAI API key from https://platform.openai.com/account/api-keys

Add the API key and click enter.

The ChatgPT has given the optimized code block; you can copy the optimized code or even replace the existing code block with the recommended one.

These features look great; this will reduce the code review effort and helps to build the code aligned with the latest improvements.

The AI tools help the developer stop focusing on language syntax; they can focus more on building the optimal logic to solve their business problems (the AI even helps to create the business logic but some level)

We should utilize the available tools and technologies to simplify our day-to-day job; at the same time, we can spend time learning new technology trends.

Additional details using ChatGPT to generate AEM code.

#Question1 — “Create a sling servlet to export AEM tag details

The servlet generated with SCR annotations.

@SlingServlet(
paths = "/bin/exporttags",
methods = "GET",
extensions = {"csv", "json"}
)

public class ExportTagsServlet extends SlingAllMethodsServlet {

#Question2 — “Please use OSGI 6 annotations

This time the same servlet with OSGI 6 annotations; even we can request for OSGI 7 annotations.

@Component(
service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=Export Tag Details Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/exporttags",
"sling.servlet.extensions={\"csv\", \"json\"}"
}
)
public class ExportTagsServlet extends SlingSafeMethodsServlet {

No more writing the code from scratch and searching through search engines to find the optimal code, but the AI models can help to write the optimized code quickly. I look forward to learning how these AI models will affect the future development landscape.

Watch out for sharing sensitive information with AI engines; we should use AI models responsibly.