Last active
November 25, 2025 16:15
-
-
Save gistlyn/c132ee0025f1af60c85431416c6ef922 to your computer and use it in GitHub Desktop.
chat
This file contains hidden or 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
| dotnet add package ServiceStack.AI.Chat |
This file contains hidden or 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
| using ServiceStack.AI; | |
| [assembly: HostingStartup(typeof(MyApp.ConfigureAiChat))] | |
| namespace MyApp; | |
| public class ConfigureAiChat : IHostingStartup | |
| { | |
| public void Configure(IWebHostBuilder builder) => builder | |
| .ConfigureServices((context, services) => { | |
| // Docs: https://docs.servicestack.net/ai-chat-api | |
| services.AddPlugin(new ChatFeature { | |
| EnableProviders = [ | |
| "servicestack", | |
| // "groq", | |
| // "google_free", | |
| // "openrouter_free", | |
| // "ollama", | |
| // "google", | |
| // "anthropic", | |
| // "openai", | |
| // "grok", | |
| // "qwen", | |
| // "z.ai", | |
| // "mistral", | |
| // "openrouter", | |
| ] | |
| }); | |
| // Persist AI Chat History, enables analytics at /admin-ui/chat | |
| services.AddSingleton<IChatStore, DbChatStore>(); | |
| // Or store history in monthly partitioned tables in PostgreSQL: | |
| // services.AddSingleton<IChatStore, PostgresChatStore>(); | |
| // Add AI Chat link to /metadata | |
| services.ConfigurePlugin<MetadataFeature>(feature => { | |
| feature.AddPluginLink("/chat", "AI Chat"); | |
| }); | |
| }); | |
| } |
This file contains hidden or 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
| using ServiceStack; | |
| using ServiceStack.Data; | |
| using ServiceStack.OrmLite; | |
| using ServiceStack.Configuration; | |
| [assembly: HostingStartup(typeof(MyApp.ConfigureApiKeys))] | |
| namespace MyApp; | |
| public class ConfigureApiKeys : IHostingStartup | |
| { | |
| public void Configure(IWebHostBuilder builder) => builder | |
| .ConfigureServices(services => | |
| { | |
| services.AddPlugin(new ApiKeysFeature | |
| { | |
| // Optional: Available Scopes Admin Users can assign to any API Key | |
| // Features = [ | |
| // "Paid", | |
| // "Tracking", | |
| // ], | |
| // Optional: Available Features Admin Users can assign to any API Key | |
| // Scopes = [ | |
| // "todo:read", | |
| // "todo:write", | |
| // ], | |
| // Optional: Limit available Scopes Users can assign to their own API Keys | |
| // UserScopes = [ | |
| // "todo:read", | |
| // ], | |
| // Optional: Limit available Features Users can assign to their own API Keys | |
| // UserFeatures = [ | |
| // "Tracking", | |
| // ], | |
| }); | |
| }) | |
| .ConfigureAppHost(appHost => | |
| { | |
| using var db = appHost.Resolve<IDbConnectionFactory>().Open(); | |
| var feature = appHost.GetPlugin<ApiKeysFeature>(); | |
| feature.InitSchema(db); | |
| // Optional: Create API Key for specified Users on Startup | |
| if (feature.ApiKeyCount(db) == 0 && db.TableExists(IdentityUsers.TableName)) | |
| { | |
| var createApiKeysFor = new [] { "[email protected]", "[email protected]" }; | |
| var users = IdentityUsers.GetByUserNames(db, createApiKeysFor); | |
| foreach (var user in users) | |
| { | |
| List<string> scopes = user.UserName == "[email protected]" | |
| ? [RoleNames.Admin] | |
| : []; | |
| feature.Insert(db, | |
| new() { Name = "Seed API Key", UserId = user.Id, UserName = user.UserName, Scopes = scopes }); | |
| } | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment