microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
teams.net/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.AI.Models.OpenAI.Extensions
Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.AI.Models.OpenAI.Extensions/ServiceCollection.cs
109lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.AspNetCore.Http; |
| 5 | using Microsoft.Extensions.DependencyInjection; |
| 6 | using Microsoft.Teams.AI.Prompts; |
| 7 | using Microsoft.Teams.Common.Logging; |
| 8 | |
| 9 | using OpenAI.Chat; |
| 10 | |
| 11 | namespace Microsoft.Teams.AI.Models.OpenAI.Extensions; |
| 12 | |
| 13 | public static class ServiceCollectionExtensions |
| 14 | { |
| 15 | public static IServiceCollection AddOpenAI(this IServiceCollection collection, OpenAIChatModel model, ChatPromptOptions? options = null) |
| 16 | { |
| 17 | var prompt = new OpenAIChatPrompt(model, options); |
| 18 | |
| 19 | collection.AddSingleton(model); |
| 20 | collection.AddSingleton<IChatModel<ChatCompletionOptions>, OpenAIChatModel>(provider => provider.GetRequiredService<OpenAIChatModel>()); |
| 21 | collection.AddSingleton(prompt); |
| 22 | return collection.AddSingleton<IChatPrompt>(provider => provider.GetRequiredService<OpenAIChatPrompt>()); |
| 23 | } |
| 24 | |
| 25 | public static IServiceCollection AddOpenAI(this IServiceCollection collection, string model, string apiKey, ChatPromptOptions? options = null) |
| 26 | { |
| 27 | var chatModel = new OpenAIChatModel(model, apiKey); |
| 28 | var prompt = new OpenAIChatPrompt(chatModel, options); |
| 29 | |
| 30 | collection.AddSingleton(chatModel); |
| 31 | collection.AddSingleton<IChatModel<ChatCompletionOptions>, OpenAIChatModel>(provider => provider.GetRequiredService<OpenAIChatModel>()); |
| 32 | collection.AddSingleton(prompt); |
| 33 | return collection.AddSingleton<IChatPrompt>(provider => provider.GetRequiredService<OpenAIChatPrompt>()); |
| 34 | } |
| 35 | |
| 36 | public static IServiceCollection AddOpenAI(this IServiceCollection collection, ChatPromptOptions? options = null) |
| 37 | { |
| 38 | collection.AddSingleton(provider => |
| 39 | { |
| 40 | var logger = provider.GetRequiredService<ILogger>(); |
| 41 | var settings = provider.GetRequiredService<OpenAISettings>(); |
| 42 | return new OpenAIChatModel(settings.Model, settings.ApiKey, new() { Logger = logger }); |
| 43 | }); |
| 44 | |
| 45 | collection.AddSingleton<IChatModel<ChatCompletionOptions>, OpenAIChatModel>(provider => provider.GetRequiredService<OpenAIChatModel>()); |
| 46 | collection.AddSingleton(provider => |
| 47 | { |
| 48 | var logger = provider.GetRequiredService<ILogger>(); |
| 49 | var model = provider.GetRequiredService<OpenAIChatModel>(); |
| 50 | return new OpenAIChatPrompt(model, (options ?? new()).WithLogger(logger)); |
| 51 | }); |
| 52 | |
| 53 | return collection.AddSingleton<IChatPrompt>(provider => provider.GetRequiredService<OpenAIChatPrompt>()); |
| 54 | } |
| 55 | |
| 56 | public static IServiceCollection AddOpenAI<T>(this IServiceCollection collection, ChatPromptOptions? options = null) where T : class |
| 57 | { |
| 58 | collection.AddScoped(provider => |
| 59 | { |
| 60 | var logger = provider.GetRequiredService<ILogger>(); |
| 61 | var settings = provider.GetRequiredService<OpenAISettings>(); |
| 62 | return new OpenAIChatModel(settings.Model, settings.ApiKey, new() { Logger = logger }); |
| 63 | }); |
| 64 | |
| 65 | return collection.AddOpenAIHelper<T>(options); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | public static IServiceCollection AddOpenAI<T>(this IServiceCollection collection, OpenAIChatModel model, ChatPromptOptions? options = null) where T : class |
| 70 | { |
| 71 | collection.AddScoped(provider => model); |
| 72 | return collection.AddOpenAIHelper<T>(options); |
| 73 | } |
| 74 | |
| 75 | private static IServiceCollection AddOpenAIHelper<T>(this IServiceCollection collection, ChatPromptOptions? options) where T : class |
| 76 | { |
| 77 | collection.AddScoped<T>(); |
| 78 | collection.AddScoped<IChatModel<ChatCompletionOptions>, OpenAIChatModel>( |
| 79 | provider => provider.GetRequiredService<OpenAIChatModel>() |
| 80 | ); |
| 81 | |
| 82 | collection.AddScoped(provider => |
| 83 | { |
| 84 | var value = provider.GetRequiredService<T>(); |
| 85 | var logger = provider.GetRequiredService<ILogger>(); |
| 86 | var model = provider.GetRequiredService<OpenAIChatModel>(); |
| 87 | return OpenAIChatPrompt.From(model, value, (options ?? new()).WithLogger(logger)); |
| 88 | }); |
| 89 | |
| 90 | collection.AddScoped<IChatPrompt>( |
| 91 | provider => provider.GetRequiredService<OpenAIChatPrompt>() |
| 92 | ); |
| 93 | |
| 94 | // Add a factory for creating scoped prompts by accessing the HttpContext |
| 95 | collection.AddSingleton<Func<OpenAIChatPrompt>>(provider => |
| 96 | { |
| 97 | return () => |
| 98 | { |
| 99 | var httpContextAccessor = provider.GetRequiredService<IHttpContextAccessor>(); |
| 100 | var httpContext = httpContextAccessor.HttpContext |
| 101 | ?? throw new InvalidOperationException("No active HttpContext. Cannot resolve OpenAIChatPrompt."); |
| 102 | |
| 103 | return httpContext.RequestServices.GetRequiredService<OpenAIChatPrompt>(); |
| 104 | }; |
| 105 | }); |
| 106 | |
| 107 | return collection; |
| 108 | } |
| 109 | } |