-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add llms.vector module for vector search using grafana-llm-app
This adds convenience functions to access the endpoints added in grafana/grafana-llm-app#33.
- Loading branch information
Showing
4 changed files
with
96 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const LLM_PLUGIN_ID = 'grafana-llm-app'; | ||
export const LLM_PLUGIN_ROUTE = `/api/plugins/${LLM_PLUGIN_ID}`; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * as openai from './openai'; | ||
export * as vector from './vector'; |
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
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,91 @@ | ||
/** | ||
* Vector search API. | ||
* | ||
* This module can be used to interact with the vector database configured | ||
* in the Grafana LLM app plugin. That plugin must be installed, enabled and configured | ||
* in order for these functions to work. | ||
* | ||
* The {@link enabled} function can be used to check if the plugin is enabled and configured. | ||
*/ | ||
|
||
import { getBackendSrv, logDebug } from "@grafana/runtime"; | ||
import { LLM_PLUGIN_ROUTE } from "./constants"; | ||
|
||
interface SearchResultPayload extends Record<string, any> { } | ||
|
||
/** | ||
* A request to search for resources in the vector database. | ||
**/ | ||
export interface SearchRequest { | ||
/** | ||
* The name of the collection to search in. | ||
* | ||
* Available collections can be obtained using the `collections` function. | ||
**/ | ||
collection: string; | ||
|
||
/** The query to search for. */ | ||
query: string; | ||
|
||
/** | ||
* The maximum number of results to return. | ||
* | ||
* Defaults to 10. | ||
**/ | ||
limit?: number; | ||
} | ||
|
||
/** | ||
* The results of a vector search. | ||
* | ||
* Results will be ordered by score, descending. | ||
*/ | ||
export interface SearchResult<T extends SearchResultPayload> { | ||
/** | ||
* The payload of the result. | ||
* | ||
* The type of this payload depends on the collection that was searched in. | ||
* Grafana core types can be found in the same module as this type. | ||
**/ | ||
payload: T; | ||
|
||
/** | ||
* The score of the result. | ||
* | ||
* This is a number between 0 and 1, where 1 is the best possible match. | ||
*/ | ||
score: number; | ||
} | ||
|
||
interface SearchResultResponse<T extends SearchResultPayload> { | ||
results: SearchResult<T>[]; | ||
} | ||
|
||
/** | ||
* Search for resources in the configured vector database. | ||
*/ | ||
export async function search<T extends SearchResultPayload>(request: SearchRequest): Promise<SearchResult<T>[]> { | ||
const response = await getBackendSrv().post<SearchResultResponse<T>>('/api/plugins/grafana-llm-app/resources/vector/search', request, { | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
return response.results; | ||
} | ||
|
||
let loggedWarning = false; | ||
|
||
/** Check if the vector API is enabled and configured via the LLM plugin. */ | ||
export const enabled = async () => { | ||
try { | ||
const settings = await getBackendSrv().get(`${LLM_PLUGIN_ROUTE}/settings`, undefined, undefined, { | ||
showSuccessAlert: false, showErrorAlert: false, | ||
}); | ||
return settings.enabled && (settings?.secureJsonFields?.openAIKey ?? false); | ||
} catch (e) { | ||
if (!loggedWarning) { | ||
logDebug(String(e)); | ||
logDebug('Failed to check if OpenAI is enabled. This is expected if the Grafana LLM plugin is not installed, and the above error can be ignored.'); | ||
loggedWarning = true; | ||
} | ||
return false; | ||
} | ||
} |