mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-29 08:33:20 +00:00
API conversation
This commit is contained in:
@@ -30,7 +30,7 @@ export default function Conversation() {
|
||||
|
||||
return (
|
||||
<div className="flex justify-center p-6">
|
||||
<div className="flex mt-20 w-10/12 flex-col transition-all md:w-1/2">
|
||||
<div className="mt-20 flex w-10/12 flex-col transition-all md:w-1/2">
|
||||
{messages.map((message, index) => {
|
||||
return (
|
||||
<ConversationBubble
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,24 @@ export const fetchAnswer = createAsyncThunk<
|
||||
{ state: RootState }
|
||||
>('fetchAnswer', async ({ question }, { getState }) => {
|
||||
const state = getState();
|
||||
const answer = await fetchAnswerApi(question, state.preference.apiKey);
|
||||
let namePath = state.preference.selectedDocs?.name;
|
||||
if (state.preference.selectedDocs?.language === namePath) {
|
||||
namePath = '.project';
|
||||
}
|
||||
|
||||
const docPath =
|
||||
state.preference.selectedDocs?.language +
|
||||
'/' +
|
||||
namePath +
|
||||
'/' +
|
||||
state.preference.selectedDocs?.version +
|
||||
'/' +
|
||||
state.preference.selectedDocs?.model;
|
||||
const answer = await fetchAnswerApi(
|
||||
question,
|
||||
state.preference.apiKey,
|
||||
docPath,
|
||||
);
|
||||
return answer;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user