Skip to content

Commit 484338d

Browse files
authored
Set up api (#1662)
* Set up api * Fix Dockerfile path * Refactor naming * Fix editorconfig path * Refactor editorconfig * Revert "Refactor editorconfig" This reverts commit 9217ac0. * Fix path * Fix editorconfig glob * Adjust folder structure * Refactor how DocumentationWebhost is reusing the proxy * Revert changes to stepper * Remove empty file * Refactor dependency resolution * Refactor mapping * Fix logging * Fix AOT build * Add services necessary for aws lambda As seen in https://docs.aws.amazon.com/lambda/latest/dg/csharp-package-asp.html#csharp-package-asp-deploy-minimal * Revert "Add services necessary for aws lambda" This reverts commit 7fd8de6. * Add system prompt to business logic * Remove dead code
1 parent ad418ac commit 484338d

26 files changed

+796
-185
lines changed

.editorconfig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ dotnet_diagnostic.IDE0305.severity = none
246246
# CS8509 already warns
247247
dotnet_diagnostic.IDE0072.severity = none
248248

249-
249+
[src/api/Elastic.Documentation.Api.Lambda/**.cs]
250+
dotnet_diagnostic.IL3050.severity = none
251+
dotnet_diagnostic.IL2026.severity = none
250252

251253
[DocumentationWebHost.cs]
252254
dotnet_diagnostic.IL3050.severity = none
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
# This workflow is used to build the API lambda
3+
# lambda function bootstrap binary that can be deployed to AWS Lambda.
4+
name: Build API Lambda
5+
6+
on:
7+
workflow_dispatch:
8+
workflow_call:
9+
inputs:
10+
ref:
11+
required: false
12+
type: string
13+
default: ${{ github.ref }}
14+
15+
jobs:
16+
build:
17+
runs-on: ubuntu-latest
18+
env:
19+
BINARY_PATH: .artifacts/Elastic.Documentation.Api.Lambda/release_linux-x64/bootstrap
20+
steps:
21+
- uses: actions/checkout@v4
22+
with:
23+
ref: ${{ inputs.ref }}
24+
- name: Amazon Linux 2023 build
25+
run: |
26+
docker build . -t api-lambda:latest -f src/api/Elastic.Documentation.Api.Lambda/Dockerfile
27+
- name: Get bootstrap binary
28+
run: |
29+
docker cp $(docker create --name tc api-lambda:latest):/app/.artifacts/publish ./.artifacts && docker rm tc
30+
- name: Inspect bootstrap binary
31+
run: |
32+
tree .artifacts
33+
stat "${BINARY_PATH}"
34+
- name: Archive artifact
35+
id: upload-artifact
36+
uses: actions/upload-artifact@v4
37+
with:
38+
name: api-lambda-binary
39+
retention-days: 1
40+
if-no-files-found: error
41+
path: ${{ env.BINARY_PATH }}

.github/workflows/ci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ jobs:
3232
- name: Validate Content Sources
3333
run: dotnet run --project src/tooling/docs-assembler -c release -- content-source validate
3434

35-
build-lambda:
35+
build-link-index-updater-lambda:
3636
uses: ./.github/workflows/build-link-index-updater-lambda.yml
37-
37+
38+
build-api-lambda:
39+
uses: ./.github/workflows/build-api-lambda.yml
40+
3841
npm:
3942
runs-on: ubuntu-latest
4043
defaults:

Directory.Packages.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</ItemGroup>
1010
<!-- AWS -->
1111
<ItemGroup>
12+
<PackageVersion Include="Amazon.Lambda.AspNetCoreServer.Hosting" Version="1.9.0" />
1213
<PackageVersion Include="Amazon.Lambda.RuntimeSupport" Version="1.13.0" />
1314
<PackageVersion Include="Amazon.Lambda.Core" Version="2.5.1" />
1415
<PackageVersion Include="Amazon.Lambda.S3Events" Version="3.1.0" />
@@ -19,6 +20,7 @@
1920
<PackageVersion Include="AWSSDK.S3" Version="4.0.0.1" />
2021
<PackageVersion Include="FakeItEasy" Version="8.3.0" />
2122
<PackageVersion Include="Elastic.Ingest.Elasticsearch" Version="0.11.3" />
23+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.4" />
2224
<PackageVersion Include="Microsoft.OpenApi" Version="2.0.0-preview9" />
2325
<PackageVersion Include="System.Text.Json" Version="9.0.5" />
2426
</ItemGroup>

docs-builder.sln

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{6FAB56
119119
config\navigation.yml = config\navigation.yml
120120
EndProjectSection
121121
EndProject
122+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "api", "api", "{B042CC78-5060-4091-B95A-79C71BA3908A}"
123+
EndProject
124+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Documentation.Api.Core", "src\api\Elastic.Documentation.Api.Core\Elastic.Documentation.Api.Core.csproj", "{F30B90AD-1A01-4A6F-9699-809FA6875B22}"
125+
EndProject
126+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Documentation.Api.Infrastructure", "src\api\Elastic.Documentation.Api.Infrastructure\Elastic.Documentation.Api.Infrastructure.csproj", "{AE3FC78E-167F-4B6E-88EC-84743EB748B7}"
127+
EndProject
128+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Documentation.Api.Lambda", "src\api\Elastic.Documentation.Api.Lambda\Elastic.Documentation.Api.Lambda.csproj", "{C6A121C5-DEB1-4FCE-9140-AF144EA98EEE}"
129+
EndProject
122130
Global
123131
GlobalSection(SolutionConfigurationPlatforms) = preSolution
124132
Debug|Any CPU = Debug|Any CPU
@@ -204,6 +212,18 @@ Global
204212
{164F55EC-9412-4CD4-81AD-3598B57632A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
205213
{164F55EC-9412-4CD4-81AD-3598B57632A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
206214
{164F55EC-9412-4CD4-81AD-3598B57632A6}.Release|Any CPU.Build.0 = Release|Any CPU
215+
{F30B90AD-1A01-4A6F-9699-809FA6875B22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
216+
{F30B90AD-1A01-4A6F-9699-809FA6875B22}.Debug|Any CPU.Build.0 = Debug|Any CPU
217+
{F30B90AD-1A01-4A6F-9699-809FA6875B22}.Release|Any CPU.ActiveCfg = Release|Any CPU
218+
{F30B90AD-1A01-4A6F-9699-809FA6875B22}.Release|Any CPU.Build.0 = Release|Any CPU
219+
{AE3FC78E-167F-4B6E-88EC-84743EB748B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
220+
{AE3FC78E-167F-4B6E-88EC-84743EB748B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
221+
{AE3FC78E-167F-4B6E-88EC-84743EB748B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
222+
{AE3FC78E-167F-4B6E-88EC-84743EB748B7}.Release|Any CPU.Build.0 = Release|Any CPU
223+
{C6A121C5-DEB1-4FCE-9140-AF144EA98EEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
224+
{C6A121C5-DEB1-4FCE-9140-AF144EA98EEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
225+
{C6A121C5-DEB1-4FCE-9140-AF144EA98EEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
226+
{C6A121C5-DEB1-4FCE-9140-AF144EA98EEE}.Release|Any CPU.Build.0 = Release|Any CPU
207227
EndGlobalSection
208228
GlobalSection(NestedProjects) = preSolution
209229
{4D198E25-C211-41DC-9E84-B15E89BD7048} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
@@ -234,5 +254,9 @@ Global
234254
{89B83007-71E6-4B57-BA78-2544BFA476DB} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
235255
{111E7029-BB29-4039-9B45-04776798A8DD} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
236256
{164F55EC-9412-4CD4-81AD-3598B57632A6} = {67B576EE-02FA-4F9B-94BC-3630BC09ECE5}
257+
{B042CC78-5060-4091-B95A-79C71BA3908A} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
258+
{F30B90AD-1A01-4A6F-9699-809FA6875B22} = {B042CC78-5060-4091-B95A-79C71BA3908A}
259+
{AE3FC78E-167F-4B6E-88EC-84743EB748B7} = {B042CC78-5060-4091-B95A-79C71BA3908A}
260+
{C6A121C5-DEB1-4FCE-9140-AF144EA98EEE} = {B042CC78-5060-4091-B95A-79C71BA3908A}
237261
EndGlobalSection
238262
EndGlobal

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/useLlmGateway.ts

Lines changed: 12 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,12 @@ import { EventSourceMessage } from '@microsoft/fetch-event-source'
44
import { useEffect, useState, useRef, useCallback } from 'react'
55
import * as z from 'zod'
66

7-
export const LlmGatewayRequestSchema = z.object({
8-
userContext: z.object({
9-
userEmail: z.string(),
10-
}),
11-
platformContext: z.object({
12-
origin: z.literal('support_portal'),
13-
useCase: z.literal('support_assistant'),
14-
metadata: z.any(),
15-
}),
16-
input: z.array(
17-
z.object({
18-
role: z.string(),
19-
message: z.string(),
20-
})
21-
),
22-
threadId: z.string(),
7+
export const AskAiRequestSchema = z.object({
8+
message: z.string(),
9+
threadId: z.string().optional(),
2310
})
2411

25-
export type LlmGatewayRequest = z.infer<typeof LlmGatewayRequestSchema>
12+
export type AskAiRequest = z.infer<typeof AskAiRequestSchema>
2613

2714
const sharedAttributes = {
2815
timestamp: z.number(),
@@ -154,8 +141,8 @@ export const useLlmGateway = (props: Props): UseLlmGatewayResponse => {
154141
[processMessage]
155142
)
156143

157-
const { sendMessage, abort } = useFetchEventSource<LlmGatewayRequest>({
158-
apiEndpoint: '/chat',
144+
const { sendMessage, abort } = useFetchEventSource<AskAiRequest>({
145+
apiEndpoint: '/_api/v1/ask-ai/stream',
159146
onMessage,
160147
onError: (error) => {
161148
setError(error)
@@ -221,64 +208,12 @@ export const useLlmGateway = (props: Props): UseLlmGatewayResponse => {
221208
}
222209
}
223210

224-
function createLlmGatewayRequest(question: string, threadId?: string) {
225-
// TODO: we should move this to the backend so that the use cannot change this
226-
// Right now, the backend is a pure proxy to the LLM gateway
227-
return LlmGatewayRequestSchema.parse({
228-
userContext: {
229-
userEmail: `elastic-docs-v3@invalid`, // Random email (will be optional in the future)
230-
},
231-
platformContext: {
232-
origin: 'support_portal',
233-
useCase: 'support_assistant',
234-
metadata: {},
235-
},
236-
input: [
237-
{
238-
role: 'user',
239-
message: `
240-
# ROLE AND GOAL
241-
You are an expert AI assistant for the Elastic Stack (Elasticsearch, Kibana, Beats, Logstash, etc.). Your sole purpose is to answer user questions based *exclusively* on the provided context from the official Elastic Documentation.
242-
243-
# CRITICAL INSTRUCTION: SINGLE-SHOT INTERACTION
244-
This is a single-turn interaction. The user cannot reply to your answer for clarification. Therefore, your response MUST be final, self-contained, and as comprehensive as possible based on the provided context.
245-
Also, keep the response as short as possible, but do not truncate the context.
246-
247-
# RULES
248-
1. **Facts** Always do RAG search to find the relevant Elastic documentation.
249-
2. **Strictly Grounded Answers:** You MUST base your answer 100% on the information from the search results. Do not use any of your pre-trained knowledge or any information outside of this context.
250-
3. **Handle Ambiguity Gracefully:** Since you cannot ask clarifying questions, if the question is broad or ambiguous (e.g., "how to improve performance"), structure your answer to cover the different interpretations supported by the context.
251-
* Acknowledge the ambiguity. For example: "Your question about 'performance' can cover several areas. Based on the documentation, here are the key aspects:"
252-
* Organize the answer with clear headings for each aspect (e.g., "Indexing Performance," "Query Performance").
253-
* But if there is a similar or related topic in the docs you can mention it and link to it.
254-
4. **Direct Answer First:** If the context directly and sufficiently answers a specific question, provide a clear, comprehensive, and well-structured answer.
255-
* Use Markdown for formatting (e.g., code blocks for configurations, bullet points for lists).
256-
* Use LaTeX for mathematical or scientific notations where appropriate (e.g., \`$E = mc^2$\`).
257-
* Make the answer as complete as possible, as this is the user's only response.
258-
* Keep the answer short and concise. We want to link users to the Elastic Documentation to find more information.
259-
5. **Handling Incomplete Answers:** If the context contains relevant information but does not fully answer the question, you MUST follow this procedure:
260-
* Start by explicitly stating that you could not find a complete answer.
261-
* Then, summarize the related information you *did* find in the context, explaining how it might be helpful.
262-
6. **Handling No Answer:** If the context is empty or completely irrelevant to the question, you MUST respond with the following, and nothing else:
263-
I was unable to find an answer to your question in the Elastic Documentation.
264-
265-
For further assistance, you may want to:
266-
* Ask the community of experts at **discuss.elastic.co**.
267-
* If you have an Elastic subscription, contact our support engineers at **support.elastic.co**."
268-
7. If you are 100% sure that something is not supported by Elastic, then say so.
269-
8. **Tone:** Your tone should be helpful, professional, and confident. It is better to provide no answer (Rule #5) than an incorrect one.
270-
* Assume that the user is using Elastic for the first time.
271-
* Assume that the user is a beginner.
272-
* Assume that the user has a limited knowledge of Elastic
273-
* Explain unusual terminology, abbreviations, or acronyms.
274-
* Always try to cite relevant Elastic documentation.
275-
`,
276-
},
277-
{
278-
role: 'user',
279-
message: question,
280-
},
281-
],
211+
function createLlmGatewayRequest(
212+
message: string,
213+
threadId?: string
214+
): AskAiRequest {
215+
return AskAiRequestSchema.parse({
216+
message,
282217
threadId,
283218
})
284219
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Elastic.Documentation.Api.Core.AskAi;
8+
9+
public class AskAiUsecase(IAskAiGateway<Stream> askAiGateway, ILogger<AskAiUsecase> logger)
10+
{
11+
public async Task<Stream> AskAi(AskAiRequest askAiRequest, Cancel ctx)
12+
{
13+
logger.LogDebug("Processing AskAiRequest: {Request}", askAiRequest);
14+
return await askAiGateway.AskAi(askAiRequest, ctx);
15+
}
16+
}
17+
18+
public record AskAiRequest(string Message, string? ThreadId)
19+
{
20+
public static string SystemPrompt =>
21+
"""
22+
Role: You are a specialized AI assistant designed to answer user questions exclusively from a set of provided documentation. Your primary purpose is to retrieve, synthesize, and present information directly from these documents.
23+
24+
## Core Directives:
25+
26+
- Source of Truth: Your only source of information is the document content provided to you for each user query. You must not use any pre-trained knowledge or external information.
27+
- Answering Style: Answer the user's question directly and comprehensively. As the user cannot ask follow-up questions, your response must be a complete, self-contained answer to their query. Do not start with phrases like "Based on the documents..."—simply provide the answer.
28+
- Handling Unknowns: If the information required to answer the question is not present in the provided documents, you must explicitly state that the answer cannot be found. Do not attempt to guess, infer, or provide a general response.
29+
- Helpful Fallback: If you cannot find a direct answer, you may suggest and link to a few related or similar topics that are present in the documentation. This provides value even when a direct answer is unavailable.
30+
- Output Format: Your final response should be a single, coherent block of text.
31+
32+
## Negative Constraints:
33+
34+
- Do not mention that you are a language model or AI.
35+
- Do not provide answers based on your general knowledge.
36+
- Do not ask the user for clarification.
37+
""";
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
namespace Elastic.Documentation.Api.Core.AskAi;
6+
7+
public interface IAskAiGateway<T>
8+
{
9+
Task<T> AskAi(AskAiRequest askAiRequest, Cancel ctx = default);
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<AssemblyName>Elastic.Documentation.Api.Core</AssemblyName>
8+
<RootNamespace>Elastic.Documentation.Api.Core</RootNamespace>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.Logging" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Text.Json.Serialization;
6+
using Elastic.Documentation.Api.Core.AskAi;
7+
8+
namespace Elastic.Documentation.Api.Core;
9+
10+
11+
[JsonSerializable(typeof(AskAiRequest))]
12+
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
13+
public partial class ApiJsonContext : JsonSerializerContext;

0 commit comments

Comments
 (0)