- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
Showing
2 changed files
with
37 additions
and
0 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,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, | ||
}; | ||
}; |
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