- 
                Notifications
    
You must be signed in to change notification settings  - Fork 32
 
Add ability to switch between LLM Gateway and Agent Builder #2128
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
Conversation
        
          
                src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/AskAi/Chat.tsx
          
            Show resolved
            Hide resolved
        
      # Conflicts: # src/Elastic.Documentation.Site/Assets/markdown/math.css # src/Elastic.Documentation.Site/_static/main.css.map
…feature/agent-builder-poc
| // Start background task to transform and write events to pipe | ||
| // Note: We intentionally don't await this task as we need to return the stream immediately | ||
| // The pipe handles synchronization between the writer (background task) and reader (returned stream) | ||
| var transformTask = Task.Run(async () => | 
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.
Use Task.Factory.StartNew(.., TaskCreationOptions.LongRunning);
I also wonder if we can just use Asp.NET HttpResponse.BodyWriter which is a PipeWriter already to directly write the transformed data to the outgoing SSE stream to avoid copying data further.
See also our very own @stevejgordon's blog post:
https://www.stevejgordon.co.uk/using-the-bodyreader-and-bodywriter-in-asp-net-core-3-0
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.
This would also remove the need the need to return the stream so eagerly and just use async/await on the reader/writer.
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.
I think I was able to address both concerns with replacing Task.Run with _ = ProcessPipeAsync(reader, pipe.Writer, cancellationToken);
and using PipeReader correctly.
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.
❤️ this! nice work, my comments are not blocking we can tackle that in a follow up.
Ask AI: provider switch, unified streaming, and improved UX
This PR introduces a simple way to choose the AI provider, unifies streaming across providers, and improves reliability and UX.
Overview
Implemented a provider-agnostic AskAI architecture using the Adapter and Factory patterns, allowing seamless switching between Agent Builder and LLM Gateway without frontend code changes.
Architecture
Backend (Abstraction Layer)
AskAiProviderResolver: Routes requests based onX-AI-ProviderHTTP header (defaults to LLM Gateway)AskAiGatewayFactory: Dynamically instantiates the correct AI gateway (Agent Builder or LLM Gateway)StreamTransformerFactory: Selects the appropriate stream transformer for the chosen providerLlmGatewayAskAiGateway- Handles Google Cloud LLM Gateway communicationAgentBuilderAskAiGateway- Handles Kibana Agent Builder communicationLlmGatewayStreamTransformer- Translates LLM Gateway SSE formatAgentBuilderStreamTransformer- Translates Agent Builder SSE formatAskAiEvent- Single event format both transformers outputFrontend (Provider Agnostic)
aiProviderStore: Zustand store managing user's provider selectionuseAskAihook: SendsX-AI-Providerheader, receives canonical eventsAiProviderSelector: UI component for switching providersAskAiEventtypes: Frontend mirrors backend's canonical event schemaKey Benefits
✅ Zero coupling: Frontend has no knowledge of provider-specific implementations
✅ Hot-swappable: Switch AI providers via a single header
✅ Extensible: Add new providers by implementing two interfaces
✅ Maintainable: Provider API changes only affect their respective transformer
✅ Testable: Each component can be tested in isolation
Technical Highlights
AskAiEventformat/docs/_api/v1/ask-ai/streamScreenshots