-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Create an ILMProvider interface and have our current implementation use it #17394
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b44216b
got an idl
PankajBhojwani e78e4d0
works
PankajBhojwani ac83d76
allow
PankajBhojwani 4b944e9
conflict
PankajBhojwani ef406ee
use id here too
PankajBhojwani 4fb4ca4
have terminal page initialize the llmprovider
PankajBhojwani e5afbae
format
PankajBhojwani 32b3d68
consts
PankajBhojwani f9e9326
rename to lmprovider
PankajBhojwani 4133239
spelling conflict
PankajBhojwani 5139b88
array of accepted models
PankajBhojwani 6479c47
auth related functions
PankajBhojwani d392aab
dispatcher
PankajBhojwani 8faf8b4
domain check
PankajBhojwani dd6b46d
hot reload works now
PankajBhojwani fc8e36d
use static strings for strings used more than once
PankajBhojwani fdee6c2
Merge branch 'feature/llm' of https://github.com/microsoft/terminal i…
PankajBhojwani 45ce94d
don't use &
PankajBhojwani 4140208
first round of nits
PankajBhojwani 054ce37
til property, winrt implements
PankajBhojwani af579dc
error types enum
PankajBhojwani a7a0a43
conflict
PankajBhojwani 80bdfd1
random newline
PankajBhojwani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,12 +28,15 @@ godbolt | |
gpt | ||
hyperlinking | ||
hyperlinks | ||
ILM | ||
Kbds | ||
kje | ||
libfuzzer | ||
liga | ||
lje | ||
Llast | ||
lm | ||
llm | ||
Lmid | ||
locl | ||
lol | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#include "pch.h" | ||
#include "AzureLLMProvider.h" | ||
#include "../../types/inc/utils.hpp" | ||
#include "LibraryResources.h" | ||
|
||
#include "AzureLLMProvider.g.cpp" | ||
|
||
using namespace winrt::Windows::Foundation; | ||
using namespace winrt::Windows::Foundation::Collections; | ||
using namespace winrt::Windows::UI::Core; | ||
using namespace winrt::Windows::UI::Xaml; | ||
using namespace winrt::Windows::UI::Xaml::Controls; | ||
using namespace winrt::Windows::System; | ||
namespace WWH = ::winrt::Windows::Web::Http; | ||
namespace WSS = ::winrt::Windows::Storage::Streams; | ||
namespace WDJ = ::winrt::Windows::Data::Json; | ||
|
||
static constexpr std::wstring_view acceptedModels[] = { | ||
L"gpt-35-turbo", | ||
L"gpt4", | ||
L"gpt4-32k", | ||
L"gpt4o", | ||
L"gpt-35-turbo-16k" | ||
}; | ||
static constexpr std::wstring_view acceptedSeverityLevel{ L"safe" }; | ||
static constexpr std::wstring_view applicationJson{ L"application/json" }; | ||
static constexpr std::wstring_view endpointString{ L"endpoint" }; | ||
static constexpr std::wstring_view keyString{ L"key" }; | ||
static constexpr std::wstring_view roleString{ L"role" }; | ||
static constexpr std::wstring_view contentString{ L"content" }; | ||
static constexpr std::wstring_view messageString{ L"message" }; | ||
static constexpr std::wstring_view errorString{ L"error" }; | ||
static constexpr std::wstring_view severityString{ L"severity" }; | ||
|
||
static constexpr std::wstring_view expectedScheme{ L"https" }; | ||
static constexpr std::wstring_view expectedHostSuffix{ L".openai.azure.com" }; | ||
|
||
namespace winrt::Microsoft::Terminal::Query::Extension::implementation | ||
{ | ||
void AzureLLMProvider::SetAuthentication(const Windows::Foundation::Collections::ValueSet& authValues) | ||
{ | ||
_azureEndpoint = unbox_value_or<hstring>(authValues.TryLookup(endpointString).try_as<IPropertyValue>(), L""); | ||
_azureKey = unbox_value_or<hstring>(authValues.TryLookup(keyString).try_as<IPropertyValue>(), L""); | ||
_httpClient = winrt::Windows::Web::Http::HttpClient{}; | ||
_httpClient.DefaultRequestHeaders().Accept().TryParseAdd(applicationJson); | ||
_httpClient.DefaultRequestHeaders().Append(L"api-key", _azureKey); | ||
} | ||
|
||
void AzureLLMProvider::ClearMessageHistory() | ||
{ | ||
_jsonMessages.Clear(); | ||
} | ||
|
||
void AzureLLMProvider::SetSystemPrompt(const winrt::hstring& systemPrompt) | ||
{ | ||
WDJ::JsonObject systemMessageObject; | ||
winrt::hstring systemMessageContent{ systemPrompt }; | ||
systemMessageObject.Insert(roleString, WDJ::JsonValue::CreateStringValue(L"system")); | ||
systemMessageObject.Insert(contentString, WDJ::JsonValue::CreateStringValue(systemMessageContent)); | ||
_jsonMessages.Append(systemMessageObject); | ||
} | ||
|
||
void AzureLLMProvider::SetContext(const Extension::IContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
winrt::Windows::Foundation::IAsyncOperation<Extension::IResponse> AzureLLMProvider::GetResponseAsync(const winrt::hstring& userPrompt) | ||
{ | ||
// Use the ErrorTypes enum to flag whether the response the user receives is an error message | ||
// we pass this enum back to the caller so they can handle it appropriately (specifically, ExtensionPalette will send the correct telemetry event) | ||
ErrorTypes errorType{ ErrorTypes::None }; | ||
hstring message{}; | ||
|
||
if (_azureEndpoint.empty()) | ||
{ | ||
message = RS_(L"CouldNotFindKeyErrorMessage"); | ||
errorType = ErrorTypes::InvalidAuth; | ||
} | ||
else | ||
{ | ||
// If the AI endpoint is not an azure open AI endpoint, return an error message | ||
Windows::Foundation::Uri parsedUri{ _azureEndpoint }; | ||
if (parsedUri.SchemeName() != expectedScheme || | ||
!til::ends_with(parsedUri.Host(), expectedHostSuffix)) | ||
{ | ||
message = RS_(L"InvalidEndpointMessage"); | ||
errorType = ErrorTypes::InvalidAuth; | ||
} | ||
} | ||
|
||
// If we don't have a message string, that means the endpoint exists and matches the regex | ||
// that we allow - now we can actually make the http request | ||
if (message.empty()) | ||
{ | ||
// Make a copy of the prompt because we are switching threads | ||
const auto promptCopy{ userPrompt }; | ||
|
||
// Make sure we are on the background thread for the http request | ||
co_await winrt::resume_background(); | ||
|
||
WWH::HttpRequestMessage request{ WWH::HttpMethod::Post(), Uri{ _azureEndpoint } }; | ||
request.Headers().Accept().TryParseAdd(applicationJson); | ||
|
||
WDJ::JsonObject jsonContent; | ||
WDJ::JsonObject messageObject; | ||
|
||
// _ActiveCommandline should be set already, we request for it the moment we become visible | ||
winrt::hstring engineeredPrompt{ promptCopy }; | ||
if (_context && !_context.ActiveCommandline().empty()) | ||
{ | ||
engineeredPrompt = promptCopy + L". The shell I am running is " + _context.ActiveCommandline(); | ||
} | ||
messageObject.Insert(roleString, WDJ::JsonValue::CreateStringValue(L"user")); | ||
messageObject.Insert(contentString, WDJ::JsonValue::CreateStringValue(engineeredPrompt)); | ||
_jsonMessages.Append(messageObject); | ||
jsonContent.SetNamedValue(L"messages", _jsonMessages); | ||
jsonContent.SetNamedValue(L"max_tokens", WDJ::JsonValue::CreateNumberValue(800)); | ||
jsonContent.SetNamedValue(L"temperature", WDJ::JsonValue::CreateNumberValue(0.7)); | ||
jsonContent.SetNamedValue(L"frequency_penalty", WDJ::JsonValue::CreateNumberValue(0)); | ||
jsonContent.SetNamedValue(L"presence_penalty", WDJ::JsonValue::CreateNumberValue(0)); | ||
jsonContent.SetNamedValue(L"top_p", WDJ::JsonValue::CreateNumberValue(0.95)); | ||
jsonContent.SetNamedValue(L"stop", WDJ::JsonValue::CreateStringValue(L"None")); | ||
const auto stringContent = jsonContent.ToString(); | ||
WWH::HttpStringContent requestContent{ | ||
stringContent, | ||
WSS::UnicodeEncoding::Utf8, | ||
L"application/json" | ||
}; | ||
|
||
request.Content(requestContent); | ||
|
||
// Send the request | ||
try | ||
{ | ||
const auto response = _httpClient.SendRequestAsync(request).get(); | ||
// Parse out the suggestion from the response | ||
const auto string{ response.Content().ReadAsStringAsync().get() }; | ||
const auto jsonResult{ WDJ::JsonObject::Parse(string) }; | ||
if (jsonResult.HasKey(errorString)) | ||
{ | ||
const auto errorObject = jsonResult.GetNamedObject(errorString); | ||
message = errorObject.GetNamedString(messageString); | ||
errorType = ErrorTypes::FromProvider; | ||
} | ||
else | ||
{ | ||
if (_verifyModelIsValidHelper(jsonResult)) | ||
{ | ||
const auto choices = jsonResult.GetNamedArray(L"choices"); | ||
const auto firstChoice = choices.GetAt(0).GetObject(); | ||
const auto messageObject = firstChoice.GetNamedObject(messageString); | ||
message = messageObject.GetNamedString(contentString); | ||
} | ||
else | ||
{ | ||
message = RS_(L"InvalidModelMessage"); | ||
errorType = ErrorTypes::InvalidModel; | ||
} | ||
} | ||
} | ||
catch (...) | ||
{ | ||
message = RS_(L"UnknownErrorMessage"); | ||
errorType = ErrorTypes::Unknown; | ||
} | ||
} | ||
|
||
// Also make a new entry in our jsonMessages list, so the AI knows the full conversation so far | ||
WDJ::JsonObject responseMessageObject; | ||
responseMessageObject.Insert(roleString, WDJ::JsonValue::CreateStringValue(L"assistant")); | ||
responseMessageObject.Insert(contentString, WDJ::JsonValue::CreateStringValue(message)); | ||
_jsonMessages.Append(responseMessageObject); | ||
|
||
co_return winrt::make<AzureResponse>(message, errorType); | ||
} | ||
|
||
bool AzureLLMProvider::_verifyModelIsValidHelper(const WDJ::JsonObject jsonResponse) | ||
{ | ||
const auto model = jsonResponse.GetNamedString(L"model"); | ||
bool modelIsAccepted{ false }; | ||
for (const auto acceptedModel : acceptedModels) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm sure if we didn't just use a |
||
{ | ||
if (model == acceptedModel) | ||
{ | ||
modelIsAccepted = true; | ||
PankajBhojwani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
break; | ||
} | ||
if (!modelIsAccepted) | ||
{ | ||
return false; | ||
} | ||
WDJ::JsonObject contentFiltersObject; | ||
// For some reason, sometimes the content filter results are in a key called "prompt_filter_results" | ||
// and sometimes they are in a key called "prompt_annotations". Check for either. | ||
if (jsonResponse.HasKey(L"prompt_filter_results")) | ||
{ | ||
contentFiltersObject = jsonResponse.GetNamedArray(L"prompt_filter_results").GetObjectAt(0); | ||
} | ||
else if (jsonResponse.HasKey(L"prompt_annotations")) | ||
{ | ||
contentFiltersObject = jsonResponse.GetNamedArray(L"prompt_annotations").GetObjectAt(0); | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
const auto contentFilters = contentFiltersObject.GetNamedObject(L"content_filter_results"); | ||
if (Feature_TerminalChatJailbreakFilter::IsEnabled() && !contentFilters.HasKey(L"jailbreak")) | ||
{ | ||
return false; | ||
} | ||
for (const auto filterPair : contentFilters) | ||
{ | ||
const auto filterLevel = filterPair.Value().GetObjectW(); | ||
if (filterLevel.HasKey(severityString)) | ||
{ | ||
if (filterLevel.GetNamedString(severityString) != acceptedSeverityLevel) | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "AzureLLMProvider.g.h" | ||
|
||
namespace winrt::Microsoft::Terminal::Query::Extension::implementation | ||
{ | ||
struct AzureLLMProvider : AzureLLMProviderT<AzureLLMProvider> | ||
{ | ||
AzureLLMProvider() = default; | ||
|
||
void ClearMessageHistory(); | ||
void SetSystemPrompt(const winrt::hstring& systemPrompt); | ||
void SetContext(const Extension::IContext context); | ||
|
||
winrt::Windows::Foundation::IAsyncOperation<Extension::IResponse> GetResponseAsync(const winrt::hstring& userPrompt); | ||
|
||
void SetAuthentication(const Windows::Foundation::Collections::ValueSet& authValues); | ||
TYPED_EVENT(AuthChanged, winrt::Microsoft::Terminal::Query::Extension::ILMProvider, Windows::Foundation::Collections::ValueSet); | ||
|
||
private: | ||
winrt::hstring _azureEndpoint; | ||
winrt::hstring _azureKey; | ||
winrt::Windows::Web::Http::HttpClient _httpClient{ nullptr }; | ||
|
||
Extension::IContext _context; | ||
|
||
winrt::Windows::Data::Json::JsonArray _jsonMessages; | ||
|
||
bool _verifyModelIsValidHelper(const Windows::Data::Json::JsonObject jsonResponse); | ||
}; | ||
|
||
struct AzureResponse : public winrt::implements<AzureResponse, winrt::Microsoft::Terminal::Query::Extension::IResponse> | ||
{ | ||
AzureResponse(const winrt::hstring& message, const winrt::Microsoft::Terminal::Query::Extension::ErrorTypes errorType) : | ||
Message{ message }, | ||
ErrorType{ errorType } {} | ||
|
||
til::property<winrt::hstring> Message; | ||
til::property<winrt::Microsoft::Terminal::Query::Extension::ErrorTypes> ErrorType; | ||
}; | ||
} | ||
|
||
namespace winrt::Microsoft::Terminal::Query::Extension::factory_implementation | ||
{ | ||
BASIC_FACTORY(AzureLLMProvider); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import "ILMProvider.idl"; | ||
|
||
namespace Microsoft.Terminal.Query.Extension | ||
{ | ||
runtimeclass AzureLLMProvider : [default] ILMProvider | ||
{ | ||
AzureLLMProvider(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW all these
get()
s could useco_await
in the future instead. That avoids theresume_background
hassle.