API conversation

This commit is contained in:
Alex
2023-02-25 13:36:03 +00:00
parent 6cd98d631d
commit 6eef53cb1b
4 changed files with 46 additions and 21 deletions

View File

@@ -3,26 +3,32 @@ import { Answer } from './conversationModels';
export function fetchAnswerApi(
question: string,
apiKey: string,
selectedDocs: string,
): Promise<Answer> {
// a mock answer generator, this is going to be replaced with real http call
return new Promise((resolve, reject) => {
setTimeout(() => {
let result = '';
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < 5) {
result += characters.charAt(
Math.floor(Math.random() * charactersLength),
);
counter += 1;
}
const randNum = getRandomInt(0, 10);
randNum < 5
? reject()
: resolve({ answer: result, query: question, result });
}, 3000);
const activeDocs = 'default';
fetch('https://docsgpt.arc53.com/api/answer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
question: question,
api_key: apiKey,
embeddings_key: apiKey,
history: localStorage.getItem('chatHistory'),
active_docs: selectedDocs,
}),
})
.then((response) => response.json())
.then((data) => {
const result = data.answer;
resolve({ answer: result, query: question, result });
})
.catch((error) => {
reject();
});
});
}