forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request elastic#8 from joemcelroy/add-hook
Add API Service
- Loading branch information
Showing
4 changed files
with
268 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
86 changes: 86 additions & 0 deletions
86
x-pack/plugins/enterprise_search/server/routes/enterprise_search/ai_playground.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { RouteDependencies } from '../../plugin'; | ||
import { elasticsearchErrorHandler } from '../../utils/elasticsearch_error_handler'; | ||
import Assist, { ConversationalChain } from '@elastic/ai-assist'; | ||
import { ChatOpenAI } from '@elastic/ai-assist/models'; | ||
import { Prompt } from '@elastic/ai-assist'; | ||
import { schema } from '@kbn/config-schema'; | ||
import Stream from 'stream'; | ||
|
||
export function registerAIPlaygroundRoutes({ log, router }: RouteDependencies) { | ||
router.post( | ||
{ | ||
path: '/internal/enterprise_search/ai_playground/chat', | ||
validate: { | ||
body: schema.object({ | ||
data: schema.any(), | ||
messages: schema.any(), | ||
}), | ||
}, | ||
}, | ||
elasticsearchErrorHandler(log, async (context, request, response) => { | ||
const { client } = (await context.core).elasticsearch; | ||
|
||
const aiClient = Assist({ | ||
es_client: client.asCurrentUser, | ||
}); | ||
|
||
const { messages, data } = await request.body; | ||
|
||
const model = new ChatOpenAI({ | ||
openAIApiKey: data.api_key, | ||
}); | ||
|
||
const chain = ConversationalChain({ | ||
model, | ||
rag: { | ||
index: data.indices, | ||
retriever: (question: string) => { | ||
return { | ||
text_expansion: { | ||
'vector.tokens': { | ||
model_id: '.elser_model_2', | ||
model_text: question, | ||
}, | ||
}, | ||
}; | ||
}, | ||
}, | ||
prompt: Prompt(data.prompt, { | ||
citations: true, | ||
context: true, | ||
type: 'openai', | ||
}), | ||
}); | ||
|
||
const stream = await chain.stream(aiClient, messages); | ||
|
||
const reader = (stream as ReadableStream).getReader(); | ||
|
||
class UIStream extends Stream.Readable { | ||
_read() { | ||
const that = this; | ||
|
||
function read() { | ||
reader.read().then(({ done, value }: { done: boolean; value?: string }) => { | ||
if (done) { | ||
that.push(null); | ||
return; | ||
} | ||
that.push(value); | ||
read(); | ||
}); | ||
} | ||
read(); | ||
} | ||
} | ||
|
||
return response.custom({ | ||
body: new UIStream(), | ||
statusCode: 200, | ||
headers: { | ||
'Content-Type': 'text/plain; charset=utf-8', | ||
}, | ||
}); | ||
}) | ||
); | ||
} |
This file contains 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
Oops, something went wrong.