Skip to content

Commit

Permalink
Merge pull request #101 from microsoft/sethjuarez/web
Browse files Browse the repository at this point in the history
fixing toc issue
  • Loading branch information
sethjuarez authored Oct 11, 2024
2 parents 27c4a4f + b6263b7 commit d6603b2
Show file tree
Hide file tree
Showing 38 changed files with 1,211 additions and 27 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
.DS_Store
*.js
.runs/
node_modules/
dist/
runtime/promptycs/Prompty.Core/bin/
runtime/promptycs/Prompty.Core/obj/
runtime/promptycs/Tests/bin/
runtime/promptycs/Tests/obj/
.env
2 changes: 2 additions & 0 deletions runtime/promptycs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
obj/
3 changes: 3 additions & 0 deletions runtime/promptycs/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.defaultSolution": "prompty-dotnet.sln"
}
21 changes: 21 additions & 0 deletions runtime/promptycs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Cassie Breviu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions runtime/promptycs/Prompty.Core.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
29 changes: 29 additions & 0 deletions runtime/promptycs/Prompty.Core.Tests/Prompty.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Prompty.Core\Prompty.Core.csproj" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions runtime/promptycs/Prompty.Core.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Prompty.Core.Tests;

public class UnitTest1
{
[Fact]
public void Test1()
{

}
}
20 changes: 20 additions & 0 deletions runtime/promptycs/Prompty.Core/BaseModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Azure.AI.OpenAI;

namespace Prompty.Core
{
public class BaseModel
{
public void TryThing() {
AzureOpenAIClient azureClient = new(
new Uri("https://your-azure-openai-resource.com"),
new DefaultAzureCredential());
ChatClient chatClient = azureClient.GetChatClient("my-gpt-35-turbo-deployment");
}
public string Prompt { get; set; }
public List<Dictionary<string, string>> Messages { get; set; }
public ChatResponseMessage ChatResponseMessage { get; set; }
public Completions CompletionResponseMessage { get; set; }
public Embeddings EmbeddingResponseMessage { get; set; }
public ImageGenerations ImageResponseMessage { get; set; }
}
}
140 changes: 140 additions & 0 deletions runtime/promptycs/Prompty.Core/Executors/AzureOpenAIExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using Azure.AI.OpenAI;
using Azure;
using Prompty.Core.Types;

namespace Prompty.Core.Executors
{
public class AzureOpenAIExecutor : IInvoker
{
private readonly OpenAIClient client;
private readonly string api;
private readonly string? deployment;
private readonly dynamic? parameters;
private readonly ChatCompletionsOptions chatCompletionsOptions;
private readonly CompletionsOptions completionsOptions;
private readonly ImageGenerationOptions imageGenerationOptions;
private readonly EmbeddingsOptions embeddingsOptions;

public AzureOpenAIExecutor(Prompty prompty, InvokerFactory invoker)
{
var invokerName = ModelType.azure_openai.ToString();
invoker.Register(InvokerType.Executor, invokerName, this);
client = new OpenAIClient(
endpoint: new Uri(prompty.Model.ModelConfiguration.AzureEndpoint),
keyCredential: new AzureKeyCredential(prompty.Model.ModelConfiguration.ApiKey)
);

api = prompty.Model.Api.ToString();
parameters = prompty.Model.Parameters;

chatCompletionsOptions = new ChatCompletionsOptions()
{
DeploymentName = prompty.Model.ModelConfiguration.AzureDeployment
};
completionsOptions = new CompletionsOptions()
{
DeploymentName = prompty.Model.ModelConfiguration.AzureDeployment
};
imageGenerationOptions = new ImageGenerationOptions()
{
DeploymentName = prompty.Model.ModelConfiguration.AzureDeployment
};
embeddingsOptions = new EmbeddingsOptions()
{
DeploymentName = prompty.Model.ModelConfiguration.AzureDeployment
};

}

public async Task<BaseModel> Invoke(BaseModel data)
{

if (api == ApiType.Chat.ToString())
{
try
{


for (int i = 0; i < data.Messages.Count; i++)
{
//parse role sting to enum value
var roleEnum = Enum.Parse<RoleType>(data.Messages[i]["role"]);

switch (roleEnum)
{
case RoleType.user:
var userMessage = new ChatRequestUserMessage(data.Messages[i]["content"]);
chatCompletionsOptions.Messages.Add(userMessage);
break;
case RoleType.system:
var systemMessage = new ChatRequestSystemMessage(data.Messages[i]["content"]);
chatCompletionsOptions.Messages.Add(systemMessage);
break;
case RoleType.assistant:
var assistantMessage = new ChatRequestAssistantMessage(data.Messages[i]["content"]);
chatCompletionsOptions.Messages.Add(assistantMessage);
break;
case RoleType.function:
//TODO: Fix parsing for Function role
var functionMessage = new ChatRequestFunctionMessage("name", data.Messages[i]["content"]);
chatCompletionsOptions.Messages.Add(functionMessage);
break;
}

}
var response = await client.GetChatCompletionsAsync(chatCompletionsOptions);
data.ChatResponseMessage = response.Value.Choices[0].Message;

}
catch (Exception error)
{
Console.Error.WriteLine(error);
}
}
else if (api == ApiType.Completion.ToString())
{
try
{
var response = await client.GetCompletionsAsync(completionsOptions);
data.CompletionResponseMessage = response.Value;

}
catch (Exception error)
{
Console.Error.WriteLine(error);
}
}
//else if (api == ApiType.Embedding.ToString())
//{
// try
// {
// var response = await client.GetEmbeddingsAsync(embeddingsOptions);
// data.EmbeddingResponseMessage = response.Value;

// }
// catch (Exception error)
// {
// Console.Error.WriteLine(error);
// }
//}
//else if (api == ApiType.Image.ToString())
//{
// try
// {
// var response = await client.GetImageGenerationsAsync(imageGenerationOptions);
// data.ImageResponseMessage = response.Value;

// }
// catch (Exception error)
// {
// Console.Error.WriteLine(error);
// }
//}


return data;
}

}

}
126 changes: 126 additions & 0 deletions runtime/promptycs/Prompty.Core/Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using global::Prompty.Core.Types;
using Microsoft.Extensions.Configuration;
using YamlDotNet.Serialization;

namespace Prompty.Core
{

public static class Helpers
{
// This is to load the appsettings.json file config
// These are the base configuration settings for the prompty file
// These can be overriden by the prompty file, or the execute method
public static PromptyModelConfig GetPromptyModelConfigFromSettings()
{
//TODO: default prompty json, can have multiple sections, need to loop thru sections?
//TODO: account for multiple prompty.json files
// Get the connection string from appsettings.json
var config = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json").Build();

var section = config.GetSection("Prompty");
// get variables from section and assign to promptymodelconfig
var promptyModelConfig = new PromptyModelConfig();
if (section != null)
{
var type = section["type"];
var apiVersion = section["api_version"];
var azureEndpoint = section["azure_endpoint"];
var azureDeployment = section["azure_deployment"];
var apiKey = section["api_key"];


if (type != null)
{
//parse type to ModelType enum
promptyModelConfig.ModelType = (ModelType)Enum.Parse(typeof(ModelType), type);

}
if (apiVersion != null)
{
promptyModelConfig.ApiVersion = apiVersion;
}
if (azureEndpoint != null)
{
promptyModelConfig.AzureEndpoint = azureEndpoint;
}
if (azureDeployment != null)
{
promptyModelConfig.AzureDeployment = azureDeployment;
}
if (apiKey != null)
{
promptyModelConfig.ApiKey = apiKey;
}
}

return promptyModelConfig;
}


public static Prompty ParsePromptyYamlFile(Prompty prompty, string promptyFrontMatterYaml)
{
// desearialize yaml front matter
// TODO: check yaml to see what props are missing? update to include template type, update so invoker descides based on prop
var deserializer = new DeserializerBuilder().Build();
var promptyFrontMatter = deserializer.Deserialize<Prompty>(promptyFrontMatterYaml);

// override props if they are not null from file
if (promptyFrontMatter.Name != null)
{
// check each prop and if not null override
if (promptyFrontMatter.Name != null)
{
prompty.Name = promptyFrontMatter.Name;
}
if (promptyFrontMatter.Description != null)
{
prompty.Description = promptyFrontMatter.Description;
}
if (promptyFrontMatter.Tags != null)
{
prompty.Tags = promptyFrontMatter.Tags;
}
if (promptyFrontMatter.Authors != null)
{
prompty.Authors = promptyFrontMatter.Authors;
}
if (promptyFrontMatter.Inputs != null)
{
prompty.Inputs = promptyFrontMatter.Inputs;
}
if(promptyFrontMatter.Outputs != null)
{
prompty.Outputs = promptyFrontMatter.Outputs;
}
if(promptyFrontMatter.Sample != null)
{
//if sample value is a string value, it should be read as a file and parsed to a dict.
if(promptyFrontMatter.Sample is string)
{
//parse the file
var sampleFile = File.ReadAllText(promptyFrontMatter.Sample);
prompty.Sample = deserializer.Deserialize<Dictionary<string, object>>(sampleFile);
}
else
{
prompty.Sample = promptyFrontMatter.Sample;
}
}
// parse out model params
if (promptyFrontMatter.Model != null)
{
//set model settings
prompty.Model = promptyFrontMatter.Model;
//override from appsettings
// prompty.Model.ModelConfiguration = Helpers.GetPromptyModelConfigFromSettings();

}
}

return prompty;

}
}
}
14 changes: 14 additions & 0 deletions runtime/promptycs/Prompty.Core/IInvoker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Prompty.Core
{
public interface IInvoker
{
public abstract Task<BaseModel> Invoke(BaseModel data);

public async Task<BaseModel> Call(BaseModel data)
{
return await Invoke(data);
}

}

}
Loading

0 comments on commit d6603b2

Please sign in to comment.