Skip to content

Commit

Permalink
Merge pull request #124 from Azure-Samples/gk/64-auto-invest-using-pl…
Browse files Browse the repository at this point in the history
…anner
  • Loading branch information
thegovind committed Jul 17, 2023
2 parents 45d2604 + 388e3b7 commit 8528167
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> “Start with the customer experience and work backwards for the technology” - Steve Jobs
>
> "Change is the only constant" - Heraclitus
> "Change is the only constant" - Ancient wisdom
<p align="center"><img src="assets/images/1.png" width=20% height=20% /></p>

Expand Down
4 changes: 2 additions & 2 deletions sandbox/usecases/auto-invest-agency-planning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
Expand Down Expand Up @@ -81,7 +81,7 @@
"source": [
"var builder = new KernelBuilder();\n",
"\n",
"var (_, model, azureEndpoint, apiKey, org) = Settings.LoadFromFile();\n",
"var (_, model, azureEndpoint, apiKey, org, bingApiKey) = Settings.LoadFromFile();\n",
"builder.WithAzureChatCompletionService(model, azureEndpoint, apiKey);\n",
"\n",
"IKernel kernel = builder.Build();"
Expand Down
40 changes: 24 additions & 16 deletions sandbox/usecases/config/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ public static class Settings
private const string SecretKey = "apikey";
private const string OrgKey = "org";
private const bool StoreConfigOnFile = true;
private const string BingApiKey = "bingApiKey";


// Prompt user for Azure Endpoint URL
public static async Task<string> AskAzureEndpoint(bool _useAzureOpenAI = true, string configFile = DefaultConfigFile)
{
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = ReadSettings(_useAzureOpenAI, configFile);
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey) = ReadSettings(_useAzureOpenAI, configFile);

// If needed prompt user for Azure endpoint
if (useAzureOpenAI && string.IsNullOrWhiteSpace(azureEndpoint))
{
azureEndpoint = await InteractiveKernel.GetInputAsync("Please enter your Azure OpenAI endpoint");
}

WriteSettings(configFile, useAzureOpenAI, model, azureEndpoint, apiKey, orgId);
WriteSettings(configFile, useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey);

// Print report
if (useAzureOpenAI)
Expand All @@ -47,7 +49,7 @@ public static async Task<string> AskAzureEndpoint(bool _useAzureOpenAI = true, s
// Prompt user for OpenAI model name / Azure OpenAI deployment name
public static async Task<string> AskModel(bool _useAzureOpenAI = true, string configFile = DefaultConfigFile)
{
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = ReadSettings(_useAzureOpenAI, configFile);
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey) = ReadSettings(_useAzureOpenAI, configFile);

// If needed prompt user for model name / deployment name
if (string.IsNullOrWhiteSpace(model))
Expand All @@ -63,7 +65,7 @@ public static async Task<string> AskModel(bool _useAzureOpenAI = true, string co
}
}

WriteSettings(configFile, useAzureOpenAI, model, azureEndpoint, apiKey, orgId);
WriteSettings(configFile, useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey);

// Print report
if (useAzureOpenAI)
Expand All @@ -85,7 +87,7 @@ public static async Task<string> AskModel(bool _useAzureOpenAI = true, string co
// Prompt user for API Key
public static async Task<string> AskApiKey(bool _useAzureOpenAI = true, string configFile = DefaultConfigFile)
{
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = ReadSettings(_useAzureOpenAI, configFile);
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey) = ReadSettings(_useAzureOpenAI, configFile);

// If needed prompt user for API key
if (string.IsNullOrWhiteSpace(apiKey))
Expand All @@ -101,7 +103,7 @@ public static async Task<string> AskApiKey(bool _useAzureOpenAI = true, string c
}
}

WriteSettings(configFile, useAzureOpenAI, model, azureEndpoint, apiKey, orgId);
WriteSettings(configFile, useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey);

// Print report
Console.WriteLine("Settings: " + (string.IsNullOrWhiteSpace(apiKey)
Expand All @@ -114,21 +116,21 @@ public static async Task<string> AskApiKey(bool _useAzureOpenAI = true, string c
// Prompt user for OpenAI Organization Id
public static async Task<string> AskOrg(bool _useAzureOpenAI = true, string configFile = DefaultConfigFile)
{
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = ReadSettings(_useAzureOpenAI, configFile);
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey) = ReadSettings(_useAzureOpenAI, configFile);

// If needed prompt user for OpenAI Org Id
if (!useAzureOpenAI && string.IsNullOrWhiteSpace(orgId))
{
orgId = await InteractiveKernel.GetInputAsync("Please enter your OpenAI Organization Id (enter 'NONE' to skip)");
}

WriteSettings(configFile, useAzureOpenAI, model, azureEndpoint, apiKey, orgId);
WriteSettings(configFile, useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey);

return orgId;
}

// Load settings from file
public static (bool useAzureOpenAI, string model, string azureEndpoint, string apiKey, string orgId)
public static (bool useAzureOpenAI, string model, string azureEndpoint, string apiKey, string orgId, string bingApiKey)
LoadFromFile(string configFile = DefaultConfigFile)
{
if (!File.Exists(DefaultConfigFile))
Expand All @@ -146,17 +148,19 @@ public static (bool useAzureOpenAI, string model, string azureEndpoint, string a
string azureEndpoint = config[EndpointKey];
string apiKey = config[SecretKey];
string orgId = config[OrgKey];
string bingApiKey = config[BingApiKey];
if (orgId == "none") { orgId = ""; }

return (useAzureOpenAI, model, azureEndpoint, apiKey, orgId);
return (useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey);
}
catch (Exception e)
{
Console.WriteLine("Something went wrong: " + e.Message);
return (true, "", "", "", "");
return (true, "", "", "", "", "");
}
}


// Delete settings file
public static void Reset(string configFile = DefaultConfigFile)
{
Expand All @@ -173,8 +177,8 @@ public static void Reset(string configFile = DefaultConfigFile)
}
}

// Read and return settings from file
private static (bool useAzureOpenAI, string model, string azureEndpoint, string apiKey, string orgId)
// Read and return settings from file
private static (bool useAzureOpenAI, string model, string azureEndpoint, string apiKey, string orgId, string bingApiKey)
ReadSettings(bool _useAzureOpenAI, string configFile)
{
// Save the preference set in the notebook
Expand All @@ -183,12 +187,13 @@ private static (bool useAzureOpenAI, string model, string azureEndpoint, string
string azureEndpoint = "";
string apiKey = "";
string orgId = "";
string bingApiKey = "";

try
{
if (File.Exists(configFile))
{
(useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = LoadFromFile(configFile);
(useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey) = LoadFromFile(configFile);
}
}
catch (Exception e)
Expand All @@ -205,14 +210,16 @@ private static (bool useAzureOpenAI, string model, string azureEndpoint, string
azureEndpoint = "";
apiKey = "";
orgId = "";
bingApiKey = "";
}

return (useAzureOpenAI, model, azureEndpoint, apiKey, orgId);
return (useAzureOpenAI, model, azureEndpoint, apiKey, orgId, bingApiKey);
}


// Write settings to file
private static void WriteSettings(
string configFile, bool useAzureOpenAI, string model, string azureEndpoint, string apiKey, string orgId)
string configFile, bool useAzureOpenAI, string model, string azureEndpoint, string apiKey, string orgId, string bingApiKey)
{
try
{
Expand All @@ -225,6 +232,7 @@ private static (bool useAzureOpenAI, string model, string azureEndpoint, string
{ EndpointKey, azureEndpoint },
{ SecretKey, apiKey },
{ OrgKey, orgId },
{ BingApiKey, bingApiKey }
};

var options = new JsonSerializerOptions { WriteIndented = true };
Expand Down
3 changes: 2 additions & 1 deletion sandbox/usecases/config/settings.json.azure-example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"model": "... your Azure OpenAI deployment name ...",
"endpoint": "https:// ... your endpoint ... .openai.azure.com/",
"apikey": "... your Azure OpenAI key ...",
"org": "" // it's not used for azure, but the parser requires it so do not delete
"org": "" // it's not used for azure, but the parser requires it so do not delete,
"bingApiKey": "... your Bing API key ..."
}
3 changes: 2 additions & 1 deletion sandbox/usecases/config/settings.json.openai-example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"endpoint": "NOT-USED-BUT-REQUIRED-FOR-PARSER",
"model": "text-davinci-003",
"apikey": "... your OpenAI key ...",
"org": ""
"org": "",
"bingApiKey": "... your Bing API key ..."
}
Loading

0 comments on commit 8528167

Please sign in to comment.