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

@ -1,5 +1,6 @@
import os import os
import json import json
import traceback
import dotenv import dotenv
import requests import requests
@ -86,7 +87,6 @@ def api_answer():
# use try and except to check for exception # use try and except to check for exception
try: try:
# check if the vectorstore is set # check if the vectorstore is set
if "active_docs" in data: if "active_docs" in data:
vectorstore = "vectors/" + data["active_docs"] vectorstore = "vectors/" + data["active_docs"]
@ -144,6 +144,8 @@ def api_answer():
# } # }
return result return result
except Exception as e: except Exception as e:
# print whole traceback
traceback.print_exc()
print(str(e)) print(str(e))
return bad_request(500,str(e)) return bad_request(500,str(e))
@ -185,4 +187,4 @@ def after_request(response):
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True) app.run(debug=True, port=5001)

View File

@ -30,7 +30,7 @@ export default function Conversation() {
return ( return (
<div className="flex justify-center p-6"> <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) => { {messages.map((message, index) => {
return ( return (
<ConversationBubble <ConversationBubble

View File

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

View File

@ -14,7 +14,24 @@ export const fetchAnswer = createAsyncThunk<
{ state: RootState } { state: RootState }
>('fetchAnswer', async ({ question }, { getState }) => { >('fetchAnswer', async ({ question }, { getState }) => {
const state = 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; return answer;
}); });