diff --git a/systems/docgpt/src/adapters/assistant.py b/systems/docgpt/src/adapters/assistant.py index 62a1616..d6c85c6 100644 --- a/systems/docgpt/src/adapters/assistant.py +++ b/systems/docgpt/src/adapters/assistant.py @@ -6,7 +6,7 @@ from langchain_core.language_models import BaseChatModel from langchain_core.vectorstores import VectorStore -from src.core.prompts import DEFAULT_PROMPT +from src.core.prompts import CONDENSE_QUESTION_PROMPT, QA_PROMPT from src.domain.assistant import Message, PromptResult, SessionId from src.port.assistant import AssistantPort @@ -51,11 +51,12 @@ def prompt(self, message: Message, *, session_id: SessionId | None = None) -> Pr qa = ConversationalRetrievalChain.from_llm( llm=self._llm, - condense_question_prompt=DEFAULT_PROMPT, + condense_question_prompt=CONDENSE_QUESTION_PROMPT, retriever=self._storage.as_retriever( search_type="similarity", search_kwargs=search_kwargs, ), + combine_docs_chain_kwargs={"prompt": QA_PROMPT}, get_chat_history=lambda v: v, memory=memory, verbose=True, diff --git a/systems/docgpt/src/core/containers.py b/systems/docgpt/src/core/containers.py index 04f221d..d6cde22 100644 --- a/systems/docgpt/src/core/containers.py +++ b/systems/docgpt/src/core/containers.py @@ -134,6 +134,7 @@ class AssistantAdapters(containers.DeclarativeContainer): ConversationBufferMemory, chat_memory=storage.memory_factory, memory_key="chat_history", + output_key="answer", ) chat: Singleton[AssistantPort] = Singleton( diff --git a/systems/docgpt/src/core/prompts.py b/systems/docgpt/src/core/prompts.py index b9b066f..fd8e60d 100644 --- a/systems/docgpt/src/core/prompts.py +++ b/systems/docgpt/src/core/prompts.py @@ -1,6 +1,6 @@ from langchain_core.prompts import PromptTemplate -_template = """ +_condense_template = """ Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language. @@ -14,4 +14,36 @@ {chat_history} Follow Up Input: {question} Standalone question:""" -DEFAULT_PROMPT = PromptTemplate.from_template(_template) + +CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_condense_template) + +_qa_template = """You are a specialized assistant for the R data.table open source project. + +Your role is to help contributors by providing clear, practical answers while also explaining context and reasoning. + +IMPORTANT - Scope of Assistance: +- You ONLY answer questions related to the data.table package, its codebase, documentation, and contribution process +- If a question is unrelated to data.table, politely redirect: "I'm specifically designed to help with the data.table package. For questions about [topic], I'd recommend consulting other resources. Is there anything about data.table I can help you with?" +- Only use the information from the provided context below - do not make up information + +Guidelines for data.table questions: +- Provide direct, actionable answers to technical questions +- Explain the "why" behind design decisions and implementations +- Reference specific code locations, functions, or documentation sections when relevant +- For complex topics, break down the explanation into clear steps +- Adapt your response depth based on the question: + * Quick questions get concise answers with optional details + * Complex questions get thorough explanations with examples +- When multiple approaches exist, present them with trade-offs +- Guide newcomers by explaining concepts they might not know +- Help experienced contributors by being precise and efficient +- If the context doesn't contain relevant information, say so clearly + +Context from the data.table codebase: +{context} + +Question: {question} + +Answer (only if related to data.table):""" + +QA_PROMPT = PromptTemplate.from_template(_qa_template)