From 817c7ab47159f6a25f9e6e3ecb4d100633e40f29 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Mon, 4 Sep 2023 08:35:35 +0200 Subject: [PATCH 1/5] feat: improve chat id passing --- ai/models.py | 3 ++- ai/pyserver.py | 4 ++- ai/question.py | 33 +++++++++++++++++-------- server/api/repos/[repo_id]/chat.post.ts | 29 +++++++++++++++------- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/ai/models.py b/ai/models.py index a0a4e93..8f73585 100644 --- a/ai/models.py +++ b/ai/models.py @@ -14,5 +14,6 @@ class IssueIndexInfo(BaseModel): class Conversation(BaseModel): repo_id: int question: str - chat_id: int + chat_id: str answer: str = "" + source_documents: List[str] = [] diff --git a/ai/pyserver.py b/ai/pyserver.py index 114a8a1..621a1bd 100644 --- a/ai/pyserver.py +++ b/ai/pyserver.py @@ -24,9 +24,11 @@ def perform_index(indexInfo: IndexInfo): @app.post("/ask") def conversation(conversationInput: Conversation): - conversationInput.answer = ask( + answer, source_documents = ask( conversationInput.repo_id, conversationInput.chat_id, conversationInput.question, ) + conversationInput.answer = answer + conversationInput.source_documents = source_documents return conversationInput diff --git a/ai/question.py b/ai/question.py index 99a2d7d..f4cb4ce 100644 --- a/ai/question.py +++ b/ai/question.py @@ -15,15 +15,29 @@ chatMemories = {} -def _get_chat(chat_id: int): +def _cleanup_chats(): + for chat_id in chatMemories.keys(): + # drop chats that are older than 5 minutes + if time.time() - chatMemories[chat_id].lastQuestion > 5 * 60: + del chatMemories[chat_id] + + +def _get_chat(chat_id: str): + _cleanup_chats() + if chat_id not in chatMemories: - chatMemories[chat_id] = ConversationBufferMemory( - memory_key="chat_history", return_messages=True, output_key="answer" - ) + chatMemories[chat_id] = { + memory: ConversationBufferMemory( + memory_key="chat_history", return_messages=True, output_key="answer" + ), + } + + chatMemories[chat_id].lastQuestion = time.time() + return chatMemories[chat_id] -def ask(repo_id: int, chat_id: int, question: str): +def ask(repo_id: int, chat_id: str, question: str): embeddings = OpenAIEmbeddings(disallowed_special=()) repo_path = os.path.join(data_path, str(repo_id)) @@ -49,14 +63,13 @@ def ask(repo_id: int, chat_id: int, question: str): end = time.time() result = qa(question) - return result["answer"] - + print(f"Answer: {result['answer']}") + print(f"Sources: {[x.metadata['source'] for x in result['source_documents']]}") end = time.time() + # TODO: return source_documents -def close_chat(chat_id: int): - if chat_id in chatMemories: - del chatMemories[chat_id] + return result["answer"], [x.metadata["source"] for x in result["source_documents"]] if __name__ == "__main__": diff --git a/server/api/repos/[repo_id]/chat.post.ts b/server/api/repos/[repo_id]/chat.post.ts index f6298ed..30f645e 100644 --- a/server/api/repos/[repo_id]/chat.post.ts +++ b/server/api/repos/[repo_id]/chat.post.ts @@ -1,7 +1,7 @@ export default defineEventHandler(async (event) => { const user = await requireUser(event); - const repoId = event.context.params?.repo_id; + const repoId = getRouterParam(event, 'repo_id'); if (!repoId) { throw createError({ statusCode: 400, @@ -11,17 +11,28 @@ export default defineEventHandler(async (event) => { const repo = await requireAccessToRepo(user, parseInt(repoId, 10)); - const message = (await readBody(event))?.message; + const body = await readBody(event); + const message = body?.message; + const chatId = body?.chat_id; + if (!message || !chatId) { + throw createError({ + statusCode: 400, + statusMessage: 'message and chat_id are required', + }); + } const config = useRuntimeConfig(); - const chatResponse = await $fetch<{ error?: string; answer: string }>(`${config.ai.url}/ask`, { - method: 'POST', - body: { - repo_id: repo.id, - chat_id: repo.id, // TODO: support opening and closing chats - question: message, + const chatResponse = await $fetch<{ error?: string; answer: string; source_documents: string[] }>( + `${config.ai.url}/ask`, + { + method: 'POST', + body: { + repo_id: repo.id, + chat_id: `${user.id}-${chatId}`, + question: message, + }, }, - }); + ); if (chatResponse.error) { console.error(chatResponse.error); From 1d5d8358618a1df893288b9c5f1ebbf82e69e2f5 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Mon, 4 Sep 2023 10:19:09 +0200 Subject: [PATCH 2/5] add chat id --- pages/repos/[repo_id]/chat.vue | 60 +++++++++++++++---------- server/api/repos/[repo_id]/chat.post.ts | 25 +++-------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/pages/repos/[repo_id]/chat.vue b/pages/repos/[repo_id]/chat.vue index 193e3f8..497f5ce 100644 --- a/pages/repos/[repo_id]/chat.vue +++ b/pages/repos/[repo_id]/chat.vue @@ -25,7 +25,7 @@ > @@ -199,6 +201,10 @@ async function reIndex() { } loading.value = false; } + +async function loadPath(path: string) { + return await $fetch(`/api/repos/${repoId}/files/tree?path=${path}`); +}