-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reuse basic framework of the POC
- Loading branch information
Showing
25 changed files
with
1,183 additions
and
30 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
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
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,87 @@ | ||
import { getClusterConfig } from 'state/utils/getBackendInfo'; | ||
import { parseWithNestedBrackets } from '../utils/parseNestedBrackets'; | ||
|
||
type GetChatResponseArgs = { | ||
prompt: string; | ||
handleChatResponse: (chunk: any) => void; | ||
handleError: () => void; | ||
sessionID: string; | ||
clusterUrl: string; | ||
token: string; | ||
certificateAuthorityData: string; | ||
}; | ||
|
||
export default async function getChatResponse({ | ||
prompt, | ||
handleChatResponse, | ||
handleError, | ||
sessionID, | ||
clusterUrl, | ||
token, | ||
certificateAuthorityData, | ||
}: GetChatResponseArgs): Promise<void> { | ||
const { backendAddress } = getClusterConfig(); | ||
const url = `${backendAddress}/api/v1/namespaces/ai-core/services/http:ai-backend-clusterip:5000/proxy/api/v1/chat`; | ||
const payload = { question: prompt, session_id: sessionID }; | ||
const k8sAuthorization = `Bearer ${token}`; | ||
|
||
fetch(url, { | ||
headers: { | ||
accept: 'application/json', | ||
'content-type': 'application/json', | ||
'X-Cluster-Certificate-Authority-Data': certificateAuthorityData, | ||
'X-Cluster-Url': clusterUrl, | ||
'X-K8s-Authorization': k8sAuthorization, | ||
'X-User': sessionID, | ||
}, | ||
body: JSON.stringify(payload), | ||
method: 'POST', | ||
}) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
const reader = response.body?.getReader(); | ||
if (!reader) { | ||
throw new Error('Failed to get reader from response body'); | ||
} | ||
const decoder = new TextDecoder(); | ||
readChunk(reader, decoder, handleChatResponse, handleError, sessionID); | ||
}) | ||
.catch(error => { | ||
handleError(); | ||
console.error('Error fetching data:', error); | ||
}); | ||
} | ||
|
||
function readChunk( | ||
reader: ReadableStreamDefaultReader<Uint8Array>, | ||
decoder: TextDecoder, | ||
handleChatResponse: (chunk: any) => void, | ||
handleError: () => void, | ||
sessionID: string, | ||
) { | ||
reader | ||
.read() | ||
.then(({ done, value }) => { | ||
if (done) { | ||
return; | ||
} | ||
// Also handles the rare case of two chunks being sent at once | ||
const receivedString = decoder.decode(value, { stream: true }); | ||
const chunks = parseWithNestedBrackets(receivedString).map(chunk => { | ||
return JSON.parse(chunk); | ||
}); | ||
chunks.forEach(chunk => { | ||
if ('error' in chunk) { | ||
throw new Error(chunk.error); | ||
} | ||
handleChatResponse(chunk); | ||
}); | ||
readChunk(reader, decoder, handleChatResponse, handleError, sessionID); | ||
}) | ||
.catch(error => { | ||
handleError(); | ||
console.error('Error reading stream:', error); | ||
}); | ||
} |
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,40 @@ | ||
import { getClusterConfig } from 'state/utils/getBackendInfo'; | ||
|
||
interface GetFollowUpQuestionsParams { | ||
sessionID?: string; | ||
handleFollowUpQuestions: (results: any) => void; | ||
clusterUrl: string; | ||
token: string; | ||
certificateAuthorityData: string; | ||
} | ||
|
||
export default async function getFollowUpQuestions({ | ||
sessionID = '', | ||
handleFollowUpQuestions, | ||
clusterUrl, | ||
token, | ||
certificateAuthorityData, | ||
}: GetFollowUpQuestionsParams): Promise<void> { | ||
try { | ||
const { backendAddress } = getClusterConfig(); | ||
const url = `${backendAddress}/api/v1/namespaces/ai-core/services/http:ai-backend-clusterip:5000/proxy/api/v1/llm/followup`; | ||
const payload = JSON.parse(`{"session_id":"${sessionID}"}`); | ||
const k8sAuthorization = `Bearer ${token}`; | ||
|
||
let { results } = await fetch(url, { | ||
headers: { | ||
accept: 'application/json', | ||
'content-type': 'application/json', | ||
'X-Cluster-Certificate-Authority-Data': certificateAuthorityData, | ||
'X-Cluster-Url': clusterUrl, | ||
'X-K8s-Authorization': k8sAuthorization, | ||
'X-User': sessionID, | ||
}, | ||
body: JSON.stringify(payload), | ||
method: 'POST', | ||
}).then(result => result.json()); | ||
handleFollowUpQuestions(results); | ||
} catch (error) { | ||
console.error('Error fetching data:', error); | ||
} | ||
} |
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,55 @@ | ||
import { getClusterConfig } from 'state/utils/getBackendInfo'; | ||
import { extractApiGroup } from 'resources/Roles/helpers'; | ||
|
||
interface GetPromptSuggestionsParams { | ||
namespace?: string; | ||
resourceType?: string; | ||
groupVersion?: string; | ||
resourceName?: string; | ||
sessionID?: string; | ||
clusterUrl: string; | ||
token: string; | ||
certificateAuthorityData: string; | ||
} | ||
|
||
// TODO add return type | ||
|
||
export default async function getPromptSuggestions({ | ||
namespace = '', | ||
resourceType = '', | ||
groupVersion = '', | ||
resourceName = '', | ||
sessionID = '', | ||
clusterUrl, | ||
token, | ||
certificateAuthorityData, | ||
}: GetPromptSuggestionsParams): Promise<any[] | false> { | ||
try { | ||
const { backendAddress } = getClusterConfig(); | ||
const url = `${backendAddress}/api/v1/namespaces/ai-core/services/http:ai-backend-clusterip:5000/proxy/api/v1/llm/init`; | ||
const apiGroup = extractApiGroup(groupVersion); | ||
const payload = JSON.parse( | ||
`{"resource_type":"${resourceType.toLowerCase()}${ | ||
apiGroup.length ? `.${apiGroup}` : '' | ||
}","resource_name":"${resourceName}","namespace":"${namespace}","session_id":"${sessionID}"}`, | ||
); | ||
const k8sAuthorization = `Bearer ${token}`; | ||
|
||
let { results } = await fetch(url, { | ||
headers: { | ||
accept: 'application/json', | ||
'content-type': 'application/json', | ||
'X-Cluster-Certificate-Authority-Data': certificateAuthorityData, | ||
'X-Cluster-Url': clusterUrl, | ||
'X-K8s-Authorization': k8sAuthorization, | ||
'X-User': sessionID, | ||
}, | ||
body: JSON.stringify(payload), | ||
method: 'POST', | ||
}).then(result => result.json()); | ||
return results; | ||
} catch (error) { | ||
console.error('Error fetching data:', error); | ||
return false; | ||
} | ||
} |
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,31 @@ | ||
.chat-container { | ||
height: 100%; | ||
overflow: hidden; | ||
|
||
.chat-list { | ||
display: flex; | ||
flex-direction: column; | ||
overflow: auto; | ||
gap: 8px; | ||
|
||
&::-webkit-scrollbar { | ||
display: none; | ||
} | ||
|
||
.left-aligned { | ||
align-self: flex-start; | ||
background-color: var(--sapBackgroundColor); | ||
border-radius: 8px 8px 8px 0; | ||
} | ||
|
||
.right-aligned { | ||
align-self: flex-end; | ||
background-color: var(--sapContent_Illustrative_Color1); | ||
border-radius: 8px 8px 0 8px; | ||
|
||
.text { | ||
color: white; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.