Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CognitiveServices] LUIS Runtime SDK #3889

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Microsoft.Azure.CognitiveServices.LUIS.Tests
{
using System;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Azure.CognitiveServices.Language.LUIS;
using Microsoft.Azure.CognitiveServices.Language.LUIS.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;

public abstract class BaseTest
{
private const HttpRecorderMode mode = HttpRecorderMode.Playback;

protected const AzureRegions region = AzureRegions.Westus;
protected const string appId = "86226c53-b7a6-416f-876b-226b2b5ab07b";
protected const string subscriptionKey = "00000000000000000000000000000000";

private string ClassName => GetType().FullName;

private ILuisRuntimeAPI GetClient(DelegatingHandler handler, string subscriptionKey = subscriptionKey)
{
return new LuisRuntimeAPI(new ApiKeyServiceClientCredentials(subscriptionKey), handlers: handler)
{
AzureRegion = region
};
}

protected async void UseClientFor(Func<ILuisRuntimeAPI, Task> doTest, string className = null, [CallerMemberName] string methodName = "")
{
using (MockContext context = MockContext.Start(className ?? ClassName, methodName))
{
HttpMockServer.Initialize(className ?? ClassName, methodName, mode);
ILuisRuntimeAPI client = GetClient(HttpMockServer.CreateInstance());
await doTest(client);
context.Stop();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.Azure.CognitiveServices.Language.LUIS;
using Microsoft.Azure.CognitiveServices.Language.LUIS.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System.Collections.Generic;
using Xunit;

namespace Microsoft.Azure.CognitiveServices.LUIS.Tests.Luis
{
public class PredictionTests : BaseTest
{
[Fact]
public void PredictionGet()
{
UseClientFor(async client =>
{
var utterance = "this is a test";
var result = await client.Prediction.GetPredictionsFromEndpointViaGetAsync(appId, utterance);

Assert.Equal("None", result.TopScoringIntent.Intent);
Assert.Equal(utterance, result.Query);
});
}

[Fact]
public void PredictionInvalidKey()
{
var headers = new Dictionary<string, List<string>> { ["Ocp-Apim-Subscription-Key"] = new List<string> { "invalid-key" } };

UseClientFor(async client =>
{
var ex = await Assert.ThrowsAsync<APIErrorException>(async () =>
{
await client.Prediction.GetPredictionsFromEndpointViaGetWithHttpMessagesAsync(appId, "test", customHeaders: headers);
});

Assert.Equal("401", ex.Body.StatusCode);
});
}

[Fact]
public void PredictionPost()
{
UseClientFor(async client =>
{
var utterance = "this is a test with post";
var result = await client.Prediction.GetPredictionsFromEndpointViaPostAsync(appId, utterance);

Assert.Equal("None", result.TopScoringIntent.Intent);
Assert.Equal(utterance, result.Query);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Test.HttpRecorder" Version="1.8.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure" Version="3.3.10" />
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure.TestFramework" Version="1.7.2" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LUIS-Runtime\Microsoft.Azure.CognitiveServices.LUIS.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="SessionRecords\**\*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"Entries": [
{
"RequestUri": "/luis/v2.0/apps/86226c53-b7a6-416f-876b-226b2b5ab07b?q=this%20is%20a%20test",
"RequestMethod": "GET",
"RequestBody": "",
"RequestHeaders": {
"Ocp-Apim-Subscription-Key": [
"00000000000000000000000000000000"
],
"User-Agent": [
"FxVersion/4.6.25815.04",
"Microsoft.Azure.CognitiveServices.Language.LUIS.LuisRuntimeAPI/2.0.0.0"
]
},
"ResponseBody": "{\r\n \"query\": \"this is a test\",\r\n \"topScoringIntent\": {\r\n \"intent\": \"None\",\r\n \"score\": 0.460794479\r\n },\r\n \"entities\": []\r\n}",
"ResponseHeaders": {
"Content-Length": [
"132"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Cache-Control": [
"no-store, proxy-revalidate, no-cache, max-age=0, private"
],
"Date": [
"Fri, 24 Nov 2017 18:04:02 GMT"
],
"Pragma": [
"no-cache"
],
"Apim-Request-Id": [
"2e63c27d-2898-4a4a-966f-2da6b7313e6f"
],
"Request-Id": [
"2e63c27d-2898-4a4a-966f-2da6b7313e6f"
],
"Request-Context": [
"appId=cid-v1:26a3540d-a02a-4998-a060-715488fd769b"
],
"X-Powered-By": [
"ASP.NET"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
],
"x-content-type-options": [
"nosniff"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"Entries": [
{
"RequestUri": "/luis/v2.0/apps/86226c53-b7a6-416f-876b-226b2b5ab07b?q=test",
"RequestMethod": "GET",
"RequestBody": "",
"RequestHeaders": {
"Ocp-Apim-Subscription-Key": [
"invalid-key"
],
"User-Agent": [
"FxVersion/4.6.25815.04",
"Microsoft.Azure.CognitiveServices.Language.LUIS.LuisRuntimeAPI/2.0.0.0"
]
},
"ResponseBody": "{\r\n \"statusCode\": 401,\r\n \"message\": \"Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription.\"\r\n}",
"ResponseHeaders": {
"Content-Length": [
"143"
],
"Content-Type": [
"application/json"
],
"Date": [
"Fri, 24 Nov 2017 18:03:59 GMT"
],
"WWW-Authenticate": [
"AzureApiManagementKey realm=\"https://westus.api.cognitive.microsoft.com/luis/v2.0/apps\",name=\"Ocp-Apim-Subscription-Key\",type=\"header\""
],
"apim-request-id": [
"17372802-65f3-4358-8cb4-2d3fd59f1591"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
],
"x-content-type-options": [
"nosniff"
]
},
"StatusCode": 401
}
],
"Names": {},
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"Entries": [
{
"RequestUri": "/luis/v2.0/apps/86226c53-b7a6-416f-876b-226b2b5ab07b",
"RequestMethod": "POST",
"RequestBody": "\"this is a test with post\"",
"RequestHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Content-Length": [
"26"
],
"Ocp-Apim-Subscription-Key": [
"00000000000000000000000000000000"
],
"User-Agent": [
"FxVersion/4.6.25815.04",
"Microsoft.Azure.CognitiveServices.Language.LUIS.LuisRuntimeAPI/2.0.0.0"
]
},
"ResponseBody": "{\r\n \"query\": \"this is a test with post\",\r\n \"topScoringIntent\": {\r\n \"intent\": \"None\",\r\n \"score\": 0.470495373\r\n },\r\n \"entities\": []\r\n}",
"ResponseHeaders": {
"Content-Length": [
"142"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Cache-Control": [
"no-store, proxy-revalidate, no-cache, max-age=0, private"
],
"Date": [
"Fri, 24 Nov 2017 18:03:59 GMT"
],
"Pragma": [
"no-cache"
],
"Apim-Request-Id": [
"81fb65ad-436a-4969-8e11-3155e5c66db6"
],
"Request-Id": [
"81fb65ad-436a-4969-8e11-3155e5c66db6"
],
"X-Powered-By": [
"ASP.NET"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
],
"x-content-type-options": [
"nosniff"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {}
}
50 changes: 50 additions & 0 deletions src/SDKs/CognitiveServices/dataPlane/Language/LUIS-Runtime.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.12
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.CognitiveServices.LUIS", "LUIS-Runtime\Microsoft.Azure.CognitiveServices.LUIS.csproj", "{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.CognitiveServices.LUIS.Tests", "LUIS-Runtime.Tests\Microsoft.Azure.CognitiveServices.LUIS.Tests.csproj", "{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Debug|x64.ActiveCfg = Debug|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Debug|x64.Build.0 = Debug|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Debug|x86.ActiveCfg = Debug|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Debug|x86.Build.0 = Debug|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Release|Any CPU.Build.0 = Release|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Release|x64.ActiveCfg = Release|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Release|x64.Build.0 = Release|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Release|x86.ActiveCfg = Release|Any CPU
{EDD669F1-B4F9-4014-BBAD-C3BFFE7C4041}.Release|x86.Build.0 = Release|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Debug|x64.ActiveCfg = Debug|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Debug|x64.Build.0 = Debug|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Debug|x86.ActiveCfg = Debug|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Debug|x86.Build.0 = Debug|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Release|Any CPU.Build.0 = Release|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Release|x64.ActiveCfg = Release|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Release|x64.Build.0 = Release|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Release|x86.ActiveCfg = Release|Any CPU
{EDD989BB-7B18-49E8-8B93-81D0740DFE8A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EFBCDA3E-F7DB-403F-9951-428BBD603FDB}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Microsoft.Azure.CognitiveServices.Language.LUIS
{
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Rest;

/// <summary>
/// Allows authentication to the API using a basic apiKey mechanism
/// </summary>
public class ApiKeyServiceClientCredentials : ServiceClientCredentials
{
private readonly string subscriptionKey;

/// <summary>
/// Creates a new instance of the ApiKeyServiceClientCredentails class
/// </summary>
/// <param name="subscriptionKey">The subscription key to authenticate and authorize as</param>
public ApiKeyServiceClientCredentials(string subscriptionKey)
{
if (string.IsNullOrWhiteSpace(subscriptionKey))
throw new ArgumentNullException("subscriptionKey");

this.subscriptionKey = subscriptionKey;
}

/// <summary>
/// Add the Basic Authentication Header to each outgoing request
/// </summary>
/// <param name="request">The outgoing request</param>
/// <param name="cancellationToken">A token to cancel the operation</param>
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
throw new ArgumentNullException("request");

request.Headers.Add("Ocp-Apim-Subscription-Key", this.subscriptionKey);

return Task.FromResult<object>(null);
}
}
}
Loading