1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

chore: Unleash AI API hook (#8441)

https://linear.app/unleash/issue/2-2791/create-a-useaiapi-react-hook

Implements a basic Unleash AI API React hook that fits our initial needs
for interacting with this API through our frontend.

Also adds a new nice-to-have script to run the frontend set to the
`demo` base path, which matches our Cloud defaults. This way you can run
the latest local cloud with the latest local frontend in an easy way.
This commit is contained in:
Nuno Góis 2024-10-14 13:29:29 +01:00 committed by GitHub
parent 9d49070cee
commit f63496d47f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,36 @@
import useAPI from '../useApi/useApi';
const ENDPOINT = 'api/admin/ai';
export type ChatMessage = {
role: 'system' | 'user' | 'assistant';
content: string;
};
export const useAIApi = () => {
const { makeRequest, createRequest, errors, loading } = useAPI({
propagateErrors: true,
});
const chat = async (messages: ChatMessage[]): Promise<ChatMessage[]> => {
const requestId = 'chat';
const req = createRequest(`${ENDPOINT}/chat`, {
method: 'POST',
body: JSON.stringify({
messages,
}),
requestId,
});
const response = await makeRequest(req.caller, req.id);
const { messages: newMessages } = await response.json();
return newMessages;
};
return {
chat,
errors,
loading,
};
};

View File

@ -41,6 +41,7 @@
"build": "yarn run clean && concurrently \"yarn:copy-templates\" \"yarn:build:frontend\" \"yarn:build:backend\"",
"dev:backend": "TZ=UTC NODE_ENV=development tsc-watch --strictNullChecks false --onSuccess \"node dist/server-dev.js\"",
"dev:frontend": "wait-on tcp:4242 && yarn --cwd ./frontend run dev",
"dev:frontend:cloud": "UNLEASH_BASE_PATH=/demo/ yarn run dev:frontend",
"dev": "concurrently \"yarn:dev:backend\" \"yarn:dev:frontend\"",
"prepare:backend": "concurrently \"yarn:copy-templates\" \"yarn:build:backend\"",
"start:dev": "yarn run clean && TZ=UTC NODE_ENV=development tsc-watch --strictNullChecks false --onSuccess \"node dist/server-dev.js\"",