MicrosoftLearning / mslearn-ai-language

Lab files for Azure AI Language modules
https://microsoftlearning.github.io/mslearn-ai-language/
MIT License
127 stars 260 forks source link

Issue with lab File /05-custom-entity-recognition/extract-entities.ps1 #2

Closed joedocker758 closed 8 months ago

joedocker758 commented 1 year ago

For the lab: 05-custom-entity-recognition/extract-entities.ps1, it seems the request URI and the request is not aligned with the schema. Suggested code is provided as follows:

# Update these with your service and deployment values
$key="<YOUR-KEY>"
$endpoint="<YOUR-ENDPOINT>"
$projectName = "customNERLab"
$deploymentName = "customExtractAds"
$verbose = $false

# Set up headers for API call
$headers = @{}
$headers.add("Ocp-Apim-Subscription-Key", $key)
$headers.add("Content-Type","application/json")

# Get text to extract entities from
$text_file = "test1.txt"
if ($args.count -gt 0)
{
    $text_file = $args[0]
}

try {
    $contents = get-content $text_file -raw -ErrorAction Stop
}
catch {
    Write-Host "`nError reading provided file, please verify file exists`n"
    Exit
}

$data = @{
    "displayName" = "Extracting entities"
    "tasks" = @(
        @{
            "kind" = "CustomEntityRecognition"
            "taskName" = "Entity Recognition"
            "parameters"= @{
                        "projectName" = $projectName
                        "deploymentName" = $deploymentName
                    }
        }
    )
    "analysisInput" = @{
        "documents" = @(
            @{
                "id" = "document_extractEntities"
                "language" = "en-us"
                "text" = $contents
            }
        )
    }
} | ConvertTo-Json -Depth 10

# Post text for entity recognition
Write-Host("`nSubmitting entity recognition task`n")
$response = Invoke-WebRequest -Method Post `
          -Uri "$($endpoint)language/analyze-text/jobs?api-version=2022-10-01-preview" `
          -Headers $headers `
          -Body $data

# Output response if desired
if ($verbose) {
    Write-Host("`nResponse header:$($response.Headers['Operation-Location'])`n")
}

# Extract the URL from the response
# to call the API to getting the analysis results
$resultUrl = $($response.Headers['Operation-Location'])

# Create the header for the REST GET with only the subscription key
$resultHeaders = @{}
$resultHeaders.Add( "Ocp-Apim-Subscription-Key", $key )

# Get the results
# Continue to request results until the analysis is "succeeded"
Write-Host "Getting results..."
Do {
    $result = Invoke-RestMethod -Method Get `
            -Uri $resultUrl `
            -Headers $resultHeaders | ConvertTo-Json -Depth 10

    $analysis = ($result | ConvertFrom-Json)
} while ($analysis.status -ne "succeeded")
Write-Host "...Done`n"

# Access the relevant fields from the analysis
$extraction = $result | ConvertFrom-Json
#$docs = $extraction.tasks.customEntityRecognitionTasks[0].results.documents
$docs = $extraction.tasks.items[0].results.documents

# Output response if desired
if ($verbose) {
    Write-Host("GET JSON Response:`n$result`n")
}

for (($idx = 0); $idx -lt $docs.Length; $idx++) {
    $item = $docs[$idx] 
    Write-Host ("Document #", ($idx+1))
    $entities = $item.entities
    foreach ($entity in $entities) {
        Write-Host ("  - Entity Category: $($entity.category)")
        Write-Host ("  - Entity Text:  $($entity.text)")
        Write-Host ("  - Confidence:  $($entity.confidenceScore)`n")
    }   
}
ivorb commented 9 months ago

I'm not seeing issues with the code as written. Could you elaborate on the misalignment that was causing issues for you?